tiramisu/tiramisu/option/option.py

715 lines
31 KiB
Python
Raw Normal View History

# -*- coding: utf-8 -*-
"option types and option description"
2017-07-08 15:59:56 +02:00
# Copyright (C) 2012-2017 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
import sys
2017-07-24 20:39:01 +02:00
from .baseoption import OnlyOption, submulti, DynSymLinkOption, validate_callback, STATIC_TUPLE
from ..i18n import _
from ..setting import log, undefined, debug
from ..autolib import carry_out_calculation
from ..error import (ConfigError, ValueWarning, PropertiesOptionError,
display_list)
2017-09-17 15:55:32 +02:00
from itertools import combinations
2017-07-24 20:39:01 +02:00
ALLOWED_CONST_LIST = ['_cons_not_equal']
if sys.version_info[0] >= 3: # pragma: no cover
xrange = range
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-10-22 09:48:08 +02:00
def __init__(self, name, doc, default=undefined, default_multi=None,
2017-07-24 20:39:01 +02:00
requires=None, multi=False, unique=undefined, callback=None,
callback_params=None, validator=None, validator_params=None,
properties=None, warnings_only=False, extra=None,
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:
raise ValueError(_('invalid multi value'))
if _multi != 1:
_setattr(self, '_multi', _multi)
if multi is not False and default is None:
default = []
if validator is not None:
if multi: # and validator_params is None:
validator_params = self._build_validator_params(validator, validator_params)
validate_callback(validator, validator_params, 'validator', self)
if validator_params is None:
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):
raise ValueError(_('unique must be a boolean'))
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)
super(Option, self).__init__(name, doc, requires=requires,
properties=properties, is_multi=is_multi)
if is_multi and default_multi is not None:
2017-11-12 14:33:05 +01:00
def test_multi_value(value):
err = self._validate(value)
if err:
raise ValueError(_("invalid default_multi value {0} "
"for option {1}: {2}").format(
str(value),
self.impl_getname(), str(err)))
if _multi is submulti:
if not isinstance(default_multi, list):
raise ValueError(_("invalid default_multi value {0} "
"for option {1}: must be a list for a submulti").format(
str(default_multi),
self.impl_getname()))
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)
err = self.impl_validate(default, is_multi=is_multi)
if err:
raise err
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, _init=True)
def impl_is_multi(self):
return getattr(self, '_multi', 1) != 1
def _launch_consistency(self, current_opt, func, option, value, context,
index, submulti_index, opts, warnings_only,
transitive):
"""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 option
: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()
all_cons_vals = []
all_cons_opts = []
val_consistencies = True
for opt in opts:
if (isinstance(opt, DynSymLinkOption) and option._dyn == opt._dyn) or \
option == opt:
# option is current option
# we have already value, so use it
all_cons_vals.append(value)
all_cons_opts.append(opt)
else:
#if context, calculate value, otherwise get default value
path = None
is_multi = opt.impl_is_multi() and not opt.impl_is_master_slaves()
if context is not undefined:
if isinstance(opt, DynSymLinkOption):
path = opt.impl_getpath(context)
else:
path = descr.impl_get_path_by_opt(opt)
if is_multi:
_index = None
else:
_index = index
2017-11-13 22:45:53 +01:00
try:
opt_value = context.getattr(path, validate=False,
index=_index,
force_permissive=True)
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
else:
opt_value = None
else: # pragma: no cover
return opt_value
2017-07-24 20:39:01 +02:00
elif index is None:
opt_value = opt.impl_getdefault()
else:
opt_value = opt.impl_getdefault()[index]
if self.impl_is_multi() and index is None:
# only check propertyerror for master/slaves is transitive
val_consistencies = False
if is_multi and isinstance(opt_value, list):
all_cons_vals.extend(opt_value)
for len_ in xrange(len(opt_value)):
all_cons_opts.append(opt)
else:
all_cons_vals.append(opt_value)
all_cons_opts.append(opt)
if val_consistencies:
err = getattr(self, func)(current_opt, all_cons_opts, all_cons_vals, warnings_only)
if 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)
warnings.warn_explicit(ValueWarning(msg, self),
ValueWarning,
self.__class__.__name__, 0)
else:
return err
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,
context=undefined,
validate=True,
force_index=None,
force_submulti_index=None,
current_opt=undefined,
is_multi=None,
display_error=True,
display_warnings=True,
multi=None,
2017-07-24 20:39:01 +02:00
setting_properties=undefined):
"""
:param value: the option's value
:param context: Config's context
:type context: :class:`tiramisu.config.Config`
:param validate: if true enables ``self._validator`` validation
: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
:param force_submulti_index: if submulti, value has to be a list
not if force_submulti_index is not None
:type force_submulti_index: integer
"""
if not validate:
return
if current_opt is undefined:
current_opt = self
if display_warnings and setting_properties is undefined and context is not undefined:
setting_properties = context.cfgimpl_get_settings()._getproperties(read_write=False)
display_warnings = display_warnings and (setting_properties is undefined or 'warnings' in setting_properties)
def _is_not_unique(value):
if display_error and self.impl_is_unique() and len(set(value)) != len(value):
for idx, val in enumerate(value):
if val in value[idx+1:]:
return ValueError(_('invalid value "{}", this value is already in "{}"').format(
val, self.impl_get_display_name()))
def calculation_validator(val, _index):
validator, validator_params = self.impl_get_validator()
if validator is not None:
if validator_params != {}:
validator_params_ = {}
for val_param, values in validator_params.items():
validator_params_[val_param] = values
#inject value in calculation
if '' in validator_params_:
lst = list(validator_params_[''])
lst.insert(0, val)
validator_params_[''] = tuple(lst)
else:
validator_params_[''] = (val,)
else:
validator_params_ = {'': (val,)}
# Raise ValueError if not valid
2017-11-12 14:33:05 +01:00
value = carry_out_calculation(current_opt,
context=context,
2017-07-24 20:39:01 +02:00
callback=validator,
callback_params=validator_params_,
index=_index,
is_validator=True)
if isinstance(value, Exception):
return value
def do_validation(_value, _index, submulti_index):
if _value is None:
error = warning = None
else:
if display_error:
# option validation
2017-11-12 14:33:05 +01:00
err = self._validate(_value,
context,
current_opt)
2017-07-24 20:39:01 +02:00
if err:
if debug: # pragma: no cover
log.debug('do_validation: value: {0}, index: {1}, '
2017-11-12 14:33:05 +01:00
'submulti_index: {2}'.format(_value,
_index,
2017-07-24 20:39:01 +02:00
submulti_index),
exc_info=True)
err_msg = '{0}'.format(err)
if err_msg:
msg = _('"{0}" is an invalid {1} for "{2}", {3}'
'').format(_value, self._display_name,
self.impl_get_display_name(), err_msg)
else:
msg = _('"{0}" is an invalid {1} for "{2}"'
'').format(_value, self._display_name,
self.impl_get_display_name())
return ValueError(msg)
error = None
is_warnings_only = getattr(self, '_warnings_only', False)
if ((display_error and not is_warnings_only) or
(display_warnings and is_warnings_only)):
error = calculation_validator(_value, _index)
if not error:
error = self._second_level_validation(_value, is_warnings_only)
if error:
if debug: # pragma: no cover
log.debug(_('do_validation for {0}: error in value').format(
self.impl_getname()), exc_info=True)
if is_warnings_only:
msg = _('attention, "{0}" could be an invalid {1} for "{2}", {3}').format(
_value, self._display_name, self.impl_get_display_name(), error)
warnings.warn_explicit(ValueWarning(msg, self),
ValueWarning,
self.__class__.__name__, 0)
error = None
if error is None:
# if context launch consistency validation
#if context is not undefined:
2017-11-12 14:33:05 +01:00
ret = self._valid_consistency(current_opt,
_value,
context,
_index,
submulti_index,
display_warnings,
2017-07-24 20:39:01 +02:00
display_error)
if isinstance(ret, ValueError):
error = ret
elif ret:
return ret
if error:
err_msg = '{0}'.format(error)
if err_msg:
msg = _('"{0}" is an invalid {1} for "{2}", {3}'
'').format(_value, self._display_name,
self.impl_get_display_name(), err_msg)
else:
msg = _('"{0}" is an invalid {1} for "{2}"'
'').format(_value, self._display_name,
self.impl_get_display_name())
return ValueError(msg)
if is_multi is None:
is_multi = self.impl_is_multi()
if not is_multi:
return do_validation(value, None, None)
elif force_index is not None:
if self.impl_is_submulti() and force_submulti_index is None:
err = _is_not_unique(value)
if err:
return err
if not isinstance(value, list):
return ValueError(_('invalid value "{0}" for "{1}" which'
' must be a list').format(
value, self.impl_get_display_name()))
for idx, val in enumerate(value):
if isinstance(val, list): # pragma: no cover
return ValueError(_('invalid value "{}" for "{}" '
'which must not be a list').format(val,
self.impl_get_display_name()))
err = do_validation(val, force_index, idx)
if err:
return err
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:
return ValueError(_('invalid value "{}", this value is already'
' in "{}"').format(value,
self.impl_get_display_name()))
return do_validation(value, force_index, force_submulti_index)
elif not isinstance(value, list):
return ValueError(_('invalid value "{0}" for "{1}" which '
'must be a list').format(value,
self.impl_getname()))
elif self.impl_is_submulti() and force_submulti_index is None:
for idx, val in enumerate(value):
err = _is_not_unique(val)
if err:
return err
if not isinstance(val, list):
return ValueError(_('invalid value "{0}" for "{1}" '
'which must be a list of list'
'').format(val,
self.impl_getname()))
for slave_idx, slave_val in enumerate(val):
err = do_validation(slave_val, idx, slave_idx)
if err:
return err
else:
err = _is_not_unique(value)
if err:
return err
for idx, val in enumerate(value):
err = do_validation(val, idx, force_submulti_index)
if err:
return err
2017-11-12 14:33:05 +01:00
return self._valid_consistency(current_opt,
None,
context,
None,
None,
display_warnings,
display_error)
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')
def _valid_consistencies(self, other_opts, init=True, func=None):
if self._is_subdyn():
2017-11-13 22:45:53 +01:00
dynod = self._subdyn()
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()
for opt in other_opts:
if opt.impl_is_submulti():
raise ConfigError(_('cannot add consistency with submulti option'))
if not isinstance(opt, Option):
raise ConfigError(_('consistency must be set with an option'))
if opt._is_subdyn():
if dynod is None:
raise ConfigError(_('almost one option in consistency is '
'in a dynoptiondescription but not all'))
2017-11-13 22:45:53 +01:00
subod = opt._subdyn()
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
def impl_add_consistency(self, func, *other_opts, **params):
"""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)
"""
if self.impl_is_readonly():
raise AttributeError(_("'{0}' ({1}) cannot add consistency, option is"
" read-only").format(
self.__class__.__name__,
self.impl_getname()))
self._valid_consistencies(other_opts, func=func)
func = '_cons_{0}'.format(func)
if func not in dir(self):
raise ConfigError(_('consistency {0} not available for this option').format(func))
all_cons_opts = tuple([self] + list(other_opts))
unknown_params = set(params.keys()) - set(['warnings_only', 'transitive'])
if unknown_params != set():
raise ValueError(_('unknown parameter {0} in consistency').format(unknown_params))
2017-07-24 20:39:01 +02:00
self._add_consistency(func, all_cons_opts, params)
#validate default value when add consistency
err = self.impl_validate(self.impl_getdefault())
if err:
self._del_consistency()
raise err
if func in ALLOWED_CONST_LIST:
for opt in all_cons_opts:
if getattr(opt, '_unique', undefined) == undefined:
opt._unique = True
if func != '_cons_not_equal':
#consistency could generate warnings or errors
2017-09-17 15:55:32 +02:00
self._has_dependency = True
for opt in all_cons_opts:
if opt != self:
self._add_dependency(opt)
opt._add_dependency(self)
2017-07-24 20:39:01 +02:00
def _valid_consistency(self, option, value, context, index, submulti_idx,
display_warnings, display_error):
if context is not undefined:
descr = context.cfgimpl_get_description()
if descr._cache_consistencies is None:
return
#consistencies is something like [('_cons_not_equal', (opt1, opt2))]
if isinstance(option, DynSymLinkOption):
consistencies = descr._cache_consistencies.get(option._impl_getopt())
else:
consistencies = descr._cache_consistencies.get(option)
else:
consistencies = option._get_consistencies()
if consistencies is not None:
for func, all_cons_opts, params in consistencies:
warnings_only = params.get('warnings_only', False)
if (warnings_only and display_warnings) or (not warnings_only and display_error):
transitive = params.get('transitive', True)
#all_cons_opts[0] is the option where func is set
if isinstance(option, DynSymLinkOption):
subpath = '.'.join(option._dyn.split('.')[:-1])
namelen = len(option._impl_getopt().impl_getname())
suffix = option.impl_getname()[namelen:]
opts = []
for opt in all_cons_opts:
name = opt.impl_getname() + suffix
path = subpath + '.' + name
opts.append(opt._impl_to_dyn(name, path))
else:
opts = all_cons_opts
err = opts[0]._launch_consistency(self, func, option, value,
context, index, submulti_idx,
opts, warnings_only,
transitive)
if err:
return err
def _cons_not_equal(self, current_opt, opts, vals, warnings_only):
equal = set()
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:
equal.add(opt_)
if equal:
if debug: # pragma: no cover
log.debug(_('_cons_not_equal: {} are not different').format(display_list(list(equal))))
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())
return ValueError(msg.format(display_list(list(equal_name))))
def _second_level_validation(self, value, warnings_only):
pass
def _impl_to_dyn(self, name, path):
return DynSymLinkOption(name, self, dyn=path)
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
def _validate_callback(self, callback, callback_params):
"""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)):
raise ValueError(_("default value not allowed if option: {0} "
"is calculated").format(self.impl_getname()))
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
def _get_extra(self, key):
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
def _add_consistency(self, func, all_cons_opts, params):
cons = (func, all_cons_opts, params)
consistencies = getattr(self, '_consistencies', None)
if consistencies is None:
self._consistencies = [cons]
else:
consistencies.append(cons)
def _del_consistency(self):
self._consistencies.pop(-1)
def _get_consistencies(self):
return getattr(self, '_consistencies', STATIC_TUPLE)
def _has_consistencies(self):
return hasattr(self, '_consistencies')
class RegexpOption(Option):
__slots__ = tuple()
2017-07-24 19:04:18 +02:00
2016-10-14 22:20:14 +02:00
def _validate(self, value, context=undefined, current_opt=undefined):
err = self._impl_valid_unicode(value)
if err:
return err
match = self._regexp.search(value)
if not match:
return ValueError()
#FIXME compatibility
_RegexpOption = RegexpOption