From 4e5ffb201d477f8c9258dc47a657239a28ebd913 Mon Sep 17 00:00:00 2001 From: Emmanuel Garette Date: Mon, 10 Sep 2018 21:59:54 +0200 Subject: [PATCH] correction in list with recursion --- test/test_masterslaves.py | 24 ++++++++++++--- tiramisu/api.py | 65 +++++++++++++++++++-------------------- 2 files changed, 52 insertions(+), 37 deletions(-) diff --git a/test/test_masterslaves.py b/test/test_masterslaves.py index d5516d4..1b9b591 100644 --- a/test/test_masterslaves.py +++ b/test/test_masterslaves.py @@ -110,13 +110,27 @@ def test_iter_on_groups(): descr = make_description() api = Config(descr) api.property.read_write() - result = api.option('creole').list('optiondescription', group_type=groups.family) + result = api.option('creole').list('optiondescription', + group_type=groups.family) group_names = [res.option.name() for res in result] assert group_names == ['general', 'interface1'] - for i in api.option('creole').list('optiondescription', group_type=groups.family): + for i in api.option('creole').list('optiondescription', + group_type=groups.family): #test StopIteration break +def test_list_recursive(): + descr = make_description() + api = Config(descr) + api.property.read_write() + result = api.option('creole').list('all') + group_names = [res.option.name() for res in result] + assert group_names == ['general', 'interface1'] + # + result = list(api.option.list(recursive=True)) + group_names = [res.option.name() for res in result] + assert group_names == ['numero_etab', 'nom_machine', 'nombre_interfaces', 'activer_proxy_client', 'mode_conteneur_actif', 'serveur_ntp', 'time_zone', 'ip_admin_eth0', 'netmask_admin_eth0'] + def test_iter_on_groups_force_permissive(): descr = make_description() @@ -141,7 +155,8 @@ def test_iter_group_on_groups_force_permissive(): api = Config(descr) api.property.read_write() api.permissive.set(frozenset(['hidden'])) - result = api.forcepermissive.option('creole').list(type='optiondescription', group_type=groups.family) + result = api.forcepermissive.option('creole').list(type='optiondescription', + group_type=groups.family) group_names = [res.option.name() for res in result] assert group_names == ['general', 'interface1', 'new'] @@ -151,7 +166,8 @@ def test_iter_on_groups_props(): api = Config(descr) api.property.read_write() api.option('creole.interface1').property.add('disabled') - result = api.option('creole').list(type='optiondescription', group_type=groups.family) + result = api.option('creole').list(type='optiondescription', + group_type=groups.family) group_names = [res.option.name() for res in result] assert group_names == ['general'] diff --git a/tiramisu/api.py b/tiramisu/api.py index 94e79c1..355111c 100644 --- a/tiramisu/api.py +++ b/tiramisu/api.py @@ -643,10 +643,10 @@ class TiramisuOption(CommonTiramisu): return self._get_option().impl_get_group_type() def _list(self, - type='all', + type='option', group_type=None): """list options in an optiondescription (only for optiondescription)""" - if type not in ('all', 'optiondescription'): + if type not in ('all', 'option', 'optiondescription'): raise APIError(_('unknown list type {}').format(type)) if group_type is not None and not isinstance(group_type, groups.GroupType): @@ -683,12 +683,11 @@ class TiramisuOption(CommonTiramisu): except PropertiesOptionError: continue if opt.impl_is_optiondescription(): - if type == 'optiondescription' and \ - (group_type and opt.impl_get_group_type() != group_type): - continue - else: - if type == 'optiondescription': + if type == 'option' or (type == 'optiondescription' and group_type and \ + opt.impl_get_group_type() != group_type): continue + elif type == 'optiondescription': + continue name = opt.impl_getname() yield TiramisuOption(name, subconfig._get_subpath(name), @@ -910,7 +909,7 @@ class TiramisuContextOption(TiramisuContext): return ret def list(self, - type='all', + type='option', group_type=None, recursive=False): """list content of an optiondescription""" @@ -922,44 +921,44 @@ class TiramisuContextOption(TiramisuContext): name, None, self.config_bag) - self.config_bag.context.getattr(name, - option_bag) - if type not in ('all', 'optiondescription'): - raise APIError(_('unknown list type {}').format(type)) - if group_type is not None and not isinstance(group_type, - groups.GroupType): - raise TypeError(_("unknown group_type: {0}").format(group_type)) - - if recursive: - if group_type: - raise APIError(_('recursive with group_type is not implemented yet')) - if self.config_bag.properties: - raise APIError(_('not implemented yet')) - for option in self.config_bag.context.cfgimpl_get_description()._cache_paths[1]: - if type == 'optiondescription' and not isinstance(option, OptionDescription): - continue - yield option - - else: - option = self.config_bag.context.cfgimpl_get_description() + if opt.impl_is_optiondescription(): + self.config_bag.context.cfgimpl_get_settings().validate_properties(option_bag) + else: + self.config_bag.context.getattr(name, + option_bag) + def _walk(option): for opt in option.impl_getchildren(self.config_bag): try: subsubconfig = _filter(opt) except PropertiesOptionError: continue if opt.impl_is_optiondescription(): - if type == 'optiondescription' and \ - (group_type and opt.impl_get_group_type() != group_type): + if recursive: + for toption in _walk(opt): + yield toption + if type == 'option' or (type == 'optiondescription' and \ + group_type and opt.impl_get_group_type() != group_type): continue else: if type == 'optiondescription': continue - name = opt.impl_getname() + path = opt.impl_getpath() + subconfig, name = self.config_bag.context.cfgimpl_get_home_by_path(path, + self.config_bag) yield TiramisuOption(name, - self.config_bag.context._get_subpath(name), + path, None, - self.config_bag.context, + subconfig, self.config_bag) + if type not in ('all', 'option', 'optiondescription'): + raise APIError(_('unknown list type {}').format(type)) + if group_type is not None and not isinstance(group_type, + groups.GroupType): + raise TypeError(_("unknown group_type: {0}").format(group_type)) + + option = self.config_bag.context.cfgimpl_get_description() + for toption in _walk(option): + yield toption class TiramisuContextConfig(TiramisuContext):