fix: dynamic variable could be optional

This commit is contained in:
egarette@silique.fr 2024-11-25 09:17:27 +01:00
parent 9782bcde95
commit db768f94af
3 changed files with 27 additions and 10 deletions

View file

@ -332,6 +332,7 @@ class JinjaCalculation(Calculation):
}
if self.version != "1.0":
default["params"][true_path]["propertyerror"] = False
default["params"][true_path]["optional"] = True
if identifier:
default["params"][true_path]["identifier"] = identifier
return default
@ -441,6 +442,8 @@ class _VariableCalculation(Calculation):
"variable": variable,
"propertyerror": self.propertyerror,
}
if isinstance(self, VariableCalculation) and self.optional:
param["optional"] = self.optional
if identifier:
param["identifier"] = identifier
params = {None: [param]}
@ -490,6 +493,12 @@ class _VariableCalculation(Calculation):
raise DictConsistencyError(msg, 21, self.xmlfiles)
else:
params[None][0]["index"] = {"index": {"type": "index"}}
if self.path in objectspace.followers:
multi = objectspace.multis[self.path] == "submulti"
else:
multi = self.path in objectspace.multis
if multi:
params["__internal_multi"] = True
return params
@ -519,7 +528,8 @@ class VariableCalculation(_VariableCalculation):
class VariablePropertyCalculation(_VariableCalculation):
attribute_name: Literal[*PROPERTY_ATTRIBUTE]
# For python 3.9 attribute_name: Literal[*PROPERTY_ATTRIBUTE]
attribute_name: Literal["frozen", "hidden", "disabled", "mandatory"]
when: Any = undefined
when_not: Any = undefined
@ -640,7 +650,8 @@ class IdentifierCalculation(_IdentifierCalculation):
class IdentifierPropertyCalculation(_IdentifierCalculation):
attribute_name: Literal[*PROPERTY_ATTRIBUTE]
# for python 3.9 attribute_name: Literal[*PROPERTY_ATTRIBUTE]
attribute_name: Literal["frozen", "hidden", "disabled", "mandatory"]
when: Any = undefined
when_not: Any = undefined

View file

@ -27,10 +27,6 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
from typing import Any
try:
from tiramisu5 import DynOptionDescription, calc_value
except ModuleNotFoundError:
from tiramisu import DynOptionDescription, calc_value
from importlib.machinery import SourceFileLoader as _SourceFileLoader
from importlib.util import (
spec_from_loader as _spec_from_loader,
@ -40,8 +36,8 @@ from jinja2 import StrictUndefined, DictLoader
from jinja2.sandbox import SandboxedEnvironment
from rougail.object_model import CONVERT_OPTION
from rougail.error import display_xmlfiles
from tiramisu import function_waiting_for_error
from tiramisu.error import ValueWarning, ConfigError, PropertiesOptionError
from tiramisu import DynOptionDescription, calc_value, function_waiting_for_error
from tiramisu.error import ValueWarning, ConfigError, PropertiesOptionError, CancelParam
from .utils import normalize_family
@ -102,8 +98,10 @@ def load_functions(path):
func[function] = getattr(func_, function)
def rougail_calc_value(*args, __default_value=None, **kwargs):
def rougail_calc_value(*args, __default_value=None, __internal_multi=False, **kwargs):
values = calc_value(*args, **kwargs)
if values is None and __internal_multi:
values = []
if __default_value is not None and values in [None, []]:
return __default_value
return values
@ -128,9 +126,13 @@ def jinja_to_function(
if "." in key:
c_kw = kw
path, var = key.rsplit(".", 1)
if isinstance(value, CancelParam):
count_o_path = value.origin_path.count('.') - value.current_path.count('.')
path = path.rsplit(".", count_o_path)[0]
for subkey in path.split("."):
c_kw = c_kw.setdefault(subkey, {})
c_kw[var] = value
if not isinstance(value, CancelParam):
c_kw[var] = value
else:
if key in kw:
raise ConfigError(

View file

@ -339,6 +339,7 @@ class Common:
return self.build_option_param(
param["variable"],
param.get("propertyerror", True),
param.get("optional", False),
param.get("identifier"),
param.get("dynamic"),
param.get("whole", False),
@ -355,6 +356,7 @@ class Common:
self,
variable,
propertyerror,
optional: bool,
identifier: Optional[str],
dynamic,
whole: bool,
@ -377,6 +379,8 @@ class Common:
ident = self.convert_str(ident)
identifiers.append(str(ident))
params.append("[" + ", ".join(identifiers) + "]")
if optional:
params.append("optional=True")
else:
param_type = "ParamOption"
if not propertyerror: