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() # 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): def test_callback_value_tuple(config_type):
val1 = StrOption('val1', "", 'val1') val1 = StrOption('val1', "", 'val1')
val2 = StrOption('val2', "", 'val2') val2 = StrOption('val2', "", 'val2')

View file

@ -117,13 +117,25 @@ class ParamValue(Param):
class ParamInformation(Param): class ParamInformation(Param):
__slots__ = ('information_name',) __slots__ = ('information_name',
'default_value',
'option',
)
def __init__(self, def __init__(self,
information_name: str, information_name: str,
default_value: Any=undefined, default_value: Any=undefined,
option: 'Option'=None
) -> None: ) -> None:
self.information_name = information_name self.information_name = information_name
self.default_value = default_value 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): class ParamSelfInformation(ParamInformation):
@ -143,12 +155,14 @@ class Calculation:
'params', 'params',
'help_function', 'help_function',
'_has_index', '_has_index',
'warnings_only') 'warnings_only',
)
def __init__(self, def __init__(self,
function: Callable, function: Callable,
params: Params=Params(), params: Params=Params(),
help_function: Optional[Callable]=None, 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) assert isinstance(function, Callable), _('first argument ({0}) must be a function').format(function)
if help_function: if help_function:
assert isinstance(help_function, Callable), _('help_function ({0}) must be a function').format(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, properties = config_bag.context.get_settings().getproperties(option_bag,
uncalculated=True, uncalculated=True,
) )
parent_option_bag, option_bag = get_option_bag(config_bag, new_value = get_value(config_bag,
option, option,
param, param,
apply_index, apply_index,
True, True,
properties=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,
)
if apply_index is None and is_follower: if apply_index is None and is_follower:
new_value[index] = value new_value[index] = value
value = new_value value = new_value
@ -282,10 +277,42 @@ def manager_callback(callback: Callable,
value = value[apply_index] value = value[apply_index]
return value return value
def get_value(param, def get_value(config_bag,
option_bag, option,
path, 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: try:
# get value # get value
value = config_bag.context.get_value(option_bag) value = config_bag.context.get_value(option_bag)
@ -351,14 +378,11 @@ def manager_callback(callback: Callable,
config_bag.context.get_settings(), config_bag.context.get_settings(),
) )
raise ConfigError(_(f'unable to get value for calculating "{option.impl_get_display_name()}", {err}')) from err 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:
if len(options_bag) > 1: parent_option_bag = options_bag[-2]
parent_option_bag = options_bag[-2]
else:
parent_option_bag = None
return parent_option_bag, options_bag[-1]
else: else:
return options_bag[-1] parent_option_bag = None
return parent_option_bag, options_bag[-1]
if isinstance(param, ParamValue): if isinstance(param, ParamValue):
return param.value return param.value
@ -369,6 +393,11 @@ def manager_callback(callback: Callable,
index, index,
config_bag, config_bag,
) )
elif param.option:
option_bag = OptionBag(param.option,
None,
config_bag,
)
else: else:
option_bag = None option_bag = None
try: try:
@ -465,16 +494,11 @@ def manager_callback(callback: Callable,
else: else:
index_ = None index_ = None
with_index = False with_index = False
path = callbk_option.impl_getpath() value = get_value(config_bag,
option_bag = get_option_bag(config_bag, callbk_option,
callbk_option, param,
param, index_,
index_, False,
False,
)
value = get_value(param,
option_bag,
path,
) )
if with_index: if with_index:
value = value[index] value = value[index]