reorganise consistencies
This commit is contained in:
@ -454,10 +454,9 @@ class BaseOption(Base):
|
||||
elif name != '_readonly':
|
||||
is_readonly = self.impl_is_readonly()
|
||||
if is_readonly:
|
||||
raise AttributeError(_("'{0}' ({1}) object attribute '{2}' is"
|
||||
" read-only").format(self.__class__.__name__,
|
||||
self,
|
||||
#self.impl_getname(),
|
||||
raise AttributeError(_('"{}" ({}) object attribute "{}" is'
|
||||
' read-only').format(self.__class__.__name__,
|
||||
self.impl_get_display_name(),
|
||||
name))
|
||||
super(BaseOption, self).__setattr__(name, value)
|
||||
|
||||
|
@ -51,7 +51,7 @@ class BroadcastOption(Option):
|
||||
vals,
|
||||
warnings_only):
|
||||
if len(vals) != 3:
|
||||
return ConfigError(_('invalid len for vals'))
|
||||
raise ConfigError(_('invalid len for vals'))
|
||||
if None in vals:
|
||||
return
|
||||
broadcast, network, netmask = vals
|
||||
|
@ -103,7 +103,7 @@ class IPOption(Option):
|
||||
vals,
|
||||
warnings_only):
|
||||
if len(vals) != 3:
|
||||
return ConfigError(_('invalid len for vals'))
|
||||
raise ConfigError(_('invalid len for vals'))
|
||||
if None in vals:
|
||||
return
|
||||
ip, network, netmask = vals
|
||||
|
@ -81,7 +81,7 @@ class NetmaskOption(Option):
|
||||
make_net,
|
||||
warnings_only):
|
||||
if len(opts) != 2:
|
||||
return ConfigError(_('invalid len for opts'))
|
||||
raise ConfigError(_('invalid len for opts'))
|
||||
msg = None
|
||||
try:
|
||||
ip = IP('{0}/{1}'.format(val_ipnetwork, val_netmask), make_net=make_net)
|
||||
@ -98,4 +98,4 @@ class NetmaskOption(Option):
|
||||
msg = _('with netmask "{0}" ("{1}")')
|
||||
if msg is not None:
|
||||
raise ValueError(msg.format(val_netmask,
|
||||
opts[1].impl_get_diplay_name()))
|
||||
opts[1].impl_get_display_name()))
|
||||
|
@ -20,7 +20,6 @@
|
||||
# the whole pypy projet is under MIT licence
|
||||
# ____________________________________________________________
|
||||
import warnings
|
||||
import sys
|
||||
import weakref
|
||||
|
||||
from .baseoption import OnlyOption, submulti, validate_calculator, STATIC_TUPLE
|
||||
@ -33,9 +32,6 @@ from ..error import (ConfigError, ValueWarning, PropertiesOptionError,
|
||||
from itertools import combinations
|
||||
ALLOWED_CONST_LIST = ['_cons_not_equal']
|
||||
|
||||
if sys.version_info[0] >= 3: # pragma: no cover
|
||||
xrange = range
|
||||
|
||||
|
||||
class Option(OnlyOption):
|
||||
"""
|
||||
@ -177,6 +173,7 @@ class Option(OnlyOption):
|
||||
def _launch_consistency(self,
|
||||
current_opt,
|
||||
func,
|
||||
cons_id,
|
||||
option,
|
||||
value,
|
||||
context,
|
||||
@ -206,73 +203,96 @@ class Option(OnlyOption):
|
||||
"""
|
||||
if context is not undefined:
|
||||
descr = context.cfgimpl_get_description()
|
||||
if config_bag is not undefined and config_bag.fromconsistency == option:
|
||||
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 config_bag is not undefined:
|
||||
if config_bag.fromconsistency == opt:
|
||||
break
|
||||
sconfig_bag = config_bag.copy('nooption')
|
||||
sconfig_bag.option = opt
|
||||
sconfig_bag.fromconsistency = option
|
||||
else:
|
||||
sconfig_bag = undefined
|
||||
if option == opt:
|
||||
# option is current option
|
||||
# we have already value, so use it
|
||||
all_cons_vals.append(value)
|
||||
all_cons_opts.append(opt)
|
||||
opt_value = value
|
||||
elif context is undefined:
|
||||
opt_value = opt.impl_getdefault()
|
||||
else:
|
||||
path = None
|
||||
#if context, calculate value, otherwise get default value
|
||||
if context is not undefined:
|
||||
if isinstance(opt, DynSymLinkOption):
|
||||
path = opt.impl_getpath(context)
|
||||
else:
|
||||
#FIXME ca devrait etre impl_getpath ??)
|
||||
path = descr.impl_get_path_by_opt(opt)
|
||||
try:
|
||||
opt_value = context.getattr(path,
|
||||
index,
|
||||
sconfig_bag)
|
||||
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
|
||||
#elif index is None:
|
||||
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:
|
||||
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]
|
||||
index_ = None
|
||||
try:
|
||||
opt_value = context.getattr(path,
|
||||
index_,
|
||||
sconfig_bag,
|
||||
iter_slave=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
|
||||
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]
|
||||
|
||||
is_multi = self.impl_is_multi()
|
||||
if is_multi and index is None:
|
||||
# only check propertyerror for master/slaves is transitive
|
||||
break
|
||||
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)
|
||||
if 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):
|
||||
raise ValueError(_('unexpected length of "{}" in constency "{}", should be "{}"'
|
||||
'').format(len(opt_value),
|
||||
opt.impl_get_display_name(),
|
||||
length))
|
||||
else:
|
||||
all_cons_vals.append(opt_value)
|
||||
all_cons_opts.append(opt)
|
||||
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:
|
||||
getattr(self, func)(current_opt,
|
||||
all_cons_opts,
|
||||
all_cons_vals,
|
||||
warnings_only)
|
||||
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}'
|
||||
@ -322,7 +342,7 @@ class Option(OnlyOption):
|
||||
|
||||
if config_bag is not undefined and \
|
||||
((check_error is True and not 'validator' in config_bag.setting_properties) or \
|
||||
(check_error is False and not 'warnings' in config_bag.setting_properties)):
|
||||
(check_error is False and not 'warnings' in config_bag.setting_properties)):
|
||||
return
|
||||
|
||||
def _is_not_unique(value):
|
||||
@ -367,117 +387,103 @@ class Option(OnlyOption):
|
||||
'which must not be a list').format(_value,
|
||||
self.impl_get_display_name()))
|
||||
#FIXME a revoir ...
|
||||
is_warnings_only = getattr(self, '_warnings_only', False)
|
||||
try:
|
||||
if _value is not None:
|
||||
if check_error:
|
||||
# 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)
|
||||
self._valid_consistency(current_opt,
|
||||
_value,
|
||||
context,
|
||||
_index,
|
||||
check_error,
|
||||
config_bag)
|
||||
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())
|
||||
else:
|
||||
msg = _('"{0}" is an invalid {1} for "{2}"'
|
||||
'').format(_value,
|
||||
self._display_name,
|
||||
self.impl_get_display_name())
|
||||
err_msg = '{0}'.format(err)
|
||||
if err_msg:
|
||||
msg += ', {}'.format(err_msg)
|
||||
if _value is not None:
|
||||
if check_error:
|
||||
raise ValueError(msg)
|
||||
else:
|
||||
warnings.warn_explicit(ValueWarning(msg, self),
|
||||
ValueWarning,
|
||||
self.__class__.__name__, 0)
|
||||
# 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)
|
||||
|
||||
if is_multi is None:
|
||||
is_multi = self.impl_is_multi()
|
||||
|
||||
if not is_multi:
|
||||
do_validation(value, None)
|
||||
elif force_index is not None:
|
||||
if self.impl_is_submulti():
|
||||
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():
|
||||
_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:
|
||||
raise ValueError(_('invalid value "{}", this value is already'
|
||||
' in "{}"').format(value,
|
||||
self.impl_get_display_name()))
|
||||
do_validation(val,
|
||||
force_index)
|
||||
elif not isinstance(value, list):
|
||||
raise ValueError(_('invalid value "{0}" for "{1}" which '
|
||||
'must be a list').format(value,
|
||||
self.impl_getname()))
|
||||
elif self.impl_is_submulti():
|
||||
for idx, lval in enumerate(value):
|
||||
_is_not_unique(lval)
|
||||
if not isinstance(lval, list):
|
||||
raise ValueError(_('invalid value "{0}" for "{1}" '
|
||||
'which must be a list of list'
|
||||
'').format(lval,
|
||||
self.impl_getname()))
|
||||
for val in lval:
|
||||
do_validation(val,
|
||||
idx)
|
||||
else:
|
||||
_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:
|
||||
raise ValueError(_('invalid value "{}", this value is already'
|
||||
' in "{}"').format(value,
|
||||
self.impl_get_display_name()))
|
||||
do_validation(value,
|
||||
force_index)
|
||||
elif not isinstance(value, list):
|
||||
raise ValueError(_('invalid value "{0}" for "{1}" which '
|
||||
'must be a list').format(value,
|
||||
self.impl_getname()))
|
||||
elif self.impl_is_submulti():
|
||||
if value:
|
||||
for idx, val in enumerate(value):
|
||||
_is_not_unique(val)
|
||||
if not isinstance(val, list):
|
||||
raise ValueError(_('invalid value "{0}" for "{1}" '
|
||||
'which must be a list of list'
|
||||
'').format(val,
|
||||
self.impl_getname()))
|
||||
for slave_val in val:
|
||||
do_validation(slave_val,
|
||||
idx)
|
||||
else:
|
||||
self._valid_consistency(current_opt,
|
||||
None,
|
||||
context,
|
||||
None,
|
||||
check_error,
|
||||
config_bag)
|
||||
else:
|
||||
_is_not_unique(value)
|
||||
if value:
|
||||
for idx, val in enumerate(value):
|
||||
do_validation(val,
|
||||
idx)
|
||||
|
||||
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())
|
||||
else:
|
||||
self._valid_consistency(current_opt,
|
||||
None,
|
||||
context,
|
||||
None,
|
||||
check_error,
|
||||
config_bag)
|
||||
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:
|
||||
warnings.warn_explicit(ValueWarning(msg, self),
|
||||
ValueWarning,
|
||||
self.__class__.__name__, 0)
|
||||
|
||||
def impl_is_dynsymlinkoption(self):
|
||||
return False
|
||||
@ -619,7 +625,7 @@ class Option(OnlyOption):
|
||||
else:
|
||||
consistencies = option._get_consistencies()
|
||||
if consistencies is not None:
|
||||
for func, all_cons_opts, params in consistencies:
|
||||
for cons_id, func, all_cons_opts, params in consistencies:
|
||||
warnings_only = params.get('warnings_only', False)
|
||||
if (warnings_only and not check_error) or (not warnings_only and check_error):
|
||||
transitive = params.get('transitive', True)
|
||||
@ -636,6 +642,7 @@ class Option(OnlyOption):
|
||||
wopt = opts[0]()
|
||||
wopt._launch_consistency(self,
|
||||
func,
|
||||
cons_id,
|
||||
option,
|
||||
value,
|
||||
context,
|
||||
@ -650,7 +657,7 @@ class Option(OnlyOption):
|
||||
opts,
|
||||
vals,
|
||||
warnings_only):
|
||||
equal = list()
|
||||
equal = []
|
||||
is_current = False
|
||||
for idx_inf, val_inf in enumerate(vals):
|
||||
for idx_sup, val_sup in enumerate(vals[idx_inf + 1:]):
|
||||
@ -659,7 +666,8 @@ class Option(OnlyOption):
|
||||
if opt_ == current_opt:
|
||||
is_current = True
|
||||
else:
|
||||
equal.append(opt_)
|
||||
if opt_ not in equal:
|
||||
equal.append(opt_)
|
||||
if equal:
|
||||
if debug: # pragma: no cover
|
||||
log.debug(_('_cons_not_equal: {} are not different').format(display_list(equal)))
|
||||
@ -742,7 +750,7 @@ class Option(OnlyOption):
|
||||
func,
|
||||
all_cons_opts,
|
||||
params):
|
||||
cons = (func, all_cons_opts, params)
|
||||
cons = (None, func, all_cons_opts, params)
|
||||
consistencies = getattr(self, '_consistencies', None)
|
||||
if consistencies is None:
|
||||
self._consistencies = [cons]
|
||||
@ -755,8 +763,14 @@ class Option(OnlyOption):
|
||||
def _get_consistencies(self):
|
||||
return getattr(self, '_consistencies', STATIC_TUPLE)
|
||||
|
||||
def _has_consistencies(self):
|
||||
return hasattr(self, '_consistencies')
|
||||
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):
|
||||
|
@ -36,6 +36,7 @@ class CacheOptionDescription(BaseOption):
|
||||
config,
|
||||
path='',
|
||||
_consistencies=None,
|
||||
_consistencies_id=0,
|
||||
cache_option=None,
|
||||
force_store_values=None,
|
||||
_dependencies=None):
|
||||
@ -66,6 +67,7 @@ class CacheOptionDescription(BaseOption):
|
||||
option._build_cache(config,
|
||||
subpath,
|
||||
_consistencies,
|
||||
_consistencies_id,
|
||||
cache_option,
|
||||
force_store_values,
|
||||
_dependencies)
|
||||
@ -74,7 +76,7 @@ class CacheOptionDescription(BaseOption):
|
||||
is_multi = option.impl_is_multi()
|
||||
if not option.impl_is_symlinkoption() and 'force_store_value' in option.impl_getproperties():
|
||||
force_store_values.append((subpath, option))
|
||||
for func, all_cons_opts, params in option._get_consistencies():
|
||||
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()
|
||||
@ -95,9 +97,11 @@ class CacheOptionDescription(BaseOption):
|
||||
'must be in same master/slaves for "{1}"').format(
|
||||
option.impl_getname(), opt.impl_getname()))
|
||||
_consistencies.setdefault(weak_opt,
|
||||
[]).append((func,
|
||||
[]).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)
|
||||
|
@ -81,8 +81,8 @@ class SymLinkOption(OnlyOption):
|
||||
def _get_consistencies(self):
|
||||
return ()
|
||||
|
||||
def _has_consistencies(self):
|
||||
return False
|
||||
def _has_consistencies(self, context):
|
||||
return option._opt._has_consistencies(context)
|
||||
|
||||
|
||||
class DynSymLinkOption(object):
|
||||
|
Reference in New Issue
Block a user