confini

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

commit a4a27351ec18de7af2ef8a140068f612da2fb467
parent 99f770b20b1213d66521a706ebebcc4f146e279a
Author: lash <dev@holbrook.no>
Date:   Wed, 11 May 2022 17:43:28 +0000

No force pgp key bundle src dir, add after the fact override and schema

Diffstat:
MCHANGELOG | 3+++
Mconfini/config.py | 60++++++++++++++++++++++++++++++++++++++++--------------------
Msetup.py | 2+-
Mtests/test_enc.py | 3++-
4 files changed, 46 insertions(+), 22 deletions(-)

diff --git a/CHANGELOG b/CHANGELOG @@ -1,3 +1,6 @@ +0.6.1 + - Make schema and override dirs settable after instantiation + - Remove forced source dir for pgp key bundles 0.6.0 - Empty entries in override config dirs now empties value in directive (was previously skipped) 0.5.7 diff --git a/confini/config.py b/confini/config.py @@ -29,21 +29,6 @@ class Config: def __init__(self, default_dir, env_prefix=None, override_dirs=[], skip_doc=False): self.parser = configparser.ConfigParser(strict=True) - self.__target_tmpdir = None - if default_dir == None: - default_dir = override_dirs - if isinstance(default_dir, list): - self.collect_from_dirs(default_dir) - else: - self.dirs = [default_dir] - if isinstance(override_dirs, str): - override_dirs = [override_dirs] - elif override_dirs == None: - override_dirs = [] - for d in override_dirs: - if not os.path.isdir(d): - raise OSError('{} is not a directory'.format(override_dirs)) - self.dirs.append(os.path.realpath(d)) self.skip_doc = skip_doc self.doc = None self.required = {} @@ -52,10 +37,42 @@ class Config: self.decrypt = [] self.env_prefix = None self.src_dirs = {} + self.__override_dirs = [] + self.__schema_dirs = [] + self.__processed = False + self.dirs = [] + if env_prefix != None: logg.info('using prefix {} for environment variable override matches'.format(env_prefix)) self.env_prefix = '{}_'.format(env_prefix) + self.add_schema_dir(default_dir) + + self.__target_tmpdir = None + + if isinstance(override_dirs, str): + override_dirs = [override_dirs] + for d in override_dirs: + self.add_override_dir(d) + + + def __collect(self): + self.collect_from_dirs(self.__schema_dirs) + for d in self.__override_dirs: + self.dirs.append(d) + + + def add_override_dir(self, v): + if not os.path.isdir(v): + raise OSError('{} is not a directory'.format(v)) + self.__override_dirs.append(v) + + + def add_schema_dir(self, v): + if not os.path.isdir(v): + raise OSError('{} is not a directory'.format(v)) + self.__schema_dirs.append(v) + def __clean(self): if self.__target_tmpdir != None: @@ -242,9 +259,11 @@ class Config: def process(self, set_as_current=False): """Concatenates all .ini files in the config directory attribute and parses them to memory """ + self.__collect() + tmp_dir = tempfile.mkdtemp() logg.debug('using tmp processing dir {}'.format(tmp_dir)) - + self.__collect_dir(tmp_dir) self.__process_schema_dir(tmp_dir, allow_empty=True) @@ -257,12 +276,12 @@ class Config: self.__clean() - def _decrypt(self, k, v, src_dir): + def _decrypt(self, k, v): if len(self.decrypt) == 0: return v for decrypter in self.decrypt: logg.debug('applying decrypt with {}'.format(str(decrypter))) - (v, r) = decrypter.decrypt(k, v, src_dir) + (v, r) = decrypter.decrypt(k, v) if r: return v return v @@ -281,7 +300,8 @@ class Config: else: return None - return self._decrypt(k, v, self.src_dirs.get(k)) + #return self._decrypt(k, v, self.src_dirs.get(k)) + return self._decrypt(k, v) def remove(self, k, strict=True): @@ -315,7 +335,7 @@ class Config: v = self.store.get(k) if type(v).__name__ == 'bool': return v - d = self._decrypt(k, v, self.src_dirs.get(k)) + d = self._decrypt(k, v) #, self.src_dirs.get(k)) if d == None: return False if d.lower() not in ['true', 'false', '0', '1', 'on', 'off']: diff --git a/setup.py b/setup.py @@ -6,7 +6,7 @@ f.close() setup( name='confini', - version='0.6.0', + version='0.6.1', description='Parse, verify and merge all ini files in a single directory', author='Louis Holbrook', author_email='dev@holbrook.no', diff --git a/tests/test_enc.py b/tests/test_enc.py @@ -26,8 +26,9 @@ class TestBasic(unittest.TestCase): def test_enc(self): inidir = os.path.join(self.wd, 'files/crypt') + logg.debug('inidir {}'.format(inidir)) c = Config(inidir) - decrypter = PGPDecrypter() + decrypter = PGPDecrypter(base_dir=inidir) c.add_decrypt(decrypter) c.process() with self.assertRaises(DecryptError):