validate value when we calculate it

This commit is contained in:
Emmanuel Garette 2017-01-26 21:01:54 +01:00
parent 5c104a2eda
commit 372ce5ea1e
3 changed files with 26 additions and 10 deletions

View File

@ -345,6 +345,17 @@ def test_callback_list():
raises(ValueError, "cfg.val1")
def test_callback_list2():
val1 = StrOption('val1', "", callback=return_list)
val2 = StrOption('val2', "", callback=return_value, callback_params={'': ((val1, False),)})
maconfig = OptionDescription('rootconfig', '', [val1, val2])
cfg = Config(maconfig)
cfg.read_write()
raises(ValueError, "cfg.val1")
#cfg.val2
raises(ValueError, "cfg.val2")
def test_callback_multi():
val1 = StrOption('val1', "", callback=return_val, multi=True)
maconfig = OptionDescription('rootconfig', '', [val1])

View File

@ -25,7 +25,7 @@ from .setting import undefined
def carry_out_calculation(option, context, callback, callback_params,
index=undefined):
index=undefined, validate=True):
"""a function that carries out a calculation for an option's value
:param option: the option
@ -162,9 +162,14 @@ def carry_out_calculation(option, context, callback, callback_params,
else:
path = context.cfgimpl_get_description(
).impl_get_path_by_opt(opt)
# don't validate if option is option that we tried to validate
if opt == option:
valid = False
else:
valid = validate
# get value
value = context.getattr(path, force_permissive=True,
validate=False, returns_raise=True)
validate=valid, returns_raise=True)
if isinstance(value, Exception):
if isinstance(value, PropertiesOptionError):
if force_permissive:

View File

@ -57,14 +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):
def _getdefaultvalue(self, opt, path, with_meta, index, submulti_index, validate):
# 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)
index=index, validate=validate)
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
@ -102,7 +102,7 @@ class Values(object):
return value
def _getvalue(self, opt, path, self_properties, index, submulti_index,
with_meta, masterlen, session):
with_meta, masterlen, session, validate):
"""actually retrieves the value
:param opt: the `option.Option()` object
@ -129,7 +129,7 @@ class Values(object):
else:
return value
return self._getdefaultvalue(opt, path, with_meta, index,
submulti_index)
submulti_index, validate)
def get_modified_values(self):
return self._p_.get_modified_values()
@ -181,7 +181,7 @@ class Values(object):
setting_properties=_setting_properties,
read_write=False,
apply_requires=False):
value = self._getdefaultvalue(opt, path, True, undefined, undefined)
value = self._getdefaultvalue(opt, path, True, undefined, undefined, validate)
if isinstance(value, Exception):
raise value
self._setvalue(opt, path, value, force_owner=owners.forced)
@ -313,7 +313,7 @@ class Values(object):
if session is None:
session = self._p_.getsession()
value = self._getvalue(opt, path, self_properties, index, submulti_index,
with_meta, masterlen, session)
with_meta, masterlen, session, validate)
if isinstance(value, Exception):
value_error = True
if isinstance(value, ConfigError):
@ -781,7 +781,7 @@ class Multi(list):
def _getdefaultvalue(self, index):
values = self._getcontext().cfgimpl_get_values()
value = values._getdefaultvalue(self.opt, self.path, True, index,
undefined)
undefined, True)
if self.opt.impl_is_submulti():
value = SubMulti(value, self.context, self.opt, self.path, index)
return value
@ -963,4 +963,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)
self._index, True)