rename Option.name to Option.id

This commit is contained in:
axolotle 2023-04-13 15:54:56 +02:00
parent e87f8ef93a
commit c1f0ac04c7
4 changed files with 45 additions and 45 deletions

View file

@ -1099,7 +1099,7 @@ def app_install(
raw_questions = manifest["install"] raw_questions = manifest["install"]
questions = ask_questions_and_parse_answers(raw_questions, prefilled_answers=args) questions = ask_questions_and_parse_answers(raw_questions, prefilled_answers=args)
args = { args = {
question.name: question.value question.id: question.value
for question in questions for question in questions
if question.value is not None if question.value is not None
} }
@ -1147,7 +1147,7 @@ def app_install(
if question.type == "password": if question.type == "password":
continue continue
app_settings[question.name] = question.value app_settings[question.id] = question.value
_set_app_settings(app_instance_name, app_settings) _set_app_settings(app_instance_name, app_settings)
@ -1202,16 +1202,16 @@ def app_install(
# Reinject user-provider passwords which are not in the app settings # Reinject user-provider passwords which are not in the app settings
# (cf a few line before) # (cf a few line before)
if question.type == "password": if question.type == "password":
env_dict[question.name] = question.value env_dict[question.id] = question.value
# We want to hav the env_dict in the log ... but not password values # We want to hav the env_dict in the log ... but not password values
env_dict_for_logging = env_dict.copy() env_dict_for_logging = env_dict.copy()
for question in questions: for question in questions:
# Or should it be more generally question.redact ? # Or should it be more generally question.redact ?
if question.type == "password": if question.type == "password":
del env_dict_for_logging[f"YNH_APP_ARG_{question.name.upper()}"] del env_dict_for_logging[f"YNH_APP_ARG_{question.id.upper()}"]
if question.name in env_dict_for_logging: if question.id in env_dict_for_logging:
del env_dict_for_logging[question.name] del env_dict_for_logging[question.id]
operation_logger.extra.update({"env": env_dict_for_logging}) operation_logger.extra.update({"env": env_dict_for_logging})
@ -2358,17 +2358,17 @@ def _set_default_ask_questions(questions, script_name="install"):
), # i18n: app_manifest_install_ask_init_admin_permission ), # i18n: app_manifest_install_ask_init_admin_permission
] ]
for question_name, question in questions.items(): for question_id, question in questions.items():
question["name"] = question_name question["id"] = question_id
# If this question corresponds to a question with default ask message... # If this question corresponds to a question with default ask message...
if any( if any(
(question.get("type"), question["name"]) == question_with_default (question.get("type"), question["id"]) == question_with_default
for question_with_default in questions_with_default for question_with_default in questions_with_default
): ):
# The key is for example "app_manifest_install_ask_domain" # The key is for example "app_manifest_install_ask_domain"
question["ask"] = m18n.n( question["ask"] = m18n.n(
f"app_manifest_{script_name}_ask_{question['name']}" f"app_manifest_{script_name}_ask_{question['id']}"
) )
# Also it in fact doesn't make sense for any of those questions to have an example value nor a default value... # Also it in fact doesn't make sense for any of those questions to have an example value nor a default value...

View file

