Skip to content

Commit

Permalink
Merge 88a7f69 into 64ea3bd
Browse files Browse the repository at this point in the history
  • Loading branch information
wbolster committed Mar 13, 2015
2 parents 64ea3bd + 88a7f69 commit d163f05
Show file tree
Hide file tree
Showing 8 changed files with 51 additions and 75 deletions.
29 changes: 16 additions & 13 deletions anyconfig/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
#
"""Public APIs of anyconfig module.
"""

import logging

import anyconfig.globals as G
import anyconfig.mergeabledict as M
import anyconfig.backend.backends as Backends
Expand All @@ -23,19 +26,19 @@
# pylint: disable=C0103
# Re-export:
list_types = Backends.list_types # flake8: noqa
getLogger = G.get_logger # flake8: noqa

# aliases:
container = M.MergeableDict
logging = G.LOGGER
# pylint: enable=C0103

logger = logging.getLogger(__name__)


def set_loglevel(level):
"""
:param level: Log level, e.g. logging.INFO and logging.WARN.
"""
logging.setLevel(level)
logger.setLevel(level)


def find_loader(config_path, forced_type=None):
Expand All @@ -47,15 +50,15 @@ def find_loader(config_path, forced_type=None):
if forced_type is not None:
cparser = Backends.find_by_type(forced_type)
if not cparser:
logging.error("No parser found for given type: %s", forced_type)
logger.error("No parser found for given type: %s", forced_type)
return None
else:
cparser = Backends.find_by_file(config_path)
if not cparser:
logging.error("No parser found for given file: %s", config_path)
logger.error("No parser found for given file: %s", config_path)
return None

logging.debug("Using config parser of type: %s", cparser.type())
logger.debug("Using config parser of type: %s", cparser.type())
return cparser


