add fullpath to make_dict
authorEmmanuel Garette <egarette@cadoles.com>
Fri, 6 Jan 2017 20:01:24 +0000 (21:01 +0100)
committerEmmanuel Garette <egarette@cadoles.com>
Fri, 6 Jan 2017 20:01:24 +0000 (21:01 +0100)
test/test_config_api.py
tiramisu/config.py
tiramisu/error.py

index 6950581..2bd1adc 100644 (file)
@@ -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()
index 711e335..9dd96d8 100644 (file)
@@ -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):
index 5c86fc1..eae8955 100644 (file)
@@ -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_ = []