add option in ParamInformation

This commit is contained in:
egarette@silique.fr 2023-06-19 17:26:28 +02:00
parent 50f0f67629
commit ad31fc85bb
2 changed files with 86 additions and 49 deletions

View file

@ -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')

View file

@ -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,31 +263,12 @@ 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,
new_value = get_value(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,
properties,
)
if apply_index is None and is_follower:
new_value[index] = 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]
else:
return 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,17 +494,12 @@ def manager_callback(callback: Callable,
else:
index_ = None
with_index = False
path = callbk_option.impl_getpath()
option_bag = get_option_bag(config_bag,
value = get_value(config_bag,
callbk_option,
param,
index_,
False,
)
value = get_value(param,
option_bag,
path,
)
if with_index:
value = value[index]
if values is not None: