confini

Parse and merge multiple ini files in python3
git clone git://git.defalsify.org/python-confini.git
Log | Files | Refs | README | LICENSE

commit 6d3c8d8e0164c84e1711dc174925f55da41e0ea5
parent 8280d7fb6d2d5acc044439f7b522d81cadc11588
Author: nolash <dev@holbrook.no>
Date:   Thu,  2 Sep 2021 10:51:00 +0200

Add module lookups for config dirs

Diffstat:
MCHANGELOG | 2++
Mconfini/config.py | 4++++
Mconfini/runnable/dump.py | 30++++++++++++++++++++++++++++--
Msetup.py | 2+-
4 files changed, 35 insertions(+), 3 deletions(-)

diff --git a/CHANGELOG b/CHANGELOG @@ -1,3 +1,5 @@ +0.4.2 + - Enable multiple config base and override for the dump tool 0.4.1 - Enable multiple config base directories when passing list as config_dir argument 0.4.0 diff --git a/confini/config.py b/confini/config.py @@ -29,6 +29,8 @@ class Config: def __init__(self, default_dir, env_prefix=None, override_dirs=[]): self.__target_tmpdir = None + if default_dir == None: + default_dir = override_dirs if isinstance(default_dir, list): self.collect_from_dirs(default_dir) else: @@ -174,6 +176,8 @@ class Config: d = os.listdir(tmp_dir) d.sort() c = 0 + + # TODO: this will fail of sections/options are repeated. should first use individual parser instances to flatten to single file (perhaps in collect_from_dirs already) for i, tmp_config_dir in enumerate(d): tmp_config_dir = os.path.join(tmp_dir, tmp_config_dir) for tmp_file in os.listdir(os.path.join(tmp_config_dir)): diff --git a/confini/runnable/dump.py b/confini/runnable/dump.py @@ -2,6 +2,9 @@ import sys import logging import argparse +import copy +import os +import stat # local imports from confini import Config @@ -13,7 +16,9 @@ logg = logging.getLogger() argparser = argparse.ArgumentParser() argparser.add_argument('-z', action='store_true', help='Truncate values in output') argparser.add_argument('-v', action='store_true', help='Be verbose') -argparser.add_argument('config_dir', type=str, help='Configuation directory to parse') +argparser.add_argument('--schema-dir', dest='schema_dir', action='append', type=str, help='Configuation directory to merge with schema definitions') +argparser.add_argument('--schema-module', dest='schema_module', action='append', type=str, default=[], help='Module path to merge with schema definitions') +argparser.add_argument('config_dir', nargs='*', type=str, help='Configuation directories to parse') args = argparser.parse_args() if args.v: @@ -21,7 +26,28 @@ if args.v: def main(): - c = Config(args.config_dir) + schema_dirs = [] + for m in args.schema_module: + md = m.replace('.', '/') + for i in range(len(sys.path)-1, -1, -1): + schema_mod_candidate = os.path.join(sys.path[i], md) + logg.debug('Probing config directory for module {} in {}'.format(m, schema_mod_candidate)) + try: + s = os.stat(schema_mod_candidate) + if not stat.S_ISDIR(s.st_mode): + continue + logg.info('Using config directory for module {} in {}'.format(m, schema_mod_candidate)) + except FileNotFoundError: + continue + schema_dirs.append(md) + + if args.schema_dir != None: + schema_dirs += args.schema_dir + + if len(schema_dirs) == 0: + schema_dirs = None + + c = Config(schema_dirs, override_dirs=args.config_dir) c.process() for k in c.store.keys(): v = c.get(k) diff --git a/setup.py b/setup.py @@ -6,7 +6,7 @@ f.close() setup( name='confini', - version='0.4.1a1', + version='0.4.2rc1', description='Parse, verify and merge all ini files in a single directory', author='Louis Holbrook', author_email='dev@holbrook.no',