From df3753c36b812814393d62a7fa3c4e7a146bbac8 Mon Sep 17 00:00:00 2001 From: gwen Date: Wed, 27 Feb 2013 11:09:13 +0100 Subject: [PATCH] finally a getvalue without a getattr --- test/test_config_api.py | 7 +++++++ tiramisu/config.py | 19 +++++++++++++++++++ 2 files changed, 26 insertions(+) diff --git a/test/test_config_api.py b/test/test_config_api.py index fcfd7e9..eac3117 100644 --- a/test/test_config_api.py +++ b/test/test_config_api.py @@ -58,6 +58,13 @@ def test_iter_subconfig(): zip(conf.gc, [("name", "ref"), ("dummy", False)]): assert name == gname assert value == gvalue + +def test_cfgimpl_get_value(): + "same as getattr." + descr = make_description() + conf = Config(descr) + assert conf.cfgimpl_get_value(('gc', 'dummy')) == False + #____________________________________________________________ def test_getpaths(): descr = make_description() diff --git a/tiramisu/config.py b/tiramisu/config.py index 1264077..2a1f9cb 100644 --- a/tiramisu/config.py +++ b/tiramisu/config.py @@ -79,6 +79,25 @@ class Config(object): raise ConfigError("setting not allowed") self._cfgimpl_context._cfgimpl_settings = settings + def cfgimpl_get_description(self): + return self._cfgimpl_descr + + def cfgimpl_get_value(self, path): + """same as multiple getattrs + + :param path: list or tuple of path + + example : ``path = (sub_conf, opt)`` makes a getattr of sub_conf, then + a getattr of opt, and then returns the opt's value. + + :returns: subconf or option's value + """ + subpaths = list(path) + subconf_or_opt = self + for subpath in subpaths: + subconf_or_opt = getattr(subconf_or_opt, subpath) + return subconf_or_opt + def _validate_duplicates(self, children): """duplicates Option names in the schema :type children: list of `Option` or `OptionDescription`