From 2788a3ac917b821d36e92ce26bcd482d1d744493 Mon Sep 17 00:00:00 2001 From: Andre Basche Date: Wed, 12 Jul 2023 00:05:27 +0200 Subject: [PATCH] Reset commands to defaults --- pyhon/commands.py | 4 ++++ pyhon/parameter/base.py | 18 ++++++++++++++---- pyhon/parameter/enum.py | 13 ++++++++++--- pyhon/parameter/fixed.py | 7 ++++++- pyhon/parameter/range.py | 17 +++++++++++++---- 5 files changed, 47 insertions(+), 12 deletions(-) diff --git a/pyhon/commands.py b/pyhon/commands.py index a00b123..d76c897 100644 --- a/pyhon/commands.py +++ b/pyhon/commands.py @@ -169,3 +169,7 @@ class HonCommand: else: result[name] = parameter return result + + def reset(self): + for parameter in self._parameters.values(): + parameter.reset() diff --git a/pyhon/parameter/base.py b/pyhon/parameter/base.py index 6bf81ca..397b9f0 100644 --- a/pyhon/parameter/base.py +++ b/pyhon/parameter/base.py @@ -7,14 +7,21 @@ if TYPE_CHECKING: class HonParameter: def __init__(self, key: str, attributes: Dict[str, Any], group: str) -> None: self._key = key - self._category: str = attributes.get("category", "") - self._typology: str = attributes.get("typology", "") - self._mandatory: int = attributes.get("mandatory", 0) + self._attributes = attributes + self._category: str = "" + self._typology: str = "" + self._mandatory: int = 0 self._value: str | float = "" self._group: str = group self._triggers: Dict[ str, List[Tuple[Callable[["HonRule"], None], "HonRule"]] ] = {} + self._set_attributes() + + def _set_attributes(self): + self._category = self._attributes.get("category", "") + self._typology = self._attributes.get("typology", "") + self._mandatory = self._attributes.get("mandatory", 0) @property def key(self) -> str: @@ -54,7 +61,7 @@ class HonParameter: return self._group def add_trigger( - self, value: str, func: Callable[["HonRule"], None], data: "HonRule" + self, value: str, func: Callable[["HonRule"], None], data: "HonRule" ) -> None: if self._value == value: func(data) @@ -85,3 +92,6 @@ class HonParameter: param[rule.param_key] = rule.param_data.get("defaultValue", "") return result + + def reset(self): + self._set_attributes() diff --git a/pyhon/parameter/enum.py b/pyhon/parameter/enum.py index 9bc4698..45309f0 100644 --- a/pyhon/parameter/enum.py +++ b/pyhon/parameter/enum.py @@ -10,12 +10,19 @@ def clean_value(value: str | float) -> str: class HonParameterEnum(HonParameter): def __init__(self, key: str, attributes: Dict[str, Any], group: str) -> None: super().__init__(key, attributes, group) - self._default = attributes.get("defaultValue") - self._value = self._default or "0" - self._values: List[str] = attributes.get("enumValues", []) + self._default = "" + self._value = "" + self._values: List[str] = [] + self._set_attributes() if self._default and clean_value(self._default.strip("[]")) not in self.values: self._values.append(self._default) + def _set_attributes(self): + super()._set_attributes() + self._default = self._attributes.get("defaultValue") + self._value = self._default or "0" + self._values: List[str] = self._attributes.get("enumValues", []) + def __repr__(self) -> str: return f"{self.__class__} (<{self.key}> {self.values})" diff --git a/pyhon/parameter/fixed.py b/pyhon/parameter/fixed.py index ceb3046..a17df93 100644 --- a/pyhon/parameter/fixed.py +++ b/pyhon/parameter/fixed.py @@ -6,7 +6,12 @@ from pyhon.parameter.base import HonParameter class HonParameterFixed(HonParameter): def __init__(self, key: str, attributes: Dict[str, Any], group: str) -> None: super().__init__(key, attributes, group) - self._value = attributes.get("fixedValue", None) + self._value = None + self._set_attributes() + + def _set_attributes(self): + super()._set_attributes() + self._value = self._attributes.get("fixedValue", None) def __repr__(self) -> str: return f"{self.__class__} (<{self.key}> fixed)" diff --git a/pyhon/parameter/range.py b/pyhon/parameter/range.py index e27314e..ef0f58a 100644 --- a/pyhon/parameter/range.py +++ b/pyhon/parameter/range.py @@ -7,10 +7,19 @@ from pyhon.parameter.base import HonParameter class HonParameterRange(HonParameter): def __init__(self, key: str, attributes: Dict[str, Any], group: str) -> None: super().__init__(key, attributes, group) - self._min: float = str_to_float(attributes["minimumValue"]) - self._max: float = str_to_float(attributes["maximumValue"]) - self._step: float = str_to_float(attributes["incrementValue"]) - self._default: float = str_to_float(attributes.get("defaultValue", self.min)) + self._min: float = 0 + self._max: float = 0 + self._step: float = 0 + self._default: float = 0 + self._value: float = 0 + self._set_attributes() + + def _set_attributes(self): + super()._set_attributes() + self._min: float = str_to_float(self._attributes["minimumValue"]) + self._max: float = str_to_float(self._attributes["maximumValue"]) + self._step: float = str_to_float(self._attributes["incrementValue"]) + self._default: float = str_to_float(self._attributes.get("defaultValue", self.min)) self._value: float = self._default def __repr__(self) -> str: