diff --git a/tiramisu/autolib.py b/tiramisu/autolib.py index c5f7fd3..f22f208 100644 --- a/tiramisu/autolib.py +++ b/tiramisu/autolib.py @@ -21,7 +21,7 @@ from typing import Any, Optional, Union, Callable, Dict, List from itertools import chain -from .error import PropertiesOptionError, ConfigError, LeadershipError +from .error import PropertiesOptionError, ConfigError, LeadershipError, ValueWarning from .i18n import _ from .setting import undefined, ConfigBag, OptionBag, Undefined from .storage import get_default_values_storages, get_default_settings_storages @@ -60,7 +60,6 @@ class Param: class ParamOption(Param): __slots__ = ('todict', - 'error', 'option', 'notraisepropertyerror', 'raisepropertyerror') @@ -116,7 +115,7 @@ class Calculation: __slots__ = ('function', 'params', 'help_function', - 'has_index', + '_has_index', 'warnings_only') def __init__(self, function: Callable, @@ -133,10 +132,8 @@ class Calculation: self.params = params for arg in chain(self.params.args, self.params.kwargs.values()): if isinstance(arg, ParamIndex): - self.has_index = True + self._has_index = True break - else: - self.has_index = False if warnings_only is True: self.warnings_only = warnings_only @@ -167,6 +164,17 @@ class Calculation: config_bag=option_bag.config_bag, leadership_must_have_index=leadership_must_have_index) + def has_index(self, current_option): + if hasattr(self, '_has_index'): + return self._has_index + self._has_index = False + for arg in chain(self.params.args, self.params.kwargs.values()): + if isinstance(arg, ParamOption) and arg.option.impl_get_leadership() and \ + arg.option.impl_get_leadership().in_same_group(current_option): + self._has_index = True + break + return self._has_index + class Break(Exception): pass @@ -326,10 +334,12 @@ def carry_out_calculation(option, - tuple with option and boolean's force_permissive (True when don't raise if PropertiesOptionError) Values could have multiple values only when key is ''.""" + def fake_items(iterator): + return ((None, i) for i in iterator) args = [] kwargs = {} if callback_params: - for callbk in callback_params.args: + for key, callbk in chain(fake_items(callback_params.args), callback_params.kwargs.items()): try: value = manager_callback(callbk, option, @@ -339,30 +349,18 @@ def carry_out_calculation(option, leadership_must_have_index) if value is undefined: return undefined - args.append(value) + if key is None: + args.append(value) + else: + kwargs[key] = value except PropertiesOptionError as err: if callbk.raisepropertyerror: raise err if callbk.todict: - args.append({'propertyerror': str(err)}) - except Break: - continue - for key, callbk in callback_params.kwargs.items(): - try: - value = manager_callback(callbk, - option, - index, - orig_value, - config_bag, - leadership_must_have_index) - if value is undefined: - return undefined - kwargs[key] = value - except PropertiesOptionError as err: - if callbk.raisepropertyerror: - raise err - if callbk.todict: - kwargs[key] = {'propertyerror': str(err)} + if key is None: + args.append({'propertyerror': str(err)}) + else: + kwargs[key] = {'propertyerror': str(err)} except Break: continue ret = calculate(option, @@ -408,9 +406,11 @@ def calculate(option, if allow_raises: raise err error = err + except ValueWarning as err: + raise err except Exception as err: - #import traceback - #traceback.print_exc() + # import traceback + # traceback.print_exc() error = err if args or kwargs: msg = _('unexpected error "{0}" in function "{1}" with arguments "{3}" and "{4}" '