commit ac8082ef43e66f44da4a52294887ba53d70cff60
parent 6e62499dd19d741cfdf7743921dcb42f7777b685
Author: nolash <dev@holbrook.no>
Date: Tue, 20 Oct 2020 10:08:35 +0200
Default value emptry string return none
Diffstat:
4 files changed, 25 insertions(+), 3 deletions(-)
diff --git a/CHANGELOG b/CHANGELOG
@@ -1,3 +1,5 @@
+0.2.6
+ - Return default on empty string value
0.2.5
- Add default value as second argument to get
0.2.4
diff --git a/confini/config.py b/confini/config.py
@@ -138,10 +138,14 @@ class Config:
def get(self, k, default=None):
- v = self.store.get(k)
+ v = self.store[k]
if v == None:
if default != None:
- logg.debug('returning default value for {}'.format(k))
+ logg.debug('returning default value for empty value {}'.format(k))
+ return default
+ if type(v).__name__ == 'str' and v == '':
+ if default != None:
+ logg.debug('returning default value for empty string value {}'.format(k))
return default
return self._decrypt(k, v)
diff --git a/setup.py b/setup.py
@@ -6,7 +6,7 @@ f.close()
setup(
name='confini',
- version='0.2.5',
+ version='0.2.6',
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_basic.py b/test/test_basic.py
@@ -16,9 +16,25 @@ class TestBasic(unittest.TestCase):
def setUp(self):
pass
+
def tearDown(self):
pass
+
+ def test_parse_default(self):
+ inidir = os.path.join(self.wd, 'files/default')
+ c = Config(inidir)
+ c.process()
+ r = c.get('FOO_BAR', 'plugh')
+ self.assertEqual(r, 'xyzzy')
+ r = c.get('FOO_BAZ', 'plugh')
+ self.assertEqual(r, 'plugh')
+ r = c.get('FOO_BAZ')
+ self.assertEqual(r, None)
+ with self.assertRaises(KeyError):
+ r = c.get('FOO_BAZBAZ')
+
+
def test_parse_two_files(self):
inidir = os.path.join(self.wd, 'files')
c = Config(inidir)