tiramisu/tiramisu/option/optiondescription.py

416 lines
19 KiB
Python

# -*- coding: utf-8 -*-
# Copyright (C) 2014-2018 Team tiramisu (see AUTHORS for all contributors)
#
# This program is free software: you can redistribute it and/or modify it
# under the terms of the GNU Lesser General Public License as published by the
# Free Software Foundation, either version 3 of the License, or (at your
# option) any later version.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
# details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
# The original `Config` design model is unproudly borrowed from
# the rough pypy's guys: http://codespeak.net/svn/pypy/dist/pypy/config/
# the whole pypy projet is under MIT licence
# ____________________________________________________________
from copy import copy
from ..i18n import _
from ..setting import ConfigBag, OptionBag, groups, undefined, owners
from .baseoption import BaseOption, OnlyOption
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')
def _build_cache(self,
config,
path='',
_consistencies=None,
_consistencies_id=0,
cache_option=None,
force_store_values=None):
"""validate duplicate option 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'))
init = True
_consistencies = {}
cache_option = []
force_store_values = []
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 isinstance(option, OptionDescription):
option._set_readonly()
option._build_cache(config,
subpath,
_consistencies,
_consistencies_id,
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()))
force_store_values.append((subpath, option))
if '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 '
'"force_default_on_freeze" property without "frozen"'
'').format(subpath))
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:
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 not opt.impl_is_master_slaves():
raise ConfigError(_('malformed consistency option "{0}" '
'must not be a multi for "{1}"').format(
option.impl_getname(), opt.impl_getname()))
elif masterslaves != opt.impl_get_master_slaves():
raise ConfigError(_('malformed consistency option "{0}" '
'must be in same masterslaves as "{1}"').format(
option.impl_getname(), opt.impl_getname()))
_consistencies.setdefault(weak_opt,
[]).append((_consistencies_id,
func,
all_cons_opts,
params))
_consistencies_id += 1
# 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():
for requires in all_requires:
for require in requires:
#if option in require is a multi:
# * option in require must be a master or a slave
# * current option must be a slave (and only a slave)
# * option in require and current option must be in same masterslaves
for require_opt, values in require[0]:
if require_opt.impl_is_multi():
if is_slave is None:
is_slave = option.impl_is_master_slaves('slave')
if is_slave:
masterslaves = option.impl_get_master_slaves()
if is_slave and require_opt.impl_is_master_slaves():
if masterslaves != require_opt.impl_get_master_slaves():
raise ValueError(_('malformed requirements option "{0}" '
'must be in same masterslaves for "{1}"').format(
require_opt.impl_getname(), option.impl_getname()))
else:
raise ValueError(_('malformed requirements option "{0}" '
'must not be a multi for "{1}"').format(
require_opt.impl_getname(), option.impl_getname()))
if init:
if 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:
raise ConflictError(_('duplicate option: {0}').format(opt))
if _consistencies != {}:
self._cache_consistencies = {}
for weak_opt, cons in _consistencies.items():
opt = weak_opt()
if opt not in cache_option:
raise ConfigError(_('consistency with option {0} '
'which is not in Config').format(
opt.impl_getname()))
self._cache_consistencies[opt] = tuple(cons)
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()
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,
owners.forced,
None,
False)
if value_setted:
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):
if name in self._children[0]:
return self._children[1][self._children[0].index(name)]
for child in self._children[1]:
if child.impl_is_dynoptiondescription():
cname = child.impl_getname()
if name.startswith(cname):
for value in child.impl_get_suffixes(config_bag):
if name == cname + value:
return SynDynOptionDescription(child,
subpath,
value)
raise AttributeError(_('unknown option "{0}" '
'in optiondescription "{1}"'
'').format(name, self.impl_getname()))
def impl_getchildren(self,
config_bag,
dyn=True):
subpath = None
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,
suffix)
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):
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):
if option.impl_is_optiondescription():
for subopt in option.impl_get_options(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)
class OptionDescription(OptionDescriptionWalk):
"""Config's schema (organisation, group) and container of Options
The `OptionsDescription` objects lives in the `tiramisu.config.Config`.
"""
__slots__ = ('_group_type',)
def __init__(self,
name,
doc,
children,
requires=None,
properties=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))
super().__init__(name,
doc=doc,
requires=requires,
properties=properties)
child_names = []
dynopt_names = []
for child in children:
name = child.impl_getname()
child_names.append(name)
if child.impl_is_dynoptiondescription():
dynopt_names.append(name)
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
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_dynoptiondescription(self):
return self.__class__.__name__ in ['DynOptionDescription',
'SynDynOptionDescription']
def impl_is_master_slaves(self, *args, **kwargs):
return False
def impl_getdoc(self):
return self.impl_get_information('doc')
# ____________________________________________________________
def impl_set_group_type(self,
group_type):
"""sets a given group object to an OptionDescription
:param group_type: an instance of `GroupType` or `MasterGroupType`
that lives in `setting.groups`
"""
if self._group_type != groups.default:
raise ValueError(_('cannot change group_type if already set '
'(old {0}, new {1})').format(self._group_type,
group_type))
if not isinstance(group_type, groups.GroupType):
raise ValueError(_('group_type: {0}'
' not allowed').format(group_type))
if isinstance(group_type, groups.MasterGroupType):
raise ConfigError('please use MasterSlaves object instead of OptionDescription')
self._group_type = group_type
def impl_get_group_type(self):
return self._group_type
def impl_validate_value(self,
*args,
**kwargs):
pass