[wip] Hook system in config panel

This commit is contained in:
ljf 2021-10-06 19:11:25 +02:00
parent 8312134fc6
commit 27273c5721
3 changed files with 21 additions and 8 deletions

View file

@ -5,6 +5,7 @@ i18n = "repository_config"
name.en = "" name.en = ""
[main.main] [main.main]
name.en = "" name.en = ""
optional = false
# if method == "tar": question["value"] = False # if method == "tar": question["value"] = False
[main.main.description] [main.main.description]
type = "string" type = "string"
@ -14,20 +15,18 @@ name.en = ""
type = "boolean" type = "boolean"
yes = true yes = true
no = false no = false
visible = "creation && is_remote" visible = "creation"
default = "no" default = "no"
[main.main.location] [main.main.location]
ask.en = "{is_remote}"
type = "string" type = "string"
visible = "creation && is_remote" visible = "creation && is_remote"
pattern.regexp = '^([^\W_A-Z]+([-]*[^\W_A-Z]+)*\.)+((xn--)?[^\W_]{2,})$' pattern.regexp = '^([^\W_A-Z]+([-]*[^\W_A-Z]+)*\.)+((xn--)?[^\W_]{2,})$'
pattern.error = '' # TODO "Please provide a valid domain" pattern.error = 'location_error' # TODO "Please provide a valid domain"
default = "" default = ""
# FIXME: can't be a domain of this instances ? # FIXME: can't be a domain of this instances ?
[main.main.is_f2f] [main.main.is_f2f]
ask.en = "{is_remote}"
help = "" help = ""
type = "boolean" type = "boolean"
yes = true yes = true

View file

@ -81,6 +81,10 @@ class BackupRepository(ConfigPanel):
#self.method = BackupMethod.get(method, self) #self.method = BackupMethod.get(method, self)
def set__domain(self, question):
# TODO query on domain name .well-known
question.value
def _get_default_values(self): def _get_default_values(self):
values = super()._get_default_values() values = super()._get_default_values()
values["public_key"] = get_ssh_public_key() values["public_key"] = get_ssh_public_key()

View file

@ -667,7 +667,7 @@ class Question(object):
# - we want to keep the previous value # - we want to keep the previous value
# - we want the default value # - we want the default value
self.value = None self.value = None
return self.value return { self.name: self.value }
for i in range(5): for i in range(5):
# Display question if no value filled or if it's a readonly message # Display question if no value filled or if it's a readonly message
@ -689,6 +689,10 @@ class Question(object):
# Normalize and validate # Normalize and validate
self.value = self.normalize(self.value, self) self.value = self.normalize(self.value, self)
self._prevalidate() self._prevalidate()
# Search for validator in hooks
validator = f"validate__{self.name}"
if validator in self.hooks:
self.hooks[validator](self)
except YunohostValidationError as e: except YunohostValidationError as e:
# If in interactive cli, re-ask the current question # If in interactive cli, re-ask the current question
if i < 4 and Moulinette.interface.type == "cli" and os.isatty(1): if i < 4 and Moulinette.interface.type == "cli" and os.isatty(1):
@ -703,7 +707,12 @@ class Question(object):
self.value = self._post_parse_value() self.value = self._post_parse_value()
return self.value # Search for post actions in hooks
post_hook = f"post_parse__{self.name}"
if post_hook in self.hooks:
self.hooks[post_hook](self)
return { self.name: self.value }
def _prevalidate(self): def _prevalidate(self):
if self.value in [None, ""] and not self.optional: if self.value in [None, ""] and not self.optional:
@ -1217,7 +1226,8 @@ ARGUMENTS_TYPE_PARSERS = {
def ask_questions_and_parse_answers( def ask_questions_and_parse_answers(
raw_questions: Dict, prefilled_answers: Union[str, Mapping[str, Any]] = {} raw_questions: Dict, prefilled_answers: Union[str, Mapping[str, Any]] = {},
hooks: Dict[str, Callable[[], None]] = {}
) -> List[Question]: ) -> List[Question]:
"""Parse arguments store in either manifest.json or actions.json or from a """Parse arguments store in either manifest.json or actions.json or from a
config panel against the user answers when they are present. config panel against the user answers when they are present.
@ -1251,7 +1261,7 @@ def ask_questions_and_parse_answers(
question_class = ARGUMENTS_TYPE_PARSERS[raw_question.get("type", "string")] question_class = ARGUMENTS_TYPE_PARSERS[raw_question.get("type", "string")]
raw_question["value"] = answers.get(raw_question["name"]) raw_question["value"] = answers.get(raw_question["name"])
question = question_class(raw_question, context=answers) question = question_class(raw_question, context=answers)
answers[question.name] = question.ask_if_needed() answers.update(question.ask_if_needed())
out.append(question) out.append(question)
return out return out