From 127dba8c5296364fa257f432893b27c882a51eaa Mon Sep 17 00:00:00 2001 From: Emmanuel Garette Date: Sun, 20 Mar 2022 21:17:24 +0100 Subject: [PATCH] add set_linked_multi_variables function --- bootstrap.py | 65 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) diff --git a/bootstrap.py b/bootstrap.py index 689814d..4eb9937 100755 --- a/bootstrap.py +++ b/bootstrap.py @@ -37,6 +37,70 @@ with open('servers.json', 'r') as server_fh: MODULES = jsonfile['modules'] +async def set_linked_multi_variables(linked_server: str, + **kwargs: dict, + ) -> None: + + if linked_server is None: + return + if linked_server not in CONFIGS: + warn_explicit(ValueWarning(f'cannot find linked server "{linked_server}"'), + ValueWarning, + __file__, + 0, + ) + return + config = CONFIGS[linked_server][0] + dico = {} + variables = {} + for key, value in kwargs.items(): + if value is None: + return + if key.startswith('linked_provider_'): + index = int(key[16]) + path = await config.information.get('provider:' + value, None) + if not path: + return + if index not in variables: + variables[index] = {'path': None, 'value': None} + variables[index]['path'] = path + elif key.startswith('linked_value_'): + index = int(key[13]) + if index not in variables: + variables[index] = {'path': None, 'value': None} + variables[index]['value'] = value + else: + raise AttributeError(f'unknown parameter {key}') + dynamic = None + slave_idx = None + await config.property.read_write() + try: + for index in sorted(list(variables)): + path = variables[index]['path'] + value = variables[index]['value'] + if dynamic: + path = path.replace('{suffix}', dynamic) + else: + dynamic = normalize_family(value) + option = config.forcepermissive.option(path, slave_idx) + multi = await option.option.ismulti() + if multi and await option.option.isfollower(): + multi = await option.option.issubmulti() + if multi: + values = await option.value.get() + if value not in values: + values.append(value) + await option.value.set(values) + if await option.option.isleader(): + slave_idx = values.index(value) + else: + await option.value.set(value) + except Exception as err: + await config.property.read_only() + raise err from err + await config.property.read_only() + + async def set_linked(linked_server: str, linked_provider: str, linked_value: str, @@ -335,6 +399,7 @@ async def build(server_name, datas, module_infos): cfg['extra_dictionaries'] = module_info.extra_dictionaries cfg['extra_annotators'].append('risotto.rougail') optiondescription = {'set_linked': set_linked, + 'set_linked_multi_variables': set_linked_multi_variables, 'get_linked_configuration': get_linked_configuration, 'set_linked_configuration': set_linked_configuration, }