fix: refactoring

This commit is contained in:
egarette@silique.fr 2025-11-26 22:05:09 +01:00
parent 03444905e5
commit a71b476307

View file

@ -38,6 +38,8 @@ from .i18n import _
class RougailUserDataQuestionary: class RougailUserDataQuestionary:
"""Rougail userdata for Questionary"""
interactive_user_datas = True interactive_user_datas = True
def __init__( def __init__(
@ -66,30 +68,14 @@ class RougailUserDataQuestionary:
) -> None: ) -> None:
self.errors = [] self.errors = []
self.warnings = [] self.warnings = []
self.questionary_show_secrets = self.rougailconfig["questionary.show_secrets"]
if "demoting_error_warning" not in self.config.property.get(): if "demoting_error_warning" not in self.config.property.get():
add_demoting = True add_demoting = True
self.config.property.add("demoting_error_warning") self.config.property.add("demoting_error_warning")
else: else:
add_demoting = False add_demoting = False
if self.rougailconfig["questionary.mandatory"]: if self.rougailconfig["questionary.mandatory"]:
current_titles = [] self.parse_mandatories()
while True:
mandatories = self.config.value.mandatory()
if not mandatories:
break
mandatory = mandatories[0]
path = mandatory.path()
if "." in path:
current_config = self.config
for idx, p in enumerate(path.split(".")[0:-1]):
current_config = current_config.option(p)
if idx < len(current_titles):
if current_titles[idx] == p:
continue
current_titles = current_titles[0:idx]
current_titles.append(p)
self.print(current_config.description(), idx)
self.display_questionary(mandatory, title_level=0)
else: else:
old_path_in_description = self.config.information.get( old_path_in_description = self.config.information.get(
"path_in_description", True "path_in_description", True
@ -108,6 +94,26 @@ class RougailUserDataQuestionary:
} }
] ]
def parse_mandatories(self):
current_titles = []
while True:
mandatories = self.config.value.mandatory()
if not mandatories:
break
mandatory = mandatories[0]
path = mandatory.path()
if "." in path:
current_config = self.config
for idx, p in enumerate(path.split(".")[0:-1]):
current_config = current_config.option(p)
if idx < len(current_titles):
if current_titles[idx] == p:
continue
current_titles = current_titles[0:idx]
current_titles.append(p)
self.print(current_config.description(), idx)
self.display_questionary(mandatory, title_level=0)
def parse(self, config, title_level=0): def parse(self, config, title_level=0):
display_title = True display_title = True
for option in config: for option in config:
@ -125,40 +131,21 @@ class RougailUserDataQuestionary:
def display_questionary(self, option, title_level): def display_questionary(self, option, title_level):
kwargs = {} kwargs = {}
option_type = option.information.get("type") option_type = option.information.get("type")
isdefault = option.owner.isdefault()
with warnings.catch_warnings(): with warnings.catch_warnings():
warnings.simplefilter("ignore") warnings.simplefilter("ignore")
default = option.value.get() default = option.value.get()
ismulti = option.ismulti() ismulti = option.ismulti()
type_obj = None
type_obj = CONVERT_OPTION.get(option_type, {}).get("func")
RougailValidator.option = option RougailValidator.option = option
RougailValidator.option_type = { RougailValidator.option_type = option_type
"type": option_type,
"func": type_obj,
}
RougailValidator.ismulti = ismulti RougailValidator.ismulti = ismulti
ori_default = default
args = [" " * title_level + "📓 " + option.description()] args = [" " * title_level + "📓 " + option.description()]
if "frozen" in option.property.get(): if "frozen" in option.property.get():
args[0] += " " + str(default) self._display_questionary_frozen(args[0], default)
question_funtion = qprint
qprint(" " + args[0])
return return
if option_type == "choice": question_funtion = self._dispatcher_questionary(
question_funtion = select option_type, RougailValidator, kwargs, option, default
RougailValidator.default = default )
kwargs["choices"] = option.value.list() ori_default = default
elif option_type == "boolean":
question_funtion = confirm
elif (
option_type == "secret"
and not self.rougailconfig["questionary.show_secrets"]
):
question_funtion = password
else:
question_funtion = text
kwargs["validate"] = RougailValidator
if ismulti: if ismulti:
kwargs["multiline"] = True kwargs["multiline"] = True
if default: if default:
@ -170,69 +157,102 @@ class RougailUserDataQuestionary:
value = RougailValidator().convert_value( value = RougailValidator().convert_value(
question_funtion(*args, **kwargs).ask(), False question_funtion(*args, **kwargs).ask(), False
) )
if isdefault and value == ori_default: if option.owner.isdefault() and value == ori_default:
option.value.reset() option.value.reset()
else: else:
option.value.set(value) option.value.set(value)
def _dispatcher_questionary(
self, option_type: str, RougailValidator, kwargs: dict, option, default: any
) -> callable:
if option_type == "choice":
RougailValidator.default = default
kwargs["choices"] = option.value.list()
return select
if option_type == "boolean":
return confirm
if option_type == "secret" and not self.questionary_show_secrets:
return password
kwargs["validate"] = RougailValidator
return text
def _display_questionary_frozen(self, args: str, default: any) -> None:
qprint(" " + args + str(default))
class RougailValidator(Validator): class RougailValidator(Validator):
"""Extend Questionary Validator to Rougail needs
Be careful it's a singleton
"""
def validate(self, document): def validate(self, document):
"""patch Questionary validate"""
return self.convert_value(document.text) return self.convert_value(document.text)
def convert_value( def convert_value(
self, self,
document, document,
validate=True, set_value=True,
): ) -> any:
if not self.ismulti: """Valid value"""
value = self._convert_a_value(document, document, validate) self.set_value = set_value
else: self.document = document
value = [] self.func = CONVERT_OPTION.get(self.option_type, {}).get("func")
if document is not None: convert_func = (
for val in document.strip().split("\n"): self._convert_multi_value if self.ismulti else self._convert_a_value
val = self._convert_a_value(val, document, validate) )
if val is not None: value = convert_func(document)
value.append(val) if self.set_value:
if validate: self._set_value(value)
try:
with warnings.catch_warnings(record=True) as warns:
self.option.value.set(value)
for warn in warns:
if isinstance(warn.message, ValueErrorWarning):
warn.message.prefix = ""
raise ValidationError(
message=str(warn.message),
cursor_position=len(document),
)
except ValueOptionError as err:
err.prefix = ""
raise ValidationError(
message=str(err),
cursor_position=len(document),
) from err
return value return value
def _convert_a_value(self, value, document, validate): def _convert_multi_value(self, value: any) -> list[any]:
if value is None: if self.document is None:
if self.option_type["type"] == "choice": return []
return self.default value = []
return None for val in self.document.strip().split("\n"):
val = self._convert_a_value(val)
if val is not None:
value.append(val)
return value
def _convert_a_value(self, value: any) -> any:
if isinstance(value, str): if isinstance(value, str):
value = value.strip() value = value.strip()
if value == "": if value in [None, ""]:
if validate and "mandatory" in self.option.property.get(): return self._convert_empty_value()
raise ValidationError( if not self.func:
message=_("Value must not be empty"), return value
cursor_position=len(document), try:
) return self.func(value)
return None except:
if self.option_type["func"]: msg = _("Not a valid {0}").format(self.option_type)
try: self._raise_validation_error(msg)
return self.option_type["func"](value)
except Exception as err: def _convert_empty_value(self) -> any:
raise ValidationError( if self.option_type == "choice":
message=_("Not a valid {0}").format(self.option_type["type"]), return self.default
cursor_position=len(document), self._validate_empty_value()
) from err return None
return value
def _set_value(self, value: any) -> None:
try:
with warnings.catch_warnings(record=True) as warns:
self.option.value.set(value)
except ValueOptionError as err:
self._raise_validation_error(err)
for warn in warns:
if isinstance(warn.message, ValueErrorWarning):
self._raise_validation_error(warn.message)
def _validate_empty_value(self) -> None:
if self.set_value and "mandatory" in self.option.property.get():
self._raise_validation_error(_("Value must not be empty"))
def _raise_validation_error(self, err) -> None:
if not isinstance(err, str):
err.prefix = ""
raise ValidationError(
message=str(err),
cursor_position=len(self.document),
)