From: Frank Brehm Date: Fri, 17 Mar 2017 10:59:27 +0000 (+0100) Subject: Adding evaluation af configurations X-Git-Tag: 0.1.2~255 X-Git-Url: https://git.uhu-banane.net/?a=commitdiff_plain;h=304a2fa3c803fe2306dfd9705f4b190ca328cb36;p=pixelpark%2Fadmin-tools.git Adding evaluation af configurations --- diff --git a/etc/.gitignore b/etc/.gitignore new file mode 100644 index 0000000..d2127d0 --- /dev/null +++ b/etc/.gitignore @@ -0,0 +1 @@ +*.ini diff --git a/etc/ldap.ini.default b/etc/ldap.ini.default new file mode 100644 index 0000000..00708c9 --- /dev/null +++ b/etc/ldap.ini.default @@ -0,0 +1,9 @@ +[LDAP] + +host = ldap.pixelpark.com +port = 389 +base_dn = o=isp +bind_dn = uid=Solaris_NSS,ou=Unix NSS,ou=Applications,o=pixelpark,o=isp +bind_pw = .nss.pro +timeout = 5 + diff --git a/pp_lib/cfg_app.py b/pp_lib/cfg_app.py index ac6b4ac..0a7225d 100644 --- a/pp_lib/cfg_app.py +++ b/pp_lib/cfg_app.py @@ -38,11 +38,11 @@ from .errors import FunctionNotImplementedError, PpAppError 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__) @@ -70,7 +70,7 @@ class PpConfigApplication(PpApplication): 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 = [] @@ -395,6 +395,21 @@ class PpConfigApplication(PpApplication): 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__": diff --git a/pp_lib/merge.py b/pp_lib/merge.py new file mode 100644 index 0000000..7daadc7 --- /dev/null +++ b/pp_lib/merge.py @@ -0,0 +1,72 @@ +#!/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