add fullpath to make_dict

This commit is contained in:
Emmanuel Garette 2017-01-06 21:01:24 +01:00
parent df90e74819
commit 66f24bd1c0
3 changed files with 36 additions and 7 deletions

View file

@ -134,6 +134,24 @@ def test_make_dict_with_disabled_in_callback():
assert d == {"s1.a": False, "int": 42} 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(): def test_find_in_config():
"finds option in config" "finds option in config"
descr = make_description() descr = make_description()

View file

@ -187,6 +187,9 @@ class SubConfig(object):
raise ConfigError(_('the context does not exist anymore')) raise ConfigError(_('the context does not exist anymore'))
return context return context
def cfgimpl_get_context(self):
return self._cfgimpl_get_context()
def cfgimpl_get_description(self): def cfgimpl_get_description(self):
if self._impl_descr is None: # pragma: optional cover if self._impl_descr is None: # pragma: optional cover
raise ConfigError(_('no option description found for this config' 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, def make_dict(self, flatten=False, _currpath=None, withoption=None,
withvalue=undefined, force_permissive=False, withvalue=undefined, force_permissive=False,
setting_properties=undefined): setting_properties=undefined, fullpath=False):
"""exports the whole config into a `dict`, for example: """exports the whole config into a `dict`, for example:
>>> print cfg.make_dict() >>> print cfg.make_dict()
@ -506,21 +509,23 @@ class SubConfig(object):
path = path[len(tmypath):] path = path[len(tmypath):]
self._make_sub_dict(opt, path, pathsvalues, _currpath, flatten, self._make_sub_dict(opt, path, pathsvalues, _currpath, flatten,
force_permissive=force_permissive, force_permissive=force_permissive,
setting_properties=setting_properties) setting_properties=setting_properties,
fullpath=fullpath)
#withoption can be set to None below ! #withoption can be set to None below !
if withoption is None: if withoption is None:
for opt in self.cfgimpl_get_description().impl_getchildren(): for opt in self.cfgimpl_get_description().impl_getchildren():
path = opt.impl_getname() path = opt.impl_getname()
self._make_sub_dict(opt, path, pathsvalues, _currpath, flatten, self._make_sub_dict(opt, path, pathsvalues, _currpath, flatten,
force_permissive=force_permissive, force_permissive=force_permissive,
setting_properties=setting_properties) setting_properties=setting_properties,
fullpath=fullpath)
if _currpath == []: if _currpath == []:
options = dict(pathsvalues) options = dict(pathsvalues)
return options return options
return pathsvalues return pathsvalues
def _make_sub_dict(self, opt, path, pathsvalues, _currpath, flatten, 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, value = self.getattr(path,
force_permissive=force_permissive, force_permissive=force_permissive,
_setting_properties=setting_properties, _setting_properties=setting_properties,
@ -533,10 +538,14 @@ class SubConfig(object):
pathsvalues += value.make_dict(flatten, pathsvalues += value.make_dict(flatten,
_currpath + path.split('.'), _currpath + path.split('.'),
force_permissive=force_permissive, force_permissive=force_permissive,
setting_properties=setting_properties) setting_properties=setting_properties,
fullpath=fullpath)
else: else:
if flatten: if flatten:
name = opt.impl_getname() name = opt.impl_getname()
else:
if fullpath:
name = '.'.join([self._impl_path, opt.impl_getname()])
else: else:
name = '.'.join(_currpath + [opt.impl_getname()]) name = '.'.join(_currpath + [opt.impl_getname()])
pathsvalues.append((name, value)) pathsvalues.append((name, value))

View file

@ -19,7 +19,9 @@ from .i18n import _
def display_list(lst, separator='and'): def display_list(lst, separator='and'):
if len(lst) == 1: if len(lst) == 0:
return ''
elif len(lst) == 1:
return lst[0] return lst[0]
else: else:
lst_ = [] lst_ = []