From 68304378051ec5ee8235e4e091bb6ed08c3466c6 Mon Sep 17 00:00:00 2001 From: Emmanuel Garette Date: Tue, 29 Oct 2024 10:58:04 +0100 Subject: [PATCH] add propertyerror as a test in jinja --- src/rougail/tiramisu.py | 48 +++++++++++-------- .../__init__.py | 0 .../dictionaries/rougail/00-base.yml | 20 ++++++++ .../makedict/after.json | 10 ++++ .../makedict/base.json | 4 ++ .../makedict/before.json | 10 ++++ .../makedict/mandatory.json | 1 + .../tiramisu/base.py | 18 +++++++ .../tiramisu/multi.py | 25 ++++++++++ .../tiramisu/no_namespace.py | 13 +++++ 10 files changed, 129 insertions(+), 20 deletions(-) create mode 100644 tests/dictionaries/04_1default_calculation_hidden_2/__init__.py create mode 100644 tests/dictionaries/04_1default_calculation_hidden_2/dictionaries/rougail/00-base.yml create mode 100644 tests/dictionaries/04_1default_calculation_hidden_2/makedict/after.json create mode 100644 tests/dictionaries/04_1default_calculation_hidden_2/makedict/base.json create mode 100644 tests/dictionaries/04_1default_calculation_hidden_2/makedict/before.json create mode 100644 tests/dictionaries/04_1default_calculation_hidden_2/makedict/mandatory.json create mode 100644 tests/dictionaries/04_1default_calculation_hidden_2/tiramisu/base.py create mode 100644 tests/dictionaries/04_1default_calculation_hidden_2/tiramisu/multi.py create mode 100644 tests/dictionaries/04_1default_calculation_hidden_2/tiramisu/no_namespace.py diff --git a/src/rougail/tiramisu.py b/src/rougail/tiramisu.py index a0bac1eb6..a01be39fe 100644 --- a/src/rougail/tiramisu.py +++ b/src/rougail/tiramisu.py @@ -27,6 +27,7 @@ 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 typing import Any try: from tiramisu5 import DynOptionDescription, calc_value except ModuleNotFoundError: @@ -42,6 +43,7 @@ from tiramisu.error import ValueWarning, ConfigError, PropertiesOptionError from .utils import normalize_family + global func dict_env = {} ENV = SandboxedEnvironment(loader=DictLoader(dict_env), undefined=StrictUndefined) @@ -49,25 +51,6 @@ func = ENV.filters ENV.compile_templates('jinja_caches', zip=None) -def load_functions(path): - global _SourceFileLoader, _spec_from_loader, _module_from_spec, func - loader = _SourceFileLoader('func', path) - spec = _spec_from_loader(loader.name, loader) - func_ = _module_from_spec(spec) - loader.exec_module(func_) - for function in dir(func_): - if function.startswith('_'): - continue - func[function] = getattr(func_, function) - - -def rougail_calc_value(*args, __default_value=None, **kwargs): - values = calc_value(*args, **kwargs) - if __default_value is not None and values in [None, []]: - return __default_value - return values - - class JinjaError: __slot__ = ('_err',) def __init__(self, err): @@ -98,6 +81,32 @@ class JinjaError: raise self._err from self._err +def test_propertyerror(value: Any) -> bool: + return isinstance(value, JinjaError) + + +ENV.tests['propertyerror'] = test_propertyerror + + +def load_functions(path): + global _SourceFileLoader, _spec_from_loader, _module_from_spec, func + loader = _SourceFileLoader('func', path) + spec = _spec_from_loader(loader.name, loader) + func_ = _module_from_spec(spec) + loader.exec_module(func_) + for function in dir(func_): + if function.startswith('_'): + continue + func[function] = getattr(func_, function) + + +def rougail_calc_value(*args, __default_value=None, **kwargs): + values = calc_value(*args, **kwargs) + if __default_value is not None and values in [None, []]: + return __default_value + return values + + @function_waiting_for_error def jinja_to_function(__internal_variable, __internal_attribute, __internal_jinja, __internal_type, __internal_multi, __internal_files, __default_value=None, **kwargs): global ENV, CONVERT_OPTION @@ -135,7 +144,6 @@ def jinja_to_function(__internal_variable, __internal_attribute, __internal_jinj return values -@function_waiting_for_error def variable_to_property(prop, value, when, inverse): if isinstance(value, PropertiesOptionError): raise value from value diff --git a/tests/dictionaries/04_1default_calculation_hidden_2/__init__.py b/tests/dictionaries/04_1default_calculation_hidden_2/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/tests/dictionaries/04_1default_calculation_hidden_2/dictionaries/rougail/00-base.yml b/tests/dictionaries/04_1default_calculation_hidden_2/dictionaries/rougail/00-base.yml new file mode 100644 index 000000000..69dea7d61 --- /dev/null +++ b/tests/dictionaries/04_1default_calculation_hidden_2/dictionaries/rougail/00-base.yml @@ -0,0 +1,20 @@ +--- +version: 1.1 + +var1: + description: a first variable + default: value + +var2: + description: a second variable + disabled: + variable: _.var1 + when: value + +var3: + description: a third variable + default: + jinja: | + {% if _.var2 is propertyerror %} + value + {% endif %} diff --git a/tests/dictionaries/04_1default_calculation_hidden_2/makedict/after.json b/tests/dictionaries/04_1default_calculation_hidden_2/makedict/after.json new file mode 100644 index 000000000..73352fd02 --- /dev/null +++ b/tests/dictionaries/04_1default_calculation_hidden_2/makedict/after.json @@ -0,0 +1,10 @@ +{ + "rougail.var1": { + "owner": "default", + "value": "value" + }, + "rougail.var3": { + "owner": "default", + "value": "value" + } +} diff --git a/tests/dictionaries/04_1default_calculation_hidden_2/makedict/base.json b/tests/dictionaries/04_1default_calculation_hidden_2/makedict/base.json new file mode 100644 index 000000000..022d67330 --- /dev/null +++ b/tests/dictionaries/04_1default_calculation_hidden_2/makedict/base.json @@ -0,0 +1,4 @@ +{ + "rougail.var1": "value", + "rougail.var3": "value" +} diff --git a/tests/dictionaries/04_1default_calculation_hidden_2/makedict/before.json b/tests/dictionaries/04_1default_calculation_hidden_2/makedict/before.json new file mode 100644 index 000000000..73352fd02 --- /dev/null +++ b/tests/dictionaries/04_1default_calculation_hidden_2/makedict/before.json @@ -0,0 +1,10 @@ +{ + "rougail.var1": { + "owner": "default", + "value": "value" + }, + "rougail.var3": { + "owner": "default", + "value": "value" + } +} diff --git a/tests/dictionaries/04_1default_calculation_hidden_2/makedict/mandatory.json b/tests/dictionaries/04_1default_calculation_hidden_2/makedict/mandatory.json new file mode 100644 index 000000000..0637a088a --- /dev/null +++ b/tests/dictionaries/04_1default_calculation_hidden_2/makedict/mandatory.json @@ -0,0 +1 @@ +[] \ No newline at end of file diff --git a/tests/dictionaries/04_1default_calculation_hidden_2/tiramisu/base.py b/tests/dictionaries/04_1default_calculation_hidden_2/tiramisu/base.py new file mode 100644 index 000000000..f05031ca7 --- /dev/null +++ b/tests/dictionaries/04_1default_calculation_hidden_2/tiramisu/base.py @@ -0,0 +1,18 @@ +from tiramisu import * +from tiramisu.setting import ALLOWED_LEADER_PROPERTIES +from re import compile as re_compile +from rougail.tiramisu import func, dict_env, load_functions, ConvertDynOptionDescription +load_functions('tests/dictionaries/../eosfunc/test.py') +try: + groups.namespace +except: + groups.addgroup('namespace') +ALLOWED_LEADER_PROPERTIES.add("basic") +ALLOWED_LEADER_PROPERTIES.add("standard") +ALLOWED_LEADER_PROPERTIES.add("advanced") +dict_env['default_rougail.var3'] = "{% if _.var2 is propertyerror %}\nvalue\n{% endif %}\n" +option_2 = StrOption(name="var1", doc="a first variable", default="value", properties=frozenset({"mandatory", "standard"}), informations={'type': 'string'}) +option_3 = StrOption(name="var2", doc="a second variable", properties=frozenset({"basic", "mandatory", Calculation(func['variable_to_property'], Params((ParamValue("disabled"), ParamOption(option_2)), kwargs={'when': ParamValue("value"), 'inverse': ParamValue(False)}), help_function=func['variable_to_property'])}), informations={'type': 'string'}) +option_4 = StrOption(name="var3", doc="a third variable", default=Calculation(func['jinja_to_function'], Params((), kwargs={'__internal_jinja': ParamValue("default_rougail.var3"), '__internal_type': ParamValue("string"), '__internal_multi': ParamValue(False), '__internal_files': ParamValue(['tests/dictionaries/04_1default_calculation_hidden_2/dictionaries/rougail/00-base.yml']), '__internal_attribute': ParamValue("default"), '__internal_variable': ParamValue("rougail.var3"), '_.var2': ParamOption(option_3, notraisepropertyerror=True)})), properties=frozenset({"mandatory", "standard"}), informations={'type': 'string'}) +optiondescription_1 = OptionDescription(name="rougail", doc="Rougail", group_type=groups.namespace, children=[option_2, option_3, option_4], properties=frozenset({"basic"})) +option_0 = OptionDescription(name="baseoption", doc="baseoption", children=[optiondescription_1]) diff --git a/tests/dictionaries/04_1default_calculation_hidden_2/tiramisu/multi.py b/tests/dictionaries/04_1default_calculation_hidden_2/tiramisu/multi.py new file mode 100644 index 000000000..559685883 --- /dev/null +++ b/tests/dictionaries/04_1default_calculation_hidden_2/tiramisu/multi.py @@ -0,0 +1,25 @@ +from tiramisu import * +from tiramisu.setting import ALLOWED_LEADER_PROPERTIES +from re import compile as re_compile +from rougail.tiramisu import func, dict_env, load_functions, ConvertDynOptionDescription +load_functions('tests/dictionaries/../eosfunc/test.py') +try: + groups.namespace +except: + groups.addgroup('namespace') +ALLOWED_LEADER_PROPERTIES.add("basic") +ALLOWED_LEADER_PROPERTIES.add("standard") +ALLOWED_LEADER_PROPERTIES.add("advanced") +dict_env['default_1.rougail.var3'] = "{% if _.var2 is propertyerror %}\nvalue\n{% endif %}\n" +dict_env['default_2.rougail.var3'] = "{% if _.var2 is propertyerror %}\nvalue\n{% endif %}\n" +option_3 = StrOption(name="var1", doc="a first variable", default="value", properties=frozenset({"mandatory", "standard"}), informations={'type': 'string'}) +option_4 = StrOption(name="var2", doc="a second variable", properties=frozenset({"basic", "mandatory", Calculation(func['variable_to_property'], Params((ParamValue("disabled"), ParamOption(option_3)), kwargs={'when': ParamValue("value"), 'inverse': ParamValue(False)}), help_function=func['variable_to_property'])}), informations={'type': 'string'}) +option_5 = StrOption(name="var3", doc="a third variable", default=Calculation(func['jinja_to_function'], Params((), kwargs={'__internal_jinja': ParamValue("default_1.rougail.var3"), '__internal_type': ParamValue("string"), '__internal_multi': ParamValue(False), '__internal_files': ParamValue(['tests/dictionaries/04_1default_calculation_hidden_2/dictionaries/rougail/00-base.yml']), '__internal_attribute': ParamValue("default"), '__internal_variable': ParamValue("1.rougail.var3"), '_.var2': ParamOption(option_4, notraisepropertyerror=True)})), properties=frozenset({"mandatory", "standard"}), informations={'type': 'string'}) +optiondescription_2 = OptionDescription(name="rougail", doc="Rougail", group_type=groups.namespace, children=[option_3, option_4, option_5], properties=frozenset({"basic"})) +optiondescription_1 = OptionDescription(name="1", doc="1", children=[optiondescription_2], properties=frozenset({"basic"})) +option_8 = StrOption(name="var1", doc="a first variable", default="value", properties=frozenset({"mandatory", "standard"}), informations={'type': 'string'}) +option_9 = StrOption(name="var2", doc="a second variable", properties=frozenset({"basic", "mandatory", Calculation(func['variable_to_property'], Params((ParamValue("disabled"), ParamOption(option_8)), kwargs={'when': ParamValue("value"), 'inverse': ParamValue(False)}), help_function=func['variable_to_property'])}), informations={'type': 'string'}) +option_10 = StrOption(name="var3", doc="a third variable", default=Calculation(func['jinja_to_function'], Params((), kwargs={'__internal_jinja': ParamValue("default_2.rougail.var3"), '__internal_type': ParamValue("string"), '__internal_multi': ParamValue(False), '__internal_files': ParamValue(['tests/dictionaries/04_1default_calculation_hidden_2/dictionaries/rougail/00-base.yml']), '__internal_attribute': ParamValue("default"), '__internal_variable': ParamValue("2.rougail.var3"), '_.var2': ParamOption(option_9, notraisepropertyerror=True)})), properties=frozenset({"mandatory", "standard"}), informations={'type': 'string'}) +optiondescription_7 = OptionDescription(name="rougail", doc="Rougail", group_type=groups.namespace, children=[option_8, option_9, option_10], properties=frozenset({"basic"})) +optiondescription_6 = OptionDescription(name="2", doc="2", children=[optiondescription_7], properties=frozenset({"basic"})) +option_0 = OptionDescription(name="baseoption", doc="baseoption", children=[optiondescription_1, optiondescription_6]) diff --git a/tests/dictionaries/04_1default_calculation_hidden_2/tiramisu/no_namespace.py b/tests/dictionaries/04_1default_calculation_hidden_2/tiramisu/no_namespace.py new file mode 100644 index 000000000..7ce1a7b3a --- /dev/null +++ b/tests/dictionaries/04_1default_calculation_hidden_2/tiramisu/no_namespace.py @@ -0,0 +1,13 @@ +from tiramisu import * +from tiramisu.setting import ALLOWED_LEADER_PROPERTIES +from re import compile as re_compile +from rougail.tiramisu import func, dict_env, load_functions, ConvertDynOptionDescription +load_functions('tests/dictionaries/../eosfunc/test.py') +ALLOWED_LEADER_PROPERTIES.add("basic") +ALLOWED_LEADER_PROPERTIES.add("standard") +ALLOWED_LEADER_PROPERTIES.add("advanced") +dict_env['default_var3'] = "{% if _.var2 is propertyerror %}\nvalue\n{% endif %}\n" +option_1 = StrOption(name="var1", doc="a first variable", default="value", properties=frozenset({"mandatory", "standard"}), informations={'type': 'string'}) +option_2 = StrOption(name="var2", doc="a second variable", properties=frozenset({"basic", "mandatory", Calculation(func['variable_to_property'], Params((ParamValue("disabled"), ParamOption(option_1)), kwargs={'when': ParamValue("value"), 'inverse': ParamValue(False)}), help_function=func['variable_to_property'])}), informations={'type': 'string'}) +option_3 = StrOption(name="var3", doc="a third variable", default=Calculation(func['jinja_to_function'], Params((), kwargs={'__internal_jinja': ParamValue("default_var3"), '__internal_type': ParamValue("string"), '__internal_multi': ParamValue(False), '__internal_files': ParamValue(['tests/dictionaries/04_1default_calculation_hidden_2/dictionaries/rougail/00-base.yml']), '__internal_attribute': ParamValue("default"), '__internal_variable': ParamValue("var3"), '_.var2': ParamOption(option_2, notraisepropertyerror=True)})), properties=frozenset({"mandatory", "standard"}), informations={'type': 'string'}) +option_0 = OptionDescription(name="baseoption", doc="baseoption", children=[option_1, option_2, option_3])