duplicate => copy + add deepcopy

This commit is contained in:
Emmanuel Garette 2018-09-05 20:22:16 +02:00
parent ec169a8dc6
commit 1816813f92
4 changed files with 163 additions and 62 deletions

View File

@ -46,9 +46,9 @@ def make_description():
return descr return descr
def test_duplicate(): def test_copy():
cfg = Config(make_description()) cfg = Config(make_description())
ncfg = cfg.config.duplicate() ncfg = cfg.config.copy()
assert cfg.option('creole.general.numero_etab').value.get() == None assert cfg.option('creole.general.numero_etab').value.get() == None
cfg.option('creole.general.numero_etab').value.set('oui') cfg.option('creole.general.numero_etab').value.set('oui')
assert cfg.option('creole.general.numero_etab').value.get() == 'oui' assert cfg.option('creole.general.numero_etab').value.get() == 'oui'
@ -63,7 +63,7 @@ def to_tuple(val):
return tuple([tuple(v) for v in val]) return tuple([tuple(v) for v in val])
def test_duplicate_force_store_value(): def test_copy_force_store_value():
descr = make_description() descr = make_description()
conf = Config(descr) conf = Config(descr)
conf2 = Config(descr) conf2 = Config(descr)

View File

@ -691,6 +691,60 @@ def test_meta_reset():
assert meta.config('conf2').option('ip_admin_eth0.ip_admin_eth0').value.get() == [] assert meta.config('conf2').option('ip_admin_eth0.ip_admin_eth0').value.get() == []
def test_meta_properties_meta_copy():
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')
conf2 = Config(interface1, session_id='conf2')
meta = MetaConfig([conf1, conf2])
meta.property.read_write()
conf3 = meta.config('conf1').config.copy(session_id='conf3')
meta2 = conf3.config.meta()
assert meta != meta2
assert meta.config('conf1').config.dict() == {'ip_admin_eth0': ['192.168.1.1']}
assert meta.config('conf2').config.dict() == {'ip_admin_eth0': ['192.168.1.1']}
assert meta.config('conf3').config.dict() == {'ip_admin_eth0': ['192.168.1.1']}
meta.option('ip_admin_eth0').value.set(['192.168.1.2'])
assert meta.config('conf1').config.dict() == {'ip_admin_eth0': ['192.168.1.2']}
assert meta.config('conf2').config.dict() == {'ip_admin_eth0': ['192.168.1.2']}
assert meta.config('conf3').config.dict() == {'ip_admin_eth0': ['192.168.1.2']}
ret = meta.value.set('ip_admin_eth0', ['192.168.1.3'], force_default_if_same=True)
assert meta.config('conf1').config.dict() == {'ip_admin_eth0': ['192.168.1.3']}
assert meta.config('conf2').config.dict() == {'ip_admin_eth0': ['192.168.1.3']}
assert meta.config('conf3').config.dict() == {'ip_admin_eth0': ['192.168.1.3']}
def test_meta_properties_meta_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')
conf2 = Config(interface1, session_id='conf2')
meta = MetaConfig([conf1, conf2])
meta.permissive.set(frozenset({'hidden'}))
meta.property.read_write()
meta2 = meta.config('conf1').config.deepcopy('conf3')
assert meta != meta2
assert meta.permissive.get() == meta2.permissive.get()
assert meta.config('conf1').config.dict() == {'ip_admin_eth0': ['192.168.1.1']}
assert meta.config('conf2').config.dict() == {'ip_admin_eth0': ['192.168.1.1']}
assert meta2.config('conf3').config.dict() == {'ip_admin_eth0': ['192.168.1.1']}
meta.option('ip_admin_eth0').value.set(['192.168.1.2'])
assert meta.config('conf1').config.dict() == {'ip_admin_eth0': ['192.168.1.2']}
assert meta.config('conf2').config.dict() == {'ip_admin_eth0': ['192.168.1.2']}
assert meta2.config('conf3').config.dict() == {'ip_admin_eth0': ['192.168.1.1']}
meta.value.set('ip_admin_eth0', ['192.168.1.3'], force_default_if_same=True)
assert meta.config('conf1').config.dict() == {'ip_admin_eth0': ['192.168.1.3']}
assert meta.config('conf2').config.dict() == {'ip_admin_eth0': ['192.168.1.3']}
assert meta2.config('conf3').config.dict() == {'ip_admin_eth0': ['192.168.1.1']}
def test_meta_properties_meta_set_value(): def test_meta_properties_meta_set_value():
ip_admin_eth0 = NetworkOption('ip_admin_eth0', "ip", multi=True, default=['192.168.1.1']) 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',)) netmask_admin_eth0 = NetmaskOption('netmask_admin_eth0', "mask", multi=True, properties=('disabled',))

View File

