Skip to content

Commit

Permalink
reorder imports to rules in PEP8 and remove alises as much as possible
Browse files Browse the repository at this point in the history
  • Loading branch information
ssato committed Mar 14, 2015
1 parent 3ced85a commit 50123ec
Show file tree
Hide file tree
Showing 23 changed files with 277 additions and 266 deletions.
39 changes: 19 additions & 20 deletions anyconfig/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,14 @@
#
"""Public APIs of anyconfig module.
"""

import logging

import anyconfig.globals as G
import anyconfig.mergeabledict as M
import anyconfig.backend.backends as Backends
import anyconfig.backend.json_ as BJ
import anyconfig.parser as P
import anyconfig.template as AT
import anyconfig.utils as U
import anyconfig.backend.backends
import anyconfig.backend.json_
import anyconfig.mergeabledict
import anyconfig.parser
import anyconfig.template
import anyconfig.utils

# pylint: disable=W0611
# Import some global constants will be re-exported:
Expand All @@ -25,10 +23,10 @@

# pylint: disable=C0103
# Re-export:
list_types = Backends.list_types # flake8: noqa
list_types = anyconfig.backend.backends.list_types # flake8: noqa

# aliases:
container = M.MergeableDict
container = anyconfig.mergeabledict.MergeableDict
# pylint: enable=C0103

logger = logging.getLogger(__name__)
Expand All @@ -48,12 +46,12 @@ def find_loader(config_path, forced_type=None):
:return: ConfigParser-inherited class object
"""
if forced_type is not None:
cparser = Backends.find_by_type(forced_type)
cparser = anyconfig.backend.backends.find_by_type(forced_type)
if not cparser:
logger.error("No parser found for given type: %s", forced_type)
return None
else:
cparser = Backends.find_by_file(config_path)
cparser = anyconfig.backend.backends.find_by_file(config_path)
if not cparser:
logger.error("No parser found for given file: %s", config_path)
return None
Expand All @@ -80,7 +78,7 @@ def single_load(config_path, forced_type=None, ignore_missing=False,
anyconfig.mergeabledict.MergeableDict by default) supports merge
operations.
"""
config_path = U.ensure_expandusr(config_path)
config_path = anyconfig.utils.ensure_expandusr(config_path)

