From f6a9b887954a9ef5a214acf5f2aa819fcfc50728 Mon Sep 17 00:00:00 2001 From: Emmanuel Garette Date: Thu, 13 Sep 2018 17:00:52 +0200 Subject: [PATCH] support multiple metaconfig and prefix --- test/test_metaconfig.py | 24 ++++++++++++++--- tiramisu/api.py | 6 +++-- tiramisu/config.py | 60 +++++++++++++++++++++++------------------ 3 files changed, 58 insertions(+), 32 deletions(-) diff --git a/test/test_metaconfig.py b/test/test_metaconfig.py index 1da4887..835be32 100644 --- a/test/test_metaconfig.py +++ b/test/test_metaconfig.py @@ -711,12 +711,12 @@ def test_meta_properties_meta_copy(): conf2 = Config(interface1, session_id='conf2') conf1.property.read_write() conf2.property.read_write() - meta = MetaConfig([conf1, conf2]) + meta = MetaConfig([conf1, conf2], session_id='meta1') meta.property.read_write() conf3 = meta.config('conf1').config.copy(session_id='conf3') - meta2 = conf3.config.meta() - assert meta != meta2 + meta2 = conf3.config.metaconfig() + assert meta.config.name() == meta2.config.name() assert meta.config('conf1').value.dict() == {'ip_admin_eth0': ['192.168.1.1']} assert meta.config('conf2').value.dict() == {'ip_admin_eth0': ['192.168.1.1']} @@ -744,7 +744,7 @@ def test_meta_properties_meta_deepcopy(): meta.permissive.set(frozenset({'hidden'})) meta.property.read_write() - meta2 = meta.config('conf1').config.deepcopy('conf3') + meta2 = meta.config('conf1').config.deepcopy(session_id='conf3') assert meta != meta2 assert meta.permissive.get() == meta2.permissive.get() @@ -761,6 +761,22 @@ def test_meta_properties_meta_deepcopy(): assert meta2.config('conf3').value.dict() == {'ip_admin_eth0': ['192.168.1.1']} +def test_meta_properties_submeta_deepcopy(): + 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',)) + interface1 = OptionDescription('ip_admin_eth0', '', [ip_admin_eth0, netmask_admin_eth0]) + conf1 = Config(interface1, session_id='conf1') + conf1.property.read_write() + meta1 = MetaConfig([conf1], session_id='meta1') + meta2 = MetaConfig([meta1], session_id='meta2') + meta_copy = conf1.config.deepcopy(session_id='conf2', + metaconfig_prefix='copy_') + assert meta_copy.config.name() == 'copy_meta2' + assert meta_copy.config('copy_meta1').config.name() == 'copy_meta1' + assert meta_copy.config('copy_meta1').config('conf2').config.name() == 'conf2' + + 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/tiramisu/api.py b/tiramisu/api.py index 049b2ce..a3b1a11 100644 --- a/tiramisu/api.py +++ b/tiramisu/api.py @@ -990,13 +990,15 @@ class TiramisuContextConfig(TiramisuContext): def _c_deepcopy(self, session_id=None, persistent=False, - storage=None): + storage=None, + metaconfig_prefix=None): return Config(self.config_bag.context.duplicate(session_id, persistent=persistent, storage=storage, + metaconfig_prefix=metaconfig_prefix, deep=True)) - def _c_meta(self): + def _c_metaconfig(self): return Config(self.config_bag.context.cfgimpl_get_meta()) def _m_new(self, diff --git a/tiramisu/config.py b/tiramisu/config.py index 1c945f1..84f5a35 100644 --- a/tiramisu/config.py +++ b/tiramisu/config.py @@ -682,41 +682,49 @@ class _CommonConfig(SubConfig): force_settings=None, storage=None, persistent=False, + metaconfig_prefix=None, + child=None, deep=False): if isinstance(self, KernelConfig): - config = KernelConfig(self._impl_descr, - _duplicate=True, - session_id=session_id, - force_values=force_values, - force_settings=force_settings, - persistent=persistent, - storage=storage) + duplicated_config = KernelConfig(self._impl_descr, + _duplicate=True, + session_id=session_id, + force_values=force_values, + force_settings=force_settings, + persistent=persistent, + storage=storage) elif isinstance(self, KernelMetaConfig): - config = KernelMetaConfig([], - _duplicate=True, - optiondescription=self._impl_descr, - session_id=session_id, - force_values=force_values, - force_settings=force_settings, - persistent=persistent, - storage=storage) + if session_id is None and metaconfig_prefix is not None: + session_id = metaconfig_prefix + self.impl_getname() + duplicated_config = KernelMetaConfig([], + _duplicate=True, + optiondescription=self._impl_descr, + session_id=session_id, + force_values=force_values, + force_settings=force_settings, + persistent=persistent, + storage=storage) else: - raise Exception('hu?') - config.cfgimpl_get_values()._p_.importation(self.cfgimpl_get_values()._p_.exportation()) - config.cfgimpl_get_settings()._p_.importation(self.cfgimpl_get_settings( + raise Exception(_('unknown type')) + duplicated_config.cfgimpl_get_values()._p_.importation(self.cfgimpl_get_values()._p_.exportation()) + duplicated_config.cfgimpl_get_settings()._p_.importation(self.cfgimpl_get_settings( )._p_.exportation()) - config.cfgimpl_get_settings()._pp_.importation(self.cfgimpl_get_settings( + duplicated_config.cfgimpl_get_settings()._pp_.importation(self.cfgimpl_get_settings( )._pp_.exportation()) + if child is not None: + duplicated_config._impl_children.append(child) + child._impl_meta = weakref.ref(duplicated_config) if self._impl_meta: if deep: - meta = self._impl_meta().duplicate(deep=deep, storage=storage, persistent=persistent) - meta._impl_children.append(config) - config._impl_meta = weakref.ref(meta) - config = meta + duplicated_config = self._impl_meta().duplicate(deep=deep, + storage=storage, + metaconfig_prefix=metaconfig_prefix, + child=duplicated_config, + persistent=persistent) else: - config._impl_meta = self._impl_meta - config._impl_meta()._impl_children.append(config) - return config + duplicated_config._impl_meta = self._impl_meta + self._impl_meta()._impl_children.append(duplicated_config) + return duplicated_config # ____________________________________________________________