From 66f24bd1c0a213b72a590035a9751910031872cc Mon Sep 17 00:00:00 2001 From: Emmanuel Garette Date: Fri, 6 Jan 2017 21:01:24 +0100 Subject: [PATCH] add fullpath to make_dict --- test/test_config_api.py | 18 ++++++++++++++++++ tiramisu/config.py | 21 +++++++++++++++------ tiramisu/error.py | 4 +++- 3 files changed, 36 insertions(+), 7 deletions(-) diff --git a/test/test_config_api.py b/test/test_config_api.py index 6950581..2bd1adc 100644 --- a/test/test_config_api.py +++ b/test/test_config_api.py @@ -134,6 +134,24 @@ def test_make_dict_with_disabled_in_callback(): assert d == {"s1.a": False, "int": 42} +def test_make_dict_fullpath(): + descr = OptionDescription("root", "", [ + OptionDescription("opt", "", [ + OptionDescription("s1", "", [ + BoolOption("a", "", default=False), + BoolOption("b", "", default=False, properties=('disabled',))]), + OptionDescription("s2", "", [ + BoolOption("a", "", default=False), + BoolOption("b", "", default=False)], properties=('disabled',)), + IntOption("int", "", default=42)]), + IntOption("introot", "", default=42)]) + config = Config(descr) + config.read_only() + assert config.make_dict() == {"opt.s1.a": False, "opt.int": 42, "introot": 42} + assert config.opt.make_dict() == {"s1.a": False, "int": 42} + assert config.opt.make_dict(fullpath=True) == {"opt.s1.a": False, "opt.int": 42} + + def test_find_in_config(): "finds option in config" descr = make_description() diff --git a/tiramisu/config.py b/tiramisu/config.py index 711e335..9dd96d8 100644 --- a/tiramisu/config.py +++ b/tiramisu/config.py @@ -187,6 +187,9 @@ class SubConfig(object): raise ConfigError(_('the context does not exist anymore')) return context + def cfgimpl_get_context(self): + return self._cfgimpl_get_context() + def cfgimpl_get_description(self): if self._impl_descr is None: # pragma: optional cover raise ConfigError(_('no option description found for this config' @@ -436,7 +439,7 @@ class SubConfig(object): def make_dict(self, flatten=False, _currpath=None, withoption=None, withvalue=undefined, force_permissive=False, - setting_properties=undefined): + setting_properties=undefined, fullpath=False): """exports the whole config into a `dict`, for example: >>> print cfg.make_dict() @@ -506,21 +509,23 @@ class SubConfig(object): path = path[len(tmypath):] self._make_sub_dict(opt, path, pathsvalues, _currpath, flatten, force_permissive=force_permissive, - setting_properties=setting_properties) + setting_properties=setting_properties, + fullpath=fullpath) #withoption can be set to None below ! if withoption is None: for opt in self.cfgimpl_get_description().impl_getchildren(): path = opt.impl_getname() self._make_sub_dict(opt, path, pathsvalues, _currpath, flatten, force_permissive=force_permissive, - setting_properties=setting_properties) + setting_properties=setting_properties, + fullpath=fullpath) if _currpath == []: options = dict(pathsvalues) return options return pathsvalues def _make_sub_dict(self, opt, path, pathsvalues, _currpath, flatten, - setting_properties, force_permissive=False): + setting_properties, force_permissive=False, fullpath=False): value = self.getattr(path, force_permissive=force_permissive, _setting_properties=setting_properties, @@ -533,12 +538,16 @@ class SubConfig(object): pathsvalues += value.make_dict(flatten, _currpath + path.split('.'), force_permissive=force_permissive, - setting_properties=setting_properties) + setting_properties=setting_properties, + fullpath=fullpath) else: if flatten: name = opt.impl_getname() else: - name = '.'.join(_currpath + [opt.impl_getname()]) + if fullpath: + name = '.'.join([self._impl_path, opt.impl_getname()]) + else: + name = '.'.join(_currpath + [opt.impl_getname()]) pathsvalues.append((name, value)) def cfgimpl_get_path(self, dyn=True): diff --git a/tiramisu/error.py b/tiramisu/error.py index 5c86fc1..eae8955 100644 --- a/tiramisu/error.py +++ b/tiramisu/error.py @@ -19,7 +19,9 @@ from .i18n import _ def display_list(lst, separator='and'): - if len(lst) == 1: + if len(lst) == 0: + return '' + elif len(lst) == 1: return lst[0] else: lst_ = []