feat: add frozen variable support

This commit is contained in:
egarette@silique.fr 2025-11-26 20:44:59 +01:00
parent 9f18680d6a
commit 84a89133fa
6 changed files with 140 additions and 65 deletions

View file

@ -1,5 +1,26 @@
"""
Silique (https://www.silique.fr)
Copyright (C) 2024-2025
distribued with GPL-2 or later license
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
"""
from .data import RougailUserDataQuestionary from .data import RougailUserDataQuestionary
from .__version__ import __version__ from .__version__ import __version__
RougailUserData = RougailUserDataQuestionary RougailUserData = RougailUserDataQuestionary
__all__ = ('RougailUserDataQuestionary',) __all__ = ("RougailUserDataQuestionary",)

View file

@ -1 +1,22 @@
"""
Silique (https://www.silique.fr)
Copyright (C) 2024-2025
distribued with GPL-2 or later license
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
"""
__version__ = "0.1.0a1" __version__ = "0.1.0a1"

View file

@ -26,6 +26,7 @@ def get_rougail_config(
*, *,
backward_compatibility=True, backward_compatibility=True,
) -> dict: ) -> dict:
"""generate rougail config"""
options = f""" options = f"""
questionary: questionary:
description: {_("Define values interactivly")} description: {_("Define values interactivly")}
@ -46,11 +47,12 @@ questionary:
type: boolean type: boolean
default: false default: false
""" """
return {'name': 'questionary', return {
'process': 'user data', "name": "questionary",
'options': options, "process": "user data",
'level': 60, "options": options,
"level": 60,
} }
__all__ = ('get_rougail_config',) __all__ = ("get_rougail_config",)

View file

