comment tiramisu/autolib.py + some modification

This commit is contained in:
Emmanuel Garette 2013-09-23 22:40:10 +02:00
parent 26e3651848
commit 3fc89be40e
2 changed files with 70 additions and 33 deletions

View File

@ -28,6 +28,12 @@ def return_value(value=None):
return value return value
def return_value2(*args, **kwargs):
value = list(args)
value.extend(kwargs.values())
return value
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)
@ -675,3 +681,13 @@ def test_callback_multi_multi():
raises(ConfigError, "cfg.val4") raises(ConfigError, "cfg.val4")
assert cfg.val5 == ['val1', 'val4', 'val2', 'val4', 'val3', 'val4'] assert cfg.val5 == ['val1', 'val4', 'val2', 'val4', 'val3', 'val4']
assert cfg.val7 == ['val1', 'val21', 'val2', 'val22', 'val3', 'val23'] assert cfg.val7 == ['val1', 'val21', 'val2', 'val22', 'val3', 'val23']
def test_multi_with_no_value():
#First option return [] (so without value)
val1 = StrOption('val1', "", ['val'], multi=True)
val2 = StrOption('val2', "", multi=True)
val3 = StrOption('val3', '', multi=True, callback=return_value, callback_params={'': ((val2, False),), 'value': ((val1, False),)})
od = OptionDescription('od', '', [val1, val2, val3])
c = Config(od)
raises(ConfigError, "c.val3")

View File

@ -41,6 +41,13 @@ def carry_out_calculation(name, config, callback, callback_params,
:param max_len: max length for a multi :param max_len: max length for a multi
:type max_len: int :type max_len: int
The callback_params is a dict. Key is used to build args (if key is '')
and kwargs (otherwise). Values are tuple of:
- values
- tuple with option and boolean's force_permissive (True when don't raise
if PropertiesOptionError)
Values could have multiple values only when key is ''.
* if no callback_params: * if no callback_params:
=> calculate() => calculate()
@ -92,7 +99,6 @@ def carry_out_calculation(name, config, callback, callback_params,
- a multi option with an other multi option but with same length - a multi option with an other multi option but with same length
opt1 == [1, 2, 3] opt1 == [1, 2, 3]
opt2 == [11, 12, 13] opt2 == [11, 12, 13]
callback_params={'': ((opt1, False), (opt2, False))}
=> calculate(1, 11) => calculate(1, 11)
=> calculate(2, 12) => calculate(2, 12)
=> calculate(3, 13) => calculate(3, 13)
@ -100,9 +106,13 @@ def carry_out_calculation(name, config, callback, callback_params,
- a multi option with an other multi option but with different length - a multi option with an other multi option but with different length
opt1 == [1, 2, 3] opt1 == [1, 2, 3]
opt2 == [11, 12] opt2 == [11, 12]
callback_params={'': ((opt1, False), (opt2, False))}
=> ConfigError() => ConfigError()
- a multi option without value with a simple option
opt1 == []
opt2 == 11
=> []
* if callback_params={'value': ((opt1, False), (opt2, False))} * if callback_params={'value': ((opt1, False), (opt2, False))}
=> ConfigError() => ConfigError()
@ -114,39 +124,47 @@ def carry_out_calculation(name, config, callback, callback_params,
If calculate return list, this list is extend to return value. If calculate return list, this list is extend to return value.
""" """
tcparams = {} tcparams = {}
# if callback_params has a callback, launch several time calculate()
one_is_multi = False one_is_multi = False
len_multi = 0 # multi's option should have same value for all option
len_multi = None
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):
# callbk is something link (opt, True|False)
option, force_permissive = callbk option, force_permissive = callbk
path = config.cfgimpl_get_description().impl_get_path_by_opt(
option)
# get value # get value
try: try:
path = config.cfgimpl_get_description().impl_get_path_by_opt(option)
value = config._getattr(path, force_permissive=True) value = config._getattr(path, force_permissive=True)
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} for: '
'{2}').format(option._name, err.proptype, '{2}').format(option._name,
err.proptype,
name)) name))
is_multi = option.impl_is_multi() is_multi = option.impl_is_multi()
if is_multi: if is_multi:
if value is not None: len_value = len(value)
len_value = len(value) if len_multi is not None and len_multi != len_value:
if len_multi != 0 and len_multi != len_value: raise ConfigError(_('unable to carry out a '
raise ConfigError(_('unable to carry out a ' 'calculation, option value with'
'calculation, option value with' ' multi types must have same '
' multi types must have same ' 'length for: {0}').format(name))
'length for: {0}').format(name)) len_multi = len_value
len_multi = len_value
one_is_multi = True one_is_multi = True
tcparams.setdefault(key, []).append((value, is_multi)) tcparams.setdefault(key, []).append((value, is_multi))
else: else:
# callbk is a value and not a multi
tcparams.setdefault(key, []).append((callbk, False)) tcparams.setdefault(key, []).append((callbk, False))
# if one value is a multi, launch several time calculate
# if index is set, return a value
# if no index, return a list
if one_is_multi: if one_is_multi:
ret = [] ret = []
if index: if index:
@ -161,19 +179,20 @@ def carry_out_calculation(name, config, callback, callback_params,
else: else:
range_ = range(len_multi) range_ = range(len_multi)
for incr in range_: for incr in range_:
tcp = {} args = []
params = [] kwargs = {}
for key, couples in tcparams.items(): for key, couples in tcparams.items():
for couple in couples: for couple in couples:
value, ismulti = couple value, ismulti = couple
if ismulti and value is not None: if ismulti:
if key == '': val = value[incr]
params.append(value[incr])
else:
tcp[key] = value[incr]
else: else:
params.append(value) val = value
calc = calculate(name, callback, params, tcp) if key == '':
args.append(val)
else:
kwargs[key] = val
calc = calculate(callback, args, kwargs)
if index: if index:
ret = calc ret = calc
else: else:
@ -183,24 +202,26 @@ def carry_out_calculation(name, config, callback, callback_params,
ret.append(calc) ret.append(calc)
return ret return ret
else: else:
tcp = {} # no value is multi
params = [] # return a single value
args = []
kwargs = {}
for key, couples in tcparams.items(): for key, couples in tcparams.items():
for couple in couples: for couple in couples:
# couple[1] (ismulti) is always False
if key == '': if key == '':
value = couple[0] args.append(couple[0])
params.append(value)
else: else:
tcp[key] = couple[0] kwargs[key] = couple[0]
return calculate(name, callback, params, tcp) return calculate(callback, args, kwargs)
def calculate(name, callback, params, tcparams): def calculate(callback, args, kwargs):
"""wrapper that launches the 'callback' """wrapper that launches the 'callback'
:param callback: callback name :param callback: callback function
:param params: in the callback's arity, the unnamed parameters :param args: in the callback's arity, the unnamed parameters
:param tcparams: in the callback's arity, the named parameters :param kwargs: in the callback's arity, the named parameters
""" """
return callback(*params, **tcparams) return callback(*args, **kwargs)