add DynOptionDescription
This commit is contained in:
@ -19,16 +19,21 @@
|
||||
# the whole pypy projet is under MIT licence
|
||||
# ____________________________________________________________
|
||||
from copy import copy
|
||||
import re
|
||||
|
||||
|
||||
from tiramisu.i18n import _
|
||||
from tiramisu.setting import groups # , log
|
||||
from .baseoption import BaseOption
|
||||
from tiramisu.setting import groups, undefined # , log
|
||||
from .baseoption import BaseOption, DynSymLinkOption, SymLinkOption, \
|
||||
allowed_character
|
||||
from . import MasterSlaves
|
||||
from tiramisu.error import ConfigError, ConflictError, ValueWarning
|
||||
from tiramisu.storage import get_storages_option
|
||||
from tiramisu.autolib import carry_out_calculation
|
||||
|
||||
|
||||
StorageOptionDescription = get_storages_option('optiondescription')
|
||||
name_regexp = re.compile(r'^{0}*$'.format(allowed_character))
|
||||
|
||||
|
||||
class OptionDescription(BaseOption, StorageOptionDescription):
|
||||
@ -42,16 +47,31 @@ class OptionDescription(BaseOption, StorageOptionDescription):
|
||||
:param children: a list of options (including optiondescriptions)
|
||||
|
||||
"""
|
||||
super(OptionDescription, self).__init__(name, doc=doc, requires=requires, properties=properties)
|
||||
child_names = [child.impl_getname() for child in children]
|
||||
super(OptionDescription, self).__init__(name, doc=doc,
|
||||
requires=requires,
|
||||
properties=properties,
|
||||
callback=False)
|
||||
child_names = []
|
||||
dynopt_names = []
|
||||
for child in children:
|
||||
name = child.impl_getname()
|
||||
child_names.append(name)
|
||||
if isinstance(child, DynOptionDescription):
|
||||
dynopt_names.append(name)
|
||||
|
||||
#better performance like this
|
||||
valid_child = copy(child_names)
|
||||
valid_child.sort()
|
||||
old = None
|
||||
for child in valid_child:
|
||||
if child == old:
|
||||
if child == old: # pragma: optional cover
|
||||
raise ConflictError(_('duplicate option name: '
|
||||
'{0}').format(child))
|
||||
if dynopt_names:
|
||||
for dynopt in dynopt_names:
|
||||
if child != dynopt and child.startswith(dynopt):
|
||||
raise ConflictError(_('option must not start as '
|
||||
'dynoptiondescription'))
|
||||
old = child
|
||||
self._add_children(child_names, children)
|
||||
self._cache_paths = None
|
||||
@ -74,19 +94,7 @@ class OptionDescription(BaseOption, StorageOptionDescription):
|
||||
"""returns a list of all paths in self, recursively
|
||||
_currpath should not be provided (helps with recursion)
|
||||
"""
|
||||
if _currpath is None:
|
||||
_currpath = []
|
||||
paths = []
|
||||
for option in self.impl_getchildren():
|
||||
attr = option.impl_getname()
|
||||
if isinstance(option, OptionDescription):
|
||||
if include_groups:
|
||||
paths.append('.'.join(_currpath + [attr]))
|
||||
paths += option.impl_getpaths(include_groups=include_groups,
|
||||
_currpath=_currpath + [attr])
|
||||
else:
|
||||
paths.append('.'.join(_currpath + [attr]))
|
||||
return paths
|
||||
return _impl_getpaths(self, include_groups, _currpath)
|
||||
|
||||
def impl_build_cache_consistency(self, _consistencies=None, cache_option=None):
|
||||
#FIXME cache_option !
|
||||
@ -96,7 +104,7 @@ class OptionDescription(BaseOption, StorageOptionDescription):
|
||||
cache_option = []
|
||||
else:
|
||||
init = False
|
||||
for option in self.impl_getchildren():
|
||||
for option in self._impl_getchildren(dyn=False):
|
||||
cache_option.append(option._get_id())
|
||||
if not isinstance(option, OptionDescription):
|
||||
for func, all_cons_opts, params in option._get_consistencies():
|
||||
@ -111,7 +119,7 @@ class OptionDescription(BaseOption, StorageOptionDescription):
|
||||
self._cache_consistencies = {}
|
||||
for opt, cons in _consistencies.items():
|
||||
#FIXME dans le cache ...
|
||||
if opt._get_id() not in cache_option:
|
||||
if opt._get_id() not in cache_option: # pragma: optional cover
|
||||
raise ConfigError(_('consistency with option {0} '
|
||||
'which is not in Config').format(
|
||||
opt.impl_getname()))
|
||||
@ -125,13 +133,13 @@ class OptionDescription(BaseOption, StorageOptionDescription):
|
||||
cache_option = []
|
||||
else:
|
||||
init = False
|
||||
for option in self.impl_getchildren():
|
||||
for option in self._impl_getchildren(dyn=False):
|
||||
#FIXME specifique id for sqlalchemy?
|
||||
#FIXME avec sqlalchemy ca marche le multi parent ? (dans des configs différentes)
|
||||
#if option.id is None:
|
||||
# raise SystemError(_("an option's id should not be None "
|
||||
# "for {0}").format(option.impl_getname()))
|
||||
if option._get_id() in cache_option:
|
||||
if option._get_id() in cache_option: # pragma: optional cover
|
||||
raise ConflictError(_('duplicate option: {0}').format(option))
|
||||
cache_option.append(option._get_id())
|
||||
option._readonly = True
|
||||
@ -147,7 +155,7 @@ class OptionDescription(BaseOption, StorageOptionDescription):
|
||||
:param group_type: an instance of `GroupType` or `MasterGroupType`
|
||||
that lives in `setting.groups`
|
||||
"""
|
||||
if self._group_type != groups.default:
|
||||
if self._group_type != groups.default: # pragma: optional cover
|
||||
raise TypeError(_('cannot change group_type if already set '
|
||||
'(old {0}, new {1})').format(self._group_type,
|
||||
group_type))
|
||||
@ -155,7 +163,7 @@ class OptionDescription(BaseOption, StorageOptionDescription):
|
||||
self._group_type = group_type
|
||||
if isinstance(group_type, groups.MasterGroupType):
|
||||
MasterSlaves(self.impl_getname(), self.impl_getchildren())
|
||||
else:
|
||||
else: # pragma: optional cover
|
||||
raise ValueError(_('group_type: {0}'
|
||||
' not allowed').format(group_type))
|
||||
|
||||
@ -166,18 +174,29 @@ class OptionDescription(BaseOption, StorageOptionDescription):
|
||||
if self._cache_consistencies is None:
|
||||
return True
|
||||
#consistencies is something like [('_cons_not_equal', (opt1, opt2))]
|
||||
consistencies = self._cache_consistencies.get(option)
|
||||
if isinstance(option, DynSymLinkOption):
|
||||
consistencies = self._cache_consistencies.get(option._opt)
|
||||
else:
|
||||
consistencies = self._cache_consistencies.get(option)
|
||||
if consistencies is not None:
|
||||
for func, all_cons_opts, params in consistencies:
|
||||
warnings_only = params.get('warnings_only', False)
|
||||
#all_cons_opts[0] is the option where func is set
|
||||
if isinstance(option, DynSymLinkOption):
|
||||
subpath = '.'.join(option._dyn.split('.')[:-1])
|
||||
namelen = len(option._opt.impl_getname())
|
||||
suffix = option.impl_getname()[namelen:]
|
||||
opts = []
|
||||
for opt in all_cons_opts:
|
||||
name = opt.impl_getname() + suffix
|
||||
path = subpath + '.' + name
|
||||
opts.append(opt._impl_to_dyn(name, path))
|
||||
else:
|
||||
opts = all_cons_opts
|
||||
try:
|
||||
all_cons_opts[0]._launch_consistency(func, option,
|
||||
value,
|
||||
context, index,
|
||||
submulti_idx,
|
||||
all_cons_opts,
|
||||
warnings_only)
|
||||
opts[0]._launch_consistency(func, option, value, context,
|
||||
index, submulti_idx, opts,
|
||||
warnings_only)
|
||||
except ValueError as err:
|
||||
if warnings_only:
|
||||
raise ValueWarning(err.message, option)
|
||||
@ -189,12 +208,13 @@ class OptionDescription(BaseOption, StorageOptionDescription):
|
||||
:param descr: parent :class:`tiramisu.option.OptionDescription`
|
||||
"""
|
||||
if descr is None:
|
||||
self.impl_build_cache_consistency()
|
||||
#FIXME faut le desactiver ?
|
||||
#self.impl_build_cache_consistency()
|
||||
self.impl_build_cache_option()
|
||||
descr = self
|
||||
super(OptionDescription, self)._impl_getstate(descr)
|
||||
self._state_group_type = str(self._group_type)
|
||||
for option in self.impl_getchildren():
|
||||
for option in self._impl_getchildren():
|
||||
option._impl_getstate(descr)
|
||||
|
||||
def __getstate__(self):
|
||||
@ -224,9 +244,12 @@ class OptionDescription(BaseOption, StorageOptionDescription):
|
||||
self.impl_build_cache_option()
|
||||
descr = self
|
||||
self._group_type = getattr(groups, self._state_group_type)
|
||||
if isinstance(self._group_type, groups.MasterGroupType):
|
||||
MasterSlaves(self.impl_getname(), self.impl_getchildren(),
|
||||
validate=False)
|
||||
del(self._state_group_type)
|
||||
super(OptionDescription, self)._impl_setstate(descr)
|
||||
for option in self.impl_getchildren():
|
||||
for option in self._impl_getchildren(dyn=False):
|
||||
option._impl_setstate(descr)
|
||||
|
||||
def __setstate__(self, state):
|
||||
@ -235,3 +258,129 @@ class OptionDescription(BaseOption, StorageOptionDescription):
|
||||
self._stated
|
||||
except AttributeError:
|
||||
self._impl_setstate()
|
||||
|
||||
def _impl_get_suffixes(self, context):
|
||||
callback, callback_params = self.impl_get_callback()
|
||||
if callback_params is None:
|
||||
callback_params = {}
|
||||
values = carry_out_calculation(self, config=context,
|
||||
callback=callback,
|
||||
callback_params=callback_params)
|
||||
if len(values) > len(set(values)):
|
||||
raise ConfigError(_('DynOptionDescription callback return not uniq value'))
|
||||
for val in values:
|
||||
if not isinstance(val, str) or re.match(name_regexp, val) is None:
|
||||
raise ValueError(_("invalid suffix: {0} for option").format(val))
|
||||
return values
|
||||
|
||||
def _impl_search_dynchild(self, name=undefined, context=undefined):
|
||||
ret = []
|
||||
for child in self._impl_st_getchildren():
|
||||
cname = child.impl_getname()
|
||||
if isinstance(child, DynOptionDescription) and \
|
||||
(name is undefined or name.startswith(cname)):
|
||||
path = cname
|
||||
for value in child._impl_get_suffixes(context):
|
||||
if name is undefined:
|
||||
ret.append(SynDynOptionDescription(child, cname + value, path + value, value))
|
||||
elif name == cname + value:
|
||||
return SynDynOptionDescription(child, name, path + value, value)
|
||||
return ret
|
||||
|
||||
def _impl_get_dynchild(self, child, suffix):
|
||||
name = child.impl_getname() + suffix
|
||||
path = self._name + suffix + '.' + name
|
||||
if isinstance(child, OptionDescription):
|
||||
return SynDynOptionDescription(child, name, path, suffix)
|
||||
else:
|
||||
return child._impl_to_dyn(name, path)
|
||||
|
||||
def _impl_getchildren(self, dyn=True, context=undefined):
|
||||
for child in self._impl_st_getchildren():
|
||||
cname = child._name
|
||||
if dyn and child.impl_is_dynoptiondescription():
|
||||
path = cname
|
||||
for value in child._impl_get_suffixes(context):
|
||||
yield SynDynOptionDescription(child,
|
||||
cname + value,
|
||||
path + value, value)
|
||||
else:
|
||||
yield child
|
||||
|
||||
def impl_getchildren(self):
|
||||
return list(self._impl_getchildren())
|
||||
|
||||
|
||||
class DynOptionDescription(OptionDescription):
|
||||
def __init__(self, name, doc, children, requires=None, properties=None,
|
||||
callback=None, callback_params=None):
|
||||
for child in children:
|
||||
if isinstance(child, OptionDescription):
|
||||
if child.impl_get_group_type() != groups.master:
|
||||
raise ConfigError(_('cannot set optiondescription in an '
|
||||
'dynoptiondescription'))
|
||||
for chld in child._impl_getchildren():
|
||||
chld._subdyn = self
|
||||
if isinstance(child, SymLinkOption):
|
||||
raise ConfigError(_('cannot set symlinkoption in an '
|
||||
'dynoptiondescription'))
|
||||
child._subdyn = self
|
||||
super(DynOptionDescription, self).__init__(name, doc, children,
|
||||
requires, properties)
|
||||
self.impl_set_callback(callback, callback_params)
|
||||
|
||||
def _validate_callback(self, callback, callback_params):
|
||||
if callback is None:
|
||||
raise ConfigError(_('callback is mandatory for dynoptiondescription'))
|
||||
|
||||
|
||||
class SynDynOptionDescription(object):
|
||||
__slots__ = ('_opt', '_name', '_path', '_suffix')
|
||||
|
||||
def __init__(self, opt, name, path, suffix):
|
||||
self._opt = opt
|
||||
self._name = name
|
||||
self._path = path
|
||||
self._suffix = suffix
|
||||
|
||||
def __getattr__(self, name, context=undefined):
|
||||
if name in dir(self._opt):
|
||||
return getattr(self._opt, name)
|
||||
return self._opt._getattr(name, self._name, self._suffix, context)
|
||||
|
||||
def impl_getname(self):
|
||||
return self._name
|
||||
|
||||
def _impl_getchildren(self, dyn=True, context=undefined):
|
||||
children = []
|
||||
for child in self._opt._impl_getchildren():
|
||||
children.append(self._opt._impl_get_dynchild(child, self._suffix))
|
||||
return children
|
||||
|
||||
def impl_getchildren(self):
|
||||
return self._impl_getchildren()
|
||||
|
||||
def impl_getpath(self, context):
|
||||
return self._path
|
||||
|
||||
def impl_getpaths(self, include_groups=False, _currpath=None):
|
||||
return _impl_getpaths(self, include_groups, _currpath)
|
||||
|
||||
|
||||
def _impl_getpaths(klass, include_groups, _currpath):
|
||||
"""returns a list of all paths in klass, recursively
|
||||
_currpath should not be provided (helps with recursion)
|
||||
"""
|
||||
if _currpath is None:
|
||||
_currpath = []
|
||||
paths = []
|
||||
for option in klass._impl_getchildren():
|
||||
attr = option.impl_getname()
|
||||
if option.impl_is_optiondescription():
|
||||
if include_groups:
|
||||
paths.append('.'.join(_currpath + [attr]))
|
||||
paths += option.impl_getpaths(include_groups=include_groups,
|
||||
_currpath=_currpath + [attr])
|
||||
else:
|
||||
paths.append('.'.join(_currpath + [attr]))
|
||||
return paths
|
||||
|
Reference in New Issue
Block a user