@ -18,13 +18,23 @@ You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
""" """
import warnings
from questionary import text, select, confirm, password, Validator, ValidationError, print as qprint
import warnings
from questionary import (
text,
select,
confirm,
password,
Validator,
ValidationError,
print as qprint,
)
from tiramisu.error import ValueOptionError, ValueErrorWarning, ValueWarning
from rougail.tiramisu import CONVERT_OPTION from rougail.tiramisu import CONVERT_OPTION
from rougail.config import RougailConfig from rougail.config import RougailConfig
from rougail.error import ExtensionError from rougail.error import ExtensionError
from tiramisu.error import ValueOptionError, ValueErrorWarning, ValueWarning
from .i18n import _
class RougailUserDataQuestionary: class RougailUserDataQuestionary:
@ -40,29 +50,28 @@ class RougailUserDataQuestionary:
self.config = config self.config = config
if rougailconfig is None: if rougailconfig is None:
rougailconfig = RougailConfig rougailconfig = RougailConfig
user_data = rougailconfig['step.user_data'] user_data = rougailconfig["step.user_data"]
if 'questionary' not in user_data: if "questionary" not in user_data:
user_data.append('questionary') user_data.append("questionary")
rougailconfig['step.user_data'] = user_data rougailconfig["step.user_data"] = user_data
user_data = rougailconfig['step.user_data'] user_data = rougailconfig["step.user_data"]
if 'questionary' not in user_data: if "questionary" not in user_data:
raise ExtensionError('questionary is not set in step.user_data') raise ExtensionError("questionary is not set in step.user_data")
self.rougailconfig = rougailconfig self.rougailconfig = rougailconfig
self.errors = []
self.warnings = []
warnings.simplefilter("always", ValueErrorWarning) warnings.simplefilter("always", ValueErrorWarning)
warnings.simplefilter("always", ValueWarning) warnings.simplefilter("always", ValueWarning)
def run( def run(
self, self,
) -> None: ) -> None:
# self.config.property.read_write() self.errors = []
if 'demoting_error_warning' not in self.config.property.get(): self.warnings = []
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 = [] current_titles = []
while True: while True:
mandatories = self.config.value.mandatory() mandatories = self.config.value.mandatory()
@ -70,9 +79,9 @@ class RougailUserDataQuestionary:
break break
mandatory = mandatories[0] mandatory = mandatories[0]
path = mandatory.path() path = mandatory.path()
if '.' in path: if "." in path:
current_config = self.config current_config = self.config
for idx, p in enumerate(path.split('.')[0:-1]): for idx, p in enumerate(path.split(".")[0:-1]):
current_config = current_config.option(p) current_config = current_config.option(p)
if idx < len(current_titles): if idx < len(current_titles):
if current_titles[idx] == p: if current_titles[idx] == p:
@ -80,14 +89,19 @@ class RougailUserDataQuestionary:
current_titles = current_titles[0:idx] current_titles = current_titles[0:idx]
current_titles.append(p) current_titles.append(p)
self.print(current_config.description(), idx) self.print(current_config.description(), idx)
self.display_questionary(mandatory) self.display_questionary(mandatory, title_level=0)
else: else:
old_path_in_description = self.config.information.get(
"path_in_description", True
)
self.config.information.set("path_in_description", False)
self.parse(self.config) self.parse(self.config)
self.config.information.set("path_in_description", old_path_in_description)
if add_demoting: if add_demoting:
self.config.property.remove('demoting_error_warning') self.config.property.remove("demoting_error_warning")
return [ return [
{ {
"source": 'Questionary', "source": "Questionary",
"errors": self.errors, "errors": self.errors,
"warnings": self.warnings, "warnings": self.warnings,
"values": [], "values": [],
@ -95,19 +109,22 @@ class RougailUserDataQuestionary:
] ]
def parse(self, config, title_level=0): def parse(self, config, title_level=0):
display_title = True
for option in config: for option in config:
if title_level and display_title:
self.print(config.description(), title_level)
display_title = False
if option.isoptiondescription(): if option.isoptiondescription():
self.print(option.description(), title_level)
self.parse(option, title_level + 1) self.parse(option, title_level + 1)
else: else:
self.display_questionary(option) self.display_questionary(option, title_level)
def print(self, title, title_level): def print(self, title, title_level):
qprint(' ' * title_level + '📂 ' + title, 'bold') qprint(" " * title_level + "📂 " + title, "bold")
def display_questionary(self, option): 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() isdefault = option.owner.isdefault()
with warnings.catch_warnings(): with warnings.catch_warnings():
warnings.simplefilter("ignore") warnings.simplefilter("ignore")
@ -116,32 +133,43 @@ class RougailUserDataQuestionary:
type_obj = None type_obj = None
type_obj = CONVERT_OPTION.get(option_type, {}).get("func") type_obj = CONVERT_OPTION.get(option_type, {}).get("func")
RougailValidator.option = option RougailValidator.option = option
RougailValidator.option_type = {'type': option_type, RougailValidator.option_type = {
'func': type_obj, "type": option_type,
"func": type_obj,
} }
RougailValidator.ismulti = ismulti RougailValidator.ismulti = ismulti
ori_default = default ori_default = default
if option_type == 'choice': args = [" " * title_level + "📓 " + option.description()]
if "frozen" in option.property.get():
args[0] += " " + str(default)
question_funtion = qprint
qprint(" " + args[0])
return
if option_type == "choice":
question_funtion = select question_funtion = select
RougailValidator.default = default RougailValidator.default = default
kwargs['choices'] = option.value.list() kwargs["choices"] = option.value.list()
elif option_type == 'boolean': elif option_type == "boolean":
question_funtion = confirm question_funtion = confirm
elif option_type == 'secret' and not self.rougailconfig['questionary.show_secrets']: elif (
option_type == "secret"
and not self.rougailconfig["questionary.show_secrets"]
):
question_funtion = password question_funtion = password
else: else:
question_funtion = text question_funtion = text
kwargs['validate'] = RougailValidator kwargs["validate"] = RougailValidator
args = ['📓 ' + option.description()]
if ismulti: if ismulti:
kwargs['multiline'] = True kwargs["multiline"] = True
if default: if default:
kwargs['default'] = "\n".join([str(d) for d in default]) kwargs["default"] = "\n".join([str(d) for d in default])
elif default is not None: elif default is not None:
if isinstance(default, (int, float)): if isinstance(default, (int, float)):
default = str(default) default = str(default)
kwargs['default'] = default kwargs["default"] = default
value = RougailValidator().convert_value(question_funtion(*args, **kwargs).ask(), False) value = RougailValidator().convert_value(
question_funtion(*args, **kwargs).ask(), False
)
if isdefault and value == ori_default: if isdefault and value == ori_default:
option.value.reset() option.value.reset()
else: else:
@ -152,7 +180,8 @@ class RougailValidator(Validator):
def validate(self, document): def validate(self, document):
return self.convert_value(document.text) return self.convert_value(document.text)
def convert_value(self, def convert_value(
self,
document, document,
validate=True, validate=True,
): ):
@ -161,7 +190,7 @@ class RougailValidator(Validator):
else: else:
value = [] value = []
if document is not None: if document is not None:
for val in document.strip().split('\n'): for val in document.strip().split("\n"):
val = self._convert_a_value(val, document, validate) val = self._convert_a_value(val, document, validate)
if val is not None: if val is not None:
value.append(val) value.append(val)
@ -171,39 +200,39 @@ class RougailValidator(Validator):
self.option.value.set(value) self.option.value.set(value)
for warn in warns: for warn in warns:
if isinstance(warn.message, ValueErrorWarning): if isinstance(warn.message, ValueErrorWarning):
warn.message.prefix = '' warn.message.prefix = ""
raise ValidationError( raise ValidationError(
message=str(warn.message), message=str(warn.message),
cursor_position=len(document), cursor_position=len(document),
) )
except ValueOptionError as err: except ValueOptionError as err:
err.prefix = '' err.prefix = ""
raise ValidationError( raise ValidationError(
message=str(err), message=str(err),
cursor_position=len(document), cursor_position=len(document),
) ) from err
return value return value
def _convert_a_value(self, value, document, validate): def _convert_a_value(self, value, document, validate):
if value is None: if value is None:
if self.option_type['type'] == 'choice': if self.option_type["type"] == "choice":
return self.default return self.default
return return None
if isinstance(value, str): if isinstance(value, str):
value = value.strip() value = value.strip()
if value == '': if value == "":
if validate and "mandatory" in self.option.property.get(): if validate and "mandatory" in self.option.property.get():
raise ValidationError( raise ValidationError(
message=f"Value must not be empty", message=_("Value must not be empty"),
cursor_position=len(document), cursor_position=len(document),
) )
return return None
if self.option_type['func']: if self.option_type["func"]:
try: try:
return self.option_type['func'](value) return self.option_type["func"](value)
except: except Exception as err:
raise ValidationError( raise ValidationError(
message=f"Not a valid {self.option_type['type']}", message=_("Not a valid {0}").format(self.option_type["type"]),
cursor_position=len(document), cursor_position=len(document),
) ) from err
return value return value

View file

@ -20,7 +20,9 @@ from gettext import translation
from pathlib import Path from pathlib import Path
t = translation( t = translation(
"rougail_user_data_questionary", str(Path(__file__).parent / "locale"), fallback=True "rougail_user_data_questionary",
str(Path(__file__).parent / "locale"),
fallback=True,
) )
_ = t.gettext _ = t.gettext