From ad31fc85bb46b346f0c0bbf9a53e6bdf1e59a315 Mon Sep 17 00:00:00 2001 From: Emmanuel Garette Date: Mon, 19 Jun 2023 17:26:28 +0200 Subject: [PATCH] add option in ParamInformation --- tests/test_option_callback.py | 13 ++++ tiramisu/autolib.py | 122 ++++++++++++++++++++-------------- 2 files changed, 86 insertions(+), 49 deletions(-) diff --git a/tests/test_option_callback.py b/tests/test_option_callback.py index b0ba4fc..31189e7 100644 --- a/tests/test_option_callback.py +++ b/tests/test_option_callback.py @@ -414,6 +414,19 @@ def test_callback_information2(config_type): # assert not list_sessions() +def test_callback_information3(config_type): + val1 = StrOption('val1', "") + val2 = StrOption('val2', "", Calculation(return_value, Params(ParamInformation('information', option=val1)))) + val1.impl_set_information('information', 'new_value') + od1 = OptionDescription('rootconfig', '', [val1, val2]) + cfg = Config(od1) + cfg.property.read_write() + cfg = get_config(cfg, config_type) + assert cfg.option('val2').value.get() == 'new_value' + #FIXME cache: cfg.option('val1').information.set('information', 'new_value2') + #assert cfg.option('val2').value.get() == 'new_value2' + + def test_callback_value_tuple(config_type): val1 = StrOption('val1', "", 'val1') val2 = StrOption('val2', "", 'val2') diff --git a/tiramisu/autolib.py b/tiramisu/autolib.py index 3ac882b..b9a05cd 100644 --- a/tiramisu/autolib.py +++ b/tiramisu/autolib.py @@ -117,13 +117,25 @@ class ParamValue(Param): class ParamInformation(Param): - __slots__ = ('information_name',) + __slots__ = ('information_name', + 'default_value', + 'option', + ) def __init__(self, information_name: str, default_value: Any=undefined, + option: 'Option'=None ) -> None: self.information_name = information_name self.default_value = default_value + if option: + if option.impl_is_symlinkoption(): + raise ValueError(_('option in ParamInformation cannot be a symlinkoption')) + if option.impl_is_follower(): + raise ValueError(_('option in ParamInformation cannot be a follower')) + if option.impl_is_dynsymlinkoption(): + raise ValueError(_('option in ParamInformation cannot be a dynamic option')) + self.option = option class ParamSelfInformation(ParamInformation): @@ -143,12 +155,14 @@ class Calculation: 'params', 'help_function', '_has_index', - 'warnings_only') + 'warnings_only', + ) def __init__(self, function: Callable, params: Params=Params(), help_function: Optional[Callable]=None, - warnings_only: bool=False): + warnings_only: bool=False, + ): assert isinstance(function, Callable), _('first argument ({0}) must be a function').format(function) if help_function: assert isinstance(help_function, Callable), _('help_function ({0}) must be a function').format(help_function) @@ -249,32 +263,13 @@ def manager_callback(callback: Callable, properties = config_bag.context.get_settings().getproperties(option_bag, uncalculated=True, ) - parent_option_bag, option_bag = get_option_bag(config_bag, - option, - param, - apply_index, - True, - properties=properties, - ) - if option.impl_is_follower() and apply_index is None: - new_value = [] - for idx in range(config_bag.context.get_length_leadership(parent_option_bag)): - parent_option_bag, option_bag = get_option_bag(config_bag, - option, - param, - idx, - True, - properties=properties, - ) - new_value.append(get_value(param, - option_bag, - path, - )) - else: - new_value = get_value(param, - option_bag, - path, - ) + new_value = get_value(config_bag, + option, + param, + apply_index, + True, + properties, + ) if apply_index is None and is_follower: new_value[index] = value value = new_value @@ -282,10 +277,42 @@ def manager_callback(callback: Callable, value = value[apply_index] return value - def get_value(param, - option_bag, - path, + def get_value(config_bag, + option, + param, + index, + self_calc, + properties=undefined, ): + parent_option_bag, option_bag = get_option_bag(config_bag, + option, + param, + index, + self_calc, + properties=properties, + ) + if option.impl_is_follower() and index is None: + value = [] + for idx in range(config_bag.context.get_length_leadership(parent_option_bag)): + parent_option_bag, option_bag = get_option_bag(config_bag, + option, + param, + idx, + self_calc, + properties=properties, + ) + value.append(_get_value(param, + option_bag, + )) + else: + value = _get_value(param, + option_bag, + ) + return value + + def _get_value(param: Params, + option_bag: OptionBag, + ) -> Any: try: # get value value = config_bag.context.get_value(option_bag) @@ -351,14 +378,11 @@ def manager_callback(callback: Callable, config_bag.context.get_settings(), ) raise ConfigError(_(f'unable to get value for calculating "{option.impl_get_display_name()}", {err}')) from err - if self_calc: - if len(options_bag) > 1: - parent_option_bag = options_bag[-2] - else: - parent_option_bag = None - return parent_option_bag, options_bag[-1] + if len(options_bag) > 1: + parent_option_bag = options_bag[-2] else: - return options_bag[-1] + parent_option_bag = None + return parent_option_bag, options_bag[-1] if isinstance(param, ParamValue): return param.value @@ -369,6 +393,11 @@ def manager_callback(callback: Callable, index, config_bag, ) + elif param.option: + option_bag = OptionBag(param.option, + None, + config_bag, + ) else: option_bag = None try: @@ -465,16 +494,11 @@ def manager_callback(callback: Callable, else: index_ = None with_index = False - path = callbk_option.impl_getpath() - option_bag = get_option_bag(config_bag, - callbk_option, - param, - index_, - False, - ) - value = get_value(param, - option_bag, - path, + value = get_value(config_bag, + callbk_option, + param, + index_, + False, ) if with_index: value = value[index]