@ -33,7 +33,7 @@ from yunohost.utils.error import YunohostError, YunohostValidationError
""" """
Argument default format: Argument default format:
{ {
"the_name": { "the_id": {
"type": "one_of_the_available_type", // "sting" is not specified "type": "one_of_the_available_type", // "sting" is not specified
"ask": { "ask": {
"en": "the question in english", "en": "the question in english",
@ -50,7 +50,7 @@ Argument default format:
} }
User answers: User answers:
{"the_name": "value", ...} {"the_id": "value", ...}
""" """
@ -443,7 +443,7 @@ class BaseTest:
assert isinstance(option, OPTIONS[raw_option["type"]]) assert isinstance(option, OPTIONS[raw_option["type"]])
assert option.type == raw_option["type"] assert option.type == raw_option["type"]
assert option.name == id_ assert option.id == id_
assert option.ask == {"en": id_} assert option.ask == {"en": id_}
assert option.readonly is (True if is_special_readonly_option else False) assert option.readonly is (True if is_special_readonly_option else False)
assert option.visible is True assert option.visible is True
@ -1913,7 +1913,7 @@ def test_options_query_string():
) )
def _assert_correct_values(options, raw_options): def _assert_correct_values(options, raw_options):
form = {option.name: option.value for option in options} form = {option.id: option.value for option in options}
for k, v in results.items(): for k, v in results.items():
if k == "file_id": if k == "file_id":
@ -1945,7 +1945,7 @@ def test_question_string_default_type():
out = ask_questions_and_parse_answers(questions, answers)[0] out = ask_questions_and_parse_answers(questions, answers)[0]
assert out.name == "some_string" assert out.id == "some_string"
assert out.type == "string" assert out.type == "string"
assert out.value == "some_value" assert out.value == "some_value"

View file

@ -521,7 +521,7 @@ class ConfigPanel:
f"Config panel question '{option['id']}' should be initialized with a value during install or upgrade.", f"Config panel question '{option['id']}' should be initialized with a value during install or upgrade.",
raw_msg=True, raw_msg=True,
) )
value = self.values[option["name"]] value = self.values[option["id"]]
# Allow to use value instead of current_value in app config script. # Allow to use value instead of current_value in app config script.
# e.g. apps may write `echo 'value: "foobar"'` in the config file (which is more intuitive that `echo 'current_value: "foobar"'` # e.g. apps may write `echo 'value: "foobar"'` in the config file (which is more intuitive that `echo 'current_value: "foobar"'`
@ -600,14 +600,14 @@ class ConfigPanel:
prefilled_answers.update(self.new_values) prefilled_answers.update(self.new_values)
questions = ask_questions_and_parse_answers( questions = ask_questions_and_parse_answers(
{question["name"]: question for question in section["options"]}, {question["id"]: question for question in section["options"]},
prefilled_answers=prefilled_answers, prefilled_answers=prefilled_answers,
current_values=self.values, current_values=self.values,
hooks=self.hooks, hooks=self.hooks,
) )
self.new_values.update( self.new_values.update(
{ {
question.name: question.value question.id: question.value
for question in questions for question in questions
if question.value is not None if question.value is not None
} }

View file

@ -245,7 +245,7 @@ class BaseOption:
self, self,
question: Dict[str, Any], question: Dict[str, Any],
): ):
self.name = question["name"] self.id = question["id"]
self.type = question.get("type", OptionType.string) self.type = question.get("type", OptionType.string)
self.visible = question.get("visible", True) self.visible = question.get("visible", True)
@ -255,10 +255,10 @@ class BaseOption:
raise YunohostError( raise YunohostError(
"config_forbidden_readonly_type", "config_forbidden_readonly_type",
type=self.type, type=self.type,
id=self.name, id=self.id,
) )
self.ask = question.get("ask", self.name) self.ask = question.get("ask", self.id)
if not isinstance(self.ask, dict): if not isinstance(self.ask, dict):
self.ask = {"en": self.ask} self.ask = {"en": self.ask}
@ -379,14 +379,14 @@ class BaseInputOption(BaseOption):
def _value_pre_validator(self): def _value_pre_validator(self):
if self.value in [None, ""] and not self.optional: if self.value in [None, ""] and not self.optional:
raise YunohostValidationError("app_argument_required", name=self.name) raise YunohostValidationError("app_argument_required", name=self.id)
# we have an answer, do some post checks # we have an answer, do some post checks
if self.value not in [None, ""]: if self.value not in [None, ""]:
if self.pattern and not re.match(self.pattern["regexp"], str(self.value)): if self.pattern and not re.match(self.pattern["regexp"], str(self.value)):
raise YunohostValidationError( raise YunohostValidationError(
self.pattern["error"], self.pattern["error"],
name=self.name, name=self.id,
value=self.value, value=self.value,
) )
@ -440,7 +440,7 @@ class PasswordOption(BaseInputOption):
self.redact = True self.redact = True
if self.default is not None: if self.default is not None:
raise YunohostValidationError( raise YunohostValidationError(
"app_argument_password_no_default", name=self.name "app_argument_password_no_default", name=self.id
) )
def _value_pre_validator(self): def _value_pre_validator(self):
@ -496,7 +496,7 @@ class NumberOption(BaseInputOption):
option = option.__dict__ if isinstance(option, BaseOption) else option option = option.__dict__ if isinstance(option, BaseOption) else option
raise YunohostValidationError( raise YunohostValidationError(
"app_argument_invalid", "app_argument_invalid",
name=option.get("name"), name=option.get("id"),
error=m18n.n("invalid_number"), error=m18n.n("invalid_number"),
) )
@ -508,14 +508,14 @@ class NumberOption(BaseInputOption):
if self.min is not None and int(self.value) < self.min: if self.min is not None and int(self.value) < self.min:
raise YunohostValidationError( raise YunohostValidationError(
"app_argument_invalid", "app_argument_invalid",
name=self.name, name=self.id,
error=m18n.n("invalid_number_min", min=self.min), error=m18n.n("invalid_number_min", min=self.min),
) )
if self.max is not None and int(self.value) > self.max: if self.max is not None and int(self.value) > self.max:
raise YunohostValidationError( raise YunohostValidationError(
"app_argument_invalid", "app_argument_invalid",
name=self.name, name=self.id,
error=m18n.n("invalid_number_max", max=self.max), error=m18n.n("invalid_number_max", max=self.max),
) )
@ -554,7 +554,7 @@ class BooleanOption(BaseInputOption):
raise YunohostValidationError( raise YunohostValidationError(
"app_argument_choice_invalid", "app_argument_choice_invalid",
name=option.get("name"), name=option.get("id"),
value=value, value=value,
choices="yes/no", choices="yes/no",
) )
@ -594,7 +594,7 @@ class BooleanOption(BaseInputOption):
raise YunohostValidationError( raise YunohostValidationError(
"app_argument_choice_invalid", "app_argument_choice_invalid",
name=option.get("name"), name=option.get("id"),
value=strvalue, value=strvalue,
choices="yes/no", choices="yes/no",
) )
@ -663,7 +663,7 @@ class WebPathOption(BaseInputOption):
if not isinstance(value, str): if not isinstance(value, str):
raise YunohostValidationError( raise YunohostValidationError(
"app_argument_invalid", "app_argument_invalid",
name=option.get("name"), name=option.get("id"),
error="Argument for path should be a string.", error="Argument for path should be a string.",
) )
@ -676,7 +676,7 @@ class WebPathOption(BaseInputOption):
elif option.get("optional") is False: elif option.get("optional") is False:
raise YunohostValidationError( raise YunohostValidationError(
"app_argument_invalid", "app_argument_invalid",
name=option.get("name"), name=option.get("id"),
error="Option is mandatory", error="Option is mandatory",
) )
@ -725,7 +725,7 @@ class FileOption(BaseInputOption):
): ):
raise YunohostValidationError( raise YunohostValidationError(
"app_argument_invalid", "app_argument_invalid",
name=self.name, name=self.id,
error=m18n.n("file_does_not_exist", path=str(self.value)), error=m18n.n("file_does_not_exist", path=str(self.value)),
) )
@ -740,7 +740,7 @@ class FileOption(BaseInputOption):
FileOption.upload_dirs += [upload_dir] FileOption.upload_dirs += [upload_dir]
logger.debug(f"Saving file {self.name} for file question into {file_path}") logger.debug(f"Saving file {self.id} for file question into {file_path}")
def is_file_path(s): def is_file_path(s):
return isinstance(s, str) and s.startswith("/") and os.path.exists(s) return isinstance(s, str) and s.startswith("/") and os.path.exists(s)
@ -813,7 +813,7 @@ class BaseChoicesOption(BaseInputOption):
if self.choices and self.value not in self.choices: if self.choices and self.value not in self.choices:
raise YunohostValidationError( raise YunohostValidationError(
"app_argument_choice_invalid", "app_argument_choice_invalid",
name=self.name, name=self.id,
value=self.value, value=self.value,
choices=", ".join(str(choice) for choice in self.choices), choices=", ".join(str(choice) for choice in self.choices),
) )
@ -853,13 +853,13 @@ class TagsOption(BaseChoicesOption):
if self.choices: if self.choices:
raise YunohostValidationError( raise YunohostValidationError(
"app_argument_choice_invalid", "app_argument_choice_invalid",
name=self.name, name=self.id,
value=self.value, value=self.value,
choices=", ".join(str(choice) for choice in self.choices), choices=", ".join(str(choice) for choice in self.choices),
) )
raise YunohostValidationError( raise YunohostValidationError(
"app_argument_invalid", "app_argument_invalid",
name=self.name, name=self.id,
error=f"'{str(self.value)}' is not a list", error=f"'{str(self.value)}' is not a list",
) )
@ -949,7 +949,7 @@ class UserOption(BaseChoicesOption):
if not self.choices: if not self.choices:
raise YunohostValidationError( raise YunohostValidationError(
"app_argument_invalid", "app_argument_invalid",
name=self.name, name=self.id,
error="You should create a YunoHost user first.", error="You should create a YunoHost user first.",
) )
@ -1033,9 +1033,9 @@ def prompt_or_validate_form(
options = [] options = []
answers = {**prefilled_answers} answers = {**prefilled_answers}
for name, raw_option in raw_options.items(): for id_, raw_option in raw_options.items():
raw_option["name"] = name raw_option["id"] = id_
raw_option["value"] = answers.get(name) raw_option["value"] = answers.get(id_)
question_class = OPTIONS[raw_option.get("type", "string")] question_class = OPTIONS[raw_option.get("type", "string")]
option = question_class(raw_option) option = question_class(raw_option)
@ -1047,7 +1047,7 @@ def prompt_or_validate_form(
else: else:
raise YunohostValidationError( raise YunohostValidationError(
"config_action_disabled", "config_action_disabled",
action=option.name, action=option.id,
help=_value_for_locale(option.help), help=_value_for_locale(option.help),
) )
@ -1060,7 +1060,7 @@ def prompt_or_validate_form(
# - we doesn't want to give a specific value # - we doesn't want to give a specific value
# - we want to keep the previous value # - we want to keep the previous value
# - we want the default value # - we want the default value
option.value = context[option.name] = None option.value = context[option.id] = None
continue continue
@ -1071,7 +1071,7 @@ def prompt_or_validate_form(
Moulinette.display(message) Moulinette.display(message)
if isinstance(option, BaseInputOption): if isinstance(option, BaseInputOption):
option.value = context[option.name] = option.current_value option.value = context[option.id] = option.current_value
continue continue
@ -1123,10 +1123,10 @@ def prompt_or_validate_form(
break break
option.value = option.values[option.name] = option._value_post_validator() option.value = option.values[option.id] = option._value_post_validator()
# Search for post actions in hooks # Search for post actions in hooks
post_hook = f"post_ask__{option.name}" post_hook = f"post_ask__{option.id}"
if post_hook in hooks: if post_hook in hooks:
option.values.update(hooks[post_hook](option)) option.values.update(hooks[post_hook](option))