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
|
return i + j + k
|
||||||
|
|
||||||
|
|
||||||
|
def is_config(config, **kwargs):
|
||||||
|
if isinstance(config, Config):
|
||||||
|
return 'yes'
|
||||||
|
else:
|
||||||
|
return 'no'
|
||||||
|
|
||||||
|
|
||||||
def make_description():
|
def make_description():
|
||||||
gcoption = ChoiceOption('name', 'GC name', ('ref', 'framework'), 'ref')
|
gcoption = ChoiceOption('name', 'GC name', ('ref', 'framework'), 'ref')
|
||||||
gcdummy = BoolOption('dummy', 'dummy', default=False)
|
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('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': (('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, '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():
|
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 key, callbacks in callback_params.items():
|
||||||
for callbk in callbacks:
|
for callbk in callbacks:
|
||||||
if isinstance(callbk, tuple):
|
if isinstance(callbk, tuple):
|
||||||
|
if len(callbk) == 1:
|
||||||
|
tcparams.setdefault(key, []).append((config, False))
|
||||||
|
else:
|
||||||
# callbk is something link (opt, True|False)
|
# callbk is something link (opt, True|False)
|
||||||
opt, force_permissive = callbk
|
opt, force_permissive = callbk
|
||||||
path = config.cfgimpl_get_description().impl_get_path_by_opt(
|
path = config.cfgimpl_get_description(
|
||||||
opt)
|
).impl_get_path_by_opt(opt)
|
||||||
# get value
|
# get value
|
||||||
try:
|
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
|
# convert to list, not modifie this multi
|
||||||
if value.__class__.__name__ == 'Multi':
|
if value.__class__.__name__ == 'Multi':
|
||||||
value = list(value)
|
value = list(value)
|
||||||
except PropertiesOptionError as err:
|
except PropertiesOptionError as err:
|
||||||
if force_permissive:
|
if force_permissive:
|
||||||
continue
|
continue
|
||||||
raise ConfigError(_('unable to carry out a calculation, '
|
raise ConfigError(_('unable to carry out a calculation'
|
||||||
'option {0} has properties: {1} for: '
|
', option {0} has properties: {1} '
|
||||||
'{2}').format(opt._name,
|
'for: {2}').format(opt._name,
|
||||||
err.proptype,
|
err.proptype,
|
||||||
option._name))
|
option._name))
|
||||||
|
|
||||||
|
@ -172,7 +176,8 @@ def carry_out_calculation(option, config, callback, callback_params,
|
||||||
is_multi = True
|
is_multi = True
|
||||||
#opt is slave, search if option is an other slaves
|
#opt is slave, search if option is an other slaves
|
||||||
elif opt.impl_get_multitype() == multitypes.slave:
|
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
|
is_multi = True
|
||||||
if is_multi:
|
if is_multi:
|
||||||
len_multi = len(value)
|
len_multi = len(value)
|
||||||
|
|
|
@ -1528,6 +1528,15 @@ def validate_callback(callback, callback_params, type_):
|
||||||
).format(type_, key))
|
).format(type_, key))
|
||||||
for callbk in callbacks:
|
for callbk in callbacks:
|
||||||
if isinstance(callbk, tuple):
|
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
|
option, force_permissive = callbk
|
||||||
if type_ == 'validator' and not force_permissive:
|
if type_ == 'validator' and not force_permissive:
|
||||||
raise ValueError(_('validator not support tuple'))
|
raise ValueError(_('validator not support tuple'))
|
||||||
|
|
Loading…
Reference in New Issue