corrections in has_index

This commit is contained in:
Emmanuel Garette 2019-11-20 08:28:26 +01:00
parent 13c0c0e256
commit 1bcad4d7ed
1 changed files with 29 additions and 29 deletions

View File

@ -21,7 +21,7 @@
from typing import Any, Optional, Union, Callable, Dict, List from typing import Any, Optional, Union, Callable, Dict, List
from itertools import chain from itertools import chain
from .error import PropertiesOptionError, ConfigError, LeadershipError from .error import PropertiesOptionError, ConfigError, LeadershipError, ValueWarning
from .i18n import _ from .i18n import _
from .setting import undefined, ConfigBag, OptionBag, Undefined from .setting import undefined, ConfigBag, OptionBag, Undefined
from .storage import get_default_values_storages, get_default_settings_storages from .storage import get_default_values_storages, get_default_settings_storages
@ -60,7 +60,6 @@ class Param:
class ParamOption(Param): class ParamOption(Param):
__slots__ = ('todict', __slots__ = ('todict',
'error',
'option', 'option',
'notraisepropertyerror', 'notraisepropertyerror',
'raisepropertyerror') 'raisepropertyerror')
@ -116,7 +115,7 @@ class Calculation:
__slots__ = ('function', __slots__ = ('function',
'params', 'params',
'help_function', 'help_function',
'has_index', '_has_index',
'warnings_only') 'warnings_only')
def __init__(self, def __init__(self,
function: Callable, function: Callable,
@ -133,10 +132,8 @@ class Calculation:
self.params = params self.params = params
for arg in chain(self.params.args, self.params.kwargs.values()): for arg in chain(self.params.args, self.params.kwargs.values()):
if isinstance(arg, ParamIndex): if isinstance(arg, ParamIndex):
self.has_index = True self._has_index = True
break break
else:
self.has_index = False
if warnings_only is True: if warnings_only is True:
self.warnings_only = warnings_only self.warnings_only = warnings_only
@ -167,6 +164,17 @@ class Calculation:
config_bag=option_bag.config_bag, config_bag=option_bag.config_bag,
leadership_must_have_index=leadership_must_have_index) 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): class Break(Exception):
pass pass
@ -326,10 +334,12 @@ def carry_out_calculation(option,
- tuple with option and boolean's force_permissive (True when don't raise - tuple with option and boolean's force_permissive (True when don't raise
if PropertiesOptionError) if PropertiesOptionError)
Values could have multiple values only when key is ''.""" Values could have multiple values only when key is ''."""
def fake_items(iterator):
return ((None, i) for i in iterator)
args = [] args = []
kwargs = {} kwargs = {}
if callback_params: if callback_params:
for callbk in callback_params.args: for key, callbk in chain(fake_items(callback_params.args), callback_params.kwargs.items()):
try: try:
value = manager_callback(callbk, value = manager_callback(callbk,
option, option,
@ -339,29 +349,17 @@ def carry_out_calculation(option,
leadership_must_have_index) leadership_must_have_index)
if value is undefined: if value is undefined:
return undefined return undefined
if key is None:
args.append(value) args.append(value)
except PropertiesOptionError as err: else:
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 kwargs[key] = value
except PropertiesOptionError as err: except PropertiesOptionError as err:
if callbk.raisepropertyerror: if callbk.raisepropertyerror:
raise err raise err
if callbk.todict: if callbk.todict:
if key is None:
args.append({'propertyerror': str(err)})
else:
kwargs[key] = {'propertyerror': str(err)} kwargs[key] = {'propertyerror': str(err)}
except Break: except Break:
continue continue
@ -408,6 +406,8 @@ def calculate(option,
if allow_raises: if allow_raises:
raise err raise err
error = err error = err
except ValueWarning as err:
raise err
except Exception as err: except Exception as err:
# import traceback # import traceback
# traceback.print_exc() # traceback.print_exc()