@ -920,10 +920,18 @@ class TiramisuContextConfig(TiramisuContext):
def name(self): def name(self):
return self.config_bag.context.impl_getname() return self.config_bag.context.impl_getname()
def duplicate(self, def _c_copy(self,
session_id=None): session_id=None):
return TiramisuAPI(self.config_bag.context.duplicate(session_id)) return TiramisuAPI(self.config_bag.context.duplicate(session_id))
def _c_deepcopy(self,
session_id=None):
return TiramisuAPI(self.config_bag.context.duplicate(session_id,
deep=True))
def _c_meta(self):
return TiramisuAPI(self.config_bag.context.cfgimpl_get_meta())
def _m_new(self, name): def _m_new(self, name):
return TiramisuAPI(self.config_bag.context.new_config(name)) return TiramisuAPI(self.config_bag.context.new_config(name))
@ -942,6 +950,8 @@ class TiramisuContextConfig(TiramisuContext):
return getattr(self, '_m_' + name) return getattr(self, '_m_' + name)
elif isinstance(self.config_bag.context, KernelGroupConfig): elif isinstance(self.config_bag.context, KernelGroupConfig):
return getattr(self, '_g_' + name) return getattr(self, '_g_' + name)
elif isinstance(self.config_bag.context, KernelConfig):
return getattr(self, '_c_' + name)
except APIError: except APIError:
raise APIError(_('{} is unknown').format(name)) raise APIError(_('{} is unknown').format(name))
raise APIError(_('{} is unknown').format(name)) raise APIError(_('{} is unknown').format(name))
@ -981,27 +991,18 @@ class TiramisuAPI(TiramisuHelp):
def __getattr__(self, subfunc: str) -> Any: def __getattr__(self, subfunc: str) -> Any:
if subfunc == 'forcepermissive': if subfunc == 'forcepermissive':
if isinstance(self._config, ConfigBag): if isinstance(self._config, ConfigBag):
config = self._config.config config_bag = self._config.copy()
force = not self._config.properties del config_bag.properties
else: else:
config = self._config config_bag = ConfigBag(context=self._config)
force = None
config_bag = ConfigBag(context=config)
config_bag.set_permissive() config_bag.set_permissive()
if force is True:
config_bag.properties = frozenset()
return TiramisuAPI(config_bag) return TiramisuAPI(config_bag)
elif subfunc == 'unrestraint': elif subfunc == 'unrestraint':
#if isinstance(self._config, ConfigBag): if isinstance(self._config, ConfigBag):
# config = self._config.context config_bag = self._config.copy()
# force = self._config.force_permissive else:
#else: config_bag = ConfigBag(context=self._config)
config = self._config
#force = None
config_bag = ConfigBag(context=config)
config_bag.properties = frozenset() config_bag.properties = frozenset()
#if force is not None:
#config_bag.force_permissive = force
return TiramisuAPI(config_bag) return TiramisuAPI(config_bag)
elif subfunc in self.registers: elif subfunc in self.registers:
if not isinstance(self._config, ConfigBag): if not isinstance(self._config, ConfigBag):

View File

