add propertyerror as a test in jinja

This commit is contained in:
egarette@silique.fr 2024-10-29 10:58:04 +01:00
parent 06fcd1cef7
commit 6830437805
10 changed files with 129 additions and 20 deletions

View file

@ -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

View file

@ -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 %}

View file

@ -0,0 +1,10 @@
{
"rougail.var1": {
"owner": "default",
"value": "value"
},
"rougail.var3": {
"owner": "default",
"value": "value"
}
}

View file

@ -0,0 +1,4 @@
{
"rougail.var1": "value",
"rougail.var3": "value"
}

View file

@ -0,0 +1,10 @@
{
"rougail.var1": {
"owner": "default",
"value": "value"
},
"rougail.var3": {
"owner": "default",
"value": "value"
}
}

View file

@ -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])

View file

@ -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])

View file

@ -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])