simplify tiramisu/option/optiondescription.py

This commit is contained in:
2018-11-13 22:10:01 +01:00
parent f04169d5a6
commit 974a178d4b
17 changed files with 302 additions and 360 deletions

View File

@ -19,72 +19,78 @@
# the whole pypy projet is under MIT licence
# ____________________________________________________________
from copy import copy
from typing import Optional, Iterator, Union, List
from ..i18n import _
from ..setting import ConfigBag, OptionBag, groups, undefined, owners
from .baseoption import BaseOption, OnlyOption
from ..setting import ConfigBag, OptionBag, groups, undefined, owners, Undefined
from .baseoption import BaseOption
from .option import ALLOWED_CONST_LIST
from .syndynoptiondescription import SynDynOptionDescription
from ..error import ConfigError, ConflictError
class CacheOptionDescription(BaseOption):
__slots__ = ('_cache_consistencies', '_cache_force_store_values')
__slots__ = ('_cache_consistencies',
'_cache_force_store_values')
def impl_already_build_caches(self) -> bool:
return self.impl_is_readonly()
def _build_cache(self,
config,
path='',
_consistencies=None,
_consistencies_id=0,
currpath: List[str]=None,
cache_option=None,
force_store_values=None):
"""validate duplicate option and set option has readonly option
force_store_values=None) -> None:
"""validate options and set option has readonly option
"""
# cache_option is None only when we start to build cache
if cache_option is None:
if self.impl_is_readonly():
raise ConfigError(_('option description seems to be part of an other '
'config'))
# _consistencies is None only when we start to build cache
if _consistencies is None:
init = True
_consistencies = {}
cache_option = []
if __debug__:
cache_option = []
force_store_values = []
currpath = []
else:
init = False
for option in self.impl_getchildren(config_bag=undefined,
dyn=False):
cache_option.append(option)
if path == '':
subpath = option.impl_getname()
else:
subpath = path + '.' + option.impl_getname()
if self.impl_is_readonly():
# cache already set
raise ConfigError(_('option description seems to be part of an other '
'config'))
for option in self.get_children(config_bag=undefined,
dyn=False):
if __debug__:
cache_option.append(option)
sub_currpath = currpath + [option.impl_getname()]
subpath = '.'.join(sub_currpath)
if isinstance(option, OptionDescription):
option._set_readonly()
option._build_cache(config,
subpath,
option._build_cache(subpath,
_consistencies,
_consistencies_id,
sub_currpath,
cache_option,
force_store_values)
else:
option._set_readonly()
is_multi = option.impl_is_multi()
if not option.impl_is_symlinkoption():
properties = option.impl_getproperties()
if 'force_store_value' in properties:
if option.impl_is_master_slaves('slave'):
# problem with index
raise ConfigError(_('the slave "{0}" cannot have '
'"force_store_value" property').format(
option.impl_get_display_name()))
if option.issubdyn():
raise ConfigError(_('the dynoption "{0}" cannot have '
'"force_store_value" property').format(
option.impl_get_display_name()))
if __debug__:
if option.impl_is_master_slaves('slave'):
# problem with index
raise ConfigError(_('the slave "{0}" cannot have '
'"force_store_value" property').format(
option.impl_get_display_name()))
if option.issubdyn():
raise ConfigError(_('the dynoption "{0}" cannot have '
'"force_store_value" property').format(
option.impl_get_display_name()))
force_store_values.append((subpath, option))
if 'force_default_on_freeze' in properties and \
if __debug__ and 'force_default_on_freeze' in properties and \
'frozen' not in properties and \
option.impl_is_master_slaves('master'):
raise ConfigError(_('a master ({0}) cannot have '
@ -93,15 +99,14 @@ class CacheOptionDescription(BaseOption):
for cons_id, func, all_cons_opts, params in option.get_consistencies():
option._valid_consistencies(all_cons_opts[1:], init=False)
if func not in ALLOWED_CONST_LIST and is_multi:
is_masterslaves = option.impl_is_master_slaves()
if not is_masterslaves:
if __debug__ and not option.impl_is_master_slaves():
raise ConfigError(_('malformed consistency option "{0}" '
'must be a masterslaves').format(
option.impl_getname()))
masterslaves = option.impl_get_master_slaves()
for weak_opt in all_cons_opts:
opt = weak_opt()
if func not in ALLOWED_CONST_LIST and is_multi:
if __debug__ and func not in ALLOWED_CONST_LIST and is_multi:
if not opt.impl_is_master_slaves():
raise ConfigError(_('malformed consistency option "{0}" '
'must not be a multi for "{1}"').format(
@ -119,10 +124,11 @@ class CacheOptionDescription(BaseOption):
# if context is set to callback, must be reset each time a value change
if hasattr(option, '_has_calc_context'):
self._add_dependency(option)
is_slave = None
if is_multi:
all_requires = option.impl_getrequires()
if all_requires != tuple():
if __debug__:
is_slave = None
if is_multi:
all_requires = option.impl_getrequires()
for requires in all_requires:
for require in requires:
#if option in require is a multi:
@ -144,8 +150,11 @@ class CacheOptionDescription(BaseOption):
raise ValueError(_('malformed requirements option "{0}" '
'must not be a multi for "{1}"').format(
require_opt.impl_getname(), option.impl_getname()))
if not hasattr(option, '_path'):
option._path = subpath
option._set_readonly()
if init:
if len(cache_option) != len(set(cache_option)):
if __debug__ and len(cache_option) != len(set(cache_option)):
for idx in range(1, len(cache_option) + 1):
opt = cache_option.pop(0)
if opt in cache_option:
@ -154,7 +163,7 @@ class CacheOptionDescription(BaseOption):
self._cache_consistencies = {}
for weak_opt, cons in _consistencies.items():
opt = weak_opt()
if opt not in cache_option:
if __debug__ and opt not in cache_option:
raise ConfigError(_('consistency with option {0} '
'which is not in Config').format(
opt.impl_getname()))
@ -162,69 +171,43 @@ class CacheOptionDescription(BaseOption):
self._cache_force_store_values = force_store_values
self._set_readonly()
def impl_already_build_caches(self):
if hasattr(self, '_cache_consistencies'):
return True
return False
def impl_build_force_store_values(self,
context):
value_setted = False
values = context.cfgimpl_get_values()
config_bag: ConfigBag) -> None:
commit = False
if not hasattr(self, '_cache_force_store_values'):
raise ConfigError(_('option description seems to be part of an other '
'config'))
values = config_bag.context.cfgimpl_get_values()
for subpath, option in self._cache_force_store_values:
if not values._p_.hasvalue(subpath):
config_bag = ConfigBag(context=context)
option_bag = OptionBag()
option_bag.set_option(option,
subpath,
None,
config_bag)
option_bag.properties = frozenset()
value = values.getvalue(option_bag)
value_setted = True
values._p_.setvalue(subpath,
value,
values.getvalue(option_bag),
owners.forced,
None,
False)
commit = True
if value_setted:
if commit:
values._p_.commit()
def _build_cache_option(self,
_currpath=None):
if self.impl_is_readonly() or \
(_currpath is None and getattr(self, '_cache_consistencies', None) is not None):
# cache already set
return
if _currpath is None:
save = True
_currpath = []
else:
save = False
for option in self.impl_getchildren(config_bag=undefined,
dyn=False):
attr = option.impl_getname()
path = str('.'.join(_currpath + [attr]))
option._path = path
if option.impl_is_optiondescription():
_currpath.append(attr)
option._build_cache_option(_currpath)
_currpath.pop()
if save and not hasattr(self, '_cache_consistencies'):
self._cache_consistencies = None
class OptionDescriptionWalk(CacheOptionDescription):
__slots__ = ('_children',)
def impl_getchild(self,
name,
config_bag,
subpath):
def get_child(self,
name: str,
config_bag: ConfigBag,
subpath: str) -> Union[BaseOption, SynDynOptionDescription]:
# if not dyn
if name in self._children[0]:
return self._children[1][self._children[0].index(name)]
# if dyn
for child in self._children[1]:
if child.impl_is_dynoptiondescription():
cname = child.impl_getname()
@ -238,17 +221,16 @@ class OptionDescriptionWalk(CacheOptionDescription):
'in optiondescription "{1}"'
'').format(name, self.impl_getname()))
def impl_getchildren(self,
config_bag,
dyn=True):
subpath = None
def get_children(self,
config_bag: Union[ConfigBag, Undefined],
dyn: bool=True) -> Iterator[Union[BaseOption, SynDynOptionDescription]]:
if not dyn or config_bag is undefined or \
config_bag.context.cfgimpl_get_description() == self:
subpath = ''
else:
subpath = self.impl_getpath()
for child in self._children[1]:
if dyn and child.impl_is_dynoptiondescription():
if subpath is None:
if config_bag.context.cfgimpl_get_description() == self:
subpath = ''
else:
subpath = self.impl_getpath()
for suffix in child.impl_get_suffixes(config_bag):
yield SynDynOptionDescription(child,
subpath,
@ -256,69 +238,22 @@ class OptionDescriptionWalk(CacheOptionDescription):
else:
yield child
def impl_get_opt_by_path(self,
path,
config_bag):
opt = self
subpath = None
for step in path.split('.'):
opt = opt.impl_getchild(step,
config_bag,
subpath)
if subpath is None:
subpath = step
else:
subpath += '.' + step
return opt
def impl_get_options(self,
bytype,
byname,
config_bag,
self_opt=None):
def get_children_recursively(self,
bytype: Optional[BaseOption],
byname: Optional[str],
config_bag: ConfigBag,
self_opt: BaseOption=None) -> Iterator[Union[BaseOption, SynDynOptionDescription]]:
if self_opt is None:
self_opt = self
def _filter_by_name(option):
return byname is None or option.impl_getname() == byname
for option in self_opt.impl_getchildren(config_bag):
for option in self_opt.get_children(config_bag):
if option.impl_is_optiondescription():
for subopt in option.impl_get_options(bytype,
byname,
config_bag):
for subopt in option.get_children_recursively(bytype,
byname,
config_bag):
yield subopt
else:
if bytype is not None:
if isinstance(option, bytype) and \
_filter_by_name(option):
yield option
elif _filter_by_name(option):
yield option
def get_dynoptions(self,
option_bag):
option = option_bag.option
dynopt = option.getsubdyn()
rootpath = dynopt.impl_getpath()
ori_index = len(rootpath) + 1
subpaths = [rootpath] + option.impl_getpath()[ori_index:].split('.')[:-1]
for suffix in dynopt.impl_get_suffixes(option_bag.config_bag):
subpath = '.'.join([subp + suffix for subp in subpaths])
yield self.impl_get_dynchild(option,
suffix,
subpath)
def impl_get_dynchild(self,
child,
suffix,
subpath):
if isinstance(child, OptionDescription):
return SynDynOptionDescription(child,
subpath,
suffix)
else:
return child.impl_get_dynoption(subpath,
suffix)
elif (byname is None or option.impl_getname() == byname) and \
(bytype is None or isinstance(option, bytype)):
yield option
class OptionDescription(OptionDescriptionWalk):
@ -328,68 +263,65 @@ class OptionDescription(OptionDescriptionWalk):
__slots__ = ('_group_type',)
def __init__(self,
name,
doc,
children,
name: str,
doc: str,
children: List[BaseOption],
requires=None,
properties=None):
properties=None) -> None:
"""
:param children: a list of options (including optiondescriptions)
"""
if not isinstance(children, list):
raise ValueError(_('children in optiondescription "{}" must be a list').format(name))
assert isinstance(children, list), _('children in optiondescription "{}" '
'must be a list').format(name)
super().__init__(name,
doc=doc,
requires=requires,
properties=properties)
child_names = []
dynopt_names = []
if __debug__:
dynopt_names = []
for child in children:
name = child.impl_getname()
child_names.append(name)
if child.impl_is_dynoptiondescription():
if __debug__ and child.impl_is_dynoptiondescription():
dynopt_names.append(name)
# before sorting
children_ = (tuple(child_names), tuple(children))
# better performance like this
child_names.sort()
old = None
for child in child_names:
if child == old:
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(_('the option\'s name "{}" start as '
'the dynoptiondescription\'s name "{}"').format(child, dynopt))
old = child
if __debug__:
# better performance like this
child_names.sort()
old = None
for child in child_names:
if child == old:
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(_('the option\'s name "{}" start as '
'the dynoptiondescription\'s name "{}"').format(child, dynopt))
old = child
self._children = children_
#self._cache_consistencies = None
# the group_type is useful for filtering OptionDescriptions in a config
self._group_type = groups.default
def impl_is_optiondescription(self):
return self.__class__.__name__ in ['OptionDescription',
'DynOptionDescription',
'SynDynOptionDescription',
'MasterSlaves']
def impl_is_optiondescription(self) -> bool:
return True
def impl_is_dynoptiondescription(self):
return self.__class__.__name__ in ['DynOptionDescription',
'SynDynOptionDescription']
def impl_is_master_slaves(self, *args, **kwargs):
def impl_is_dynoptiondescription(self) -> bool:
return False
def impl_getdoc(self):
return self.impl_get_information('doc')
def impl_is_master_slaves(self,
*args,
**kwargs) -> bool:
return False
# ____________________________________________________________
def impl_set_group_type(self,
group_type):
group_type: groups.GroupType) -> None:
"""sets a given group object to an OptionDescription
:param group_type: an instance of `GroupType` or `MasterGroupType`
@ -406,10 +338,5 @@ class OptionDescription(OptionDescriptionWalk):
raise ConfigError('please use MasterSlaves object instead of OptionDescription')
self._group_type = group_type
def impl_get_group_type(self):
def impl_get_group_type(self) -> groups.GroupType:
return self._group_type
def impl_validate_value(self,
*args,
**kwargs):
pass