@ -684,18 +684,39 @@ class _CommonConfig(SubConfig):
session_id=None, session_id=None,
force_values=None, force_values=None,
force_settings=None, force_settings=None,
storage=None): storage=None,
config = KernelConfig(self._impl_descr, deep=False):
_duplicate=True, if isinstance(self, KernelConfig):
session_id=session_id, config = KernelConfig(self._impl_descr,
force_values=force_values, _duplicate=True,
force_settings=force_settings, session_id=session_id,
storage=storage) force_values=force_values,
force_settings=force_settings,
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,
storage=storage)
else:
raise Exception('hu?')
config.cfgimpl_get_values()._p_.importation(self.cfgimpl_get_values()._p_.exportation()) config.cfgimpl_get_values()._p_.importation(self.cfgimpl_get_values()._p_.exportation())
config.cfgimpl_get_settings()._p_.importation(self.cfgimpl_get_settings( config.cfgimpl_get_settings()._p_.importation(self.cfgimpl_get_settings(
)._p_.exportation()) )._p_.exportation())
config.cfgimpl_get_settings()._pp_.importation(self.cfgimpl_get_settings( config.cfgimpl_get_settings()._pp_.importation(self.cfgimpl_get_settings(
)._pp_.exportation()) )._pp_.exportation())
if self._impl_meta:
if deep:
meta = self._impl_meta().duplicate(deep=deep)
meta._impl_children.append(config)
config._impl_meta = weakref.ref(meta)
config = meta
else:
config._impl_meta = self._impl_meta
config._impl_meta()._impl_children.append(config)
return config return config
@ -750,10 +771,10 @@ class KernelConfig(_CommonConfig):
permissives) permissives)
self._impl_values = Values(self, self._impl_values = Values(self,
values) values)
super(KernelConfig, self).__init__(descr, super().__init__(descr,
weakref.ref(self), weakref.ref(self),
ConfigBag(self), ConfigBag(self),
None) None)
if _duplicate is False and (force_settings is None or force_values is None): if _duplicate is False and (force_settings is None or force_values is None):
self._impl_build_all_caches() self._impl_build_all_caches()
self._impl_name = session_id self._impl_name = session_id
@ -774,7 +795,10 @@ class KernelGroupConfig(_CommonConfig):
children, children,
session_id=None, session_id=None,
persistent=False, persistent=False,
force_settings=None,
force_values=None,
_descr=None, _descr=None,
_duplicate=False,
storage=None): storage=None):
if not isinstance(children, list): if not isinstance(children, list):
raise ValueError(_("groupconfig's children must be a list")) raise ValueError(_("groupconfig's children must be a list"))
@ -795,19 +819,31 @@ class KernelGroupConfig(_CommonConfig):
raise ConflictError(_('config name must be uniq in ' raise ConflictError(_('config name must be uniq in '
'groupconfig for "{0}"').format(name)) 'groupconfig for "{0}"').format(name))
self._impl_children = children self._impl_children = children
properties, permissives, values, session_id = get_storages(self,
session_id, if force_settings is not None and force_values is not None:
persistent, self._impl_settings = Settings(self,
storage=storage) force_settings[0],
self._impl_settings = Settings(self, force_settings[1])
properties, self._impl_values = Values(self,
permissives) force_values)
self._impl_values = Values(self, values) else:
properties, permissives, values, session_id = get_storages(self,
session_id,
persistent,
storage=storage)
if not valid_name(session_id):
raise ValueError(_("invalid session ID: {0} for config").format(session_id))
self._impl_settings = Settings(self,
properties,
permissives)
self._impl_values = Values(self,
values)
self._impl_meta = None self._impl_meta = None
super(KernelGroupConfig, self).__init__(_descr, super().__init__(_descr,
weakref.ref(self), weakref.ref(self),
ConfigBag(self), ConfigBag(self),
None) None)
#undocumented option used only in test script #undocumented option used only in test script
self._impl_name = session_id self._impl_name = session_id
@ -820,8 +856,8 @@ class KernelGroupConfig(_CommonConfig):
if resetted_opts is None: if resetted_opts is None:
resetted_opts = [] resetted_opts = []
if isinstance(self, KernelMetaConfig): if isinstance(self, KernelMetaConfig):
super(KernelGroupConfig, self).cfgimpl_reset_cache(option_bag, super().cfgimpl_reset_cache(option_bag,
resetted_opts=copy(resetted_opts)) resetted_opts=copy(resetted_opts))
for child in self._impl_children: for child in self._impl_children:
child.cfgimpl_reset_cache(option_bag, child.cfgimpl_reset_cache(option_bag,
resetted_opts=copy(resetted_opts)) resetted_opts=copy(resetted_opts))
@ -946,15 +982,22 @@ class KernelMetaConfig(KernelGroupConfig):
children, children,
session_id=None, session_id=None,
persistent=False, persistent=False,
optiondescription=None): force_settings=None,
force_values=None,
optiondescription=None,
storage=None,
_duplicate=False):
descr = None descr = None
if optiondescription is not None: if optiondescription is not None:
new_children = [] if not _duplicate:
for child_session_id in children: new_children = []
new_children.append(KernelConfig(optiondescription, for child_session_id in children:
persistent=persistent, new_children.append(KernelConfig(optiondescription,
session_id=child_session_id)) persistent=persistent,
children = new_children session_id=child_session_id))
children = new_children
else:
descr = optiondescription
for child in children: for child in children:
if not isinstance(child, _CommonConfig): if not isinstance(child, _CommonConfig):
try: try:
@ -972,10 +1015,13 @@ class KernelMetaConfig(KernelGroupConfig):
'have the same optiondescription')) 'have the same optiondescription'))
child._impl_meta = weakref.ref(self) child._impl_meta = weakref.ref(self)
super(KernelMetaConfig, self).__init__(children, super().__init__(children,
session_id, session_id,
persistent, persistent,
descr) force_settings=force_settings,
force_values=force_values,
storage=storage,
_descr=descr)
def set_value(self, def set_value(self,
path, path,
@ -995,12 +1041,12 @@ class KernelMetaConfig(KernelGroupConfig):
raise ValueError(_('force_default, force_default_if_same or ' raise ValueError(_('force_default, force_default_if_same or '
'force_dont_change_value cannot be set with' 'force_dont_change_value cannot be set with'
' only_config')) ' only_config'))
return super(KernelMetaConfig, self).set_value(path, return super().set_value(path,
index, index,
value, value,
config_bag, config_bag,
only_config=only_config, only_config=only_config,
_commit=_commit) _commit=_commit)
ret = [] ret = []
if force_default or force_default_if_same or force_dont_change_value: if force_default or force_default_if_same or force_dont_change_value:
if force_default and force_dont_change_value: if force_default and force_dont_change_value: