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

View file

@ -273,53 +273,57 @@ class SubConfig(object):
name, force_permissive=force_permissive,
returns_raise=returns_raise)
if isinstance(homeconfig, Exception):
return homeconfig
return homeconfig.getattr(name, force_permissive=force_permissive,
validate=validate,
_setting_properties=_setting_properties,
_self_properties=_self_properties,
index=index, returns_raise=returns_raise)
context = self._cfgimpl_get_context()
option = self.cfgimpl_get_description().__getattr__(name,
context=context)
subpath = self._get_subpath(name)
if isinstance(option, DynSymLinkOption):
return self.cfgimpl_get_values()._get_cached_value(
option, path=subpath,
validate=validate,
force_permissive=force_permissive,
setting_properties=_setting_properties,
self_properties=_self_properties,
index=index,
returns_raise=returns_raise)
elif isinstance(option, SymLinkOption): # pragma: no dynoptiondescription cover
path = context.cfgimpl_get_description().impl_get_path_by_opt(
option._impl_getopt())
return context.getattr(path, validate=validate,
force_permissive=force_permissive,
_setting_properties=_setting_properties,
_self_properties=_self_properties,
index=index, returns_raise=returns_raise)
elif option.impl_is_optiondescription():
props = self.cfgimpl_get_settings().validate_properties(
option, True, False, path=subpath,
force_permissive=force_permissive,
self_properties=_self_properties,
setting_properties=_setting_properties)
if props:
if returns_raise:
return props
else:
raise props
return SubConfig(option, self._impl_context, subpath)
cfg = homeconfig
else:
cfg = homeconfig.getattr(name, force_permissive=force_permissive,
validate=validate,
_setting_properties=_setting_properties,
_self_properties=_self_properties,
index=index, returns_raise=returns_raise)
else:
return self.cfgimpl_get_values()._get_cached_value(
option, path=subpath,
validate=validate,
force_permissive=force_permissive,
setting_properties=_setting_properties,
self_properties=_self_properties,
index=index, returns_raise=returns_raise)
context = self._cfgimpl_get_context()
option = self.cfgimpl_get_description().__getattr__(name,
context=context)
subpath = self._get_subpath(name)
if isinstance(option, DynSymLinkOption):
cfg = self.cfgimpl_get_values()._get_cached_value(
option, path=subpath,
validate=validate,
force_permissive=force_permissive,
setting_properties=_setting_properties,
self_properties=_self_properties,
index=index)
elif isinstance(option, SymLinkOption): # pragma: no dynoptiondescription cover
path = context.cfgimpl_get_description().impl_get_path_by_opt(
option._impl_getopt())
cfg = context.getattr(path, validate=validate,
force_permissive=force_permissive,
_setting_properties=_setting_properties,
_self_properties=_self_properties,
index=index, returns_raise=True)
elif option.impl_is_optiondescription():
props = self.cfgimpl_get_settings().validate_properties(
option, True, False, path=subpath,
force_permissive=force_permissive,
self_properties=_self_properties,
setting_properties=_setting_properties)
if props:
if returns_raise:
return props
else:
raise props
return SubConfig(option, self._impl_context, subpath)
else:
cfg = self.cfgimpl_get_values()._get_cached_value(
option, path=subpath,
validate=validate,
force_permissive=force_permissive,
setting_properties=_setting_properties,
self_properties=_self_properties,
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',
check_properties=True, force_permissive=False):

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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