validate value when we calculate it
This commit is contained in:
parent
5c104a2eda
commit
372ce5ea1e
@ -345,6 +345,17 @@ def test_callback_list():
|
|||||||
raises(ValueError, "cfg.val1")
|
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():
|
def test_callback_multi():
|
||||||
val1 = StrOption('val1', "", callback=return_val, multi=True)
|
val1 = StrOption('val1', "", callback=return_val, multi=True)
|
||||||
maconfig = OptionDescription('rootconfig', '', [val1])
|
maconfig = OptionDescription('rootconfig', '', [val1])
|
||||||
|
@ -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):
|
index=undefined, validate=True):
|
||||||
"""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,9 +162,14 @@ def carry_out_calculation(option, context, callback, callback_params,
|
|||||||
else:
|
else:
|
||||||
path = context.cfgimpl_get_description(
|
path = context.cfgimpl_get_description(
|
||||||
).impl_get_path_by_opt(opt)
|
).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
|
# get value
|
||||||
value = context.getattr(path, force_permissive=True,
|
value = context.getattr(path, force_permissive=True,
|
||||||
validate=False, returns_raise=True)
|
validate=valid, 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:
|
||||||
|
@ -57,14 +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, validate):
|
||||||
# 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, validate=validate)
|
||||||
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
|
||||||
@ -102,7 +102,7 @@ class Values(object):
|
|||||||
return value
|
return value
|
||||||
|
|
||||||
def _getvalue(self, opt, path, self_properties, index, submulti_index,
|
def _getvalue(self, opt, path, self_properties, index, submulti_index,
|
||||||
with_meta, masterlen, session):
|
with_meta, masterlen, session, validate):
|
||||||
"""actually retrieves the value
|
"""actually retrieves the value
|
||||||
|
|
||||||
:param opt: the `option.Option()` object
|
:param opt: the `option.Option()` object
|
||||||
@ -129,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)
|
submulti_index, validate)
|
||||||
|
|
||||||
def get_modified_values(self):
|
def get_modified_values(self):
|
||||||
return self._p_.get_modified_values()
|
return self._p_.get_modified_values()
|
||||||
@ -181,7 +181,7 @@ 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)
|
value = self._getdefaultvalue(opt, path, True, undefined, undefined, validate)
|
||||||
if isinstance(value, Exception):
|
if isinstance(value, Exception):
|
||||||
raise value
|
raise value
|
||||||
self._setvalue(opt, path, value, force_owner=owners.forced)
|
self._setvalue(opt, path, value, force_owner=owners.forced)
|
||||||
@ -313,7 +313,7 @@ class Values(object):
|
|||||||
if session is None:
|
if session is None:
|
||||||
session = self._p_.getsession()
|
session = self._p_.getsession()
|
||||||
value = self._getvalue(opt, path, self_properties, index, submulti_index,
|
value = self._getvalue(opt, path, self_properties, index, submulti_index,
|
||||||
with_meta, masterlen, session)
|
with_meta, masterlen, session, validate)
|
||||||
if isinstance(value, Exception):
|
if isinstance(value, Exception):
|
||||||
value_error = True
|
value_error = True
|
||||||
if isinstance(value, ConfigError):
|
if isinstance(value, ConfigError):
|
||||||
@ -781,7 +781,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)
|
undefined, True)
|
||||||
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
|
||||||
@ -963,4 +963,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)
|
self._index, True)
|
||||||
|
Loading…
Reference in New Issue
Block a user