diff --git a/src/utils/form.py b/src/utils/form.py index 55bbfc6ca..871a9bd22 100644 --- a/src/utils/form.py +++ b/src/utils/form.py @@ -19,6 +19,7 @@ import ast import datetime import operator as op +import os import re import urllib from enum import Enum @@ -1147,6 +1148,33 @@ def prompt_form( return form +def prompt_or_fill_raw_questions( + raw_options: dict[str, Any], + args: Union[str, None] = None, + args_file=None, # FIXME TYPING + name: str = "DynamicYunoForm" +): + """ + Helper that from a raw list of questions and potential preffiled answers + tries to build a form from those questions and validate prompted or preffilled answers. + """ + prefilled_answers = parse_prefilled_values(args, args_file) + # Parse/validate raw questions + options = OptionsContainer(**raw_options) + options.translate_options() + # Build the form from those questions and instantiate it without + # parsing/validation (construct) since it may contains required questions. + form = build_form(options.options, name=name).construct() + + if Moulinette.interface.type == "cli" and os.isatty(1): + form = prompt_form(options.options, form, prefilled_answers=prefilled_answers) + else: # API or CLI without tty? + form = fill_form(options.options, form, prefilled_answers=prefilled_answers) + + # return the list of options directly, the object itself is no longer needed + return (options.options, form) + + # ╭───────────────────────────────────────────────────────╮ # │ ┌─╴╷ ╷╭─┐╷ │ # │ ├─╴│╭╯├─┤│ │