confini

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

commit 8280d7fb6d2d5acc044439f7b522d81cadc11588
parent eaa8af585c824a30707d565e928ca0846d5d0bc7
Author: nolash <dev@holbrook.no>
Date:   Sun, 18 Jul 2021 13:31:29 +0200

Handle multiple base directories

Diffstat:
MCHANGELOG | 2++
Mconfini/config.py | 29++++++++++++++++++++++++++++-
Msetup.py | 2+-
3 files changed, 31 insertions(+), 2 deletions(-)

diff --git a/CHANGELOG b/CHANGELOG @@ -1,3 +1,5 @@ +0.4.1 + - Enable multiple config base directories when passing list as config_dir argument 0.4.0 - Enable multiple config override directories 0.3.6 diff --git a/confini/config.py b/confini/config.py @@ -1,9 +1,12 @@ +# standard imports import logging import sys import os import tempfile import configparser import re + +# external imports import gnupg logg = logging.getLogger('confini') @@ -17,13 +20,19 @@ def set_current(conf, description=''): current_config = conf + + class Config: parser = configparser.ConfigParser(strict=True) default_censor_string = '***' def __init__(self, default_dir, env_prefix=None, override_dirs=[]): - self.dirs = [default_dir] + self.__target_tmpdir = None + if isinstance(default_dir, list): + self.collect_from_dirs(default_dir) + else: + self.dirs = [default_dir] for d in override_dirs: if not os.path.isdir(d): raise OSError('{} is not a directory'.format(override_dirs)) @@ -38,6 +47,24 @@ class Config: logg.info('using prefix {} for environment variable override matches'.format(env_prefix)) self.env_prefix = '{}_'.format(env_prefix) + def collect_from_dirs(self, dirs): + self.__target_tmpdir = tempfile.TemporaryDirectory() + self.dirs = [self.__target_tmpdir.name] + for i, d in enumerate(dirs): + for filename_in in os.listdir(d): + if re.match(r'.+\.ini$', filename_in) == None: + continue + filename_out = '{}_{}'.format(i, filename_in) + in_filepath = os.path.join(d, filename_in) + out_filepath = os.path.join(self.dirs[0], filename_out) + fr = open(in_filepath, 'rb') + fw = open(out_filepath, 'wb') + fw.write(fr.read()) + fw.close() + fr.close() + logg.debug('base config {} will be processed as {}'.format(in_filepath, out_filepath)) +# return target_dir.name + def add_decrypt(self, decrypter): self.decrypt.append(decrypter) diff --git a/setup.py b/setup.py @@ -6,7 +6,7 @@ f.close() setup( name='confini', - version='0.4.0a2', + version='0.4.1a1', description='Parse, verify and merge all ini files in a single directory', author='Louis Holbrook', author_email='dev@holbrook.no',