confini

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

commit 9638ea60a8d00f373ea9a6686dd3542e0716c049
parent eb824b45e1c0b0a47032fc20e886cee17b3a856e
Author: nolash <dev@holbrook.no>
Date:   Fri, 19 Feb 2021 12:07:10 +0100

Add test for add guard and lots of missing files

Diffstat:
MCHANGELOG | 1+
Mconfini/config.py | 9+++++++--
Asetup.cfg | 15+++++++++++++++
Msetup.py | 2+-
Atest/files/default/foo.ini | 3+++
Atest/files/translate/foo.ini | 10++++++++++
Mtest/test_basic.py | 14++++++++++++++
Atest/test_translate.py | 49+++++++++++++++++++++++++++++++++++++++++++++++++
8 files changed, 100 insertions(+), 3 deletions(-)

diff --git a/CHANGELOG b/CHANGELOG @@ -1,5 +1,6 @@ 0.3.5 - Fix logging bug in 0.3.4 update + - Add key exist guard in add method 0.3.4 - Fix bug in 0.3.3 update 0.3.3 diff --git a/confini/config.py b/confini/config.py @@ -43,7 +43,12 @@ class Config: self.env_prefix = '{}_'.format(env_prefix) - def add(self, value, constant_name): + def add(self, value, constant_name, exists_ok=False): + if self.store.get(constant_name) != None: + if not exists_ok: + raise AttributeError('config key {} already exists'.format(constant_name)) + else: + logg.debug('overwriting key {}'.format(constant_name)) self.store[constant_name] = value @@ -101,7 +106,7 @@ class Config: val = self.store.get(cn, v) else: logg.info('{} {} overrides {}'.format(dct_description, cn_env, cn)) - self.add(val, cn) + self.add(val, cn, exists_ok=True) def process(self, set_as_current=False): diff --git a/setup.cfg b/setup.cfg @@ -0,0 +1,15 @@ +[metadata] +classifiers = + Environment :: No Input/Output (Daemon) + Programming Language :: Python :: 3 + Operating System :: OS Independent + Development Status :: 3 - Alpha + Intended Audience :: Developers + Topic :: Software Development :: Libraries + License :: OSI Approved :: GNU General Public License v3 or later (GPLv3+) +license_files = + LICENSE.txt + +[options.entry_points] +console_scripts = + confini-dump = confini.runnable.dump:main diff --git a/setup.py b/setup.py @@ -6,7 +6,7 @@ f.close() setup( name='confini', - version='0.3.6b2', + version='0.3.6rc1', description='Parse, verify and merge all ini files in a single directory', author='Louis Holbrook', author_email='dev@holbrook.no', diff --git a/test/files/default/foo.ini b/test/files/default/foo.ini @@ -0,0 +1,3 @@ +[FOO] +bar = xyzzy +baz = diff --git a/test/files/translate/foo.ini b/test/files/translate/foo.ini @@ -0,0 +1,10 @@ +[true] +a = true +b = True +c = 1 + +[false] +a = false +b = False +c = 0 +d = diff --git a/test/test_basic.py b/test/test_basic.py @@ -35,6 +35,17 @@ class TestBasic(unittest.TestCase): r = c.get('FOO_BAZBAZ') + def test_overwrite_guard(self): + inidir = os.path.join(self.wd, 'files/default') + c = Config(inidir) + c.process() + with self.assertRaises(AttributeError): + c.add('xxx', 'FOO_BAR') + c.add('xxx', 'FOO_BAR', exists_ok=True) + r = c.get('FOO_BAR') + self.assertEqual(r, 'xxx') + + def test_parse_two_files(self): inidir = os.path.join(self.wd, 'files') c = Config(inidir) @@ -58,5 +69,8 @@ class TestBasic(unittest.TestCase): self.assertFalse(c.validate()) logg.debug(c) + + + if __name__ == '__main__': unittest.main() diff --git a/test/test_translate.py b/test/test_translate.py @@ -0,0 +1,49 @@ +#!/usr/bin/python + +import os +import unittest +import logging + +from confini import Config + +logging.basicConfig(level=logging.DEBUG) +logg = logging.getLogger() + + +class TestTranslate(unittest.TestCase): + + wd = os.path.dirname(__file__) + + def setUp(self): + pass + + + def tearDown(self): + pass + + + def test_parse_default(self): + inidir = os.path.join(self.wd, 'files/translate') + c = Config(inidir) + c.process() + + self.assertTrue(c.get('TRUE_A')) + self.assertTrue(c.get('TRUE_B')) + self.assertTrue(c.get('TRUE_C')) + + self.assertFalse(c.true('FALSE_A')) + self.assertFalse(c.true('FALSE_B')) + self.assertFalse(c.true('FALSE_C')) + self.assertIsNone(c.get('FALSE_D')) + + o = { + 'TRUE_A': True, + 'FALSE_A': False, + } + c.dict_override(o, 'test') + self.assertTrue(c.true('TRUE_A')) + self.assertFalse(c.true('FALSE_A')) + + +if __name__ == '__main__': + unittest.main()