feat: use suffix in property calculation

This commit is contained in:
egarette@silique.fr 2024-09-02 15:47:33 +02:00
parent 29cabb37c2
commit d360203bf8
2 changed files with 56 additions and 15 deletions

View file

@ -216,19 +216,16 @@ extra_dictionaries:
description: Extra namespaces description: Extra namespaces
type: leadership type: leadership
disabled: disabled:
type: jinja variable: main_namespace
jinja: | when: null
{% if not main_namespace %}
main_namespace not available
{% endif %}
names: names:
description: 'Extra namespace name' description: 'Extra namespace name'
alternative_name: x alternative_name: xn
multi: true multi: true
mandatory: false mandatory: false
directories: directories:
description: Directories where extra dictionary files are placed description: Directories where extra dictionary files are placed
alternative_name: d alternative_name: xd
type: unix_filename type: unix_filename
params: params:
allow_relative: true allow_relative: true
@ -238,7 +235,7 @@ extra_dictionaries:
multi: true multi: true
functions_files: functions_files:
description: File with functions description: File with functions
alternative_name: f alternative_name: c
type: unix_filename type: unix_filename
params: params:
allow_relative: true allow_relative: true

View file

@ -102,6 +102,7 @@ CONVERT_OPTION = {
example="644", example="644",
), ),
"choice": dict(opttype="ChoiceOption", example="a_choice"), "choice": dict(opttype="ChoiceOption", example="a_choice"),
"regexp": dict(opttype="RegexpOption"),
# #
"symlink": dict(opttype="SymLinkOption"), "symlink": dict(opttype="SymLinkOption"),
} }
@ -588,20 +589,61 @@ class InformationCalculation(Calculation):
} }
class SuffixCalculation(Calculation): class _SuffixCalculation(Calculation):
attribute_name: Literal["default", "choice", "dynamic"]
suffix: Optional[int] = None suffix: Optional[int] = None
def get_suffix(self) -> dict:
suffix = {"type": "suffix"}
if self.suffix is not None:
suffix["suffix"] = self.suffix
return suffix
class SuffixCalculation(_SuffixCalculation):
attribute_name: Literal["default", "choice", "dynamic"]
def to_function( def to_function(
self, self,
objectspace, objectspace,
) -> dict: ) -> dict:
suffix = {"type": "suffix"}
if self.suffix is not None:
suffix["suffix"] = self.suffix
return { return {
"function": "calc_value", "function": "calc_value",
"params": {None: [suffix]}, "params": {None: [self.get_suffix()]},
}
class SuffixPropertyCalculation(_SuffixCalculation):
attribute_name: Literal[*PROPERTY_ATTRIBUTE]
when: Any = undefined
when_not: Any = undefined
def to_function(
self,
objectspace,
) -> dict:
if self.version == "1.0":
msg = f'when is not allowed in format version 1.0 for attribute "{self.attribute_name}"'
raise DictConsistencyError(msg, 105, variable.xmlfiles)
if self.when is not undefined:
if self.when_not is not undefined:
msg = f'the suffix has an invalid attribute "{self.attribute_name}", when and when_not cannot set together'
raise DictConsistencyError(msg, 35, variable.xmlfiles)
when = self.when
inverse = False
elif self.when_not is not undefined:
when = self.when_not
inverse = True
else:
msg = f'the suffix has an invalid attribute "{self.attribute_name}", when and when_not cannot set together'
raise DictConsistencyError
params = {None: [self.attribute_name, self.get_suffix()],
"when": when,
"inverse": inverse,
}
return {
"function": "variable_to_property",
"params": params,
"help": "variable_to_property",
} }
@ -632,7 +674,7 @@ CALCULATION_PROPERTY_TYPES = {
"jinja": JinjaCalculation, "jinja": JinjaCalculation,
"variable": VariablePropertyCalculation, "variable": VariablePropertyCalculation,
"information": InformationCalculation, "information": InformationCalculation,
"suffix": SuffixCalculation, "suffix": SuffixPropertyCalculation,
"index": IndexCalculation, "index": IndexCalculation,
} }
BASETYPE_CALC = Union[StrictBool, StrictInt, StrictFloat, StrictStr, Calculation, None] BASETYPE_CALC = Union[StrictBool, StrictInt, StrictFloat, StrictStr, Calculation, None]
@ -667,6 +709,7 @@ class Variable(BaseModel):
description: Optional[str] = None description: Optional[str] = None
default: Union[List[BASETYPE_CALC], BASETYPE_CALC] = None default: Union[List[BASETYPE_CALC], BASETYPE_CALC] = None
choices: Optional[Union[List[BASETYPE_CALC], Calculation]] = None choices: Optional[Union[List[BASETYPE_CALC], Calculation]] = None
regexp: Optional[str] = None
params: Optional[List[Param]] = None params: Optional[List[Param]] = None
validators: Optional[List[Calculation]] = None validators: Optional[List[Calculation]] = None
multi: Optional[bool] = None multi: Optional[bool] = None
@ -678,6 +721,7 @@ class Variable(BaseModel):
auto_save: bool = False auto_save: bool = False
mode: Optional[str] = None mode: Optional[str] = None
test: Optional[list] = None test: Optional[list] = None
examples: Optional[list] = None
path: str path: str
namespace: Optional[str] namespace: Optional[str]
version: str version: str