from ..storage import get_storages_option
from . import MasterSlaves
-
if sys.version_info[0] >= 3: # pragma: optional cover
xrange = range
all_cons_opts.append(opt)
if val_consistencies:
- return getattr(self, func)(current_opt, all_cons_opts, all_cons_vals, warnings_only)
+ err = getattr(self, func)(current_opt, all_cons_opts, all_cons_vals, warnings_only)
+ if err:
+ if warnings_only:
+ if isinstance(err, ValueError):
+ msg = _('attention, "{0}" could be an invalid {1} for "{2}", {3}').format(
+ value, self._display_name, self.impl_get_display_name(), err)
+ else:
+ msg = '{}'.format(err)
+ warnings.warn_explicit(ValueWarning(msg, self),
+ ValueWarning,
+ self.__class__.__name__, 0)
+ else:
+ return err
def impl_validate(self, value, context=undefined, validate=True,
force_index=None, force_submulti_index=None,
current_opt=undefined, is_multi=None,
- display_warnings=True, multi=None):
+ display_error=True, display_warnings=True, multi=None):
"""
:param value: the option's value
:param context: Config's context
if current_opt is undefined:
current_opt = self
+ display_warnings = display_warnings and (context is undefined or
+ 'warnings' in context.cfgimpl_get_settings())
def _is_not_unique(value):
- if self.impl_is_unique() and len(set(value)) != len(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(
if _value is None:
error = warning = None
else:
- # option validation
- err = self._validate(_value, context, current_opt)
- if err:
- if debug:
- log.debug('do_validation: value: {0}, index: {1}, '
- 'submulti_index: {2}'.format(_value, _index,
- submulti_index),
- exc_info=True)
- err_msg = '{0}'.format(err)
- name = self.impl_getdoc()
- if name is None or name == '':
- name = self.impl_getname()
- 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)
- warning = None
+ if display_error:
+ # option validation
+ err = self._validate(_value, context, current_opt)
+ if err:
+ if debug:
+ log.debug('do_validation: value: {0}, index: {1}, '
+ 'submulti_index: {2}'.format(_value, _index,
+ 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
- if display_warnings:
+ if (display_error and not self._is_warnings_only()) or (display_warnings and self._is_warnings_only()):
error = calculation_validator(_value)
if not error:
error = self._second_level_validation(_value, self._is_warnings_only())
log.debug(_('do_validation for {0}: error in value').format(
self.impl_getname()), exc_info=True)
if self._is_warnings_only():
- warning = error
+ 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 and warning is None:
+ if error is None:
# if context launch consistency validation
#if context is not undefined:
ret = self._valid_consistency(current_opt, _value, context,
- _index, submulti_index)
- if ret:
- if isinstance(ret, ValueWarning):
- if display_warnings:
- warning = ret
- elif isinstance(ret, ValueError):
- error = ret
- else:
- return ret
- if warning:
- msg = _('attention, "{0}" could be an invalid {1} for "{2}", {3}').format(
- _value, self._display_name, self.impl_get_display_name(), warning)
- if context is undefined or 'warnings' in \
- context.cfgimpl_get_settings():
- warnings.warn_explicit(ValueWarning(msg, self),
- ValueWarning,
- self.__class__.__name__, 0)
- elif error:
+ _index, submulti_index, display_warnings,
+ 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}'
self.impl_get_display_name())
return ValueError(msg)
- # generic calculation
- #if context is not undefined:
- # descr = context.cfgimpl_get_description()
-
if is_multi is None:
is_multi = self.impl_is_multi()
if err:
return err
return self._valid_consistency(current_opt, None, context,
- None, None)
+ None, None, display_warnings, display_error)
def impl_is_dynsymlinkoption(self):
return False
#consistency could generate warnings or errors
self._set_has_dependency()
- def _valid_consistency(self, option, value, context, index, submulti_idx):
+ 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:
if consistencies is not None:
for func, all_cons_opts, params in consistencies:
warnings_only = params.get('warnings_only', False)
- 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:
- if warnings_only:
- return ValueWarning(str(err), option)
+ 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):
def impl_validate(self, value, context=undefined, validate=True,
force_index=None, force_submulti_index=None, is_multi=None,
- display_warnings=True, multi=None):
+ display_error=True, display_warnings=True, multi=None):
return self._impl_getopt().impl_validate(value, context, validate,
force_index,
force_submulti_index,
current_opt=self,
is_multi=is_multi,
+ display_error=display_error,
display_warnings=display_warnings,
multi=multi)
def __getitem__(self, opt):
"enables us to use the pythonic dictionary-like access to values"
- return self.getitem(opt)
+ return self._get_cached_value(opt)
def getitem(self, opt, validate=True, force_permissive=False):
"""
value = self._getvalue(opt, path, self_properties, index, submulti_index,
with_meta, masterlen, session)
if isinstance(value, Exception):
+ value_error = True
if isinstance(value, ConfigError):
# For calculating properties, we need value (ie for mandatory
# value).
else:
raise value
else:
+ value_error = False
if index is undefined:
force_index = None
else:
'validator' in setting_properties,
force_index=force_index,
force_submulti_index=force_submulti_index,
- display_warnings=display_warnings)
+ display_error=True,
+ display_warnings=False)
if err:
config_error = err
value = None
index=index)
if props:
return props
+ if not value_error and validate and display_warnings:
+ opt.impl_validate(value, context,
+ 'validator' in setting_properties,
+ force_index=force_index,
+ force_submulti_index=force_submulti_index,
+ display_error=False,
+ display_warnings=display_warnings)
if config_error is not None:
return config_error
return value
session=session, not_raises=not_raises)
if props and not_raises:
return
- err = opt.impl_validate(value, fake_context)
+ err = opt.impl_validate(value, fake_context, display_warnings=False)
if err:
raise err
+ opt.impl_validate(value, fake_context, display_error=False)
self._setvalue(opt, path, value)
def _setvalue(self, opt, path, value, force_owner=undefined, index=None):