tiramisu/tiramisu/option/optiondescription.py

453 lines
20 KiB
Python
Raw Normal View History

# -*- coding: utf-8 -*-
2018-01-26 07:33:47 +01:00
# 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
2014-06-19 23:22:39 +02:00
from ..i18n import _
2017-12-19 23:11:45 +01:00
from ..setting import ConfigBag, groups, undefined, owners
2018-04-09 21:37:49 +02:00
from .baseoption import BaseOption, OnlyOption
2017-12-02 22:53:57 +01:00
from .option import ALLOWED_CONST_LIST, DynSymLinkOption
from .syndynoptiondescription import SynDynOptionDescription
from ..error import ConfigError, ConflictError
2016-03-19 21:27:37 +01:00
2017-07-22 16:26:06 +02:00
class CacheOptionDescription(BaseOption):
__slots__ = ('_cache_paths', '_cache_consistencies', '_cache_force_store_values')
2017-11-23 16:56:14 +01:00
def _build_cache(self,
config,
path='',
_consistencies=None,
2017-12-23 10:40:41 +01:00
_consistencies_id=0,
2017-11-23 16:56:14 +01:00
cache_option=None,
2018-04-12 23:04:33 +02:00
force_store_values=None):
2015-12-22 22:06:14 +01:00
"""validate duplicate option and set option has readonly option
"""
2017-09-17 15:55:32 +02:00
# cache_option is None only when we start to build cache
2015-12-22 22:06:14 +01:00
if cache_option is None:
2016-09-22 08:27:18 +02:00
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
2017-09-17 15:55:32 +02:00
2017-12-19 23:11:45 +01:00
for option in self.impl_getchildren(config_bag=undefined,
2017-11-28 22:42:30 +01:00
dyn=False):
2017-07-22 16:26:06 +02:00
cache_option.append(option)
if path == '':
subpath = option.impl_getname()
else:
subpath = path + '.' + option.impl_getname()
2015-12-22 22:06:14 +01:00
if isinstance(option, OptionDescription):
2017-09-17 15:55:32 +02:00
option._set_readonly()
2017-11-23 16:56:14 +01:00
option._build_cache(config,
subpath,
_consistencies,
2017-12-23 10:40:41 +01:00
_consistencies_id,
2017-11-23 16:56:14 +01:00
cache_option,
2018-04-12 23:04:33 +02:00
force_store_values)
2015-12-22 22:06:14 +01:00
else:
2017-09-17 15:55:32 +02:00
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:
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))
2018-01-05 23:32:00 +01:00
for cons_id, func, all_cons_opts, params in option.get_consistencies():
2016-10-16 21:46:17 +02:00
option._valid_consistencies(all_cons_opts[1:], init=False)
2017-07-22 16:26:06 +02:00
if func not in ALLOWED_CONST_LIST and is_multi:
2017-07-08 15:59:56 +02:00
is_masterslaves = option.impl_is_master_slaves()
if not is_masterslaves:
2017-07-21 18:03:34 +02:00
raise ConfigError(_('malformed consistency option "{0}" '
2018-04-12 23:04:33 +02:00
'must be a masterslaves').format(
option.impl_getname()))
masterslaves = option.impl_get_master_slaves()
2017-12-19 23:11:45 +01:00
for weak_opt in all_cons_opts:
opt = weak_opt()
2017-07-22 16:26:06 +02:00
if func not in ALLOWED_CONST_LIST and is_multi:
if not opt.impl_is_master_slaves():
2017-07-21 18:03:34 +02:00
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():
2017-07-21 18:03:34 +02:00
raise ConfigError(_('malformed consistency option "{0}" '
2018-04-12 23:04:33 +02:00
'must be in same masterslaves as "{1}"').format(
option.impl_getname(), opt.impl_getname()))
2017-12-19 23:11:45 +01:00
_consistencies.setdefault(weak_opt,
2017-12-23 10:40:41 +01:00
[]).append((_consistencies_id,
func,
all_cons_opts,
params))
2017-12-23 10:40:41 +01:00
_consistencies_id += 1
2017-09-17 15:55:32 +02:00
# if context is set to callback, must be reset each time a value change
if hasattr(option, '_has_calc_context'):
2017-12-13 22:15:34 +01:00
self._add_dependency(option)
2015-12-22 22:06:14 +01:00
is_slave = None
if is_multi:
2015-12-22 22:06:14 +01:00
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)
2018-04-12 23:04:33 +02:00
# * option in require and current option must be in same masterslaves
2017-05-20 16:28:19 +02:00
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():
2018-04-12 23:04:33 +02:00
raise ValueError(_('malformed requirements option "{0}" '
'must be in same masterslaves for "{1}"').format(
2017-05-20 16:28:19 +02:00
require_opt.impl_getname(), option.impl_getname()))
else:
2017-11-20 17:01:36 +01:00
raise ValueError(_('malformed requirements option "{0}" '
'must not be a multi for "{1}"').format(
2015-12-22 22:06:14 +01:00
require_opt.impl_getname(), option.impl_getname()))
if init:
2014-11-10 23:15:08 +01:00
if len(cache_option) != len(set(cache_option)):
for idx in range(1, len(cache_option) + 1):
2014-11-10 23:15:08 +01:00
opt = cache_option.pop(0)
if opt in cache_option:
raise ConflictError(_('duplicate option: {0}').format(opt))
2015-12-22 22:06:14 +01:00
if _consistencies != {}:
self._cache_consistencies = {}
2017-12-19 23:11:45 +01:00
for weak_opt, cons in _consistencies.items():
opt = weak_opt()
2018-04-12 23:04:33 +02:00
if opt not in cache_option:
2015-12-22 22:06:14 +01:00
raise ConfigError(_('consistency with option {0} '
'which is not in Config').format(
opt.impl_getname()))
self._cache_consistencies[opt] = tuple(cons)
2016-09-22 08:27:18 +02:00
self._cache_force_store_values = force_store_values
2017-09-17 15:55:32 +02:00
self._set_readonly()
2017-07-22 16:26:06 +02:00
def impl_already_build_caches(self):
2017-12-02 22:53:57 +01:00
if hasattr(self, '_cache_paths'):
return True
return False
2016-09-22 08:27:18 +02:00
2017-11-23 16:56:14 +01:00
def impl_build_force_store_values(self,
2018-04-06 23:51:25 +02:00
context):
2017-12-13 22:15:34 +01:00
value_setted = False
values = context.cfgimpl_get_values()
2016-09-22 08:27:18 +02:00
for subpath, option in self._cache_force_store_values:
if option.impl_is_master_slaves('slave'):
# problem with index
2018-04-12 23:04:33 +02:00
raise ConfigError(_('the slave "{0}" cannot have '
2018-06-25 21:40:16 +02:00
'"force_store_value" property').format(
option.impl_get_display_name()))
2018-04-09 21:37:49 +02:00
if option.issubdyn():
2018-04-12 23:04:33 +02:00
raise ConfigError(_('the dynoption "{0}" cannot have '
2018-06-25 21:40:16 +02:00
'"force_store_value" property').format(
option.impl_get_display_name()))
2018-04-06 23:51:25 +02:00
if not values._p_.hasvalue(subpath):
2017-12-19 23:11:45 +01:00
config_bag = ConfigBag(config=context, option=option)
2018-06-25 21:40:16 +02:00
config_bag.properties = frozenset()
2017-12-19 23:11:45 +01:00
value = values.getvalue(subpath,
None,
config_bag)
2017-12-13 22:15:34 +01:00
value_setted = True
values._p_.setvalue(subpath,
value,
owners.forced,
None,
False)
2017-07-16 23:11:12 +02:00
2017-12-13 22:15:34 +01:00
if value_setted:
values._p_.commit()
2016-09-22 08:27:18 +02:00
2017-11-23 16:56:14 +01:00
def _build_cache_option(self,
_currpath=None,
cache_path=None,
cache_option=None):
2017-07-22 16:26:06 +02:00
if self.impl_is_readonly() or (_currpath is None and getattr(self, '_cache_paths', None) is not None):
# cache already set
return
if _currpath is None:
save = True
_currpath = []
else:
save = False
if cache_path is None:
cache_path = []
cache_option = []
2017-12-19 23:11:45 +01:00
for option in self.impl_getchildren(config_bag=undefined,
2017-11-28 22:42:30 +01:00
dyn=False):
2017-07-22 16:26:06 +02:00
attr = option.impl_getname()
path = str('.'.join(_currpath + [attr]))
cache_option.append(option)
cache_path.append(path)
if option.impl_is_optiondescription():
_currpath.append(attr)
2017-11-23 16:56:14 +01:00
option._build_cache_option(_currpath,
cache_path,
cache_option)
2017-07-22 16:26:06 +02:00
_currpath.pop()
if save:
2017-12-02 22:53:57 +01:00
self._cache_paths = (tuple(cache_option), tuple(cache_path))
2017-07-22 16:26:06 +02:00
class OptionDescriptionWalk(CacheOptionDescription):
__slots__ = ('_children',)
2018-04-09 21:37:49 +02:00
def build_dynoptions(self,
option,
config_bag):
dynopt = option.getsubdyn()
rootpath = self.impl_get_path_by_opt(dynopt)
2018-04-12 23:04:33 +02:00
ori_index = len(rootpath) + 1
subpaths = [rootpath] + option.impl_getpath(config_bag.config)[ori_index:].split('.')[:-1]
2018-04-09 21:37:49 +02:00
for suffix in dynopt._impl_get_suffixes(config_bag):
subpath = '.'.join([subp + suffix for subp in subpaths])
if isinstance(option, OnlyOption):
yield DynSymLinkOption(option,
subpath,
suffix)
else:
yield SynDynOptionDescription(option,
subpath,
suffix)
2017-11-13 22:45:53 +01:00
def impl_get_options_paths(self,
bytype,
byname,
_subpath,
2017-12-19 23:11:45 +01:00
config_bag):
2018-04-10 12:33:51 +02:00
def _filter_by_type(path,
option):
if isinstance(option,
bytype):
2018-04-12 23:04:33 +02:00
return _filter_by_name(path, option)
2017-07-22 16:26:06 +02:00
2017-11-13 22:45:53 +01:00
def _filter_by_name(path,
option):
2017-07-22 16:26:06 +02:00
name = option.impl_getname()
2018-04-09 21:37:49 +02:00
if option.issubdyn():
2017-07-22 16:26:06 +02:00
if byname.startswith(name):
2018-04-09 21:37:49 +02:00
for doption in self.build_dynoptions(option, config_bag):
if byname == doption.impl_getname():
dpath = doption.impl_getpath(config_bag.config)
2018-04-12 23:04:33 +02:00
return (dpath, doption)
elif byname == name:
return (path, option)
2017-07-22 16:26:06 +02:00
def _filter(path, option):
if bytype is not None:
2018-04-12 23:04:33 +02:00
return _filter_by_type(path, option)
return _filter_by_name(path, option)
2017-07-22 16:26:06 +02:00
opts, paths = self._cache_paths
2018-04-10 12:33:51 +02:00
for index, option in enumerate(opts):
2017-07-22 16:26:06 +02:00
if option.impl_is_optiondescription():
continue
2018-04-10 12:33:51 +02:00
path = paths[index]
2017-07-22 16:26:06 +02:00
if _subpath is not None and not path.startswith(_subpath + '.'):
continue
2018-04-12 23:04:33 +02:00
ret = _filter(path, option)
if ret:
yield ret
2017-07-22 16:26:06 +02:00
2017-11-29 23:25:34 +01:00
def impl_getchild(self,
name,
2017-12-19 23:11:45 +01:00
config_bag,
2017-12-02 22:53:57 +01:00
subconfig):
if name in self._children[0]:
child = self._children[1][self._children[0].index(name)]
if not child.impl_is_dynoptiondescription():
return child
else:
2017-11-29 23:25:34 +01:00
child = self._impl_search_dynchild(name,
2017-12-19 23:11:45 +01:00
subconfig,
config_bag)
2017-11-29 23:25:34 +01:00
if child:
return child
2018-04-11 18:32:13 +02:00
raise AttributeError(_('unknown option "{0}" '
'in optiondescription "{1}"'
2017-11-29 23:25:34 +01:00
'').format(name, self.impl_getname()))
2017-07-22 16:26:06 +02:00
2017-11-23 16:56:14 +01:00
def impl_get_opt_by_path(self,
path):
2017-07-22 16:26:06 +02:00
return self._cache_paths[0][self._cache_paths[1].index(path)]
2017-11-23 16:56:14 +01:00
def impl_get_path_by_opt(self,
opt):
2017-07-22 16:26:06 +02:00
return self._cache_paths[1][self._cache_paths[0].index(opt)]
2017-11-28 22:42:30 +01:00
def impl_getchildren(self,
2017-12-19 23:11:45 +01:00
config_bag,
dyn=True):
2018-04-09 21:37:49 +02:00
subpath = None
2017-11-28 22:42:30 +01:00
for child in self._impl_st_getchildren():
2017-07-22 16:26:06 +02:00
if dyn and child.impl_is_dynoptiondescription():
2018-04-12 23:04:33 +02:00
if config_bag.config is None: # pragma: no cover
2018-03-24 22:37:48 +01:00
raise ConfigError(_('need context'))
2018-04-09 21:37:49 +02:00
if subpath is None:
if config_bag.config.cfgimpl_get_description() == self:
subpath = ''
2018-03-24 22:37:48 +01:00
else:
2018-04-09 21:37:49 +02:00
subpath = self.impl_getpath(config_bag.config)
2017-12-19 23:11:45 +01:00
sconfig_bag = config_bag.copy('nooption')
sconfig_bag.option = child
2018-04-09 21:37:49 +02:00
for suffix in child._impl_get_suffixes(sconfig_bag):
2017-07-22 16:26:06 +02:00
yield SynDynOptionDescription(child,
2018-04-09 21:37:49 +02:00
subpath,
suffix)
2017-07-22 16:26:06 +02:00
else:
yield child
2017-11-28 22:42:30 +01:00
def _impl_st_getchildren(self,
only_dyn=False):
for child in self._children[1]:
if only_dyn is False or child.impl_is_dynoptiondescription():
yield child
2017-11-23 16:56:14 +01:00
def _impl_search_dynchild(self,
name,
2017-12-02 22:53:57 +01:00
subconfig,
2017-12-19 23:11:45 +01:00
config_bag):
2017-11-28 22:42:30 +01:00
for child in self._impl_st_getchildren(only_dyn=True):
2017-12-19 23:11:45 +01:00
sconfig_bag = config_bag.copy('nooption')
sconfig_bag.option = child
2017-07-22 16:26:06 +02:00
cname = child.impl_getname()
if name.startswith(cname):
2017-12-19 23:11:45 +01:00
for value in child._impl_get_suffixes(sconfig_bag):
2017-07-22 16:26:06 +02:00
if name == cname + value:
2017-11-23 16:56:14 +01:00
return SynDynOptionDescription(child,
2017-12-02 22:53:57 +01:00
subconfig.cfgimpl_get_path(),
2017-11-23 16:56:14 +01:00
value)
2017-07-22 16:26:06 +02:00
2017-11-23 16:56:14 +01:00
def _impl_get_dynchild(self,
child,
2017-12-02 22:53:57 +01:00
suffix,
subpath):
2017-07-22 16:26:06 +02:00
if isinstance(child, OptionDescription):
2017-11-23 16:56:14 +01:00
return SynDynOptionDescription(child,
2017-12-02 22:53:57 +01:00
subpath,
2017-11-23 16:56:14 +01:00
suffix)
2017-07-22 16:26:06 +02:00
else:
2017-12-02 22:53:57 +01:00
return DynSymLinkOption(child,
subpath,
suffix)
2017-07-22 16:26:06 +02:00
class OptionDescription(OptionDescriptionWalk):
"""Config's schema (organisation, group) and container of Options
The `OptionsDescription` objects lives in the `tiramisu.config.Config`.
"""
__slots__ = ('_group_type',)
2017-11-23 16:56:14 +01:00
def __init__(self,
name,
doc,
children,
requires=None,
properties=None):
2017-07-22 16:26:06 +02:00
"""
:param children: a list of options (including optiondescriptions)
"""
2017-11-28 22:42:30 +01:00
if not isinstance(children, list):
2017-11-29 23:25:34 +01:00
raise ValueError(_('children in optiondescription "{}" must be a list').format(name))
2018-03-12 11:58:49 +01:00
super().__init__(name,
doc=doc,
requires=requires,
properties=properties)
2017-07-22 16:26:06 +02:00
child_names = []
dynopt_names = []
for child in children:
name = child.impl_getname()
child_names.append(name)
if child.impl_is_dynoptiondescription():
2017-07-22 16:26:06 +02:00
dynopt_names.append(name)
#better performance like this
valid_child = copy(child_names)
valid_child.sort()
old = None
for child in valid_child:
2018-04-12 23:04:33 +02:00
if child == old:
2017-07-22 16:26:06 +02:00
raise ConflictError(_('duplicate option name: '
2017-11-20 17:01:36 +01:00
'"{0}"').format(child))
2017-07-22 16:26:06 +02:00
if dynopt_names:
for dynopt in dynopt_names:
if child != dynopt and child.startswith(dynopt):
2017-11-20 17:01:36 +01:00
raise ConflictError(_('the option\'s name "{}" start as '
'the dynoptiondescription\'s name "{}"').format(child, dynopt))
2017-07-22 16:26:06 +02:00
old = child
2017-11-29 23:25:34 +01:00
self._children = (tuple(child_names), tuple(children))
self._cache_consistencies = None
2017-07-22 16:26:06 +02:00
# the group_type is useful for filtering OptionDescriptions in a config
2017-11-29 23:25:34 +01:00
self._group_type = groups.default
2017-07-22 16:26:06 +02:00
2018-03-24 22:37:48 +01:00
def impl_is_master_slaves(self, *args, **kwargs):
2017-12-02 22:53:57 +01:00
return False
2017-07-22 16:26:06 +02:00
def impl_getdoc(self):
return self.impl_get_information('doc')
2017-11-23 16:56:14 +01:00
def impl_validate(self,
*args,
**kwargs):
2017-07-22 16:26:06 +02:00
"""usefull for OptionDescription"""
pass
# ____________________________________________________________
2017-11-23 16:56:14 +01:00
def impl_set_group_type(self,
2017-11-29 23:25:34 +01:00
group_type):
"""sets a given group object to an OptionDescription
:param group_type: an instance of `GroupType` or `MasterGroupType`
that lives in `setting.groups`
"""
2018-04-12 23:04:33 +02:00
if self._group_type != groups.default:
raise TypeError(_('cannot change group_type if already set '
2017-11-29 23:25:34 +01:00
'(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))
2017-11-29 23:25:34 +01:00
if isinstance(group_type, groups.MasterGroupType):
raise ConfigError('please use MasterSlaves object instead of OptionDescription')
self._group_type = group_type
2017-07-22 16:26:06 +02:00
def impl_get_group_type(self):
return self._group_type
2017-11-13 22:45:53 +01:00
def impl_validate_value(self,
2017-12-19 23:11:45 +01:00
*args,
**kwargs):
2017-11-12 14:33:05 +01:00
pass