corrections in has_index

This commit is contained in:
Emmanuel Garette 2019-11-20 08:28:26 +01:00
parent 13c0c0e256
commit 1bcad4d7ed

View file

@ -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,29 +349,17 @@ def carry_out_calculation(option,
leadership_must_have_index)
if value is undefined:
return undefined
if key is None:
args.append(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
else:
kwargs[key] = value
except PropertiesOptionError as err:
if callbk.raisepropertyerror:
raise err
if callbk.todict:
if key is None:
args.append({'propertyerror': str(err)})
else:
kwargs[key] = {'propertyerror': str(err)}
except Break:
continue
@ -408,6 +406,8 @@ 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()