Expand Down Expand Up @@ -83,15 +86,15 @@ def single_load(config_path, forced_type=None, ignore_missing=False,
if cparser is None:
return None

logging.info("Loading: %s", config_path)
logger.info("Loading: %s", config_path)
if template:
try:
logging.debug("Compiling: %s", config_path)
logger.debug("Compiling: %s", config_path)
config_content = AT.render(config_path, context)
return cparser.loads(config_content, ignore_missing=ignore_missing,
**kwargs)
except:
logging.warn("Failed to compile %s, fallback to no template "
logger.warn("Failed to compile %s, fallback to no template "
"mode", config_path)

return cparser.load(config_path, ignore_missing=ignore_missing,
Expand Down Expand Up @@ -200,10 +203,10 @@ def loads(config_content, forced_type=None, template=True, context={},

if template:
try:
logging.debug("Compiling")
logger.debug("Compiling")
config_content = AT.render_s(config_content, context)
except:
logging.warn("Failed to compile and fallback to no template "
logger.warn("Failed to compile and fallback to no template "
"mode: '%s'", config_content[:50] + '...')

return cparser.loads(config_content, **kwargs)
Expand All @@ -220,7 +223,7 @@ def _find_dumper(config_path, forced_type=None):
cparser = find_loader(config_path, forced_type)

if cparser is None or not getattr(cparser, "dump", False):
logging.warn("Dump method not implemented. Fallback to "
logger.warn("Dump method not implemented. Fallback to "
"JsonConfigParser")
cparser = BJ.JsonConfigParser()

Expand All @@ -240,7 +243,7 @@ def dump(data, config_path, forced_type=None, **kwargs):
"""
dumper = _find_dumper(config_path, forced_type)

logging.info("Dumping: %s", config_path)
logger.info("Dumping: %s", config_path)
dumper.dump(data, config_path, **kwargs)


Expand Down
11 changes: 7 additions & 4 deletions anyconfig/backend/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,19 @@
# Copyright (C) 2012, 2013 Satoru SATOH <ssato @ redhat.com>
# License: MIT
#

import logging
import os

from anyconfig.compat import StringIO
from anyconfig.globals import LOGGER as logging

import anyconfig.mergeabledict as D
import anyconfig.utils as U
import os.path
import os

SUPPORTED = False

logger = logging.getLogger(__name__)


def mk_opt_args(keys, kwargs):
"""
Expand Down Expand Up @@ -42,7 +45,7 @@ def mk_dump_dir_if_not_exist(f):
dumpdir = os.path.dirname(f)

if not os.path.exists(dumpdir):
logging.debug("Creating output dir as it's not found: %s", dumpdir)
logger.debug("Creating output dir as it's not found: %s", dumpdir)
os.makedirs(dumpdir)


Expand Down
10 changes: 6 additions & 4 deletions anyconfig/backend/ini_.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,16 @@
# Copyright (C) 2011 - 2013 Satoru SATOH <ssato @ redhat.com>
# License: MIT
#

import logging
import sys

from anyconfig.compat import configparser, iteritems
from anyconfig.globals import LOGGER as logging

import anyconfig.backend.base as Base
import anyconfig.parser as P

import sys

logger = logging.getLogger(__name__)

SUPPORTED = True # It should be available w/ python dist always.

Expand Down Expand Up @@ -81,7 +83,7 @@ def _load_impl(config_fp, sep=_SEP, **kwargs):
config[s][k] = _parse(v, sep)

except Exception:
logging.warn(sys.exc_info()[-1])
logger.warn(sys.exc_info()[-1])
raise

return config
Expand Down
13 changes: 8 additions & 5 deletions anyconfig/backend/xml_.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,16 @@
# License: MIT
#
# pylint: disable=R0921
from anyconfig.globals import LOGGER as logging

import logging

import anyconfig.backend.base as Base
import anyconfig.compat as AC

logger = logging.getLogger(__name__)

SUPPORTED = True

try:
# First, try lxml which is compatible with elementtree and looks faster a
# lot. See also: http://getpython3.com/diveintopython3/xml.html
Expand All @@ -21,8 +24,8 @@
try:
import elementtree.ElementTree as etree
except ImportError:
logging.warn("ElementTree module is not available. Disabled "
"XML support.")
logger.warn("ElementTree module is not available. Disabled "
"XML support.")
SUPPORTED = False


Expand All @@ -42,8 +45,8 @@ def etree_getroot_fromsrc(src):
return etree.parse(src).getroot()
else:
def _dummy_fun(*args, **kwargs):
logging.warn("Return None as XML module is not available: "
"args=%s, kwargs=%s", ','.join(args), str(kwargs))
logger.warn("Return None as XML module is not available: "
"args=%s, kwargs=%s", ','.join(args), str(kwargs))
return None

etree_getroot_fromstring = etree_getroot_fromsrc = _dummy_fun
Expand Down
14 changes: 8 additions & 6 deletions anyconfig/backend/yaml_.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,18 @@
# Copyright (C) 2011 - 2013 Satoru SATOH <ssato @ redhat.com>
# License: MIT
#
from anyconfig.globals import LOGGER as logging

import logging
import anyconfig.backend.base as Base

logger = logging.getLogger(__name__)

SUPPORTED = False
try:
import yaml
SUPPORTED = True
except ImportError:
logging.warn("YAML module is not available. Disabled its support.")
logger.warn("YAML module is not available. Disabled its support.")


if SUPPORTED:
Expand All @@ -33,13 +35,13 @@ def yaml_dump(data, fp, **kwargs):
return yaml.dump(data, fp, **kwargs)
else:
def yaml_load(*args, **kwargs):
logging.warn("Return {} as YAML module is not available: "
"args=%s, kwargs=%s", ','.join(args), str(kwargs))
logger.warn("Return {} as YAML module is not available: "
"args=%s, kwargs=%s", ','.join(args), str(kwargs))
return {}

def yaml_dump(*args, **kwargs):
logging.warn("Do nothing as YAML module is not available: "
"args=%s, kwargs=%s", ','.join(args), str(kwargs))
logger.warn("Do nothing as YAML module is not available: "
"args=%s, kwargs=%s", ','.join(args), str(kwargs))
pass


Expand Down
25 changes: 0 additions & 25 deletions anyconfig/globals.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,32 +7,7 @@
import logging
import os


AUTHOR = 'Satoru SATOH <ssat@redhat.com>'
VERSION = "0.0.5"

_LOGGING_FORMAT = "%(asctime)s %(name)s: [%(levelname)s] %(message)s"


def get_logger(name="anyconfig", log_format=_LOGGING_FORMAT,
level=logging.WARNING):
"""
Initialize custom logger.
"""
if os.environ.get("ANYCONFIG_DEBUG", False):
level = logging.DEBUG

logging.basicConfig(level=level, format=log_format)
logger = logging.getLogger(name)

handler = logging.StreamHandler()
handler.setLevel(level)
handler.setFormatter(logging.Formatter(log_format))
logger.addHandler(handler)

return logger


LOGGER = get_logger()

# vim:sw=4:ts=4:et:
10 changes: 6 additions & 4 deletions anyconfig/template.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@
# Author: Satoru SATOH <ssato redhat.com>
# License: MIT
#
from anyconfig.compat import raw_input, copen
from anyconfig.globals import LOGGER

import os.path
import logging
import os

from anyconfig.compat import raw_input, copen

logger = logging.getLogger(__name__)

TEMPLATE_SUPPORT = False
try:
import jinja2
Expand All @@ -21,7 +23,7 @@ def tmpl_env(paths):
return jinja2.Environment(loader=jinja2.FileSystemLoader(paths))

except ImportError:
LOGGER.warn("Jinja2 is not available on your system, so "
logger.warn("Jinja2 is not available on your system, so "
"template support will be disabled.")

class TemplateNotFound(RuntimeError):
Expand Down
14 changes: 0 additions & 14 deletions anyconfig/tests/globals.py

This file was deleted.

0 comments on commit d163f05

Please sign in to comment.