From 87594307c9af9cada75d95ce9bd02625d990f6c7 Mon Sep 17 00:00:00 2001 From: Emmanuel Garette Date: Sat, 15 Sep 2018 10:34:15 +0200 Subject: [PATCH] copy context owner when user copied a config/metaconfig Context owner is now in values (no more in settings). The context owner is set in storage. So when copy/deepcopy a config, owner is copied to. For user, when export values, context owner is not returned by default. --- test/test_metaconfig.py | 49 ++++++++++++++++++++++++++++ test/test_storage.py | 4 +-- tiramisu/api.py | 17 +++++++--- tiramisu/config.py | 4 +-- tiramisu/setting.py | 19 ++--------- tiramisu/storage/dictionary/value.py | 5 ++- tiramisu/value.py | 27 +++++++++++++-- 7 files changed, 98 insertions(+), 27 deletions(-) diff --git a/test/test_metaconfig.py b/test/test_metaconfig.py index 835be32..b43ea51 100644 --- a/test/test_metaconfig.py +++ b/test/test_metaconfig.py @@ -185,6 +185,15 @@ def test_meta_new_config(): assert len(list(meta.config.list())) == 3 +def test_meta_new_metaconfig(): + od = make_description() + meta = MetaConfig(['name1', 'name2'], optiondescription=od) + meta.config.new('newconf1', type='metaconfig') + meta.config('newconf1').config.new('newconf2', type='metaconfig') + meta.config('newconf1').config('newconf2').config.new('newconf3') + assert meta.config('newconf1').config('newconf2').config('newconf3').config.name() == 'newconf3' + + def test_meta_pop_config(): od = make_description() meta = MetaConfig(['name1', 'name2'], optiondescription=od) @@ -777,6 +786,46 @@ def test_meta_properties_submeta_deepcopy(): assert meta_copy.config('copy_meta1').config('conf2').config.name() == 'conf2' +def test_meta_properties_submeta_deepcopy_owner(): + ip_admin_eth0 = NetworkOption('ip_admin_eth0', "ip") + netmask_admin_eth0 = NetmaskOption('netmask_admin_eth0', "mask") + interface1 = OptionDescription('ip_admin_eth0', '', [ip_admin_eth0, netmask_admin_eth0]) + conf1 = Config(interface1, session_id='conf1') + conf1.owner.set('conf1_user') + conf1.property.read_write() + meta1 = MetaConfig([conf1], session_id='meta1') + meta1.owner.set('meta1_user') + meta2 = MetaConfig([meta1], session_id='meta2') + meta2.owner.set('meta2_user') + # + conf1.option('ip_admin_eth0').value.set('192.168.0.1') + assert conf1.option('ip_admin_eth0').owner.get() == 'conf1_user' + meta1.option('ip_admin_eth0').value.set('192.168.0.2') + assert meta1.option('ip_admin_eth0').owner.get() == 'meta1_user' + meta2.option('ip_admin_eth0').value.set('192.168.0.3') + assert meta2.option('ip_admin_eth0').owner.get() == 'meta2_user' + # + meta2_copy = conf1.config.deepcopy(session_id='conf2', + metaconfig_prefix='copy_') + meta2_copy.option('netmask_admin_eth0').value.set('255.255.255.255') + assert meta2_copy.option('ip_admin_eth0').value.get() == '192.168.0.3' + assert meta2_copy.option('ip_admin_eth0').owner.get() == 'meta2_user' + assert meta2_copy.option('netmask_admin_eth0').owner.get() == 'meta2_user' + # + meta1_copy = meta2_copy.config('copy_meta1') + meta1_copy.option('netmask_admin_eth0').value.set('255.255.255.255') + assert meta1_copy.option('ip_admin_eth0').value.get() == '192.168.0.2' + assert meta1_copy.option('ip_admin_eth0').owner.get() == 'meta1_user' + assert meta1_copy.option('netmask_admin_eth0').owner.get() == 'meta1_user' + # + conf2 = meta1_copy.config('conf2') + conf2.owner.set('conf2_user') + conf2.option('netmask_admin_eth0').value.set('255.255.255.255') + assert conf2.option('netmask_admin_eth0').owner.get() == 'conf2_user' + assert conf2.option('ip_admin_eth0').value.get() == '192.168.0.1' + assert conf2.option('ip_admin_eth0').owner.get() == 'conf1_user' + + def test_meta_properties_meta_set_value(): ip_admin_eth0 = NetworkOption('ip_admin_eth0', "ip", multi=True, default=['192.168.1.1']) netmask_admin_eth0 = NetmaskOption('netmask_admin_eth0', "mask", multi=True, properties=('disabled',)) diff --git a/test/test_storage.py b/test/test_storage.py index a7edc80..7e70578 100644 --- a/test/test_storage.py +++ b/test/test_storage.py @@ -34,9 +34,9 @@ def test_delete_not_persisten(): Config(o, session_id='test_persistent', persistent=True) except: c = Config(o, session_id='not_test_persistent') - assert list_sessions('all') == ['not_test_persistent'] + assert 'not_test_persistent' in list_sessions('all') del c - assert list_sessions('all') == [] + assert 'not_test_persistent' not in list_sessions('all') # c = Config(o, session_id='not_test_persistent') raises(ValueError, "delete_session('not_test_persistent')") diff --git a/tiramisu/api.py b/tiramisu/api.py index a3b1a11..e088811 100644 --- a/tiramisu/api.py +++ b/tiramisu/api.py @@ -763,9 +763,18 @@ class TiramisuContextValue(TiramisuContext): withoption=withoption, withvalue=withvalue) - def exportation(self): + def exportation(self, + with_default_owner: bool=False): """export all values""" - return self.config_bag.context.cfgimpl_get_values()._p_.exportation() + exportation = self.config_bag.context.cfgimpl_get_values()._p_.exportation() + if not with_default_owner: + exportation = [list(exportation[0]), list(exportation[1]), list(exportation[2]), list(exportation[3])] + index = exportation[0].index(None) + exportation[0].pop(index) + exportation[1].pop(index) + exportation[2].pop(index) + exportation[3].pop(index) + return exportation def importation(self, values): """import values""" @@ -778,7 +787,7 @@ class TiramisuContextOwner(TiramisuContext): def get(self): """get default owner""" - return self.config_bag.context.cfgimpl_get_settings().getowner() + return self.config_bag.context.cfgimpl_get_values().get_context_owner() def set(self, owner): """set default owner""" @@ -787,7 +796,7 @@ class TiramisuContextOwner(TiramisuContext): except AttributeError: owners.addowner(owner) obj_owner = getattr(owners, owner) - self.config_bag.context.cfgimpl_get_settings().setowner(obj_owner) + self.config_bag.context.cfgimpl_get_values().set_context_owner(obj_owner) class TiramisuContextProperty(TiramisuContext): diff --git a/tiramisu/config.py b/tiramisu/config.py index 84f5a35..ffd2409 100644 --- a/tiramisu/config.py +++ b/tiramisu/config.py @@ -1135,7 +1135,7 @@ class KernelMetaConfig(KernelGroupConfig): session_id, type_='config', persistent=False): - if session_id in [child._impl_name for child in self._impl_children]: # pragma: no cover + if session_id in [child._impl_name for child in self._impl_children]: raise ConflictError(_('config name must be uniq in ' 'groupconfig for {0}').format(session_id)) if type_ == 'config': @@ -1147,7 +1147,7 @@ class KernelMetaConfig(KernelGroupConfig): optiondescription=self._impl_descr, session_id=session_id, persistent=persistent) - else: + else: # pragma: no cover raise ConfigError(_('unknown type {}').format(type_)) # Copy context properties/permissives config.cfgimpl_get_settings().set_context_properties(self.cfgimpl_get_settings().get_context_properties(), config) diff --git a/tiramisu/setting.py b/tiramisu/setting.py index 2e62c91..0123018 100644 --- a/tiramisu/setting.py +++ b/tiramisu/setting.py @@ -325,7 +325,9 @@ undefined = Undefined() #____________________________________________________________ class Settings(object): "``config.Config()``'s configuration options settings" - __slots__ = ('_owner', '_p_', '_pp_', '__weakref__') + __slots__ = ('_p_', + '_pp_', + '__weakref__') def __init__(self, properties, @@ -340,7 +342,6 @@ class Settings(object): - sqlite3 -> persistent """ # generic owner - self._owner = owners.user self._p_ = properties self._pp_ = permissives @@ -779,17 +780,3 @@ class Settings(object): self._read(rw_remove, rw_append, context) - - #____________________________________________________________ - # default owner methods - - def setowner(self, - owner): - ":param owner: sets the default value for owner at the Config level" - if owner in forbidden_owners: - raise ValueError(_('set owner "{0}" is forbidden').format(str(owner))) - - self._owner = owner - - def getowner(self): - return self._owner diff --git a/tiramisu/storage/dictionary/value.py b/tiramisu/storage/dictionary/value.py index 9289327..eb9ba6b 100644 --- a/tiramisu/storage/dictionary/value.py +++ b/tiramisu/storage/dictionary/value.py @@ -183,7 +183,10 @@ class Values(Cache): self._values = tuple(values) # owner - def setowner(self, path, owner, index=None): + def setowner(self, + path, + owner, + index=None): """change owner for a path """ idx = self._values[0].index(path) diff --git a/tiramisu/value.py b/tiramisu/value.py index 7451e61..fbad7a1 100644 --- a/tiramisu/value.py +++ b/tiramisu/value.py @@ -31,7 +31,8 @@ class Values(object): '__weakref__') def __init__(self, - storage): + storage, + default_owner=owners.user): """ Initializes the values's dict. @@ -40,6 +41,12 @@ class Values(object): """ # store the storage self._p_ = storage + # set default owner + self._p_.setvalue(None, + None, + default_owner, + None, + True) #______________________________________________________________________ # get value @@ -253,7 +260,7 @@ class Values(object): _commit): context = option_bag.config_bag.context - owner = context.cfgimpl_get_settings().getowner() + owner = self.get_context_owner() if 'validator' in option_bag.config_bag.properties: if option_bag.index is not None or option_bag.option._has_consistencies(context): # set value to a fake config when option has dependency @@ -610,3 +617,19 @@ class Values(object): [], context, od_config_bag) + + #____________________________________________________________ + # default owner methods + def set_context_owner(self, + owner): + ":param owner: sets the default value for owner at the Config level" + if owner in forbidden_owners: + raise ValueError(_('set owner "{0}" is forbidden').format(str(owner))) + + self._p_.setowner(None, + owner, + index=None) + + def get_context_owner(self): + return self._p_.getowner(None, + None)