cparser = find_loader(config_path, forced_type)
if cparser is None:
Expand All @@ -90,7 +88,7 @@ def single_load(config_path, forced_type=None, ignore_missing=False,
if template:
try:
logger.debug("Compiling: %s", config_path)
config_content = AT.render(config_path, context)
config_content = anyconfig.template.render(config_path, context)
return cparser.loads(config_content, ignore_missing=ignore_missing,
**kwargs)
except:
Expand Down Expand Up @@ -133,7 +131,7 @@ def multi_load(paths, forced_type=None, merge=MS_DICTS, marker='*',
assert merge in MERGE_STRATEGIES, "Invalid merge strategy: " + merge

if marker in paths:
paths = U.sglob(paths)
paths = anyconfig.utils.sglob(paths)

config = container.create(context) if context else container()
for path in paths:
Expand Down Expand Up @@ -171,7 +169,7 @@ def load(path_specs, forced_type=None, merge=MS_DICTS, marker='*',
anyconfig.mergeabledict.MergeableDict by default) supports merge
operations.
"""
if marker in path_specs or U.is_iterable(path_specs):
if marker in path_specs or anyconfig.utils.is_iterable(path_specs):
return multi_load(path_specs, forced_type, merge, marker,
ignore_missing, template, context, **kwargs)
else:
Expand All @@ -195,16 +193,17 @@ def loads(config_content, forced_type=None, template=True, context={},
operations.
"""
if forced_type is None:
return P.parse(config_content)
return anyconfig.parser.parse(config_content)

cparser = find_loader(None, forced_type)
if cparser is None:
return P.parse(config_content)
return anyconfig.parser.parse(config_content)

if template:
try:
logger.debug("Compiling")
config_content = AT.render_s(config_content, context)
config_content = anyconfig.template.render_s(config_content,
context)
except:
logger.warn("Failed to compile and fallback to no template "
"mode: '%s'", config_content[:50] + '...')
Expand All @@ -225,7 +224,7 @@ def _find_dumper(config_path, forced_type=None):
if cparser is None or not getattr(cparser, "dump", False):
logger.warn("Dump method not implemented. Fallback to "
"JsonConfigParser")
cparser = BJ.JsonConfigParser()
cparser = anyconfig.backend.json_.JsonConfigParser()

return cparser

Expand Down
44 changes: 23 additions & 21 deletions anyconfig/backend/backends.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
#
# Copyright (C) 2011 - 2013 Satoru SATOH <ssato @ redhat.com>
# Copyright (C) 2011 - 2015 Satoru SATOH <ssato @ redhat.com>
# License: MIT
#
from anyconfig.compat import cmp
# pylint: disable=W0622
from itertools import groupby
from operator import methodcaller

import anyconfig.backend.ini_ as BINI
import anyconfig.backend.json_ as BJSON
import anyconfig.backend.xml_ as BXML
import anyconfig.backend.yaml_ as BYAML
import anyconfig.utils as U
import itertools
import operator
import pkg_resources

import anyconfig.backend.ini_
import anyconfig.backend.json_
import anyconfig.backend.xml_
import anyconfig.backend.yaml_
import anyconfig.compat
import anyconfig.utils

_CPs = [p for p in [BINI.IniConfigParser, BJSON.JsonConfigParser,
BYAML.YamlConfigParser, BXML.XmlConfigParser,
] if p.supports()]

_CPs = [p for p in [anyconfig.backend.ini_.IniConfigParser,
anyconfig.backend.json_.JsonConfigParser,
anyconfig.backend.xml_.XmlConfigParser,
anyconfig.backend.yaml_.YamlConfigParser] if p.supports()]

for e in pkg_resources.iter_entry_points("anyconfig_backends"):
try:
Expand All @@ -29,7 +29,7 @@
def cmp_cps(lhs, rhs):
"""Compare config parsers by these priorities.
"""
return cmp(lhs.priority(), rhs.priority())
return anyconfig.compat.cmp(lhs.priority(), rhs.priority())


def fst(tpl):
Expand All @@ -49,7 +49,7 @@ def snd(tpl):


def groupby_key(xs, kf):
return groupby(sorted(xs, key=kf), key=kf)
return itertools.groupby(sorted(xs, key=kf), key=kf)


def uniq(iterable):
Expand All @@ -69,19 +69,21 @@ def list_parsers_by_type(cps=_CPs):
"""
:return: List (generator) of (config_type, [config_parser])
"""
return ((t, sorted(p, key=methodcaller("priority"))) for t, p in
groupby_key(cps, methodcaller("type")))
return ((t, sorted(p, key=operator.methodcaller("priority"))) for t, p
in groupby_key(cps, operator.methodcaller("type")))


def _list_xppairs(xps):
return sorted((snd(xp) for xp in xps), key=methodcaller("priority"))
return sorted((snd(xp) for xp in xps),
key=operator.methodcaller("priority"))


def list_parsers_by_extension(cps=_CPs):
"""
:return: List (generator) of (config_ext, [config_parser])
"""
cps_by_ext = U.concat(([(x, p) for x in p.extensions()] for p in cps))
cps_by_ext = anyconfig.utils.concat(([(x, p) for x in p.extensions()] for p
in cps))

return ((x, _list_xppairs(xps)) for x, xps in groupby_key(cps_by_ext, fst))

Expand All @@ -92,7 +94,7 @@ def find_by_file(config_file, cps=_CPs):
:param config_file: Config file path
"""
ext = U.get_file_extension(config_file)
ext = anyconfig.utils.get_file_extension(config_file)
for x, ps in list_parsers_by_extension(cps):
if x == ext:
return ps[-1]
Expand Down
17 changes: 8 additions & 9 deletions anyconfig/backend/base.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
#
# Copyright (C) 2012, 2013 Satoru SATOH <ssato @ redhat.com>
# Copyright (C) 2012 - 2015 Satoru SATOH <ssato @ redhat.com>
# License: MIT
#

import logging
import os

from anyconfig.compat import StringIO

import anyconfig.mergeabledict as D
import anyconfig.utils as U
import anyconfig.compat
import anyconfig.mergeabledict
import anyconfig.utils

SUPPORTED = False

Expand Down Expand Up @@ -54,7 +52,7 @@ class ConfigParser(object):
_type = None
_priority = 0 # 0 (lowest priority) .. 99 (highest priority)
_extensions = []
_container = D.MergeableDict
_container = anyconfig.mergeabledict.MergeableDict
_supported = False

_load_opts = []
Expand All @@ -78,7 +76,8 @@ def supports(cls, config_file=None):
return cls._supported
else:
return cls._supported and \
U.get_file_extension(config_file) in cls._extensions
anyconfig.utils.get_file_extension(config_file) \
in cls._extensions

@classmethod
def container(cls):
Expand Down Expand Up @@ -110,7 +109,7 @@ def loads(cls, config_content, **kwargs):
:return: cls.container() object holding config parameters
"""
config_fp = StringIO(config_content)
config_fp = anyconfig.compat.StringIO(config_content)
create = cls.container().create
return create(cls.load_impl(config_fp,
**mk_opt_args(cls._load_opts, kwargs)))
Expand Down
7 changes: 3 additions & 4 deletions anyconfig/backend/ini_.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
#
# Copyright (C) 2011 - 2013 Satoru SATOH <ssato @ redhat.com>
# Copyright (C) 2011 - 2015 Satoru SATOH <ssato @ redhat.com>
# License: MIT
#

import logging
import sys

from anyconfig.compat import configparser, iteritems

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

from anyconfig.compat import configparser, iteritems

logger = logging.getLogger(__name__)

SUPPORTED = True # It should be available w/ python dist always.
Expand Down
6 changes: 3 additions & 3 deletions anyconfig/backend/json_.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
#
# Copyright (C) 2011 - 2014 Satoru SATOH <ssato @ redhat.com>
# Copyright (C) 2011 - 2015 Satoru SATOH <ssato @ redhat.com>
# License: MIT
#
# Ref. python -c "import json; help(json)"
#
import anyconfig.backend.base as Base
import anyconfig.compat as C
import anyconfig.compat


SUPPORTED = True
Expand All @@ -30,7 +30,7 @@ def dict_to_container(json_obj_dict):

# It seems that 'encoding' argument is not allowed in json.load[s] and
# json.dump[s] in JSON module in python 3.x.
if not C.IS_PYTHON_3:
if not anyconfig.compat.IS_PYTHON_3:
_LOAD_OPTS.append("encoding")
_DUMP_OPTS.append("encoding")

Expand Down
30 changes: 17 additions & 13 deletions anyconfig/backend/tests/backends.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
#
# Copyright (C) 2012 - 2014 Satoru SATOH <ssato @ redhat.com>
# Copyright (C) 2012 - 2015 Satoru SATOH <ssato @ redhat.com>
# License: MIT
#
import anyconfig.backend.backends as T
import random
import unittest

import anyconfig.backend.backends as TT
import anyconfig.backend.ini_ as BINI
import anyconfig.backend.json_ as BJSON
import anyconfig.backend.yaml_ as BYAML


class Test_00_pure_functions(unittest.TestCase):

Expand All @@ -15,36 +19,36 @@ def test_10_find_by_file(self):
jsn_cfs = ["/a/b/c.jsn", "/a/b/c.json", "/a/b/c.js"]
yml_cfs = ["/a/b/c.yml", "/a/b/c.yaml"]

self.assertTrue(ini_cf, T.BINI.IniConfigParser)
self.assertTrue(T.find_by_file(unknown_cf) is None)
self.assertTrue(ini_cf, BINI.IniConfigParser)
self.assertTrue(TT.find_by_file(unknown_cf) is None)

for f in jsn_cfs:
self.assertTrue(f, T.BJSON.JsonConfigParser)
self.assertTrue(f, BJSON.JsonConfigParser)

for f in yml_cfs:
self.assertTrue(f, T.BYAML.YamlConfigParser)
self.assertTrue(f, BYAML.YamlConfigParser)

def test_20_find_by_type(self):
ini_t = "ini"
jsn_t = "json"
yml_t = "yaml"
unknown_t = "unknown_type"

self.assertTrue(ini_t, T.BINI.IniConfigParser)
self.assertTrue(jsn_t, T.BJSON.JsonConfigParser)
self.assertTrue(yml_t, T.BYAML.YamlConfigParser)
self.assertTrue(T.find_by_type(unknown_t) is None)
self.assertTrue(ini_t, BINI.IniConfigParser)
self.assertTrue(jsn_t, BJSON.JsonConfigParser)
self.assertTrue(yml_t, BYAML.YamlConfigParser)
self.assertTrue(TT.find_by_type(unknown_t) is None)

def test_30_list_types(self):
types = T.list_types()
types = TT.list_types()

self.assertTrue(isinstance(types, list))
self.assertTrue(bool(list)) # ensure it's not empty.

def test_40_cmp_cps(self):
cps = T._CPs
cps = TT._CPs
if cps:
x = T.cmp_cps(random.choice(cps), random.choice(cps))
x = TT.cmp_cps(random.choice(cps), random.choice(cps))
self.assertTrue(x in (-1, 0, 1))

# vim:sw=4:ts=4:et:
Loading

0 comments on commit 50123ec

Please sign in to comment.