Config could have multiple parents
This commit is contained in:
@ -780,7 +780,7 @@ class _TiramisuOptionDescription(_TiramisuOption, TiramisuConfig):
|
||||
option_bag = OptionBag()
|
||||
option_bag.set_option(opt,
|
||||
opt.impl_getpath(),
|
||||
None,
|
||||
None,
|
||||
config_bag)
|
||||
if opt.impl_is_optiondescription():
|
||||
config_bag.context.cfgimpl_get_settings().validate_properties(option_bag)
|
||||
@ -1339,6 +1339,7 @@ class _TiramisuContextConfig(TiramisuConfig, _TiramisuContextConfigReset):
|
||||
session_id=None,
|
||||
persistent=False,
|
||||
storage=None):
|
||||
"""Copy current config"""
|
||||
return self._return_config(self._config_bag.context.duplicate(session_id,
|
||||
persistent=persistent,
|
||||
storage=storage))
|
||||
@ -1348,6 +1349,7 @@ class _TiramisuContextConfig(TiramisuConfig, _TiramisuContextConfigReset):
|
||||
persistent=False,
|
||||
storage=None,
|
||||
metaconfig_prefix=None):
|
||||
"""Copy current config with all parents"""
|
||||
return self._return_config(self._config_bag.context.duplicate(session_id,
|
||||
persistent=persistent,
|
||||
storage=storage,
|
||||
@ -1355,9 +1357,19 @@ class _TiramisuContextConfig(TiramisuConfig, _TiramisuContextConfigReset):
|
||||
deep=True))
|
||||
|
||||
def metaconfig(self):
|
||||
return self._return_config(self._config_bag.context.cfgimpl_get_meta())
|
||||
"""Get first meta configuration (obsolete please use parents)"""
|
||||
try:
|
||||
return next(self.parents())
|
||||
except StopIteration:
|
||||
return None
|
||||
|
||||
def parents(self):
|
||||
"""Get all parents of current config"""
|
||||
for parent in self._config_bag.context.get_parents():
|
||||
yield self._return_config(parent)
|
||||
|
||||
def path(self):
|
||||
"""Get path from config (all parents name)"""
|
||||
return self._config_bag.context.cfgimpl_get_config_path()
|
||||
|
||||
|
||||
|
@ -599,7 +599,7 @@ class _CommonConfig(SubConfig):
|
||||
'_impl_settings',
|
||||
'_impl_properties_cache',
|
||||
'_impl_permissives_cache',
|
||||
'_impl_meta',
|
||||
'parents',
|
||||
'impl_type')
|
||||
|
||||
def _impl_build_all_caches(self):
|
||||
@ -609,9 +609,9 @@ class _CommonConfig(SubConfig):
|
||||
config_bag = ConfigBag(context=self)
|
||||
descr.impl_build_force_store_values(config_bag)
|
||||
|
||||
def cfgimpl_get_meta(self):
|
||||
if self._impl_meta is not None:
|
||||
return self._impl_meta()
|
||||
def get_parents(self):
|
||||
for parent in self.parents:
|
||||
yield parent()
|
||||
|
||||
# information
|
||||
def impl_set_information(self, key, value):
|
||||
@ -689,19 +689,27 @@ class _CommonConfig(SubConfig):
|
||||
duplicated_config.cfgimpl_reset_cache(None, None)
|
||||
if child is not None:
|
||||
duplicated_config._impl_children.append(child)
|
||||
child._impl_meta = weakref.ref(duplicated_config)
|
||||
if self._impl_meta:
|
||||
child.parents.append(weakref.ref(duplicated_config))
|
||||
if self.parents:
|
||||
if deep:
|
||||
duplicated_config = self._impl_meta().duplicate(deep=deep,
|
||||
storage=storage,
|
||||
metaconfig_prefix=metaconfig_prefix,
|
||||
child=duplicated_config,
|
||||
persistent=persistent)
|
||||
for parent in self.parents:
|
||||
duplicated_config = parent().duplicate(deep=deep,
|
||||
storage=storage,
|
||||
metaconfig_prefix=metaconfig_prefix,
|
||||
child=duplicated_config,
|
||||
persistent=persistent)
|
||||
else:
|
||||
duplicated_config._impl_meta = self._impl_meta
|
||||
self._impl_meta()._impl_children.append(duplicated_config)
|
||||
duplicated_config.parents = self.parents
|
||||
for parent in self.parents:
|
||||
parent()._impl_children.append(duplicated_config)
|
||||
return duplicated_config
|
||||
|
||||
def cfgimpl_get_config_path(self):
|
||||
path = self._impl_name
|
||||
for parent in self.parents:
|
||||
path = parent().cfgimpl_get_config_path() + '.' + path
|
||||
return path
|
||||
|
||||
|
||||
# ____________________________________________________________
|
||||
class KernelConfig(_CommonConfig):
|
||||
@ -733,7 +741,7 @@ class KernelConfig(_CommonConfig):
|
||||
:param persistent: if persistent, don't delete storage when leaving
|
||||
:type persistent: `boolean`
|
||||
"""
|
||||
self._impl_meta = None
|
||||
self.parents = []
|
||||
self._impl_symlink = []
|
||||
self._display_name = display_name
|
||||
if isinstance(descr, Leadership):
|
||||
@ -774,11 +782,6 @@ class KernelConfig(_CommonConfig):
|
||||
def impl_getname(self):
|
||||
return self._impl_name
|
||||
|
||||
def cfgimpl_get_config_path(self):
|
||||
if self._impl_meta is None or self._impl_meta() is None:
|
||||
return self._impl_name
|
||||
return self._impl_meta().cfgimpl_get_config_path() + '.' + self._impl_name
|
||||
|
||||
|
||||
class KernelGroupConfig(_CommonConfig):
|
||||
__slots__ = ('__weakref__',
|
||||
@ -804,7 +807,7 @@ class KernelGroupConfig(_CommonConfig):
|
||||
raise ConflictError(_('config name must be uniq in '
|
||||
'groupconfig for "{0}"').format(name))
|
||||
self._impl_children = children
|
||||
self._impl_meta = None
|
||||
self.parents = []
|
||||
session_id = gen_storage_id(session_id, self)
|
||||
assert valid_name(session_id), _("invalid session ID: {0} for config").format(session_id)
|
||||
super().__init__(_descr,
|
||||
@ -973,11 +976,6 @@ class KernelGroupConfig(_CommonConfig):
|
||||
return child
|
||||
raise ConfigError(_('unknown config "{}"').format(name))
|
||||
|
||||
def cfgimpl_get_config_path(self):
|
||||
if self._impl_meta is None or self._impl_meta() is None:
|
||||
return self._impl_name
|
||||
return self._impl_meta().cfgimpl_get_config_path() + '.' + self._impl_name
|
||||
|
||||
|
||||
class KernelMixConfig(KernelGroupConfig):
|
||||
__slots__ = ('_display_name',
|
||||
@ -998,9 +996,7 @@ class KernelMixConfig(KernelGroupConfig):
|
||||
for child in children:
|
||||
if not isinstance(child, (KernelConfig, KernelMixConfig)):
|
||||
raise TypeError(_("child must be a Config, MixConfig or MetaConfig"))
|
||||
if child.cfgimpl_get_meta() is not None:
|
||||
raise ValueError(_("child has already a {}config's").format(self.impl_type))
|
||||
child._impl_meta = weakref.ref(self)
|
||||
child.parents.append(weakref.ref(self))
|
||||
properties, permissives, values, session_id = get_storages(self,
|
||||
session_id,
|
||||
persistent,
|
||||
@ -1183,13 +1179,11 @@ class KernelMixConfig(KernelGroupConfig):
|
||||
def add_config(self,
|
||||
apiconfig):
|
||||
config = apiconfig._config_bag.context
|
||||
if config._impl_meta is not None:
|
||||
raise ConflictError(_('config is already in a metaconfig'))
|
||||
if config.impl_getname() in [child.impl_getname() for child in self._impl_children]:
|
||||
raise ConflictError(_('config name must be uniq in '
|
||||
'groupconfig for {0}').format(config.impl_getname()))
|
||||
|
||||
config._impl_meta = weakref.ref(self)
|
||||
config.parents.append(weakref.ref(self))
|
||||
self._impl_children.append(config)
|
||||
config.cfgimpl_reset_cache(None, None)
|
||||
|
||||
@ -1199,12 +1193,21 @@ class KernelMixConfig(KernelGroupConfig):
|
||||
if session_id is not None:
|
||||
for idx, child in enumerate(self._impl_children):
|
||||
if session_id == child.impl_getname():
|
||||
child._impl_meta = None
|
||||
child.cfgimpl_reset_cache(None, None)
|
||||
return self._impl_children.pop(idx)
|
||||
raise ConfigError(_('cannot find the config {}').format(session_id))
|
||||
self._impl_children.pop(idx)
|
||||
break
|
||||
else:
|
||||
raise ConfigError(_('cannot find the config {}').format(session_id))
|
||||
if config is not None:
|
||||
self._impl_children.pop(config)
|
||||
self._impl_children.remove(config)
|
||||
child = config
|
||||
for index, parent in enumerate(child.parents):
|
||||
if parent() == self:
|
||||
child.parents.pop(index)
|
||||
break
|
||||
else:
|
||||
raise ConfigError(_('cannot find the config {}').format(self.session_id))
|
||||
return child
|
||||
|
||||
|
||||
class KernelMetaConfig(KernelMixConfig):
|
||||
@ -1285,7 +1288,7 @@ class KernelMetaConfig(KernelMixConfig):
|
||||
config.cfgimpl_get_settings().rw_remove = self.cfgimpl_get_settings().rw_remove
|
||||
config.cfgimpl_get_settings().default_properties = self.cfgimpl_get_settings().default_properties
|
||||
|
||||
config._impl_meta = weakref.ref(self)
|
||||
config.parents.append(weakref.ref(self))
|
||||
self._impl_children.append(config)
|
||||
return config
|
||||
|
||||
|
@ -140,13 +140,13 @@ class Values(object):
|
||||
def getdefaultvalue(self,
|
||||
option_bag):
|
||||
"""get default value:
|
||||
- get meta config value or
|
||||
- get parents config value or
|
||||
- get calculated value or
|
||||
- get default value
|
||||
"""
|
||||
moption_bag = self._get_meta(option_bag)
|
||||
if moption_bag:
|
||||
# retrieved value from meta config
|
||||
moption_bag = self._get_modified_parent(option_bag)
|
||||
if moption_bag is not None:
|
||||
# retrieved value from parent config
|
||||
return moption_bag.config_bag.context.cfgimpl_get_values().get_cached_value(moption_bag)
|
||||
|
||||
if option_bag.option.impl_has_callback():
|
||||
@ -328,38 +328,56 @@ class Values(object):
|
||||
option_bag.index,
|
||||
commit)
|
||||
|
||||
def _get_meta(self,
|
||||
option_bag):
|
||||
context = option_bag.config_bag.context
|
||||
meta = context.cfgimpl_get_meta()
|
||||
if meta is None:
|
||||
return None
|
||||
def _get_modified_parent(self,
|
||||
option_bag):
|
||||
""" Search in differents parents a Config with a modified value and return it
|
||||
If not found, return None
|
||||
For follower option, return the Config where leader is modified
|
||||
"""
|
||||
def build_option_bag(option_bag, parent):
|
||||
doption_bag = option_bag.copy()
|
||||
config_bag = option_bag.config_bag.copy()
|
||||
config_bag.context = parent
|
||||
config_bag.unrestraint()
|
||||
doption_bag.config_bag = config_bag
|
||||
return doption_bag
|
||||
|
||||
if option_bag.option.impl_is_follower():
|
||||
leader = option_bag.option.impl_get_leadership().get_leader()
|
||||
leaderpath = leader.impl_getpath()
|
||||
# follower could be a "meta" only if leader hasn't value
|
||||
if self._p_.hasvalue(leaderpath,
|
||||
index=None):
|
||||
return None
|
||||
doption_bag = option_bag.copy()
|
||||
config_bag = option_bag.config_bag.copy()
|
||||
config_bag.context = meta
|
||||
doption_bag.config_bag = config_bag
|
||||
if 'force_metaconfig_on_freeze' in option_bag.properties:
|
||||
# remove force_metaconfig_on_freeze only if option in metaconfig
|
||||
# hasn't force_metaconfig_on_freeze properties
|
||||
ori_properties = doption_bag.properties
|
||||
del doption_bag.properties
|
||||
if not self.force_to_metaconfig(doption_bag):
|
||||
doption_bag.properties = ori_properties - {'force_metaconfig_on_freeze'}
|
||||
else:
|
||||
doption_bag.properties = ori_properties
|
||||
config_bag.unrestraint()
|
||||
meta_option_bag = meta.cfgimpl_get_values().getowner(doption_bag,
|
||||
only_default=True)
|
||||
if meta_option_bag == owners.default:
|
||||
return None
|
||||
return meta_option_bag
|
||||
config_bag = option_bag.config_bag
|
||||
leader_option_bag = OptionBag()
|
||||
leader_option_bag.set_option(leader,
|
||||
leaderpath,
|
||||
None,
|
||||
config_bag)
|
||||
leader_option_bag = self._get_modified_parent(leader_option_bag)
|
||||
if leader_option_bag is None:
|
||||
return None
|
||||
new_config_bag = leader_option_bag.config_bag
|
||||
if not new_config_bag.context.cfgimpl_get_values()._p_.hasvalue(option_bag.path,
|
||||
index=option_bag.index):
|
||||
return None
|
||||
return build_option_bag(option_bag, new_config_bag.context)
|
||||
for parent in option_bag.config_bag.context.get_parents():
|
||||
doption_bag = build_option_bag(option_bag, parent)
|
||||
if 'force_metaconfig_on_freeze' in option_bag.properties:
|
||||
# remove force_metaconfig_on_freeze only if option in metaconfig
|
||||
# hasn't force_metaconfig_on_freeze properties
|
||||
ori_properties = doption_bag.properties
|
||||
del doption_bag.properties
|
||||
if not self.force_to_metaconfig(doption_bag):
|
||||
doption_bag.properties = ori_properties - {'force_metaconfig_on_freeze'}
|
||||
else:
|
||||
doption_bag.properties = ori_properties
|
||||
parent_owner = parent.cfgimpl_get_values().getowner(doption_bag,
|
||||
only_default=True)
|
||||
if parent_owner != owners.default:
|
||||
return doption_bag
|
||||
return None
|
||||
|
||||
|
||||
#______________________________________________________________________
|
||||
@ -408,8 +426,8 @@ class Values(object):
|
||||
index=option_bag.index)
|
||||
if validate_meta is not False and (owner is owners.default or \
|
||||
'frozen' in option_bag.properties and 'force_metaconfig_on_freeze' in option_bag.properties):
|
||||
moption_bag = self._get_meta(option_bag)
|
||||
if moption_bag:
|
||||
moption_bag = self._get_modified_parent(option_bag)
|
||||
if moption_bag is not None:
|
||||
owner = moption_bag.config_bag.context.cfgimpl_get_values().getowner(moption_bag,
|
||||
only_default=only_default)
|
||||
elif 'force_metaconfig_on_freeze' in option_bag.properties:
|
||||
|
Reference in New Issue
Block a user