context could be useful in a callback, now we can retrieve context in a function (be careful to infinite loop)
This commit is contained in:
parent
b3d04a1f68
commit
835d6d6da0
|
@ -44,6 +44,13 @@ def return_calc(i, j, k):
|
|||
return i + j + k
|
||||
|
||||
|
||||
def is_config(config, **kwargs):
|
||||
if isinstance(config, Config):
|
||||
return 'yes'
|
||||
else:
|
||||
return 'no'
|
||||
|
||||
|
||||
def make_description():
|
||||
gcoption = ChoiceOption('name', 'GC name', ('ref', 'framework'), 'ref')
|
||||
gcdummy = BoolOption('dummy', 'dummy', default=False)
|
||||
|
@ -254,6 +261,29 @@ def test_callback_invalid():
|
|||
raises(ValueError, "StrOption('val2', '', callback=return_value, callback_params={'': 'string'})")
|
||||
raises(ValueError, "StrOption('val4', '', callback=return_value, callback_params={'value': (('string', False),)})")
|
||||
raises(ValueError, "StrOption('val4', '', callback=return_value, callback_params={'value': ((val1, 'string'),)})")
|
||||
raises(ValueError, "StrOption('val4', '', callback=return_value, callback_params={'value': ((val1, False, 'unknown'),)})")
|
||||
raises(ValueError, "StrOption('val4', '', callback=return_value, callback_params={'value': ((val1,),)})")
|
||||
|
||||
|
||||
def test_callback_with_context():
|
||||
val1 = StrOption("val1", "", callback=is_config, callback_params={'': ((None,),), 'value': ('string',)})
|
||||
maconfig = OptionDescription('rootconfig', '', [val1])
|
||||
cfg = Config(maconfig)
|
||||
assert cfg.val1 == 'yes'
|
||||
|
||||
|
||||
def test_callback_with_context_named():
|
||||
val1 = StrOption("val1", "", callback=is_config, callback_params={'config': ((None,),)})
|
||||
maconfig = OptionDescription('rootconfig', '', [val1])
|
||||
cfg = Config(maconfig)
|
||||
assert cfg.val1 == 'yes'
|
||||
|
||||
|
||||
def test_callback_with_error():
|
||||
val1 = StrOption("val1", "", callback=is_config, callback_params={'': ('string',), 'value': ('string',)})
|
||||
maconfig = OptionDescription('rootconfig', '', [val1])
|
||||
cfg = Config(maconfig)
|
||||
assert cfg.val1 == 'no'
|
||||
|
||||
|
||||
def test_callback_value():
|
||||
|
|
|
@ -145,22 +145,26 @@ def carry_out_calculation(option, config, callback, callback_params,
|
|||
for key, callbacks in callback_params.items():
|
||||
for callbk in callbacks:
|
||||
if isinstance(callbk, tuple):
|
||||
if len(callbk) == 1:
|
||||
tcparams.setdefault(key, []).append((config, False))
|
||||
else:
|
||||
# callbk is something link (opt, True|False)
|
||||
opt, force_permissive = callbk
|
||||
path = config.cfgimpl_get_description().impl_get_path_by_opt(
|
||||
opt)
|
||||
path = config.cfgimpl_get_description(
|
||||
).impl_get_path_by_opt(opt)
|
||||
# get value
|
||||
try:
|
||||
value = config._getattr(path, force_permissive=True, validate=False)
|
||||
value = config._getattr(path, force_permissive=True,
|
||||
validate=False)
|
||||
# convert to list, not modifie this multi
|
||||
if value.__class__.__name__ == 'Multi':
|
||||
value = list(value)
|
||||
except PropertiesOptionError as err:
|
||||
if force_permissive:
|
||||
continue
|
||||
raise ConfigError(_('unable to carry out a calculation, '
|
||||
'option {0} has properties: {1} for: '
|
||||
'{2}').format(opt._name,
|
||||
raise ConfigError(_('unable to carry out a calculation'
|
||||
', option {0} has properties: {1} '
|
||||
'for: {2}').format(opt._name,
|
||||
err.proptype,
|
||||
option._name))
|
||||
|
||||
|
@ -172,7 +176,8 @@ def carry_out_calculation(option, config, callback, callback_params,
|
|||
is_multi = True
|
||||
#opt is slave, search if option is an other slaves
|
||||
elif opt.impl_get_multitype() == multitypes.slave:
|
||||
if option in opt.impl_get_master_slaves().impl_get_master_slaves():
|
||||
if option in opt.impl_get_master_slaves(
|
||||
).impl_get_master_slaves():
|
||||
is_multi = True
|
||||
if is_multi:
|
||||
len_multi = len(value)
|
||||
|
|
|
@ -1528,13 +1528,22 @@ def validate_callback(callback, callback_params, type_):
|
|||
).format(type_, key))
|
||||
for callbk in callbacks:
|
||||
if isinstance(callbk, tuple):
|
||||
if len(callbk) == 1:
|
||||
if callbk != (None,):
|
||||
raise ValueError(_('{0}_params with length of '
|
||||
'tuple as 1 must only have '
|
||||
'None as first value'))
|
||||
elif len(callbk) != 2:
|
||||
raise ValueError(_('{0}_params must only 1 or 2 as '
|
||||
'length'))
|
||||
else:
|
||||
option, force_permissive = callbk
|
||||
if type_ == 'validator' and not force_permissive:
|
||||
raise ValueError(_('validator not support tuple'))
|
||||
if not isinstance(option, Option) and not \
|
||||
isinstance(option, SymLinkOption):
|
||||
raise ValueError(_('{0}_params must have an option '
|
||||
'not a {0} for first argument'
|
||||
raise ValueError(_('{0}_params must have an option'
|
||||
' not a {0} for first argument'
|
||||
).format(type_, type(option)))
|
||||
if force_permissive not in [True, False]:
|
||||
raise ValueError(_('{0}_params must have a boolean'
|
||||
|
|
Loading…
Reference in New Issue