tiramisu/tiramisu/option/option.py

756 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-11-20 17:01:36 +01:00
import weakref
2017-12-07 22:20:19 +01:00
from .baseoption import OnlyOption, submulti, validate_callback, STATIC_TUPLE
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)
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-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:
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:
2017-12-07 21:42:04 +01:00
validator_params = self._build_validator_params(validator,
validator_params)
2017-07-24 20:39:01 +02:00
2017-12-07 21:42:04 +01:00
validate_callback(validator,
validator_params,
'validator',
self)
2017-07-24 20:39:01 +02:00
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)
2017-12-07 21:42:04 +01:00
super(Option, self).__init__(name,
doc,
requires=requires,
properties=properties,
is_multi=is_multi)
2017-07-24 20:39:01 +02:00
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} "
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):
raise ValueError(_("invalid default_multi value {0} "
2017-12-07 21:42:04 +01:00
"for option {1}: must be a list for a submulti"
"").format(str(default_multi),
self.impl_getname()))
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-07 21:42:04 +01:00
err = self.impl_validate(default,
is_multi=is_multi)
2017-07-24 20:39:01 +02:00
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)
2017-12-07 21:42:04 +01:00
self.impl_set_callback(callback,
callback_params,
_init=True)
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-11-20 17:01:36 +01:00
def _launch_consistency(self,
current_opt,
func,
option,
value,
context,
index,
opts,
warnings_only,
transitive,
setting_properties):
2017-07-24 20:39:01 +02:00
"""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`
2017-11-28 22:42:30 +01:00
:param value: new value of this opion
2017-07-24 20:39:01 +02:00
: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
2017-11-20 17:01:36 +01:00
for wopt in opts:
opt = wopt()
2017-07-24 20:39:01 +02:00
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:
path = None
is_multi = opt.impl_is_multi() and not opt.impl_is_master_slaves()
2017-11-28 22:42:30 +01:00
#if context, calculate value, otherwise get default value
2017-07-24 20:39:01 +02:00
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:
2017-11-20 17:01:36 +01:00
opt_value = context.getattr(path,
setting_properties,
validate=False,
2017-11-13 22:45:53 +01:00
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
2017-11-28 22:42:30 +01:00
#elif index is None:
2017-07-24 20:39:01 +02:00
else:
2017-11-28 22:42:30 +01:00
opt_value = opt.impl_getdefault()
if index is not None:
if len(opt_value) >= index:
opt_value = opt.impl_getdefault_multi()
else:
opt_value = opt_value[index]
2017-07-24 20:39:01 +02:00
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:
2017-11-28 22:42:30 +01:00
try:
getattr(self, func)(current_opt, all_cons_opts, all_cons_vals, warnings_only)
except ValueError as err:
2017-07-24 20:39:01 +02:00
if warnings_only:
2017-11-28 22:42:30 +01:00
msg = _('attention, "{0}" could be an invalid {1} for "{2}", {3}'
'').format(value,
self._display_name,
current_opt.impl_get_display_name(),
err)
2017-07-24 20:39:01 +02:00
warnings.warn_explicit(ValueWarning(msg, self),
ValueWarning,
self.__class__.__name__, 0)
else:
2017-11-28 22:42:30 +01:00
raise err
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,
context=undefined,
force_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`
: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
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):
2017-12-07 21:42:04 +01:00
#FIXME pourquoi la longueur doit etre egal ???
2017-07-24 20:39:01 +02:00
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:]:
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:
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-12-07 21:42:04 +01:00
carry_out_calculation(current_opt,
context=context,
callback=validator,
callback_params=validator_params_,
setting_properties=setting_properties,
index=_index,
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
raise ValueError(_('invalid value "{}" for "{}" '
'which must not be a list').format(_value,
self.impl_get_display_name()))
is_warnings_only = getattr(self, '_warnings_only', False)
try:
if _value is not None:
if display_error:
# option validation
self._validate(_value,
context,
current_opt)
if ((display_error and not is_warnings_only) or
(display_warnings and is_warnings_only)):
calculation_validator(_value,
_index)
self._second_level_validation(_value,
is_warnings_only)
self._valid_consistency(current_opt,
_value,
context,
_index,
display_warnings,
display_error,
setting_properties)
except ValueError as err:
if debug: # pragma: no cover
log.debug('do_validation: value: {0}, index: {1}:'
' {2}'.format(_value,
_index,
err),
exc_info=True)
if is_warnings_only:
msg = _('attention, "{0}" could be an invalid {1} for "{2}"'
'').format(_value,
self._display_name,
self.impl_get_display_name())
2017-07-24 20:39:01 +02:00
else:
msg = _('"{0}" is an invalid {1} for "{2}"'
2017-12-07 21:42:04 +01:00
'').format(_value,
self._display_name,
2017-07-24 20:39:01 +02:00
self.impl_get_display_name())
2017-12-07 21:42:04 +01:00
err_msg = '{0}'.format(err)
if err_msg:
msg += ', {}'.format(err_msg)
if is_warnings_only:
warnings.warn_explicit(ValueWarning(msg, self),
ValueWarning,
self.__class__.__name__, 0)
else:
raise ValueError(msg)
2017-07-24 20:39:01 +02:00
if is_multi is None:
is_multi = self.impl_is_multi()
if not is_multi:
2017-12-07 21:42:04 +01:00
do_validation(value, None)
2017-07-24 20:39:01 +02:00
elif force_index is not None:
2017-11-20 17:01:36 +01:00
if self.impl_is_submulti():
2017-12-07 21:42:04 +01:00
_is_not_unique(value)
2017-07-24 20:39:01 +02:00
for idx, val in enumerate(value):
2017-12-07 21:42:04 +01:00
do_validation(val,
force_index)
2017-07-24 20:39:01 +02:00
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-11-28 22:42:30 +01:00
raise ValueError(_('invalid value "{}", this value is already'
2017-12-07 21:42:04 +01:00
' in "{}"').format(value,
self.impl_get_display_name()))
do_validation(value,
force_index)
2017-07-24 20:39:01 +02:00
elif not isinstance(value, list):
2017-11-28 22:42:30 +01:00
raise ValueError(_('invalid value "{0}" for "{1}" which '
2017-12-07 21:42:04 +01:00
'must be a list').format(value,
self.impl_getname()))
2017-11-20 17:01:36 +01:00
elif self.impl_is_submulti():
2017-07-24 20:39:01 +02:00
for idx, val in enumerate(value):
2017-12-07 21:42:04 +01:00
_is_not_unique(val)
2017-07-24 20:39:01 +02:00
if not isinstance(val, list):
2017-11-28 22:42:30 +01:00
raise ValueError(_('invalid value "{0}" for "{1}" '
2017-12-07 21:42:04 +01:00
'which must be a list of list'
'').format(val,
self.impl_getname()))
2017-11-20 17:01:36 +01:00
for slave_val in val:
2017-12-07 21:42:04 +01:00
do_validation(slave_val,
idx)
2017-07-24 20:39:01 +02:00
else:
2017-12-07 21:42:04 +01:00
_is_not_unique(value)
2017-07-24 20:39:01 +02:00
for idx, val in enumerate(value):
2017-12-07 21:42:04 +01:00
do_validation(val,
idx)
#self._valid_consistency(current_opt,
# None,
# context,
# None,
# display_warnings,
# display_error,
# setting_properties)
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):
2017-07-24 20:39:01 +02:00
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()
2017-11-20 17:01:36 +01:00
for wopt in other_opts:
if isinstance(wopt, weakref.ReferenceType):
opt = wopt()
else:
opt = wopt
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))
2017-07-24 20:39:01 +02:00
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
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
err = self.impl_validate(self.impl_getdefault())
if err:
self._del_consistency()
raise err
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,
display_warnings,
display_error,
setting_properties):
2017-07-24 20:39:01 +02:00
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):
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:
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])
2017-11-23 16:56:14 +01:00
namelen = len(option.impl_getopt().impl_getname())
2017-07-24 20:39:01 +02:00
suffix = option.impl_getname()[namelen:]
opts = []
for opt in all_cons_opts:
2017-12-02 22:53:57 +01:00
opts.append(DynSymLinkOption(opt,
subpath,
suffix))
2017-07-24 20:39:01 +02:00
else:
opts = all_cons_opts
2017-12-07 21:42:04 +01:00
opts[0]()._launch_consistency(self,
func,
option,
value,
context,
index,
opts,
warnings_only,
transitive,
setting_properties)
2017-07-24 20:39:01 +02:00
2017-11-23 16:56:14 +01:00
def _cons_not_equal(self,
current_opt,
opts,
vals,
warnings_only):
2017-07-24 20:39:01 +02:00
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())
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-11-23 16:56:14 +01:00
def _validate_callback(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-07-24 20:39:01 +02:00
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
2017-11-23 16:56:14 +01:00
def _validate(self,
value,
context=undefined,
current_opt=undefined):
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()