confini

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

commit 3ac1f8e41ec669062c9fcf0df8e6354d29495552
parent b87b21649ddf86bdf884a279c0d776abb8d22c63
Author: nolash <dev@holbrook.no>
Date:   Thu, 17 Dec 2020 13:04:59 +0100

Fix bug in dict override code

Diffstat:
Mconfini/config.py | 35++++++++++++++++++++++++-----------
Mtest/test_env.py | 19++++++++++++++++++-
2 files changed, 42 insertions(+), 12 deletions(-)

diff --git a/confini/config.py b/confini/config.py @@ -77,19 +77,32 @@ class Config: return '{}_{}'.format(section.upper(), directive.upper()) - def process_override(self, dct, dct_description): + def _sections_override(self, dct, dct_description): for s in self.parser.sections(): for k in self.parser[s]: cn = Config.to_constant_name(k, s) - cn_env = cn - if self.env_prefix != None: - cn_env = self.env_prefix + cn - val = dct.get(cn_env) - if val == None or val == '': - val = self.parser[s][k] - else: - logg.info('{} {} overrides {}'.format(dct_description, cn_env, cn)) - self.add(val, cn) + self.override(cn, self.parser[s][k], dct, dct_description) + + + def dict_override(self, dct, dct_description): + for k in dct.keys(): + try: + logg.debug('v {} {}'.format(k, dct)) + self.override(k, self.store[k], dct, dct_description) + except KeyError: + logg.warning('override key {} have no match in config store'.format(cn)) + + + def override(self, cn, v, dct, dct_description): + cn_env = cn + if self.env_prefix != None: + cn_env = self.env_prefix + cn + val = dct.get(cn_env) + if val == None or val == '': + val = self.store.get(cn, v) + else: + logg.info('{} {} overrides {}'.format(dct_description, cn_env, cn)) + self.add(val, cn) def process(self, set_as_current=False): @@ -112,7 +125,7 @@ class Config: tmp.close() self.parser.read(tmpname) os.unlink(tmpname) - self.process_override(os.environ, 'environment variable') + self._sections_override(os.environ, 'environment variable') if set_as_current: set_current(self, description=self.dir) diff --git a/test/test_env.py b/test/test_env.py @@ -29,7 +29,7 @@ class TestEnv(unittest.TestCase): 'XYZZY_BERT': 'oscar', 'BAR_FOO': None, } - c.process_override(override_dict, 'arbitrary dict') + c.dict_override(override_dict, 'arbitrary dict') expect = { 'FOO_BAR': '666', @@ -39,6 +39,23 @@ class TestEnv(unittest.TestCase): } self.assertDictEqual(expect, c.store) + for k in c.store: + logg.debug('have now {} {}'.format(k, c.store[k])) + override_dict = { + 'BAR_FOO': 'barbarbar', + } + c.dict_override(override_dict, 'arbitrary dict') + + expect = { + 'FOO_BAR': '666', + 'FOO_BAZ': '029a', + 'BAR_FOO': 'barbarbar', + 'XYZZY_BERT': 'oscar', + } + self.assertDictEqual(expect, c.store) + + + def test_env_a_override(self): os.environ['FOO_BAR'] = '43'