from .common import pp, terminal_can_colors, to_bytes
-from .rec_dict import RecursiveDictionary
+from .merge import merge_structure
from .app import PpApplication
-__version__ = '0.3.3'
+__version__ = '0.4.1'
LOG = logging.getLogger(__name__)
self._cfg_encoding = cfg_encoding
self._need_config_file = bool(need_config_file)
- self.cfg = RecursiveDictionary()
+ self.cfg = {}
self._cfg_dir = None
self.cfg_stems = []
self.handle_error(msg, "Configuration error")
continue
+ cfg = {}
+ for section in config.sections():
+ if not section in cfg:
+ cfg[section] = {}
+ for (key, value) in config.items(section):
+ k = key.lower()
+ cfg[section][k] = value
+ if self.verbose > 2:
+ LOG.debug("Evaluated config from {f!r}:\n{c}".format(
+ f=cfg_file, c=pp(cfg)))
+ self.cfg = merge_structure(self.cfg, cfg)
+
+ if self.verbose > 1:
+ LOG.debug("Evaluated config total:\n{}".format(self.cfg))
+
# =============================================================================
if __name__ == "__main__":
--- /dev/null
+#!/usr/bin/python
+# -*- coding: utf-8 -*-
+"""
+@author: Frank Brehm
+@contact: frank.brehm@pixelpark.com
+"""
+
+import itertools
+
+# =============================================================================
+class ZipExhausted(Exception):
+ pass
+
+
+# =============================================================================
+def izip_longest(*args, **kwds):
+ '''
+ Function izip_longest() does not exists anymore in Python3 itertools.
+ Taken from https://docs.python.org/2/library/itertools.html#itertools.izip_longest
+ '''
+ # izip_longest('ABCD', 'xy', fillvalue='-') --> Ax By C- D-
+
+ fillvalue = kwds.get('fillvalue')
+ counter = [len(args) - 1]
+
+ # ------------------
+ def sentinel():
+ if not counter[0]:
+ raise ZipExhausted
+ counter[0] -= 1
+ yield fillvalue
+
+ # ------------------
+ fillers = itertools.repeat(fillvalue)
+ iterators = [itertools.chain(it, sentinel(), fillers) for it in args]
+ try:
+ while iterators:
+ yield tuple(map(next, iterators))
+ except ZipExhausted:
+ pass
+
+
+# =============================================================================
+def merge_structure(a, b):
+ '''
+ Taken from https://gist.github.com/saurabh-hirani/6f3f5d119076df70e0da
+ '''
+ if isinstance(a, dict) and isinstance(b, dict):
+ d = dict(a)
+ d.update({k: merge_structure(a.get(k, None), b[k]) for k in b})
+ return d
+
+ if isinstance(a, list) and isinstance(b, list):
+ is_a_nested = any(x for x in a if isinstance(x, list) or isinstance(x, dict))
+ is_b_nested = any(x for x in b if isinstance(x, list) or isinstance(x, dict))
+ if is_a_nested or is_b_nested:
+ return [merge_structure(x, y) for x, y in izip_longest(a, b)]
+ else:
+ return a + b
+
+ return a if b is None else b
+
+
+# =============================================================================
+
+if __name__ == "__main__":
+
+ pass
+
+# =============================================================================
+
+# vim: tabstop=4 expandtab shiftwidth=4 softtabstop=4