feat: can change defaut params for an option

This commit is contained in:
egarette@silique.fr 2025-02-10 10:32:48 +01:00
parent df2fcb467f
commit cc5aaf6393
21 changed files with 122 additions and 45 deletions

View file

@ -9,7 +9,7 @@ Cadoles (http://www.cadoles.com)
Copyright (C) 2019-2021 Copyright (C) 2019-2021
Silique (https://www.silique.fr) Silique (https://www.silique.fr)
Copyright (C) 2022-2024 Copyright (C) 2022-2025
This program is free software: you can redistribute it and/or modify it This program is free software: you can redistribute it and/or modify it
under the terms of the GNU Lesser General Public License as published by the under the terms of the GNU Lesser General Public License as published by the

View file

@ -9,7 +9,7 @@ Cadoles (http://www.cadoles.com)
Copyright (C) 2019-2021 Copyright (C) 2019-2021
Silique (https://www.silique.fr) Silique (https://www.silique.fr)
Copyright (C) 2022-2024 Copyright (C) 2022-2025
This program is free software: you can redistribute it and/or modify it This program is free software: you can redistribute it and/or modify it
under the terms of the GNU Lesser General Public License as published by the under the terms of the GNU Lesser General Public License as published by the

View file

@ -9,7 +9,7 @@ Cadoles (http://www.cadoles.com)
Copyright (C) 2019-2021 Copyright (C) 2019-2021
Silique (https://www.silique.fr) Silique (https://www.silique.fr)
Copyright (C) 2022-2024 Copyright (C) 2022-2025
This program is free software: you can redistribute it and/or modify it This program is free software: you can redistribute it and/or modify it
under the terms of the GNU Lesser General Public License as published by the under the terms of the GNU Lesser General Public License as published by the

View file

@ -9,7 +9,7 @@ Cadoles (http://www.cadoles.com)
Copyright (C) 2019-2021 Copyright (C) 2019-2021
Silique (https://www.silique.fr) Silique (https://www.silique.fr)
Copyright (C) 2022-2024 Copyright (C) 2022-2025
This program is free software: you can redistribute it and/or modify it This program is free software: you can redistribute it and/or modify it
under the terms of the GNU Lesser General Public License as published by the under the terms of the GNU Lesser General Public License as published by the

View file

@ -9,7 +9,7 @@ Cadoles (http://www.cadoles.com)
Copyright (C) 2019-2021 Copyright (C) 2019-2021
Silique (https://www.silique.fr) Silique (https://www.silique.fr)
Copyright (C) 2022-2024 Copyright (C) 2022-2025
This program is free software: you can redistribute it and/or modify it This program is free software: you can redistribute it and/or modify it
under the terms of the GNU Lesser General Public License as published by the under the terms of the GNU Lesser General Public License as published by the

View file

@ -9,7 +9,7 @@ Cadoles (http://www.cadoles.com)
Copyright (C) 2019-2021 Copyright (C) 2019-2021
Silique (https://www.silique.fr) Silique (https://www.silique.fr)
Copyright (C) 2022-2024 Copyright (C) 2022-2025
This program is free software: you can redistribute it and/or modify it This program is free software: you can redistribute it and/or modify it
under the terms of the GNU Lesser General Public License as published by the under the terms of the GNU Lesser General Public License as published by the

View file

@ -10,7 +10,7 @@ Cadoles (http://www.cadoles.com)
Copyright (C) 2019-2021 Copyright (C) 2019-2021
Silique (https://www.silique.fr) Silique (https://www.silique.fr)
Copyright (C) 2022-2024 Copyright (C) 2022-2025
This program is free software: you can redistribute it and/or modify it This program is free software: you can redistribute it and/or modify it
under the terms of the GNU Lesser General Public License as published by the under the terms of the GNU Lesser General Public License as published by the
@ -33,6 +33,7 @@ from tiramisu import Config
from ruamel.yaml import YAML from ruamel.yaml import YAML
from .utils import _, load_modules, normalize_family from .utils import _, load_modules, normalize_family
from .convert import RougailConvert from .convert import RougailConvert
from .object_model import get_convert_option_types
if version_info.major == 3 and version_info.minor: if version_info.major == 3 and version_info.minor:
import rougail.structural_commandline.object_model import rougail.structural_commandline.object_model
@ -72,39 +73,52 @@ def get_level(module):
class _RougailConfig: class _RougailConfig:
def __init__(self, backward_compatibility: bool, root, extra_vars: dict): def __init__(self, backward_compatibility: bool,
add_extra_options: bool):
self.backward_compatibility = backward_compatibility self.backward_compatibility = backward_compatibility
self.add_extra_options = add_extra_options
self.root = None
def copy(self):
rougailconfig = _RougailConfig(
self.backward_compatibility,
self.add_extra_options,
)
if self.root:
rougailconfig.config.value.importation(self.config.value.exportation())
rougailconfig.config.property.importation(self.config.property.exportation())
rougailconfig.config.property.read_only()
rougailconfig.root = self.root
rougailconfig.config = self.config
rougailconfig.extra_vars = self.extra_vars
for variable in self.not_in_tiramisu:
value = getattr(self, variable)
if not isinstance(value, str):
value = value.copy()
setattr(rougailconfig, variable, value)
return rougailconfig
def generate_config(self):
root, extra_vars = _rougail_config(self.backward_compatibility, self.add_extra_options)
self.root = root self.root = root
self.config = Config( self.config = Config(
self.root, self.root,
) )
self.config.property.read_only()
self.extra_vars = extra_vars self.extra_vars = extra_vars
self.not_in_tiramisu = NOT_IN_TIRAMISU | extra_vars self.not_in_tiramisu = NOT_IN_TIRAMISU | extra_vars
for variable, default_value in self.not_in_tiramisu.items(): for variable, default_value in self.not_in_tiramisu.items():
if not isinstance(default_value, str): if not isinstance(default_value, str):
default_value = default_value.copy() default_value = default_value.copy()
setattr(self, variable, default_value) setattr(self, variable, default_value)
self.config.property.read_only()
def copy(self):
rougailconfig = _RougailConfig(
self.backward_compatibility, self.root, self.extra_vars
)
rougailconfig.config.value.importation(self.config.value.exportation())
rougailconfig.config.property.importation(self.config.property.exportation())
rougailconfig.config.property.read_only()
for variable in self.not_in_tiramisu:
value = getattr(self, variable)
if not isinstance(value, str):
value = value.copy()
setattr(rougailconfig, variable, value)
return rougailconfig
def __setitem__( def __setitem__(
self, self,
key, key,
value, value,
) -> None: ) -> None:
if self.root is None:
self.generate_config()
if key in self.not_in_tiramisu: if key in self.not_in_tiramisu:
setattr(self, key, value) setattr(self, key, value)
else: else:
@ -130,6 +144,8 @@ class _RougailConfig:
self, self,
key, key,
) -> None: ) -> None:
if self.root is None:
self.generate_config()
if key in self.not_in_tiramisu: if key in self.not_in_tiramisu:
return getattr(self, key) return getattr(self, key)
if key == "export_with_import": if key == "export_with_import":
@ -199,11 +215,10 @@ class FakeRougailConvert(RougailConvert):
self.load_unexist_redefine = False self.load_unexist_redefine = False
def get_rougail_config( def _rougail_config(
*,
backward_compatibility: bool = True, backward_compatibility: bool = True,
add_extra_options: bool = True, add_extra_options: bool = True,
) -> _RougailConfig: ) -> "OptionDescription":
rougail_options = f"""default_dictionary_format_version: rougail_options = f"""default_dictionary_format_version:
description: Dictionary format version by default, if not specified in dictionary file description: Dictionary format version by default, if not specified in dictionary file
alternative_name: v alternative_name: v
@ -403,6 +418,23 @@ load_unexist_redefine:
NAME=normalize_family(process), NAME=normalize_family(process),
PROP=prop, PROP=prop,
) )
rougail_process += f"""
default_params:
description: {_("Default parameters for option type")}
"""
for typ, params in get_convert_option_types():
rougail_process += f"""
{typ}:
"""
for key, key_type, multi, value in params:
rougail_process += f"""
{key}:
type: {key_type}
multi: {multi}
mandatory: false
default: {value}
"""
rougail_options += rougail_process rougail_options += rougail_process
# print(rougail_options) # print(rougail_options)
convert = FakeRougailConvert(add_extra_options) convert = FakeRougailConvert(add_extra_options)
@ -438,10 +470,17 @@ load_unexist_redefine:
tiram_obj = convert.save() tiram_obj = convert.save()
optiondescription = {} optiondescription = {}
exec(tiram_obj, {}, optiondescription) # pylint: disable=W0122 exec(tiram_obj, {}, optiondescription) # pylint: disable=W0122
return optiondescription["option_0"], extra_vars
def get_rougail_config(
*,
backward_compatibility: bool = True,
add_extra_options: bool = True,
) -> _RougailConfig:
return _RougailConfig( return _RougailConfig(
backward_compatibility, backward_compatibility,
optiondescription["option_0"], add_extra_options,
extra_vars=extra_vars,
) )

View file

@ -10,7 +10,7 @@ Cadoles (http://www.cadoles.com)
Copyright (C) 2019-2021 Copyright (C) 2019-2021
Silique (https://www.silique.fr) Silique (https://www.silique.fr)
Copyright (C) 2022-2024 Copyright (C) 2022-2025
This program is free software: you can redistribute it and/or modify it This program is free software: you can redistribute it and/or modify it
under the terms of the GNU Lesser General Public License as published by the under the terms of the GNU Lesser General Public License as published by the
@ -176,6 +176,16 @@ class ParserVariable:
self.output = rougailconfig["step.output"] self.output = rougailconfig["step.output"]
self.tiramisu_cache = rougailconfig["tiramisu_cache"] self.tiramisu_cache = rougailconfig["tiramisu_cache"]
self.load_unexist_redefine = rougailconfig["load_unexist_redefine"] self.load_unexist_redefine = rougailconfig["load_unexist_redefine"]
# change default initkwargs in CONVERT_OPTION
if hasattr(rougailconfig, 'config'):
for sub_od in rougailconfig.config.option('default_params'):
for option in sub_od:
if option.owner.isdefault():
continue
convert_option = CONVERT_OPTION[sub_od.name()]
if "initkwargs" not in convert_option:
convert_option["initkwargs"] = {}
convert_option["initkwargs"][option.name()] = option.value.get()
def init(self): def init(self):
if self.is_init: if self.is_init:

View file

@ -9,7 +9,7 @@ Cadoles (http://www.cadoles.com)
Copyright (C) 2019-2021 Copyright (C) 2019-2021
Silique (https://www.silique.fr) Silique (https://www.silique.fr)
Copyright (C) 2022-2024 Copyright (C) 2022-2025
This program is free software: you can redistribute it and/or modify it This program is free software: you can redistribute it and/or modify it
under the terms of the GNU Lesser General Public License as published by the under the terms of the GNU Lesser General Public License as published by the

View file

@ -8,7 +8,7 @@ Cadoles (http://www.cadoles.com)
Copyright (C) 2019-2021 Copyright (C) 2019-2021
Silique (https://www.silique.fr) Silique (https://www.silique.fr)
Copyright (C) 2022-2024 Copyright (C) 2022-2025
This program is free software: you can redistribute it and/or modify it This program is free software: you can redistribute it and/or modify it
under the terms of the GNU Lesser General Public License as published by the under the terms of the GNU Lesser General Public License as published by the

View file

@ -1,7 +1,7 @@
"""Rougail object model """Rougail object model
Silique (https://www.silique.fr) Silique (https://www.silique.fr)
Copyright (C) 2023-2024 Copyright (C) 2023-2025
This program is free software: you can redistribute it and/or modify it This program is free software: you can redistribute it and/or modify it
under the terms of the GNU Lesser General Public License as published by the under the terms of the GNU Lesser General Public License as published by the
@ -27,6 +27,7 @@ from pydantic import (
ConfigDict, ConfigDict,
) )
from tiramisu import undefined from tiramisu import undefined
import tiramisu
from .utils import get_jinja_variable_to_param from .utils import get_jinja_variable_to_param
from .error import DictConsistencyError, VariableCalculationDependencyError from .error import DictConsistencyError, VariableCalculationDependencyError
@ -105,6 +106,35 @@ CONVERT_OPTION = {
} }
def get_convert_option_types():
for typ, datas in CONVERT_OPTION.items():
obj = getattr(tiramisu, datas["opttype"])
initkwargs = datas.get("initkwargs", {})
if obj == tiramisu.SymLinkOption:
continue
if obj == tiramisu.ChoiceOption:
inst = obj('a', 'a', ('a',), **initkwargs)
else:
inst = obj('a', 'a', **initkwargs)
extra = getattr(inst, '_extra', {})
if not extra:
continue
params = []
for key, value in extra.items():
if key.startswith('_'):
continue
multi = False
if isinstance(value, bool):
key_type = 'boolean'
elif isinstance(value, str):
key_type = 'string'
elif isinstance(value, list):
key_type = 'string'
multi = True
params.append((key, key_type, multi, value))
yield typ, params
class Param(BaseModel): class Param(BaseModel):
key: str key: str
model_config = ConfigDict(extra="forbid") model_config = ConfigDict(extra="forbid")

View file

@ -1,5 +1,5 @@
""" """
Copyright (C) 2024 Copyright (C) 2024-2025
This program is free software: you can redistribute it and/or modify it This program is free software: you can redistribute it and/or modify it
under the terms of the GNU Lesser General Public License as published by the under the terms of the GNU Lesser General Public License as published by the

View file

@ -1,6 +1,6 @@
""" """
Silique (https://www.silique.fr) Silique (https://www.silique.fr)
Copyright (C) 2024 Copyright (C) 2024-2025
This program is free software: you can redistribute it and/or modify it This program is free software: you can redistribute it and/or modify it
under the terms of the GNU Lesser General Public License as published by the under the terms of the GNU Lesser General Public License as published by the

View file

@ -2,7 +2,7 @@
Config file for Rougail-structural_commandline Config file for Rougail-structural_commandline
Silique (https://www.silique.fr) Silique (https://www.silique.fr)
Copyright (C) 2024 Copyright (C) 2024-2025
This program is free software: you can redistribute it and/or modify it This program is free software: you can redistribute it and/or modify it
under the terms of the GNU Lesser General Public License as published by the under the terms of the GNU Lesser General Public License as published by the

View file

@ -1,7 +1,7 @@
"""Annotate to add specify attribute for tiramisu-cmdline """Annotate to add specify attribute for tiramisu-cmdline
Silique (https://www.silique.fr) Silique (https://www.silique.fr)
Copyright (C) 2024 Copyright (C) 2024-2025
This program is free software: you can redistribute it and/or modify it This program is free software: you can redistribute it and/or modify it
under the terms of the GNU Lesser General Public License as published by the under the terms of the GNU Lesser General Public License as published by the

View file

@ -1,6 +1,6 @@
""" """
Silique (https://www.silique.fr) Silique (https://www.silique.fr)
Copyright (C) 2022-2024 Copyright (C) 2022-2025
This program is free software: you can redistribute it and/or modify it This program is free software: you can redistribute it and/or modify it
under the terms of the GNU Lesser General Public License as published by the under the terms of the GNU Lesser General Public License as published by the

View file

@ -1,6 +1,6 @@
""" """
Silique (https://www.silique.fr) Silique (https://www.silique.fr)
Copyright (C) 2024 Copyright (C) 2024-2025
This program is free software: you can redistribute it and/or modify it This program is free software: you can redistribute it and/or modify it
under the terms of the GNU Lesser General Public License as published by the under the terms of the GNU Lesser General Public License as published by the

View file

@ -9,7 +9,7 @@ Cadoles (http://www.cadoles.com)
Copyright (C) 2019-2021 Copyright (C) 2019-2021
Silique (https://www.silique.fr) Silique (https://www.silique.fr)
Copyright (C) 2022-2024 Copyright (C) 2022-2025
This program is free software: you can redistribute it and/or modify it This program is free software: you can redistribute it and/or modify it
under the terms of the GNU Lesser General Public License as published by the under the terms of the GNU Lesser General Public License as published by the

View file

@ -10,7 +10,7 @@ Cadoles (http://www.cadoles.com)
Copyright (C) 2019-2021 Copyright (C) 2019-2021
Silique (https://www.silique.fr) Silique (https://www.silique.fr)
Copyright (C) 2022-2024 Copyright (C) 2022-2025
This program is free software: you can redistribute it and/or modify it This program is free software: you can redistribute it and/or modify it
under the terms of the GNU Lesser General Public License as published by the under the terms of the GNU Lesser General Public License as published by the

View file

@ -1,6 +1,6 @@
""" """
Silique (https://www.silique.fr) Silique (https://www.silique.fr)
Copyright (C) 2022-2024 Copyright (C) 2022-2025
distribued with GPL-2 or later license distribued with GPL-2 or later license
@ -151,7 +151,7 @@ class UserDatas:
if path not in self.values: if path not in self.values:
continue continue
options = self.values[path].get("options", {}) options = self.values[path].get("options", {})
if options.get('allow_secrets_variables', False) is True and option.type() == 'password': if options.get('allow_secrets_variables', True) is False and option.type() == 'password':
self.errors.append(_('the variable "{0}" contains secrets and should not be defined in {1}').format(path, self.values[path]["source"])) self.errors.append(_('the variable "{0}" contains secrets and should not be defined in {1}').format(path, self.values[path]["source"]))
continue continue
value = self.values[path]["values"] value = self.values[path]["values"]

View file

@ -9,7 +9,7 @@ Cadoles (http://www.cadoles.com)
Copyright (C) 2019-2021 Copyright (C) 2019-2021
Silique (https://www.silique.fr) Silique (https://www.silique.fr)
Copyright (C) 2022-2024 Copyright (C) 2022-2025
This program is free software: you can redistribute it and/or modify it This program is free software: you can redistribute it and/or modify it
under the terms of the GNU Lesser General Public License as published by the under the terms of the GNU Lesser General Public License as published by the
@ -159,7 +159,5 @@ def get_jinja_variable_to_param(
break break
if root_path: if root_path:
yield {}, None, root_path yield {}, None, root_path
else:
yield {}, None, vpath
for variable_path, data in founded_variables.items(): for variable_path, data in founded_variables.items():
yield data[1], data[0], variable_path yield data[1], data[0], variable_path