confini

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

commit b87b21649ddf86bdf884a279c0d776abb8d22c63
parent 2e116935021584bc8531d97d738344b1c5572566
Author: nolash <dev@holbrook.no>
Date:   Thu, 17 Dec 2020 10:38:40 +0100

Export dict override method

Diffstat:
MCHANGELOG | 2++
Mconfini/config.py | 8++++----
Msetup.py | 2+-
Mtest/test_env.py | 23+++++++++++++++++++----
4 files changed, 26 insertions(+), 9 deletions(-)

diff --git a/CHANGELOG b/CHANGELOG @@ -1,3 +1,5 @@ +0.3.3 + - Export overrider function to enable use with arbitrary dict 0.3.2 - Move config dump script to entry_point.console_scripts - Add truncate value option to dump tool diff --git a/confini/config.py b/confini/config.py @@ -77,18 +77,18 @@ class Config: return '{}_{}'.format(section.upper(), directive.upper()) - def _process_env(self): + def process_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 = os.environ.get(cn_env) + val = dct.get(cn_env) if val == None or val == '': val = self.parser[s][k] else: - logg.info('environment variable {} overrides {}'.format(cn_env, cn)) + logg.info('{} {} overrides {}'.format(dct_description, cn_env, cn)) self.add(val, cn) @@ -112,7 +112,7 @@ class Config: tmp.close() self.parser.read(tmpname) os.unlink(tmpname) - self._process_env() + self.process_override(os.environ, 'environment variable') if set_as_current: set_current(self, description=self.dir) diff --git a/setup.py b/setup.py @@ -6,7 +6,7 @@ f.close() setup( name='confini', - version='0.3.2', + version='0.3.3', description='Parse, verify and merge all ini files in a single directory', author='Louis Holbrook', author_email='dev@holbrook.no', diff --git a/test/test_env.py b/test/test_env.py @@ -19,19 +19,33 @@ class TestEnv(unittest.TestCase): def tearDown(self): pass - def test_env_a_override(self): - os.environ['FOO_BAR'] = '43' + def test_dict_override(self): inidir = os.path.join(self.wd, 'files') c = Config(inidir) c.process() + + override_dict = { + 'FOO_BAR': '666', + 'XYZZY_BERT': 'oscar', + 'BAR_FOO': None, + } + c.process_override(override_dict, 'arbitrary dict') + expect = { - 'FOO_BAR': '43', + 'FOO_BAR': '666', 'FOO_BAZ': '029a', 'BAR_FOO': 'oof', - 'XYZZY_BERT': 'ernie', + 'XYZZY_BERT': 'oscar', } self.assertDictEqual(expect, c.store) + + def test_env_a_override(self): + os.environ['FOO_BAR'] = '43' + inidir = os.path.join(self.wd, 'files') + c = Config(inidir) + c.process() + os.environ['ZZZ_FOO_BAR'] = '44' inidir = os.path.join(self.wd, 'files') c = Config(inidir, 'ZZZ') @@ -55,5 +69,6 @@ class TestEnv(unittest.TestCase): 'XYZZY_BERT': 'ernie', } self.assertDictEqual(expect, c.store) + if __name__ == '__main__': unittest.main()