From 657b401124e4bd7cf81cf53b7ea2e8c8b2d07e4d Mon Sep 17 00:00:00 2001 From: Emmanuel Garette Date: Sun, 9 Sep 2018 13:50:38 +0200 Subject: [PATCH] can remove all data in options (value, properties and permissives) with new function in API: c.config.reset() --- test/test_config_api.py | 20 ++++++ test/test_metaconfig.py | 2 +- tiramisu/api.py | 103 ++++++++++++++++----------- tiramisu/config.py | 2 - tiramisu/storage/dictionary/value.py | 7 +- tiramisu/storage/sqlite3/value.py | 4 ++ 6 files changed, 94 insertions(+), 44 deletions(-) diff --git a/test/test_config_api.py b/test/test_config_api.py index d92d628..935cbf0 100644 --- a/test/test_config_api.py +++ b/test/test_config_api.py @@ -342,3 +342,23 @@ def test_help(): od2 = OptionDescription('o', '', [od1]) cfg = Config(od2) cfg.help(_display=False, _valid=True) + + +def test_config_reset(): + descr = make_description() + c = Config(descr) + assert not c.option('gc.gc2.bool').value.get() + assert not c.option('boolop').property.get() + assert not c.option('boolop').permissive.get() + # + c.option('gc.gc2.bool').value.set(True) + c.option('boolop').property.add('test') + c.option('float').permissive.set(frozenset(['test'])) + assert c.option('gc.gc2.bool').value.get() + assert c.option('boolop').property.get() + assert c.option('float').permissive.get() + # + c.config.reset() + assert not c.option('gc.gc2.bool').value.get() + assert not c.option('boolop').property.get() + assert not c.option('float').permissive.get() diff --git a/test/test_metaconfig.py b/test/test_metaconfig.py index f73bc48..ebecafd 100644 --- a/test/test_metaconfig.py +++ b/test/test_metaconfig.py @@ -686,7 +686,7 @@ def test_meta_reset(): assert meta.option('ip_admin_eth0.ip_admin_eth0').value.get() == ['192.168.1.1'] assert meta.config('conf1').option('ip_admin_eth0.ip_admin_eth0').value.get() == ['192.168.1.2'] assert meta.config('conf2').option('ip_admin_eth0.ip_admin_eth0').value.get() == ['192.168.1.1'] - meta.value.reset('ip_admin_eth0.ip_admin_eth0') + meta.option('ip_admin_eth0.ip_admin_eth0').value.reset(children=True) assert meta.option('ip_admin_eth0.ip_admin_eth0').value.get() == [] assert meta.config('conf1').option('ip_admin_eth0.ip_admin_eth0').value.get() == [] assert meta.config('conf2').option('ip_admin_eth0.ip_admin_eth0').value.get() == [] diff --git a/tiramisu/api.py b/tiramisu/api.py index 6d2ab2e..1194921 100644 --- a/tiramisu/api.py +++ b/tiramisu/api.py @@ -392,13 +392,12 @@ class TiramisuOptionInformation(CommonTiramisuOption): def set(self, name, value): """set information for a key name""" - #FIXME ? - self.config_bag.context.impl_set_information(name, value) + option = self.option_bag.option + self.option_bag.option.impl_set_information(name, value) def reset(self, name): """remove information for a key name""" - #FIXME ? - self.config_bag.context.impl_del_information(name) + self.option_bag.option.impl_del_information(name) class TiramisuOptionValue(CommonTiramisuOption): @@ -446,6 +445,28 @@ class TiramisuOptionValue(CommonTiramisuOption): self._test_slave_index() self.subconfig.delattr(self.option_bag) + def _g_reset(self, + itself=None, + children=None): + """reset value for a GroupConfig or a MetaConfig""" + config_bag = self.option_bag.config_bag + if isinstance(config_bag.context, KernelMetaConfig): + if children is None: + children = False + if itself is None: + itself = True + else: + if children is None: + children = True + if itself is True: + raise APIError(_('cannot remove value in a GroupConfig')) + itself = False + if children: + config_bag.context.reset(self.option_bag.path, + config_bag) + if itself: + self._o_reset() + def _m_len_master(self): """length of master option (only for slave option)""" option = self.option_bag.option @@ -462,26 +483,6 @@ class TiramisuOptionValue(CommonTiramisuOption): self._length = self.subconfig.cfgimpl_get_length_slave(self.option_bag) return self._length - def __getattr__(self, name: str) -> Callable: - option = self.option_bag.option - if name.startswith('_'): - # not a valid function - pass - elif name == 'list' and isinstance(option, ChoiceOption): - return self._c_list - elif name == 'pop' and option.impl_is_master_slaves('master'): - return self._m_pop - elif name == 'len': - if option.impl_is_master_slaves('slave'): - return self._s_len_slave - if option.impl_is_master_slaves('master'): - return self._m_len_master - elif name == 'dict' and option.impl_is_optiondescription(): - return self._od_dict - elif not option.impl_is_optiondescription(): - return getattr(self, '_o_' + name) - raise APIError(_('{} is unknown').format(name)) - def _c_list(self): """all values available for an option (only for choiceoption)""" option = self.option_bag.option @@ -507,6 +508,28 @@ class TiramisuOptionValue(CommonTiramisuOption): withoption=withoption, withvalue=withvalue) + def __getattr__(self, name: str) -> Callable: + option = self.option_bag.option + if name.startswith('_'): + # not a valid function + pass + elif name == 'list' and isinstance(option, ChoiceOption): + return self._c_list + elif name == 'pop' and option.impl_is_master_slaves('master'): + return self._m_pop + elif name == 'len': + if option.impl_is_master_slaves('slave'): + return self._s_len_slave + if option.impl_is_master_slaves('master'): + return self._m_len_master + elif name == 'dict' and option.impl_is_optiondescription(): + return self._od_dict + elif not option.impl_is_optiondescription(): + if name == 'reset' and isinstance(self.option_bag.config_bag.context, KernelGroupConfig): + return getattr(self, '_g_' + name) + return getattr(self, '_o_' + name) + raise APIError(_('{} is unknown').format(name)) + def registers(registers: Dict[str, type], prefix: str) -> None: for module_name in globals().keys(): @@ -717,12 +740,6 @@ class TiramisuContextValue(TiramisuContext): self.config_bag, **kwargs) - def reset(self, - path): - """reset value for a GroupConfig or a MetaConfig""" - self.config_bag.context.reset(path, - self.config_bag) - def dict(self, flatten=False, withvalue=undefined, @@ -884,16 +901,6 @@ class TiramisuContextOption(TiramisuContext): ret.append(t_option) return ret - def get(self, name): - option = self.config_bag.context.cfgimpl_get_description().impl_getchild(name, - self.config_bag, - self.config_bag.context) - return TiramisuOption(name, - name, - None, - self.config_bag.context, - self.config_bag) - def list(self, type='all', group_type=None, @@ -976,6 +983,9 @@ class TiramisuContextConfig(TiramisuContext): def _c_meta(self): return Config(self.config_bag.context.cfgimpl_get_meta()) + def _c_reset(self): + pass + def _m_new(self, name): return Config(self.config_bag.context.new_config(name)) @@ -986,6 +996,19 @@ class TiramisuContextConfig(TiramisuContext): for child in self.config_bag.context.cfgimpl_get_children(): yield Config(child) + def _m_reset(self): + self._c_reset() + + def _c_reset(self): + # Option's values + self.config_bag.context.cfgimpl_get_values()._p_.importation((tuple(), tuple(), tuple(), tuple())) + # Option's properties + self.config_bag.context.cfgimpl_get_settings()._p_.importation({}) + # Option's permissives + self.config_bag.context.cfgimpl_get_settings()._pp_.importation({}) + # Remove cache + self.config_bag.context.cfgimpl_reset_cache(None, None) + def __getattr__(self, name: str) -> Callable: if not name.startswith('_'): diff --git a/tiramisu/config.py b/tiramisu/config.py index fcccefa..506b620 100644 --- a/tiramisu/config.py +++ b/tiramisu/config.py @@ -1116,8 +1116,6 @@ class KernelMetaConfig(KernelGroupConfig): option_bag.config_bag.context = child child.cfgimpl_get_values().reset(option_bag, _commit=False) - option_bag.config_bag.context = self - self.cfgimpl_get_values().reset(option_bag) def new_config(self, session_id, diff --git a/tiramisu/storage/dictionary/value.py b/tiramisu/storage/dictionary/value.py index f0ef835..7a80e8c 100644 --- a/tiramisu/storage/dictionary/value.py +++ b/tiramisu/storage/dictionary/value.py @@ -25,7 +25,9 @@ DEBUG = False class Values(Cache): - __slots__ = ('_values', '_informations', '__weakref__') + __slots__ = ('_values', + '_informations', + '__weakref__') def __init__(self, storage): """init plugin means create values storage @@ -286,6 +288,9 @@ class Values(Cache): if raises: raise ValueError(_("information's item not found {0}").format(key)) + def del_informations(self): + self._informations = {} + def exportation(self): return self._values diff --git a/tiramisu/storage/sqlite3/value.py b/tiramisu/storage/sqlite3/value.py index f33eac3..96cdc9f 100644 --- a/tiramisu/storage/sqlite3/value.py +++ b/tiramisu/storage/sqlite3/value.py @@ -227,6 +227,10 @@ class Values(Sqlite3DB): self._storage.execute("DELETE FROM information WHERE key = ? AND session_id = ?", (key, self._session_id)) + def del_informations(self): + self._storage.execute("DELETE FROM information WHERE session_id = ?", + (self._session_id,)) + def exportation(self): if DEBUG: print('exportation')