2
0
Fork 0

feat: add parse_bool() and parse_list() methods for config object

This commit is contained in:
Lance Edgar 2024-11-23 15:33:00 -06:00
parent 2deba45588
commit 49e77d7407
2 changed files with 128 additions and 8 deletions

View file

@ -268,14 +268,14 @@ class WuttaConfig:
# bring in any "required" files # bring in any "required" files
requires = config.get(f'{self.appname}.config.require') requires = config.get(f'{self.appname}.config.require')
if requires: if requires:
for path in parse_list(requires): for path in self.parse_list(requires):
path = path % {'here': here} path = path % {'here': here}
self._load_ini_configs(path, configs, require=True) self._load_ini_configs(path, configs, require=True)
# bring in any "included" files # bring in any "included" files
includes = config.get(f'{self.appname}.config.include') includes = config.get(f'{self.appname}.config.include')
if includes: if includes:
for path in parse_list(includes): for path in self.parse_list(includes):
path = path % {'here': here} path = path % {'here': here}
self._load_ini_configs(path, configs, require=False) self._load_ini_configs(path, configs, require=False)
@ -475,11 +475,10 @@ class WuttaConfig:
Retrieve a boolean value from config. Retrieve a boolean value from config.
Accepts same params as :meth:`get()` but if a value is found, Accepts same params as :meth:`get()` but if a value is found,
it will be coerced to boolean via it will be coerced to boolean via :meth:`parse_bool()`.
:func:`~wuttjamaican.util.parse_bool()`.
""" """
value = self.get(*args, **kwargs) value = self.get(*args, **kwargs)
return parse_bool(value) return self.parse_bool(value)
def get_int(self, *args, **kwargs): def get_int(self, *args, **kwargs):
""" """
@ -498,15 +497,14 @@ class WuttaConfig:
Retrieve a list value from config. Retrieve a list value from config.
Accepts same params as :meth:`get()` but if a value is found, Accepts same params as :meth:`get()` but if a value is found,
it will be coerced to list via it will be coerced to list via :meth:`parse_list()`.
:func:`~wuttjamaican.util.parse_list()`.
:returns: If a value is found, a list is returned. If no :returns: If a value is found, a list is returned. If no
value, returns ``None``. value, returns ``None``.
""" """
value = self.get(*args, **kwargs) value = self.get(*args, **kwargs)
if value is not None: if value is not None:
return parse_list(value) return self.parse_list(value)
def get_dict(self, prefix): def get_dict(self, prefix):
""" """
@ -549,6 +547,20 @@ class WuttaConfig:
return values.as_dict() return values.as_dict()
def parse_bool(self, value):
"""
Convenience wrapper for
:func:`wuttjamaican.util.parse_bool()`.
"""
return parse_bool(value)
def parse_list(self, value):
"""
Convenience wrapper for
:func:`wuttjamaican.util.parse_list()`.
"""
return parse_list(value)
def _configure_logging(self): def _configure_logging(self):
""" """
This will save the current config parser defaults to a This will save the current config parser defaults to a

View file

@ -17,6 +17,9 @@ from wuttjamaican.testing import FileTestCase
class TestWuttaConfig(FileTestCase): class TestWuttaConfig(FileTestCase):
def make_config(self, **kwargs):
return mod.WuttaConfig(**kwargs)
def test_contstructor_basic(self): def test_contstructor_basic(self):
config = conf.WuttaConfig() config = conf.WuttaConfig()
self.assertEqual(config.appname, 'wutta') self.assertEqual(config.appname, 'wutta')
@ -410,6 +413,111 @@ configure_logging = true
config.setdefault('foo.bar', 'hello world') config.setdefault('foo.bar', 'hello world')
self.assertEqual(config.get_list('foo.bar'), ['hello', 'world']) self.assertEqual(config.get_list('foo.bar'), ['hello', 'world'])
def test_parse_bool_null(self):
config = self.make_config()
self.assertIsNone(config.parse_bool(None))
def test_parse_bool_bool(self):
config = self.make_config()
self.assertTrue(config.parse_bool(True))
self.assertFalse(config.parse_bool(False))
def test_parse_bool_string_true(self):
config = self.make_config()
self.assertTrue(config.parse_bool('true'))
self.assertTrue(config.parse_bool('yes'))
self.assertTrue(config.parse_bool('y'))
self.assertTrue(config.parse_bool('on'))
self.assertTrue(config.parse_bool('1'))
def test_parse_bool_string_false(self):
config = self.make_config()
self.assertFalse(config.parse_bool('false'))
self.assertFalse(config.parse_bool('no'))
self.assertFalse(config.parse_bool('n'))
self.assertFalse(config.parse_bool('off'))
self.assertFalse(config.parse_bool('0'))
# nb. assume false for unrecognized input
self.assertFalse(config.parse_bool('whatever-else'))
def test_parse_list_null(self):
config = self.make_config()
value = config.parse_list(None)
self.assertIsInstance(value, list)
self.assertEqual(len(value), 0)
def test_parse_list_list_instance(self):
config = self.make_config()
mylist = []
value = config.parse_list(mylist)
self.assertIs(value, mylist)
def test_parse_list_single_value(self):
config = self.make_config()
value = config.parse_list('foo')
self.assertEqual(len(value), 1)
self.assertEqual(value[0], 'foo')
def test_parse_list_single_value_padded_by_spaces(self):
config = self.make_config()
value = config.parse_list(' foo ')
self.assertEqual(len(value), 1)
self.assertEqual(value[0], 'foo')
def test_parse_list_slash_is_not_a_separator(self):
config = self.make_config()
value = config.parse_list('/dev/null')
self.assertEqual(len(value), 1)
self.assertEqual(value[0], '/dev/null')
def test_parse_list_multiple_values_separated_by_whitespace(self):
config = self.make_config()
value = config.parse_list('foo bar baz')
self.assertEqual(len(value), 3)
self.assertEqual(value[0], 'foo')
self.assertEqual(value[1], 'bar')
self.assertEqual(value[2], 'baz')
def test_parse_list_multiple_values_separated_by_commas(self):
config = self.make_config()
value = config.parse_list('foo,bar,baz')
self.assertEqual(len(value), 3)
self.assertEqual(value[0], 'foo')
self.assertEqual(value[1], 'bar')
self.assertEqual(value[2], 'baz')
def test_parse_list_multiple_values_separated_by_whitespace_and_commas(self):
config = self.make_config()
value = config.parse_list(' foo, bar baz')
self.assertEqual(len(value), 3)
self.assertEqual(value[0], 'foo')
self.assertEqual(value[1], 'bar')
self.assertEqual(value[2], 'baz')
def test_parse_list_multiple_values_separated_by_whitespace_and_commas_with_some_quoting(self):
config = self.make_config()
value = config.parse_list("""
foo
"C:\\some path\\with spaces\\and, a comma",
baz
""")
self.assertEqual(len(value), 3)
self.assertEqual(value[0], 'foo')
self.assertEqual(value[1], 'C:\\some path\\with spaces\\and, a comma')
self.assertEqual(value[2], 'baz')
def test_parse_list_multiple_values_separated_by_whitespace_and_commas_with_single_quotes(self):
config = self.make_config()
value = config.parse_list("""
foo
'C:\\some path\\with spaces\\and, a comma',
baz
""")
self.assertEqual(len(value), 3)
self.assertEqual(value[0], 'foo')
self.assertEqual(value[1], 'C:\\some path\\with spaces\\and, a comma')
self.assertEqual(value[2], 'baz')
def test_get_app(self): def test_get_app(self):
# default handler # default handler