tiramisu/tiramisu/option/option.py

780 lines
32 KiB
Python
Raw Normal View History

# -*- coding: utf-8 -*-
"option types and option description"
2018-01-26 07:33:47 +01:00
# Copyright (C) 2012-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
# ____________________________________________________________
2017-07-24 20:39:01 +02:00
import warnings
2017-11-20 17:01:36 +01:00
import weakref
2018-04-16 19:51:13 +02:00
from .baseoption import OnlyOption, submulti, STATIC_TUPLE
2017-12-07 22:20:19 +01:00
from .symlinkoption import DynSymLinkOption
2017-07-24 20:39:01 +02:00
from ..i18n import _
from ..setting import log, undefined, debug
from ..autolib import carry_out_calculation
from ..error import (ConfigError, ValueWarning, PropertiesOptionError,
display_list)
from ..function import Params, ParamValue
2017-07-24 20:39:01 +02:00
ALLOWED_CONST_LIST = ['_cons_not_equal']
class Option(OnlyOption):
"""
Abstract base class for configuration option's.
Reminder: an Option object is **not** a container for the value.
"""
__slots__ = ('_extra',
'_warnings_only',
'_allow_empty_list',
#multi
'_multi',
'_unique',
#value
'_default',
'_default_multi',
#calcul
'_val_call',
#
'_master_slaves',
'_choice_values',
'_choice_values_params',
)
_empty = ''
2017-12-07 21:42:04 +01:00
def __init__(self,
name,
doc,
default=undefined,
default_multi=None,
requires=None,
multi=False,
unique=undefined,
callback=None,
callback_params=None,
validator=None,
validator_params=None,
properties=None,
warnings_only=False,
extra=None,
2017-07-24 20:39:01 +02:00
allow_empty_list=undefined):
_setattr = object.__setattr__
if not multi and default_multi is not None:
raise ValueError(_("default_multi is set whereas multi is False"
" in option: {0}").format(name))
2017-10-22 09:48:08 +02:00
if default is undefined:
if multi is False:
default = None
else:
default = []
2017-07-24 20:39:01 +02:00
if multi is True:
is_multi = True
_multi = 0
elif multi is False:
is_multi = False
_multi = 1
elif multi is submulti:
is_multi = True
_multi = submulti
else:
2018-04-12 23:04:33 +02:00
raise ValueError(_('invalid multi type "{}"').format(multi))
2017-07-24 20:39:01 +02:00
if _multi != 1:
_setattr(self, '_multi', _multi)
if multi is not False and default is None:
default = []
2018-03-19 08:33:53 +01:00
super().__init__(name,
doc,
requires=requires,
properties=properties,
is_multi=is_multi)
2017-07-24 20:39:01 +02:00
if validator is not None:
2017-12-19 23:11:45 +01:00
validator_params = self._build_calculator_params(validator,
validator_params,
2018-04-16 19:51:13 +02:00
'validator',
2017-12-19 23:11:45 +01:00
add_value=True)
2018-04-06 23:51:25 +02:00
if validator_params == {}:
2017-07-24 20:39:01 +02:00
val_call = (validator,)
else:
val_call = (validator, validator_params)
self._val_call = (val_call, None)
if extra is not None:
_setattr(self, '_extra', extra)
if unique != undefined and not isinstance(unique, bool):
2018-04-12 23:04:33 +02:00
raise ValueError(_('unique must be a boolean, not "{}"').format(unique))
2017-07-24 20:39:01 +02:00
if not is_multi and unique is True:
raise ValueError(_('unique must be set only with multi value'))
if warnings_only is True:
_setattr(self, '_warnings_only', warnings_only)
if allow_empty_list is not undefined:
_setattr(self, '_allow_empty_list', allow_empty_list)
if is_multi and default_multi is not None:
2017-11-12 14:33:05 +01:00
def test_multi_value(value):
2017-12-19 23:11:45 +01:00
try:
self._validate(value,
undefined)
except ValueError as err:
2017-11-12 14:33:05 +01:00
raise ValueError(_("invalid default_multi value {0} "
2017-12-07 21:42:04 +01:00
"for option {1}: {2}").format(str(value),
self.impl_getname(),
str(err)))
2017-11-12 14:33:05 +01:00
if _multi is submulti:
if not isinstance(default_multi, list):
2018-04-12 23:04:33 +02:00
raise ValueError(_('invalid default_multi value "{0}" '
'for option "{1}", must be a list for a submulti'
'').format(str(default_multi),
self.impl_get_display_name()))
2017-11-12 14:33:05 +01:00
for value in default_multi:
test_multi_value(value)
else:
test_multi_value(default_multi)
2017-07-24 20:39:01 +02:00
_setattr(self, '_default_multi', default_multi)
if unique is not undefined:
_setattr(self, '_unique', unique)
2017-12-13 22:15:34 +01:00
self.impl_validate(default,
2017-12-19 23:11:45 +01:00
is_multi=is_multi,
config_bag=undefined)
2017-07-24 20:39:01 +02:00
if (is_multi and default != []) or \
(not is_multi and default is not None):
if is_multi:
default = tuple(default)
_setattr(self, '_default', default)
self._impl_set_callback(callback,
callback_params)
2017-07-24 20:39:01 +02:00
def impl_is_multi(self):
return getattr(self, '_multi', 1) != 1
2017-12-07 21:42:04 +01:00
def _validate(self,
*args,
**kwargs):
pass
2017-07-24 20:39:01 +02:00
def impl_is_unique(self):
return getattr(self, '_unique', False)
def impl_get_validator(self):
val = getattr(self, '_val_call', (None,))[0]
if val is None:
ret_val = (None, {})
elif len(val) == 1:
ret_val = (val[0], {})
else:
ret_val = val
return ret_val
2017-11-12 14:33:05 +01:00
def impl_validate(self,
value,
2017-12-19 23:11:45 +01:00
config_bag,
2017-11-12 14:33:05 +01:00
context=undefined,
force_index=None,
current_opt=undefined,
is_multi=None,
2017-12-13 22:15:34 +01:00
check_error=True,
2017-12-19 23:11:45 +01:00
multi=None):
2017-07-24 20:39:01 +02:00
"""
:param value: the option's value
:param context: Config's context
:type context: :class:`tiramisu.config.Config`
:type validate: boolean
:param force_index: if multi, value has to be a list
not if force_index is not None
:type force_index: integer
"""
if current_opt is undefined:
current_opt = self
2017-12-13 22:15:34 +01:00
2017-12-19 23:11:45 +01:00
if config_bag is not undefined and \
((check_error is True and not 'validator' in config_bag.setting_properties) or \
2017-12-23 10:40:41 +01:00
(check_error is False and not 'warnings' in config_bag.setting_properties)):
2017-12-13 22:15:34 +01:00
return
2017-07-24 20:39:01 +02:00
def _is_not_unique(value):
2017-12-07 21:42:04 +01:00
#FIXME pourquoi la longueur doit etre egal ???
2017-12-13 22:15:34 +01:00
if check_error and self.impl_is_unique() and len(set(value)) != len(value):
2017-07-24 20:39:01 +02:00
for idx, val in enumerate(value):
if val in value[idx+1:]:
2017-11-28 22:42:30 +01:00
raise ValueError(_('invalid value "{}", this value is already in "{}"'
2017-12-07 21:42:04 +01:00
'').format(val,
self.impl_get_display_name()))
2017-07-24 20:39:01 +02:00
2017-11-20 17:01:36 +01:00
def calculation_validator(val,
_index):
2017-07-24 20:39:01 +02:00
validator, validator_params = self.impl_get_validator()
if validator is not None:
#inject value in calculation
if validator_params is None:
args = []
kwargs = None
2017-07-24 20:39:01 +02:00
else:
args = list(validator_params.args)
kwargs = validator_params.kwargs
args.insert(0, ParamValue(val))
validator_params_ = Params(tuple(args), kwargs)
2017-07-24 20:39:01 +02:00
# Raise ValueError if not valid
2017-12-07 21:42:04 +01:00
carry_out_calculation(current_opt,
context=context,
callback=validator,
callback_params=validator_params_,
index=_index,
2017-12-27 15:48:49 +01:00
orig_value=value,
2017-12-19 23:11:45 +01:00
config_bag=config_bag,
2017-12-07 21:42:04 +01:00
is_validator=True)
2017-07-24 20:39:01 +02:00
2017-11-20 17:01:36 +01:00
def do_validation(_value,
_index):
2017-12-07 21:42:04 +01:00
if isinstance(_value, list): # pragma: no cover
2017-12-30 18:31:56 +01:00
raise ValueError(_('which must not be a list').format(_value,
2017-12-07 21:42:04 +01:00
self.impl_get_display_name()))
2017-12-13 22:15:34 +01:00
#FIXME a revoir ...
2017-12-23 10:40:41 +01:00
if _value is not None:
2017-12-13 22:15:34 +01:00
if check_error:
2017-12-23 10:40:41 +01:00
# option validation
if config_bag is undefined:
setting_properties = None
else:
setting_properties = config_bag.setting_properties
self._validate(_value,
config_bag,
current_opt)
if ((check_error and not is_warnings_only) or
(not check_error and is_warnings_only)):
calculation_validator(_value,
_index)
self._second_level_validation(_value,
is_warnings_only)
2017-07-24 20:39:01 +02:00
if is_multi is None:
is_multi = self.impl_is_multi()
2017-12-23 10:40:41 +01:00
is_warnings_only = getattr(self, '_warnings_only', False)
try:
val = value
if not is_multi:
do_validation(val, None)
elif force_index is not None:
if self.impl_is_submulti():
2017-12-30 18:31:56 +01:00
if not isinstance(value, list):
raise ValueError(_('which must be a list'))
2017-12-23 10:40:41 +01:00
_is_not_unique(value)
for idx, val in enumerate(value):
do_validation(val,
force_index)
else:
if multi is not None and self.impl_is_unique() and value in multi:
if not self.impl_is_submulti() and len(multi) - 1 >= force_index:
lst = list(multi)
lst.pop(force_index)
else:
lst = multi
if value in lst:
2017-12-30 18:31:56 +01:00
raise ValueError(_('this value is not uniq'))
2017-12-07 21:42:04 +01:00
do_validation(val,
force_index)
2017-12-23 10:40:41 +01:00
elif not isinstance(value, list):
2017-12-30 18:31:56 +01:00
raise ValueError(_('which must be a list'))
2017-12-23 10:40:41 +01:00
elif self.impl_is_submulti():
for idx, lval in enumerate(value):
_is_not_unique(lval)
if not isinstance(lval, list):
2017-12-30 18:31:56 +01:00
raise ValueError(_('which "{}" must be a list of list'
'').format(lval))
2017-12-23 10:40:41 +01:00
for val in lval:
do_validation(val,
2017-12-19 23:11:45 +01:00
idx)
else:
2017-12-23 10:40:41 +01:00
_is_not_unique(value)
2017-12-19 23:11:45 +01:00
for idx, val in enumerate(value):
do_validation(val,
idx)
2017-12-23 10:40:41 +01:00
self._valid_consistency(current_opt,
value,
context,
force_index,
check_error,
config_bag)
except ValueError as err:
if debug: # pragma: no cover
log.debug('do_validation: value: {0}, index: {1}:'
' {2}'.format(val,
_index,
err),
exc_info=True)
if is_warnings_only:
msg = _('attention, "{0}" could be an invalid {1} for "{2}"'
'').format(val,
self._display_name,
self.impl_get_display_name())
2017-12-19 23:11:45 +01:00
else:
2017-12-23 10:40:41 +01:00
msg = _('"{0}" is an invalid {1} for "{2}"'
'').format(val,
self._display_name,
self.impl_get_display_name())
err_msg = '{0}'.format(err)
if err_msg:
msg += ', {}'.format(err_msg)
if check_error:
raise ValueError(msg)
else:
2018-03-24 22:37:48 +01:00
warnings.warn_explicit(ValueWarning(msg, weakref.ref(self)),
2017-12-23 10:40:41 +01:00
ValueWarning,
self.__class__.__name__, 0)
2018-03-19 08:33:53 +01:00
2017-07-24 20:39:01 +02:00
def impl_is_dynsymlinkoption(self):
return False
def impl_is_master_slaves(self, type_='both'):
"""FIXME
"""
master_slaves = self.impl_get_master_slaves()
if master_slaves is not None:
if type_ in ('both', 'master') and \
master_slaves.is_master(self):
return True
if type_ in ('both', 'slave') and \
not master_slaves.is_master(self):
return True
return False
def impl_get_master_slaves(self):
2017-11-13 22:45:53 +01:00
masterslave = getattr(self, '_master_slaves', None)
if masterslave is None:
return masterslave
return masterslave()
2017-07-24 20:39:01 +02:00
def impl_getdoc(self):
"accesses the Option's doc"
return self.impl_get_information('doc')
2017-11-20 17:01:36 +01:00
def _valid_consistencies(self,
other_opts,
init=True,
func=None):
2018-04-09 21:37:49 +02:00
if self.issubdyn():
dynod = self.getsubdyn()
2017-07-24 20:39:01 +02:00
else:
dynod = None
if self.impl_is_submulti():
raise ConfigError(_('cannot add consistency with submulti option'))
is_multi = self.impl_is_multi()
2017-12-19 23:11:45 +01:00
for opt in other_opts:
if isinstance(opt, weakref.ReferenceType):
opt = opt()
2017-07-24 20:39:01 +02:00
if opt.impl_is_submulti():
raise ConfigError(_('cannot add consistency with submulti option'))
if not isinstance(opt, Option):
2017-12-04 20:05:36 +01:00
raise ConfigError(_('consistency must be set with an option, not {}').format(opt))
2018-04-09 21:37:49 +02:00
if opt.issubdyn():
2017-07-24 20:39:01 +02:00
if dynod is None:
raise ConfigError(_('almost one option in consistency is '
'in a dynoptiondescription but not all'))
2018-04-09 21:37:49 +02:00
subod = opt.getsubdyn()
2017-11-13 22:45:53 +01:00
if dynod != subod:
2017-07-24 20:39:01 +02:00
raise ConfigError(_('option in consistency must be in same'
' dynoptiondescription'))
2017-11-13 22:45:53 +01:00
dynod = subod
2017-07-24 20:39:01 +02:00
elif dynod is not None:
raise ConfigError(_('almost one option in consistency is in a '
'dynoptiondescription but not all'))
if self is opt:
raise ConfigError(_('cannot add consistency with itself'))
if is_multi != opt.impl_is_multi():
raise ConfigError(_('every options in consistency must be '
'multi or none'))
if init:
# FIXME
if func != 'not_equal':
2017-09-17 15:55:32 +02:00
opt._has_dependency = True
2017-07-24 20:39:01 +02:00
2017-11-20 17:01:36 +01:00
def impl_add_consistency(self,
func,
*other_opts,
**params):
2017-07-24 20:39:01 +02:00
"""Add consistency means that value will be validate with other_opts
option's values.
:param func: function's name
:type func: `str`
:param other_opts: options used to validate value
:type other_opts: `list` of `tiramisu.option.Option`
:param params: extra params (warnings_only and transitive are allowed)
"""
2017-11-20 17:01:36 +01:00
if self.impl_is_readonly():
2017-07-24 20:39:01 +02:00
raise AttributeError(_("'{0}' ({1}) cannot add consistency, option is"
" read-only").format(
self.__class__.__name__,
self.impl_getname()))
2017-11-20 17:01:36 +01:00
self._valid_consistencies(other_opts,
func=func)
2017-07-24 20:39:01 +02:00
func = '_cons_{0}'.format(func)
if func not in dir(self):
raise ConfigError(_('consistency {0} not available for this option').format(func))
2017-11-20 17:01:36 +01:00
options = [weakref.ref(self)]
for option in other_opts:
options.append(weakref.ref(option))
all_cons_opts = tuple(options)
2017-07-24 20:39:01 +02:00
unknown_params = set(params.keys()) - set(['warnings_only', 'transitive'])
if unknown_params != set():
raise ValueError(_('unknown parameter {0} in consistency').format(unknown_params))
2017-11-20 17:01:36 +01:00
self._add_consistency(func,
all_cons_opts,
params)
2017-07-24 20:39:01 +02:00
#validate default value when add consistency
2017-12-19 23:11:45 +01:00
#FIXME validation!
self.impl_validate(self.impl_getdefault(),
undefined)
self.impl_validate(self.impl_getdefault(),
undefined,
check_error=False)
2017-12-13 22:15:34 +01:00
#FIXME
#if err:
# self._del_consistency()
2017-07-24 20:39:01 +02:00
if func != '_cons_not_equal':
#consistency could generate warnings or errors
2017-09-17 15:55:32 +02:00
self._has_dependency = True
2017-11-20 17:01:36 +01:00
for wopt in all_cons_opts:
opt = wopt()
if func in ALLOWED_CONST_LIST:
if getattr(opt, '_unique', undefined) == undefined:
opt._unique = True
2017-09-17 15:55:32 +02:00
if opt != self:
self._add_dependency(opt)
opt._add_dependency(self)
2017-07-24 20:39:01 +02:00
2017-11-20 17:01:36 +01:00
def _valid_consistency(self,
option,
value,
context,
index,
2017-12-13 22:15:34 +01:00
check_error,
2017-12-19 23:11:45 +01:00
config_bag):
2017-07-24 20:39:01 +02:00
if context is not undefined:
descr = context.cfgimpl_get_description()
2017-12-23 10:55:06 +01:00
# no consistency found at all
2017-07-24 20:39:01 +02:00
if descr._cache_consistencies is None:
return
2017-12-23 10:55:06 +01:00
# get consistencies for this option
2017-07-24 20:39:01 +02:00
if isinstance(option, DynSymLinkOption):
2017-11-23 16:56:14 +01:00
consistencies = descr._cache_consistencies.get(option.impl_getopt())
2017-07-24 20:39:01 +02:00
else:
consistencies = descr._cache_consistencies.get(option)
else:
2017-12-23 10:55:06 +01:00
# is no context, get consistencies in option
2018-01-05 23:32:00 +01:00
consistencies = option.get_consistencies()
2017-07-24 20:39:01 +02:00
if consistencies is not None:
2017-12-23 10:40:41 +01:00
for cons_id, func, all_cons_opts, params in consistencies:
2017-07-24 20:39:01 +02:00
warnings_only = params.get('warnings_only', False)
2017-12-13 22:15:34 +01:00
if (warnings_only and not check_error) or (not warnings_only and check_error):
2017-07-24 20:39:01 +02:00
transitive = params.get('transitive', True)
#all_cons_opts[0] is the option where func is set
if isinstance(option, DynSymLinkOption):
opts = []
for opt in all_cons_opts:
2017-12-19 23:11:45 +01:00
opts.append(DynSymLinkOption(opt(),
option._rootpath,
option._suffix))
wopt = opts[0]
2017-07-24 20:39:01 +02:00
else:
opts = all_cons_opts
2017-12-19 23:11:45 +01:00
wopt = opts[0]()
wopt._launch_consistency(self,
func,
2017-12-23 10:40:41 +01:00
cons_id,
2017-12-19 23:11:45 +01:00
option,
value,
context,
index,
opts,
warnings_only,
transitive,
config_bag)
2017-07-24 20:39:01 +02:00
2017-12-23 10:55:06 +01:00
def _launch_consistency(self,
current_opt,
func,
cons_id,
option,
value,
context,
index,
opts,
warnings_only,
transitive,
config_bag):
"""Launch consistency now
:param func: function name, this name should start with _cons_
:type func: `str`
:param option: option that value is changing
:type option: `tiramisu.option.Option`
:param value: new value of this opion
:param context: Config's context, if None, check default value instead
:type context: `tiramisu.config.Config`
:param index: only for multi option, consistency should be launch for
specified index
:type index: `int`
:param opts: all options concerne by this consistency
:type opts: `list` of `tiramisu.option.Option`
:param warnings_only: specific raise error for warning
:type warnings_only: `boolean`
:param transitive: propertyerror is transitive
:type transitive: `boolean`
"""
if context is not undefined:
descr = context.cfgimpl_get_description()
if config_bag is not undefined and cons_id in config_bag.fromconsistency:
return
all_cons_vals = []
all_cons_opts = []
length = None
for opt in opts:
if isinstance(opt, weakref.ReferenceType):
opt = opt()
if option == opt:
# option is current option
# we have already value, so use it
opt_value = value
elif context is undefined:
opt_value = opt.impl_getdefault()
else:
#if context, calculate value, otherwise get default value
sconfig_bag = config_bag.copy('nooption')
sconfig_bag.option = opt
sconfig_bag.fromconsistency.append(cons_id)
sconfig_bag.force_permissive = True
path = opt.impl_getpath(context)
if opt.impl_is_master_slaves('slave'):
index_ = index
else:
index_ = None
try:
opt_value = context.getattr(path,
index_,
2018-04-06 23:51:25 +02:00
sconfig_bag)
2017-12-23 10:55:06 +01:00
except PropertiesOptionError as err:
if debug: # pragma: no cover
log.debug('propertyerror in _launch_consistency: {0}'.format(err))
if transitive:
err.set_orig_opt(option)
raise err
opt_value = None
if not option == opt and opt_value is not None and index is not None and \
(context is undefined or \
not opt.impl_is_master_slaves('slave')):
if len(opt_value) <= index:
opt_value = opt.impl_getdefault_multi()
else:
opt_value = opt_value[index]
if opt_value is not None and opt.impl_is_multi() and index is None and func not in ALLOWED_CONST_LIST:
if length is not None and length != len(opt_value):
if context is undefined:
return
raise ValueError(_('unexpected length of "{}" in constency "{}", should be "{}"'
'').format(len(opt_value),
opt.impl_get_display_name(),
length))
else:
length = len(opt_value)
is_multi = True
else:
is_multi = False
if isinstance(opt_value, list) and func in ALLOWED_CONST_LIST:
for value_ in opt_value:
if isinstance(value_, list):
for val in value_:
all_cons_vals.append((False, val))
all_cons_opts.append(opt)
else:
all_cons_vals.append((False, value_))
all_cons_opts.append(opt)
else:
all_cons_vals.append((is_multi, opt_value))
all_cons_opts.append(opt)
else:
try:
all_values = []
if length is None:
all_value = []
for is_multi, values in all_cons_vals:
all_value.append(values)
all_values = [all_value]
else:
for idx in range(length):
all_value = []
for is_multi, values in all_cons_vals:
if not is_multi:
all_value.append(values)
else:
all_value.append(values[idx])
all_values.append(all_value)
for values in all_values:
getattr(self, func)(current_opt,
all_cons_opts,
values,
warnings_only)
except ValueError as err:
if warnings_only:
msg = _('attention, "{0}" could be an invalid {1} for "{2}", {3}'
'').format(value,
self._display_name,
current_opt.impl_get_display_name(),
err)
2018-03-24 22:37:48 +01:00
warnings.warn_explicit(ValueWarning(msg, weakref.ref(self)),
2017-12-23 10:55:06 +01:00
ValueWarning,
self.__class__.__name__, 0)
else:
raise err
2017-11-23 16:56:14 +01:00
def _cons_not_equal(self,
current_opt,
opts,
vals,
warnings_only):
2017-12-23 10:40:41 +01:00
equal = []
2017-07-24 20:39:01 +02:00
is_current = False
for idx_inf, val_inf in enumerate(vals):
for idx_sup, val_sup in enumerate(vals[idx_inf + 1:]):
if val_inf == val_sup is not None:
for opt_ in [opts[idx_inf], opts[idx_inf + idx_sup + 1]]:
if opt_ == current_opt:
is_current = True
else:
2017-12-23 10:40:41 +01:00
if opt_ not in equal:
equal.append(opt_)
2017-07-24 20:39:01 +02:00
if equal:
if debug: # pragma: no cover
2017-12-19 23:11:45 +01:00
log.debug(_('_cons_not_equal: {} are not different').format(display_list(equal)))
2017-07-24 20:39:01 +02:00
if is_current:
if warnings_only:
msg = _('should be different from the value of {}')
else:
msg = _('must be different from the value of {}')
else:
if warnings_only:
msg = _('value for {} should be different')
else:
msg = _('value for {} must be different')
equal_name = []
for opt in equal:
equal_name.append(opt.impl_get_display_name())
2017-11-28 22:42:30 +01:00
raise ValueError(msg.format(display_list(list(equal_name))))
2017-07-24 20:39:01 +02:00
2017-11-23 16:56:14 +01:00
def _second_level_validation(self,
value,
warnings_only):
2017-07-24 20:39:01 +02:00
pass
def impl_getdefault_multi(self):
"accessing the default value for a multi"
2017-11-12 14:33:05 +01:00
if self.impl_is_submulti():
default_value = []
else:
default_value = None
return getattr(self, '_default_multi', default_value)
2017-07-24 20:39:01 +02:00
2017-12-19 23:11:45 +01:00
def _validate_calculator(self,
callback,
callback_params):
2017-07-24 20:39:01 +02:00
"""callback_params:
* None
* {'': ((option, permissive),), 'ip': ((None,), (option, permissive))
"""
if callback is None:
return
2017-11-12 14:33:05 +01:00
default_multi = getattr(self, '_default_multi', None)
2017-07-24 20:39:01 +02:00
is_multi = self.impl_is_multi()
default = self.impl_getdefault()
if (not is_multi and (default is not None or default_multi is not None)) or \
(is_multi and (default != [] or default_multi is not None)):
2017-11-28 22:42:30 +01:00
raise ValueError(_('default value not allowed if option "{0}" '
'is calculated').format(self.impl_getname()))
2017-07-24 20:39:01 +02:00
def impl_getdefault(self):
"accessing the default value"
is_multi = self.impl_is_multi()
default = getattr(self, '_default', undefined)
if default is undefined:
if is_multi:
default = []
else:
default = None
else:
if is_multi:
default = list(default)
return default
2017-11-23 16:56:14 +01:00
def _get_extra(self,
key):
2017-07-24 20:39:01 +02:00
extra = self._extra
if isinstance(extra, tuple):
return extra[1][extra[0].index(key)]
else:
return extra[key]
def impl_is_submulti(self):
return getattr(self, '_multi', 1) == 2
def impl_allow_empty_list(self):
return getattr(self, '_allow_empty_list', undefined)
#____________________________________________________________
# consistency
2017-11-20 17:01:36 +01:00
def _add_consistency(self,
func,
all_cons_opts,
params):
2017-12-23 10:40:41 +01:00
cons = (None, func, all_cons_opts, params)
2017-07-24 20:39:01 +02:00
consistencies = getattr(self, '_consistencies', None)
if consistencies is None:
self._consistencies = [cons]
else:
consistencies.append(cons)
def _del_consistency(self):
self._consistencies.pop(-1)
2018-01-05 23:32:00 +01:00
def get_consistencies(self):
2017-07-24 20:39:01 +02:00
return getattr(self, '_consistencies', STATIC_TUPLE)
2017-12-23 10:40:41 +01:00
def _has_consistencies(self, context):
if context is undefined:
return False
descr = context.cfgimpl_get_description()
if descr._cache_consistencies is None:
return False
return self in descr._cache_consistencies
class RegexpOption(Option):
__slots__ = tuple()
2017-07-24 19:04:18 +02:00
2017-11-23 16:56:14 +01:00
def _validate(self,
value,
2017-12-19 23:11:45 +01:00
*args,
**kwargs):
2017-12-07 22:20:19 +01:00
err = self._impl_valid_string(value)
if err:
return err
match = self._regexp.search(value)
if not match:
2017-11-28 22:42:30 +01:00
raise ValueError()