518 lines
23 KiB
Python
518 lines
23 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, groups, undefined, owners
|
|
from .baseoption import BaseOption
|
|
from .option import ALLOWED_CONST_LIST, DynSymLinkOption
|
|
from .syndynoptiondescription import SynDynOptionDescription
|
|
from ..error import ConfigError, ConflictError
|
|
|
|
|
|
class CacheOptionDescription(BaseOption):
|
|
__slots__ = ('_cache_paths', '_cache_consistencies', '_cache_force_store_values')
|
|
|
|
def _build_cache(self,
|
|
config,
|
|
path='',
|
|
_consistencies=None,
|
|
_consistencies_id=0,
|
|
cache_option=None,
|
|
force_store_values=None,
|
|
_dependencies=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 = []
|
|
_dependencies = []
|
|
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,
|
|
_dependencies)
|
|
else:
|
|
option._set_readonly()
|
|
is_multi = option.impl_is_multi()
|
|
if not option.impl_is_symlinkoption():
|
|
if 'force_store_value' in option.impl_getproperties():
|
|
force_store_values.append((subpath, option))
|
|
if 'force_default_on_freeze' in option.impl_getproperties() and \
|
|
option.impl_is_master_slaves('master'):
|
|
raise ConfigError(_('a master ({0}) cannot have '
|
|
'force_default_on_freeze property').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 master/slaves').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 master/slaves for "{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 master/slaves
|
|
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 master/slaves 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: # pragma: optional cover
|
|
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
|
|
if _dependencies:
|
|
self._dependencies = tuple(_dependencies)
|
|
self._set_readonly()
|
|
|
|
def impl_already_build_caches(self):
|
|
if hasattr(self, '_cache_paths'):
|
|
return True
|
|
return False
|
|
|
|
def impl_build_force_store_values(self,
|
|
context,
|
|
force_store_values):
|
|
value_setted = False
|
|
values = context.cfgimpl_get_values()
|
|
for subpath, option in self._cache_force_store_values:
|
|
if option.impl_is_master_slaves('slave'):
|
|
# problem with index
|
|
raise ConfigError(_('a slave ({0}) cannot have '
|
|
'force_store_value property').format(subpath))
|
|
if option._is_subdyn():
|
|
raise ConfigError(_('a dynoption ({0}) cannot have '
|
|
'force_store_value property').format(subpath))
|
|
if force_store_values is False:
|
|
raise Exception('ok ca existe ...')
|
|
if force_store_values and not values._p_.hasvalue(subpath):
|
|
config_bag = ConfigBag(config=context, option=option)
|
|
value = values.getvalue(subpath,
|
|
None,
|
|
config_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,
|
|
cache_path=None,
|
|
cache_option=None):
|
|
|
|
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 = []
|
|
for option in self.impl_getchildren(config_bag=undefined,
|
|
dyn=False):
|
|
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)
|
|
option._build_cache_option(_currpath,
|
|
cache_path,
|
|
cache_option)
|
|
_currpath.pop()
|
|
if save:
|
|
self._cache_paths = (tuple(cache_option), tuple(cache_path))
|
|
|
|
|
|
class OptionDescriptionWalk(CacheOptionDescription):
|
|
__slots__ = ('_children',)
|
|
|
|
def impl_get_options_paths(self,
|
|
bytype,
|
|
byname,
|
|
_subpath,
|
|
only_first,
|
|
config_bag):
|
|
find_results = []
|
|
|
|
def _rebuild_dynpath(path,
|
|
suffix,
|
|
dynopt):
|
|
found = False
|
|
spath = path.split('.')
|
|
for length in range(1, len(spath)):
|
|
subpath = '.'.join(spath[0:length])
|
|
subopt = self.impl_get_opt_by_path(subpath)
|
|
if dynopt == subopt:
|
|
found = True
|
|
break
|
|
if not found: # pragma: no cover
|
|
raise ConfigError(_('cannot find dynpath'))
|
|
subpath = subpath + suffix
|
|
for slength in range(length, len(spath)):
|
|
subpath = subpath + '.' + spath[slength] + suffix
|
|
return subpath
|
|
|
|
def _filter_by_name(path,
|
|
option):
|
|
name = option.impl_getname()
|
|
if option._is_subdyn():
|
|
found = False
|
|
if byname.startswith(name):
|
|
subdyn = option._subdyn()
|
|
for suffix in subdyn._impl_get_suffixes(config_bag):
|
|
if byname == name + suffix:
|
|
found = True
|
|
path = _rebuild_dynpath(path,
|
|
suffix,
|
|
subdyn)
|
|
if '.' in path:
|
|
subpath = path.rsplit('.', 1)[0]
|
|
else:
|
|
subpath = ''
|
|
option = DynSymLinkOption(option,
|
|
subpath,
|
|
suffix)
|
|
break
|
|
if not found:
|
|
return False
|
|
else:
|
|
if not byname == name:
|
|
return False
|
|
find_results.append((path, option))
|
|
return True
|
|
|
|
def _filter_by_type(path,
|
|
option):
|
|
if isinstance(option,
|
|
bytype):
|
|
#if byname is not None, check option byname in _filter_by_name
|
|
#not here
|
|
if byname is None:
|
|
if option._is_subdyn():
|
|
name = option.impl_getname()
|
|
for suffix in option._subdyn._impl_get_suffixes(config_bag):
|
|
path = _rebuild_dynpath(path,
|
|
suffix,
|
|
option._subdyn)
|
|
if '.' in path:
|
|
subpath = path.rsplit('.', 1)[0]
|
|
else:
|
|
subpath = ''
|
|
doption = DynSymLinkOption(option,
|
|
subpath,
|
|
suffix)
|
|
find_results.append((subpath, doption))
|
|
else:
|
|
find_results.append((path, option))
|
|
return True
|
|
return False
|
|
|
|
def _filter(path, option):
|
|
if bytype is not None:
|
|
retval = _filter_by_type(path, option)
|
|
if byname is None:
|
|
return retval
|
|
if byname is not None:
|
|
return _filter_by_name(path, option)
|
|
|
|
opts, paths = self._cache_paths
|
|
for index, path in enumerate(paths):
|
|
option = opts[index]
|
|
if option.impl_is_optiondescription():
|
|
continue
|
|
if _subpath is not None and not path.startswith(_subpath + '.'):
|
|
continue
|
|
if bytype == byname is None:
|
|
if option._is_subdyn():
|
|
name = option.impl_getname()
|
|
for suffix in option._subdyn._impl_get_suffixes(config_bag):
|
|
path = _rebuild_dynpath(path,
|
|
suffix,
|
|
option._subdyn)
|
|
if '.' in path:
|
|
subpath = path.rsplit('.', 1)[0]
|
|
else:
|
|
subpath = ''
|
|
doption = DynSymLinkOption(option,
|
|
subpath,
|
|
suffix)
|
|
find_results.append((path, doption))
|
|
else:
|
|
find_results.append((path, option))
|
|
else:
|
|
if _filter(path, option) is False:
|
|
continue
|
|
if only_first:
|
|
return find_results
|
|
return find_results
|
|
|
|
def impl_getchild(self,
|
|
name,
|
|
config_bag,
|
|
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:
|
|
child = self._impl_search_dynchild(name,
|
|
subconfig,
|
|
config_bag)
|
|
if child:
|
|
return child
|
|
raise AttributeError(_('unknown Option {0} '
|
|
'in OptionDescription {1}'
|
|
'').format(name, self.impl_getname()))
|
|
|
|
def impl_get_opt_by_path(self,
|
|
path):
|
|
if getattr(self, '_cache_paths', None) is None:
|
|
raise ConfigError(_('use impl_get_opt_by_path only with root OptionDescription'))
|
|
if path not in self._cache_paths[1]:
|
|
raise AttributeError(_('no option for path "{}"').format(path))
|
|
return self._cache_paths[0][self._cache_paths[1].index(path)]
|
|
|
|
def impl_get_path_by_opt(self,
|
|
opt):
|
|
if getattr(self, '_cache_paths', None) is None:
|
|
raise ConfigError(_('use impl_get_path_by_opt only with root OptionDescription'))
|
|
if opt not in self._cache_paths[0]:
|
|
raise AttributeError(_('no option "{}" found').format(opt))
|
|
return self._cache_paths[1][self._cache_paths[0].index(opt)]
|
|
|
|
def impl_getchildren(self,
|
|
config_bag,
|
|
dyn=True):
|
|
for child in self._impl_st_getchildren():
|
|
cname = child.impl_getname()
|
|
if dyn and child.impl_is_dynoptiondescription():
|
|
sconfig_bag = config_bag.copy('nooption')
|
|
sconfig_bag.option = child
|
|
for value in child._impl_get_suffixes(sconfig_bag):
|
|
yield SynDynOptionDescription(child,
|
|
cname + value,
|
|
value)
|
|
else:
|
|
yield child
|
|
|
|
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
|
|
|
|
def _impl_search_dynchild(self,
|
|
name,
|
|
subconfig,
|
|
config_bag):
|
|
for child in self._impl_st_getchildren(only_dyn=True):
|
|
sconfig_bag = config_bag.copy('nooption')
|
|
sconfig_bag.option = child
|
|
cname = child.impl_getname()
|
|
if name.startswith(cname):
|
|
for value in child._impl_get_suffixes(sconfig_bag):
|
|
if name == cname + value:
|
|
return SynDynOptionDescription(child,
|
|
subconfig.cfgimpl_get_path(),
|
|
value)
|
|
|
|
def _impl_get_dynchild(self,
|
|
child,
|
|
suffix,
|
|
subpath):
|
|
if isinstance(child, OptionDescription):
|
|
return SynDynOptionDescription(child,
|
|
subpath,
|
|
suffix)
|
|
else:
|
|
return DynSymLinkOption(child,
|
|
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)
|
|
|
|
#better performance like this
|
|
valid_child = copy(child_names)
|
|
valid_child.sort()
|
|
old = None
|
|
for child in valid_child:
|
|
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(_('the option\'s name "{}" start as '
|
|
'the dynoptiondescription\'s name "{}"').format(child, dynopt))
|
|
old = child
|
|
self._children = (tuple(child_names), tuple(children))
|
|
self._cache_consistencies = None
|
|
# the group_type is useful for filtering OptionDescriptions in a config
|
|
self._group_type = groups.default
|
|
|
|
def impl_is_master_slaves(self):
|
|
return False
|
|
|
|
def impl_getdoc(self):
|
|
return self.impl_get_information('doc')
|
|
|
|
def impl_validate(self,
|
|
*args,
|
|
**kwargs):
|
|
"""usefull for OptionDescription"""
|
|
pass
|
|
|
|
# ____________________________________________________________
|
|
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: # pragma: optional cover
|
|
raise TypeError(_('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
|