returns_raise is now mandatory in core

This commit is contained in:
Emmanuel Garette 2016-10-14 22:20:14 +02:00
parent 24ec5a9112
commit 4d02254234
8 changed files with 136 additions and 171 deletions

View File

@ -25,7 +25,7 @@ from .setting import undefined
def carry_out_calculation(option, context, callback, callback_params, def carry_out_calculation(option, context, callback, callback_params,
index=undefined, returns_raise=False): index=undefined):
"""a function that carries out a calculation for an option's value """a function that carries out a calculation for an option's value
:param option: the option :param option: the option
@ -162,8 +162,7 @@ def carry_out_calculation(option, context, callback, callback_params,
).impl_get_path_by_opt(opt) ).impl_get_path_by_opt(opt)
# get value # get value
value = context.getattr(path, force_permissive=True, value = context.getattr(path, force_permissive=True,
validate=False, validate=False, returns_raise=True)
returns_raise=True)
if isinstance(value, Exception): if isinstance(value, Exception):
if isinstance(value, PropertiesOptionError): if isinstance(value, PropertiesOptionError):
if force_permissive: if force_permissive:
@ -173,10 +172,7 @@ def carry_out_calculation(option, context, callback, callback_params,
'for: {2}').format(opt.impl_getname(), 'for: {2}').format(opt.impl_getname(),
value.proptype, value.proptype,
option.impl_getname())) option.impl_getname()))
if returns_raise:
return err return err
else:
raise err
else: else:
raise value raise value
# convert to list, not modifie this multi # convert to list, not modifie this multi
@ -213,7 +209,7 @@ def carry_out_calculation(option, context, callback, callback_params,
args.append(val) args.append(val)
else: else:
kwargs[key] = val kwargs[key] = val
return calculate(callback, args, kwargs, returns_raise) return calculate(callback, args, kwargs)
else: else:
# no value is multi # no value is multi
# return a single value # return a single value
@ -226,7 +222,7 @@ def carry_out_calculation(option, context, callback, callback_params,
args.append(couple[0]) args.append(couple[0])
else: else:
kwargs[key] = couple[0] kwargs[key] = couple[0]
ret = calculate(callback, args, kwargs, returns_raise) ret = calculate(callback, args, kwargs)
if not option.impl_is_optiondescription() and callback_params != {} and isinstance(ret, list) and \ if not option.impl_is_optiondescription() and callback_params != {} and isinstance(ret, list) and \
option.impl_is_master_slaves('slave'): option.impl_is_master_slaves('slave'):
if not has_option and index not in [None, undefined]: if not has_option and index not in [None, undefined]:
@ -241,7 +237,7 @@ def carry_out_calculation(option, context, callback, callback_params,
return ret return ret
def calculate(callback, args, kwargs, returns_raise): def calculate(callback, args, kwargs):
"""wrapper that launches the 'callback' """wrapper that launches the 'callback'
:param callback: callback function :param callback: callback function
@ -249,10 +245,7 @@ def calculate(callback, args, kwargs, returns_raise):
:param kwargs: in the callback's arity, the named parameters :param kwargs: in the callback's arity, the named parameters
""" """
if returns_raise:
try: try:
return callback(*args, **kwargs) return callback(*args, **kwargs)
except ValueError as err: except ValueError as err:
return err return err
else:
return callback(*args, **kwargs)

View File

@ -273,33 +273,34 @@ class SubConfig(object):
name, force_permissive=force_permissive, name, force_permissive=force_permissive,
returns_raise=returns_raise) returns_raise=returns_raise)
if isinstance(homeconfig, Exception): if isinstance(homeconfig, Exception):
return homeconfig cfg = homeconfig
return homeconfig.getattr(name, force_permissive=force_permissive, else:
cfg = homeconfig.getattr(name, force_permissive=force_permissive,
validate=validate, validate=validate,
_setting_properties=_setting_properties, _setting_properties=_setting_properties,
_self_properties=_self_properties, _self_properties=_self_properties,
index=index, returns_raise=returns_raise) index=index, returns_raise=returns_raise)
else:
context = self._cfgimpl_get_context() context = self._cfgimpl_get_context()
option = self.cfgimpl_get_description().__getattr__(name, option = self.cfgimpl_get_description().__getattr__(name,
context=context) context=context)
subpath = self._get_subpath(name) subpath = self._get_subpath(name)
if isinstance(option, DynSymLinkOption): if isinstance(option, DynSymLinkOption):
return self.cfgimpl_get_values()._get_cached_value( cfg = self.cfgimpl_get_values()._get_cached_value(
option, path=subpath, option, path=subpath,
validate=validate, validate=validate,
force_permissive=force_permissive, force_permissive=force_permissive,
setting_properties=_setting_properties, setting_properties=_setting_properties,
self_properties=_self_properties, self_properties=_self_properties,
index=index, index=index)
returns_raise=returns_raise)
elif isinstance(option, SymLinkOption): # pragma: no dynoptiondescription cover elif isinstance(option, SymLinkOption): # pragma: no dynoptiondescription cover
path = context.cfgimpl_get_description().impl_get_path_by_opt( path = context.cfgimpl_get_description().impl_get_path_by_opt(
option._impl_getopt()) option._impl_getopt())
return context.getattr(path, validate=validate, cfg = context.getattr(path, validate=validate,
force_permissive=force_permissive, force_permissive=force_permissive,
_setting_properties=_setting_properties, _setting_properties=_setting_properties,
_self_properties=_self_properties, _self_properties=_self_properties,
index=index, returns_raise=returns_raise) index=index, returns_raise=True)
elif option.impl_is_optiondescription(): elif option.impl_is_optiondescription():
props = self.cfgimpl_get_settings().validate_properties( props = self.cfgimpl_get_settings().validate_properties(
option, True, False, path=subpath, option, True, False, path=subpath,
@ -313,13 +314,16 @@ class SubConfig(object):
raise props raise props
return SubConfig(option, self._impl_context, subpath) return SubConfig(option, self._impl_context, subpath)
else: else:
return self.cfgimpl_get_values()._get_cached_value( cfg = self.cfgimpl_get_values()._get_cached_value(
option, path=subpath, option, path=subpath,
validate=validate, validate=validate,
force_permissive=force_permissive, force_permissive=force_permissive,
setting_properties=_setting_properties, setting_properties=_setting_properties,
self_properties=_self_properties, self_properties=_self_properties,
index=index, returns_raise=returns_raise) index=index)
if not returns_raise and isinstance(cfg, Exception):
raise cfg
return cfg
def find(self, bytype=None, byname=None, byvalue=undefined, type_='option', def find(self, bytype=None, byname=None, byvalue=undefined, type_='option',
check_properties=True, force_permissive=False): check_properties=True, force_permissive=False):

View File

@ -477,8 +477,7 @@ class Option(OnlyOption):
# Raise ValueError if not valid # Raise ValueError if not valid
value = carry_out_calculation(current_opt, context=context, value = carry_out_calculation(current_opt, context=context,
callback=validator, callback=validator,
callback_params=validator_params_, callback_params=validator_params_)
returns_raise=True)
if isinstance(value, Exception): if isinstance(value, Exception):
return value return value
@ -487,8 +486,7 @@ class Option(OnlyOption):
error = warning = None error = warning = None
else: else:
# option validation # option validation
err = self._validate(_value, context, current_opt, err = self._validate(_value, context, current_opt)
returns_raise=True)
if err: if err:
if debug: if debug:
log.debug('do_validation: value: {0}, index: {1}, ' log.debug('do_validation: value: {0}, index: {1}, '

View File

@ -106,39 +106,39 @@ class MasterSlaves(object):
for slave in self.getslaves(opt): for slave in self.getslaves(opt):
if not values.is_default_owner(slave, validate_properties=False, if not values.is_default_owner(slave, validate_properties=False,
validate_meta=False, index=index): validate_meta=False, index=index):
values._get_cached_value(slave, validate=False, multi = values._get_cached_value(slave, validate=False,
validate_properties=False validate_properties=False,
).pop(index, force=True) )
pass if isinstance(multi, Exception):
raise multi
multi.pop(index, force=True)
def getitem(self, values, opt, path, validate, force_permissive, def getitem(self, values, opt, path, validate, force_permissive,
trusted_cached_properties, validate_properties, session, trusted_cached_properties, validate_properties, session,
slave_path=undefined, slave_value=undefined, slave_path=undefined, slave_value=undefined,
setting_properties=undefined, self_properties=undefined, index=None, setting_properties=undefined, self_properties=undefined, index=None):
returns_raise=False):
if self.is_master(opt): if self.is_master(opt):
return self._getmaster(values, opt, path, validate, return self._getmaster(values, opt, path, validate,
force_permissive, force_permissive,
validate_properties, slave_path, validate_properties, slave_path,
slave_value, self_properties, index, slave_value, self_properties, index,
returns_raise, setting_properties, session) setting_properties, session)
else: else:
return self._getslave(values, opt, path, validate, return self._getslave(values, opt, path, validate,
force_permissive, trusted_cached_properties, force_permissive, trusted_cached_properties,
validate_properties, setting_properties, validate_properties, setting_properties,
self_properties, index, returns_raise, self_properties, index,
session) session)
def _getmaster(self, values, opt, path, validate, force_permissive, def _getmaster(self, values, opt, path, validate, force_permissive,
validate_properties, c_slave_path, validate_properties, c_slave_path,
c_slave_value, self_properties, index, returns_raise, c_slave_value, self_properties, index,
setting_properties, session): setting_properties, session):
value = values._get_cached_value(opt, path=path, validate=validate, value = values._get_cached_value(opt, path=path, validate=validate,
force_permissive=force_permissive, force_permissive=force_permissive,
validate_properties=validate_properties, validate_properties=validate_properties,
self_properties=self_properties, self_properties=self_properties,
from_masterslave=True, index=index, from_masterslave=True, index=index,
returns_raise=True,
setting_properties=setting_properties) setting_properties=setting_properties)
if isinstance(value, Exception): if isinstance(value, Exception):
return value return value
@ -152,7 +152,7 @@ class MasterSlaves(object):
def _getslave(self, values, opt, path, validate, force_permissive, def _getslave(self, values, opt, path, validate, force_permissive,
trusted_cached_properties, validate_properties, setting_properties, trusted_cached_properties, validate_properties, setting_properties,
self_properties, index, returns_raise, session): self_properties, index, session):
""" """
if master has length 0: if master has length 0:
return [] return []
@ -179,7 +179,7 @@ class MasterSlaves(object):
masterp = master.impl_getpath(context) masterp = master.impl_getpath(context)
masterlen = self.get_length(values, opt, session, validate, undefined, masterlen = self.get_length(values, opt, session, validate, undefined,
undefined, force_permissive, undefined, force_permissive,
master=master, returns_raise=returns_raise) master=master)
if isinstance(masterlen, Exception): if isinstance(masterlen, Exception):
return masterlen return masterlen
master_is_meta = values._is_meta(master, masterp, session) master_is_meta = values._is_meta(master, masterp, session)
@ -194,10 +194,7 @@ class MasterSlaves(object):
force_permissive=force_permissive, force_permissive=force_permissive,
setting_properties=setting_properties) setting_properties=setting_properties)
if props: if props:
if returns_raise:
return props return props
else:
raise props
else: else:
one_has_value = False one_has_value = False
if index is None: if index is None:
@ -216,8 +213,7 @@ class MasterSlaves(object):
#self_properties=self_properties, #self_properties=self_properties,
setting_properties=setting_properties, setting_properties=setting_properties,
masterlen=masterlen, masterlen=masterlen,
from_masterslave=True, from_masterslave=True)
returns_raise=True)
if isinstance(value, Exception): if isinstance(value, Exception):
if isinstance(value, PropertiesOptionError): if isinstance(value, PropertiesOptionError):
err = value err = value
@ -233,13 +229,10 @@ class MasterSlaves(object):
one_has_value = True one_has_value = True
if not one_has_value: if not one_has_value:
#raise last err #raise last err
if returns_raise:
return err return err
else:
raise err
return multi return multi
def validate(self, values, opt, value, path, returns_raise, session): def validate(self, values, opt, value, path, session):
if self.is_master(opt): if self.is_master(opt):
masterlen = len(value) masterlen = len(value)
#for regen slave path #for regen slave path
@ -249,7 +242,7 @@ class MasterSlaves(object):
slavelen = values._p_.get_max_length(slave_path, session) slavelen = values._p_.get_max_length(slave_path, session)
self.validate_slave_length(masterlen, slavelen, slave.impl_getname(), opt) self.validate_slave_length(masterlen, slavelen, slave.impl_getname(), opt)
else: else:
val_len = self.get_length(values, opt, session, slave_path=path, returns_raise=returns_raise) val_len = self.get_length(values, opt, session, slave_path=path)
if isinstance(val_len, Exception): if isinstance(val_len, Exception):
return val_len return val_len
self.validate_slave_length(val_len, self.validate_slave_length(val_len,
@ -258,7 +251,7 @@ class MasterSlaves(object):
def get_length(self, values, opt, session, validate=True, slave_path=undefined, def get_length(self, values, opt, session, validate=True, slave_path=undefined,
slave_value=undefined, force_permissive=False, master=None, slave_value=undefined, force_permissive=False, master=None,
masterp=None, returns_raise=False): masterp=None):
"""get master len with slave option""" """get master len with slave option"""
if master is None: if master is None:
master = self.getmaster(opt) master = self.getmaster(opt)
@ -268,7 +261,7 @@ class MasterSlaves(object):
slave_path = undefined slave_path = undefined
value = self.getitem(values, master, masterp, validate, value = self.getitem(values, master, masterp, validate,
force_permissive, None, True, session, slave_path=slave_path, force_permissive, None, True, session, slave_path=slave_path,
slave_value=slave_value, returns_raise=returns_raise) slave_value=slave_value)
if isinstance(value, Exception): if isinstance(value, Exception):
return value return value
return len(value) return len(value)

View File

@ -70,8 +70,7 @@ class ChoiceOption(Option):
session=session) session=session)
self.commit(session) self.commit(session)
def impl_get_values(self, context, current_opt=undefined, def impl_get_values(self, context, current_opt=undefined):
returns_raise=False):
if current_opt is undefined: if current_opt is undefined:
current_opt = self current_opt = self
params = undefined params = undefined
@ -87,8 +86,7 @@ class ChoiceOption(Option):
values_params = self.impl_get_choice_values_params() values_params = self.impl_get_choice_values_params()
values = carry_out_calculation(current_opt, context=context, values = carry_out_calculation(current_opt, context=context,
callback=values, callback=values,
callback_params=values_params, callback_params=values_params)
returns_raise=returns_raise)
if isinstance(values, Exception): if isinstance(values, Exception):
return values return values
if values is not undefined and not isinstance(values, list): # pragma: optional cover if values is not undefined and not isinstance(values, list): # pragma: optional cover
@ -97,10 +95,8 @@ class ChoiceOption(Option):
return values return values
def _validate(self, value, context=undefined, current_opt=undefined, def _validate(self, value, context=undefined, current_opt=undefined):
returns_raise=False): values = self.impl_get_values(context, current_opt=current_opt)
values = self.impl_get_values(context, current_opt=current_opt,
returns_raise=returns_raise)
if isinstance(values, Exception): if isinstance(values, Exception):
return values return values
if values is not undefined and not value in values: # pragma: optional cover if values is not undefined and not value in values: # pragma: optional cover
@ -117,8 +113,7 @@ class BoolOption(Option):
__slots__ = tuple() __slots__ = tuple()
_display_name = _('boolean') _display_name = _('boolean')
def _validate(self, value, context=undefined, current_opt=undefined, def _validate(self, value, context=undefined, current_opt=undefined):
returns_raise=False):
if not isinstance(value, bool): if not isinstance(value, bool):
return ValueError() # pragma: optional cover return ValueError() # pragma: optional cover
@ -128,8 +123,7 @@ class IntOption(Option):
__slots__ = tuple() __slots__ = tuple()
_display_name = _('integer') _display_name = _('integer')
def _validate(self, value, context=undefined, current_opt=undefined, def _validate(self, value, context=undefined, current_opt=undefined):
returns_raise=False):
if not isinstance(value, int): if not isinstance(value, int):
return ValueError() # pragma: optional cover return ValueError() # pragma: optional cover
@ -139,8 +133,7 @@ class FloatOption(Option):
__slots__ = tuple() __slots__ = tuple()
_display_name = _('float') _display_name = _('float')
def _validate(self, value, context=undefined, current_opt=undefined, def _validate(self, value, context=undefined, current_opt=undefined):
returns_raise=False):
if not isinstance(value, float): if not isinstance(value, float):
return ValueError() # pragma: optional cover return ValueError() # pragma: optional cover
@ -150,8 +143,7 @@ class StrOption(Option):
__slots__ = tuple() __slots__ = tuple()
_display_name = _('string') _display_name = _('string')
def _validate(self, value, context=undefined, current_opt=undefined, def _validate(self, value, context=undefined, current_opt=undefined):
returns_raise=False):
if not isinstance(value, str): if not isinstance(value, str):
return ValueError() # pragma: optional cover return ValueError() # pragma: optional cover
@ -168,8 +160,7 @@ else:
_empty = u'' _empty = u''
_display_name = _('unicode string') _display_name = _('unicode string')
def _validate(self, value, context=undefined, current_opt=undefined, def _validate(self, value, context=undefined, current_opt=undefined):
returns_raise=False):
if not isinstance(value, unicode): if not isinstance(value, unicode):
return ValueError() # pragma: optional cover return ValueError() # pragma: optional cover
@ -179,8 +170,7 @@ class PasswordOption(Option):
__slots__ = tuple() __slots__ = tuple()
_display_name = _('password') _display_name = _('password')
def _validate(self, value, context=undefined, current_opt=undefined, def _validate(self, value, context=undefined, current_opt=undefined):
returns_raise=False):
err = self._impl_valid_unicode(value) err = self._impl_valid_unicode(value)
if err: if err:
return err return err
@ -210,8 +200,7 @@ class IPOption(Option):
warnings_only=warnings_only, warnings_only=warnings_only,
extra=extra) extra=extra)
def _validate(self, value, context=undefined, current_opt=undefined, def _validate(self, value, context=undefined, current_opt=undefined):
returns_raise=False):
# sometimes an ip term starts with a zero # sometimes an ip term starts with a zero
# but this does not fit in some case, for example bind does not like it # but this does not fit in some case, for example bind does not like it
err = self._impl_valid_unicode(value) err = self._impl_valid_unicode(value)
@ -315,8 +304,7 @@ class PortOption(Option):
warnings_only=warnings_only, warnings_only=warnings_only,
extra=extra) extra=extra)
def _validate(self, value, context=undefined, current_opt=undefined, def _validate(self, value, context=undefined, current_opt=undefined):
returns_raise=False):
if isinstance(value, int): if isinstance(value, int):
if sys.version_info[0] >= 3: # pragma: optional cover if sys.version_info[0] >= 3: # pragma: optional cover
value = str(value) value = str(value)
@ -350,8 +338,7 @@ class NetworkOption(Option):
__slots__ = tuple() __slots__ = tuple()
_display_name = _('network address') _display_name = _('network address')
def _validate(self, value, context=undefined, current_opt=undefined, def _validate(self, value, context=undefined, current_opt=undefined):
returns_raise=False):
err = self._impl_valid_unicode(value) err = self._impl_valid_unicode(value)
if err: if err:
return err return err
@ -380,8 +367,7 @@ class NetmaskOption(Option):
__slots__ = tuple() __slots__ = tuple()
_display_name = _('netmask address') _display_name = _('netmask address')
def _validate(self, value, context=undefined, current_opt=undefined, def _validate(self, value, context=undefined, current_opt=undefined):
returns_raise=False):
err = self._impl_valid_unicode(value) err = self._impl_valid_unicode(value)
if err: if err:
return err return err
@ -434,8 +420,7 @@ class BroadcastOption(Option):
__slots__ = tuple() __slots__ = tuple()
_display_name = _('broadcast address') _display_name = _('broadcast address')
def _validate(self, value, context=undefined, current_opt=undefined, def _validate(self, value, context=undefined, current_opt=undefined):
returns_raise=False):
err = self._impl_valid_unicode(value) err = self._impl_valid_unicode(value)
if err: if err:
return err return err
@ -494,8 +479,7 @@ class DomainnameOption(Option):
warnings_only=warnings_only, warnings_only=warnings_only,
extra=extra) extra=extra)
def _validate(self, value, context=undefined, current_opt=undefined, def _validate(self, value, context=undefined, current_opt=undefined):
returns_raise=False):
err = self._impl_valid_unicode(value) err = self._impl_valid_unicode(value)
if err: if err:
return err return err
@ -564,8 +548,7 @@ class EmailOption(DomainnameOption):
username_re = re.compile(r"^[\w!#$%&'*+\-/=?^`{|}~.]+$") username_re = re.compile(r"^[\w!#$%&'*+\-/=?^`{|}~.]+$")
_display_name = _('email address') _display_name = _('email address')
def _validate(self, value, context=undefined, current_opt=undefined, def _validate(self, value, context=undefined, current_opt=undefined):
returns_raise=False):
err = self._impl_valid_unicode(value) err = self._impl_valid_unicode(value)
if err: if err:
return err return err
@ -590,8 +573,7 @@ class URLOption(DomainnameOption):
path_re = re.compile(r"^[A-Za-z0-9\-\._~:/\?#\[\]@!%\$&\'\(\)\*\+,;=]+$") path_re = re.compile(r"^[A-Za-z0-9\-\._~:/\?#\[\]@!%\$&\'\(\)\*\+,;=]+$")
_display_name = _('URL') _display_name = _('URL')
def _validate(self, value, context=undefined, current_opt=undefined, def _validate(self, value, context=undefined, current_opt=undefined):
returns_raise=False):
err = self._impl_valid_unicode(value) err = self._impl_valid_unicode(value)
if err: if err:
return err return err
@ -638,8 +620,7 @@ class UsernameOption(Option):
username_re = re.compile(r"^[a-z_][a-z0-9_-]{0,30}[$a-z0-9_-]{0,1}$") username_re = re.compile(r"^[a-z_][a-z0-9_-]{0,30}[$a-z0-9_-]{0,1}$")
_display_name = _('username') _display_name = _('username')
def _validate(self, value, context=undefined, current_opt=undefined, def _validate(self, value, context=undefined, current_opt=undefined):
returns_raise=False):
err = self._impl_valid_unicode(value) err = self._impl_valid_unicode(value)
if err: if err:
return err return err
@ -653,8 +634,7 @@ class FilenameOption(Option):
path_re = re.compile(r"^[a-zA-Z0-9\-\._~/+]+$") path_re = re.compile(r"^[a-zA-Z0-9\-\._~/+]+$")
_display_name = _('file name') _display_name = _('file name')
def _validate(self, value, context=undefined, current_opt=undefined, def _validate(self, value, context=undefined, current_opt=undefined):
returns_raise=False):
err = self._impl_valid_unicode(value) err = self._impl_valid_unicode(value)
if err: if err:
return err return err

View File

@ -305,6 +305,8 @@ class OptionDescription(BaseOption, StorageOptionDescription):
values = carry_out_calculation(self, context=context, values = carry_out_calculation(self, context=context,
callback=callback, callback=callback,
callback_params=callback_params) callback_params=callback_params)
if isinstance(values, Exception):
raise values
if len(values) > len(set(values)): if len(values) > len(set(values)):
raise ConfigError(_('DynOptionDescription callback return not uniq value')) raise ConfigError(_('DynOptionDescription callback return not uniq value'))
for val in values: for val in values:

View File

@ -98,7 +98,7 @@ class StorageBase(object):
_setattr(self, '_default', default) _setattr(self, '_default', default)
if is_multi and default_multi is not None: if is_multi and default_multi is not None:
err = self._validate(default_multi, returns_raise=True) err = self._validate(default_multi)
if err: if err:
raise ValueError(_("invalid default_multi value {0} " raise ValueError(_("invalid default_multi value {0} "
"for option {1}: {2}").format( "for option {1}: {2}").format(

View File

@ -57,16 +57,14 @@ class Values(object):
def _get_multi(self, opt, path): def _get_multi(self, opt, path):
return Multi([], self.context, opt, path) return Multi([], self.context, opt, path)
def _getdefaultvalue(self, opt, path, with_meta, index, submulti_index, def _getdefaultvalue(self, opt, path, with_meta, index, submulti_index):
returns_raise):
# if value has callback and is not set # if value has callback and is not set
if opt.impl_has_callback(): if opt.impl_has_callback():
callback, callback_params = opt.impl_get_callback() callback, callback_params = opt.impl_get_callback()
value = carry_out_calculation(opt, context=self._getcontext(), value = carry_out_calculation(opt, context=self._getcontext(),
callback=callback, callback=callback,
callback_params=callback_params, callback_params=callback_params,
index=index, index=index)
returns_raise=returns_raise)
if isinstance(value, list) and index is not None: if isinstance(value, list) and index is not None:
#if return a list and index is set, return value only if #if return a list and index is set, return value only if
#it's a submulti without submulti_index and without list of list #it's a submulti without submulti_index and without list of list
@ -80,7 +78,7 @@ class Values(object):
if meta is not None: if meta is not None:
value = meta.cfgimpl_get_values( value = meta.cfgimpl_get_values(
)._get_cached_value(opt, path, index=index, submulti_index=submulti_index, )._get_cached_value(opt, path, index=index, submulti_index=submulti_index,
from_masterslave=True, returns_raise=True) from_masterslave=True)
if isinstance(value, Exception): if isinstance(value, Exception):
if not isinstance(value, PropertiesOptionError): if not isinstance(value, PropertiesOptionError):
raise value raise value
@ -131,7 +129,7 @@ class Values(object):
else: else:
return value return value
return self._getdefaultvalue(opt, path, with_meta, index, return self._getdefaultvalue(opt, path, with_meta, index,
submulti_index, True) submulti_index)
def get_modified_values(self): def get_modified_values(self):
return self._p_.get_modified_values() return self._p_.get_modified_values()
@ -170,9 +168,11 @@ class Values(object):
fake_context = context._gen_fake_values(session) fake_context = context._gen_fake_values(session)
fake_value = fake_context.cfgimpl_get_values() fake_value = fake_context.cfgimpl_get_values()
fake_value.reset(opt, path, validate=False) fake_value.reset(opt, path, validate=False)
fake_value._get_cached_value(opt, path, ret = fake_value._get_cached_value(opt, path,
setting_properties=_setting_properties, setting_properties=_setting_properties,
check_frozen=True) check_frozen=True)
if isinstance(ret, Exception):
raise ret
if opt.impl_is_master_slaves('master'): if opt.impl_is_master_slaves('master'):
opt.impl_get_master_slaves().reset(opt, self, _setting_properties) opt.impl_get_master_slaves().reset(opt, self, _setting_properties)
if hasvalue: if hasvalue:
@ -181,7 +181,9 @@ class Values(object):
setting_properties=_setting_properties, setting_properties=_setting_properties,
read_write=False, read_write=False,
apply_requires=False): apply_requires=False):
value = self._getdefaultvalue(opt, path, True, undefined, undefined, False) value = self._getdefaultvalue(opt, path, True, undefined, undefined)
if isinstance(value, Exception):
raise value
self._setvalue(opt, path, value, force_owner=owners.forced) self._setvalue(opt, path, value, force_owner=owners.forced)
else: else:
self._p_.resetvalue(path, session) self._p_.resetvalue(path, session)
@ -225,7 +227,7 @@ class Values(object):
setting_properties=undefined, self_properties=undefined, setting_properties=undefined, self_properties=undefined,
index=None, submulti_index=undefined, from_masterslave=False, index=None, submulti_index=undefined, from_masterslave=False,
with_meta=True, masterlen=undefined, check_frozen=False, with_meta=True, masterlen=undefined, check_frozen=False,
returns_raise=False, session=None, display_warnings=True): session=None, display_warnings=True):
context = self._getcontext() context = self._getcontext()
settings = context.cfgimpl_get_settings() settings = context.cfgimpl_get_settings()
if path is None: if path is None:
@ -254,10 +256,7 @@ class Values(object):
self_properties=self_properties, self_properties=self_properties,
index=index) index=index)
if props: if props:
if returns_raise:
return props return props
else:
raise props
return value return value
if session is None: if session is None:
session = self._p_.getsession() session = self._p_.getsession()
@ -270,8 +269,7 @@ class Values(object):
session, session,
setting_properties=setting_properties, setting_properties=setting_properties,
index=index, index=index,
self_properties=self_properties, self_properties=self_properties)
returns_raise=returns_raise)
else: else:
val = self._get_validated_value(opt, path, validate, val = self._get_validated_value(opt, path, validate,
force_permissive, force_permissive,
@ -283,14 +281,10 @@ class Values(object):
index=index, index=index,
submulti_index=submulti_index, submulti_index=submulti_index,
check_frozen=check_frozen, check_frozen=check_frozen,
returns_raise=returns_raise,
session=session, session=session,
display_warnings=display_warnings) display_warnings=display_warnings)
if isinstance(val, Exception): if isinstance(val, Exception):
if returns_raise:
return val return val
else:
raise val
# cache doesn't work with SubMulti yet # cache doesn't work with SubMulti yet
if not isinstance(val, SubMulti) and 'cache' in setting_properties and \ if not isinstance(val, SubMulti) and 'cache' in setting_properties and \
validate and validate_properties and force_permissive is False \ validate and validate_properties and force_permissive is False \
@ -307,7 +301,7 @@ class Values(object):
index=None, submulti_index=undefined, index=None, submulti_index=undefined,
with_meta=True, setting_properties=undefined, with_meta=True, setting_properties=undefined,
self_properties=undefined, masterlen=undefined, self_properties=undefined, masterlen=undefined,
check_frozen=False, returns_raise=False, check_frozen=False,
session=None, display_warnings=True): session=None, display_warnings=True):
"""same has getitem but don't touch the cache """same has getitem but don't touch the cache
index is None for slave value, if value returned is not a list, just return [] index is None for slave value, if value returned is not a list, just return []
@ -378,15 +372,9 @@ class Values(object):
self_properties=self_properties, self_properties=self_properties,
index=index) index=index)
if props: if props:
if returns_raise:
return props return props
else:
raise props
if config_error is not None: if config_error is not None:
if returns_raise:
return config_error return config_error
else:
raise config_error
return value return value
def __setitem__(self, opt, value): # pragma: optional cover def __setitem__(self, opt, value): # pragma: optional cover
@ -445,11 +433,13 @@ class Values(object):
def validate(self, opt, value, path, check_frozen=True, force_permissive=False, def validate(self, opt, value, path, check_frozen=True, force_permissive=False,
setting_properties=undefined, valid_masterslave=True, setting_properties=undefined, valid_masterslave=True,
not_raises=False, returns_raise=False, session=None): not_raises=False, session=None):
if valid_masterslave and opt.impl_is_master_slaves(): if valid_masterslave and opt.impl_is_master_slaves():
if session is None: if session is None:
session = self._p_.getsession() session = self._p_.getsession()
opt.impl_get_master_slaves().validate(self, opt, value, path, returns_raise, session) val = opt.impl_get_master_slaves().validate(self, opt, value, path, session)
if isinstance(val, Exception):
return val
props = self._getcontext().cfgimpl_get_settings().validate_properties(opt, props = self._getcontext().cfgimpl_get_settings().validate_properties(opt,
False, False,
check_frozen, check_frozen,
@ -507,8 +497,11 @@ class Values(object):
if 'frozen' in self_properties and 'force_default_on_freeze' in self_properties: if 'frozen' in self_properties and 'force_default_on_freeze' in self_properties:
return owners.default return owners.default
if validate_properties: if validate_properties:
self._get_cached_value(opt, path, True, force_permissive, None, True, value = self._get_cached_value(opt, path, True, force_permissive, None, True,
self_properties=self_properties, session=session) self_properties=self_properties, session=session)
if isinstance(value, Exception):
raise value
owner = self._p_.getowner(path, owners.default, session, only_default=only_default, index=index) owner = self._p_.getowner(path, owners.default, session, only_default=only_default, index=index)
if validate_meta is undefined: if validate_meta is undefined:
if opt.impl_is_master_slaves('slave'): if opt.impl_is_master_slaves('slave'):
@ -641,7 +634,7 @@ class Values(object):
force_permissive=True, force_permissive=True,
setting_properties=setting_properties, setting_properties=setting_properties,
self_properties=self_properties, self_properties=self_properties,
validate=True, returns_raise=True, validate=True,
display_warnings=False) display_warnings=False)
if not isinstance(err, Exception): if not isinstance(err, Exception):
pass pass
@ -769,7 +762,7 @@ class Multi(list):
def _getdefaultvalue(self, index): def _getdefaultvalue(self, index):
values = self._getcontext().cfgimpl_get_values() values = self._getcontext().cfgimpl_get_values()
value = values._getdefaultvalue(self.opt, self.path, True, index, value = values._getdefaultvalue(self.opt, self.path, True, index,
undefined, True) undefined)
if self.opt.impl_is_submulti(): if self.opt.impl_is_submulti():
value = SubMulti(value, self.context, self.opt, self.path, index) value = SubMulti(value, self.context, self.opt, self.path, index)
return value return value
@ -795,6 +788,8 @@ class Multi(list):
fake_multi = fake_context.cfgimpl_get_values()._get_cached_value( fake_multi = fake_context.cfgimpl_get_values()._get_cached_value(
self.opt, path=self.path, validate=False, self.opt, path=self.path, validate=False,
force_permissive=force_permissive) force_permissive=force_permissive)
if isinstance(fake_multi, Exception):
raise fake_multi
fake_multi.append(value, validate=False, force=True, fake_multi.append(value, validate=False, force=True,
setitem=setitem) setitem=setitem)
self._validate(value, fake_context, index, True) self._validate(value, fake_context, index, True)
@ -943,4 +938,4 @@ class SubMulti(Multi):
def _getdefaultvalue(self, index): def _getdefaultvalue(self, index):
values = self._getcontext().cfgimpl_get_values() values = self._getcontext().cfgimpl_get_values()
return values._getdefaultvalue(self.opt, self.path, True, index, return values._getdefaultvalue(self.opt, self.path, True, index,
self._index, True) self._index)