From bfb50457536d7c45aa33c3f2091a7a10b0d4851a Mon Sep 17 00:00:00 2001 From: gwen Date: Tue, 6 Nov 2012 15:19:36 +0100 Subject: [PATCH] test mandatory --- test/test_parsing_group.py | 19 +++++++++++++----- tiramisu/config.py | 40 ++++++++++++++++++++++++-------------- 2 files changed, 39 insertions(+), 20 deletions(-) diff --git a/test/test_parsing_group.py b/test/test_parsing_group.py index 992a3f8..d28c59e 100644 --- a/test/test_parsing_group.py +++ b/test/test_parsing_group.py @@ -9,7 +9,7 @@ def make_description(): nombre_interfaces = IntOption('nombre_interfaces', "nombre d'interfaces à activer", default=1) activer_proxy_client = BoolOption('activer_proxy_client', "utiliser un proxy", - default=False) + default=False) mode_conteneur_actif = BoolOption('mode_conteneur_actif', "le serveur est en mode conteneur", default=False) # hidden (variable cachée) @@ -25,9 +25,9 @@ def make_description(): interface1 = OptionDescription('interface1', '', [master]) interface1.set_group_type('group') - general = OptionDescription('general', '', [numero_etab, nom_machine, + general = OptionDescription('general', '', [numero_etab, nom_machine, nombre_interfaces, activer_proxy_client, - mode_conteneur_actif, adresse_serveur_ntp, + mode_conteneur_actif, adresse_serveur_ntp, time_zone]) general.set_group_type('family') creole = OptionDescription('creole', 'first tiramisu configuration', [general, interface1]) @@ -39,7 +39,7 @@ def test_base_config(): config = Config(descr) assert config.creole.general.activer_proxy_client == False assert config.creole.general.nom_machine == "eoleng" - assert config.get('nom_machine') == "eoleng" + assert config.get('nom_machine') == "eoleng" result = {'general.numero_etab': None, 'general.nombre_interfaces': 1, 'general.serveur_ntp': [], 'interface1.ip_admin_eth0.ip_admin_eth0': None, 'general.mode_conteneur_actif': False, 'general.time_zone': 'Paris', @@ -67,4 +67,13 @@ def test_iter_on_groups(): result = list(config.creole.iter_groups()) group_names = [res[0] for res in result] assert group_names == ['general', 'interface1'] - + +def test_iter_on_empty_group(): + config = Config(OptionDescription("name", "descr", [] )) + result = list(config.iter_groups()) + assert result == [] + for i in config.iter_groups(): + pass + for i in config: + pass + assert [] == list(config) diff --git a/tiramisu/config.py b/tiramisu/config.py index b7b3252..ed5c881 100644 --- a/tiramisu/config.py +++ b/tiramisu/config.py @@ -209,6 +209,16 @@ class Config(object): return True return False + def _test_mandatory(self, path, opt): + # mandatory options + homeconfig = self._cfgimpl_get_toplevel() + mandatory = homeconfig._cfgimpl_mandatory + if opt.is_mandatory() and mandatory: + if self._is_empty(opt) and \ + opt.is_empty_by_default(): + raise MandatoryError("option: {0} is mandatory " + "and shall have a value".format(path)) + def __getattr__(self, name): return self._getattr(name) @@ -299,15 +309,7 @@ class Config(object): raise ConfigError('invalid calculated value returned' ' for option {0}'.format(name)) self._cfgimpl_values[name] = _result - - # mandatory options - homeconfig = self._cfgimpl_get_toplevel() - mandatory = homeconfig._cfgimpl_mandatory - if opt_or_descr.is_mandatory() and mandatory: - if self._is_empty(opt_or_descr) and \ - opt_or_descr.is_empty_by_default(): - raise MandatoryError("option: {0} is mandatory " - "and shall have a value".format(name)) + self._test_mandatory(name, opt_or_descr) # frozen and force default if not opt_or_descr.has_callback() and opt_or_descr.is_forced_on_freeze(): return opt_or_descr.getdefault() @@ -577,11 +579,11 @@ class Config(object): groups = [group_type] for child in self._cfgimpl_descr._children: if isinstance(child, OptionDescription): - try: - if child.get_group_type() in groups: - yield child._name, getattr(self, child._name) - except: - pass # hidden, disabled option + try: + if child.get_group_type() in groups: + yield child._name, getattr(self, child._name) + except: + pass # hidden, disabled option # ______________________________________________________________________ def __str__(self, indent=""): "Config's string representation" @@ -742,7 +744,15 @@ def mandatory_warnings(config): config._cfgimpl_get_toplevel()._cfgimpl_mandatory = True for path in config._cfgimpl_descr.getpaths(include_groups=True): try: - value = getattr(config, path) + if '.' in path: + homeconfig, path = config._cfgimpl_get_home_by_path(path) + else: + homeconfig = config + opt = getattr(homeconfig._cfgimpl_descr, path) + #for PropertiesOptionError + homeconfig._validate(path, opt) + if not isinstance(opt, OptionDescription): + homeconfig._test_mandatory(path, opt) except MandatoryError: yield path except PropertiesOptionError: