feat: use Calculation

This commit is contained in:
egarette@silique.fr 2025-12-22 08:53:11 +01:00
parent a86a8bdc28
commit 89112ba2e1

View file

@ -25,7 +25,7 @@ from os import environ
from shutil import which
from tiramisu.error import display_list
from tiramisu.error import ConfigError, display_list
from rougail.error import ExtensionError
from .i18n import _
@ -179,7 +179,7 @@ class RougailUserDataBitwarden:
values[option.path(uncalculated=True)] = (set_password, self.leader_informations, self.command)
def set_password(leader_informations, command, *, option, warnings, errors):
def set_password(leader_informations, command, *, option):
path = option.path()
leader_key = None
if option.isfollower():
@ -187,92 +187,78 @@ def set_password(leader_informations, command, *, option, warnings, errors):
if leader_path in leader_informations:
leader_key = leader_informations[leader_path][option.index()]
if not option.isleader():
return get_values(command, option, warnings, errors, leader_key=leader_key)
return get_values(command, option, leader_key=leader_key)
leader_informations[path], option_values = get_values(
command, option, warnings, errors, multiple=True, leader_key=option.value.default()[0]
command, option, multiple=True, leader_key=option.value.default()[0]
)
return option_values
def get_values(command, option, warnings, errors, *, multiple=False, leader_key=None):
def get_values(command, option, *, multiple=False, leader_key=None):
if leader_key:
key = leader_key
else:
key = option.value.default()
path = option.path()
type_ = option.information.get("type")
if "validator" not in option.property.get():
option.property.add("validator")
if not isinstance(key, str):
errors.append(
_('the default value for "{0}" must be the Bitwarden item name').format(
path
raise ConfigError(
_('the default value must be the Bitwarden item name')
)
)
if multiple:
return [], []
return None
try:
data = command.get_key(key, multiple)
except Exception as exc:
errors.append(
raise ConfigError(
_(
'cannot execute the "{0}" commandline from Bitwarden for "{1}": {2}'
).format(command, path, exc)
'cannot execute the "{0}" commandline from Bitwarden: {1}'
).format(command, exc)
)
if multiple:
return [], []
return None
if not data:
errors.append(
_('item "{0}" in Bitwarden is not found for "{1}"').format(
key, path
raise ConfigError(
_('item "{0}" in Bitwarden is not found"').format(
key
)
)
if multiple:
return [], []
return None
if len(data) != 1:
names = [d["name"] for d in data]
if multiple:
ret = []
return names, [
get_value(key, path, type_, d, errors) for d in data
get_value(key, type_, d) for d in data
]
errors.append(
raise ConfigError(
_(
'several items found with name "{0}" in Bitwarden for "{1}": "{2}"'
).format(key, path, '", "'.join(names))
'several items found with name "{0}" in Bitwarden: "{1}"'
).format(key, '", "'.join(names))
)
return None
value = get_value(key, path, type_, data[0], errors)
value = get_value(key, type_, data[0])
if multiple:
return [data[0]["name"]], [value]
return value
def get_value( key_bitwarden: str, path: str, type_: str, data: dict, errors: dict) -> str:
def get_value( key_bitwarden: str, type_: str, data: dict) -> str:
try:
if type_ == "secret":
value = data["login"]["password"]
else:
value = data["login"]["username"]
except Exception as exc:
errors.append(
_('unexpected datas "{0}" from Bitwarden for "{1}": {2}').format(
key_bitwarden, path, exc
raise ConfigError(
_('unexpected datas "{0}" from Bitwarden: {2}').format(
key_bitwarden, exc
)
)
value = None
else:
if value is None:
if type_ == "secret":
bw_type = _("password")
else:
bw_type = _("username")
errors.append(
_('item "{0}" in Bitwarden has no {1} for "{2}"').format(
key_bitwarden, bw_type, path
raise ConfigError(
_('item "{0}" in Bitwarden has no {1}').format(
key_bitwarden, bw_type
)
)
return value