Merge pull request 'add set_linked_multi_variables function' (#5) from gnunux/risotto:main into main

Reviewed-on: https://cloud.silique.fr/gitea/risotto/risotto/pulls/5
This commit is contained in:
gnunux@silique.fr 2022-03-20 20:18:10 +00:00
commit eccc9a008d

View file

@ -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,
}