From 03187745bfe1efb204ec8a6a4ec38f8efe0784eb Mon Sep 17 00:00:00 2001 From: Andre Basche Date: Sun, 16 Apr 2023 01:43:37 +0200 Subject: [PATCH] Split up parameters --- pyhon/appliance.py | 2 +- pyhon/commands.py | 12 ++- pyhon/parameter.py | 150 ------------------------------------ pyhon/parameter/__init__.py | 0 pyhon/parameter/base.py | 30 ++++++++ pyhon/parameter/enum.py | 29 +++++++ pyhon/parameter/fixed.py | 21 +++++ pyhon/parameter/program.py | 33 ++++++++ pyhon/parameter/range.py | 49 ++++++++++++ 9 files changed, 168 insertions(+), 158 deletions(-) delete mode 100644 pyhon/parameter.py create mode 100644 pyhon/parameter/__init__.py create mode 100644 pyhon/parameter/base.py create mode 100644 pyhon/parameter/enum.py create mode 100644 pyhon/parameter/fixed.py create mode 100644 pyhon/parameter/program.py create mode 100644 pyhon/parameter/range.py diff --git a/pyhon/appliance.py b/pyhon/appliance.py index 00e9716..d54df40 100644 --- a/pyhon/appliance.py +++ b/pyhon/appliance.py @@ -5,7 +5,7 @@ from typing import TYPE_CHECKING from pyhon import helper from pyhon.commands import HonCommand -from pyhon.parameter import HonParameterFixed +from pyhon.parameter.fixed import HonParameterFixed if TYPE_CHECKING: from pyhon import HonAPI diff --git a/pyhon/commands.py b/pyhon/commands.py index c7c7537..03f70d0 100644 --- a/pyhon/commands.py +++ b/pyhon/commands.py @@ -1,12 +1,10 @@ from typing import Optional, Dict, Any, List, TYPE_CHECKING -from pyhon.parameter import ( - HonParameterFixed, - HonParameterEnum, - HonParameterRange, - HonParameterProgram, - HonParameter, -) +from pyhon.parameter.base import HonParameter +from pyhon.parameter.enum import HonParameterEnum +from pyhon.parameter.fixed import HonParameterFixed +from pyhon.parameter.program import HonParameterProgram +from pyhon.parameter.range import HonParameterRange if TYPE_CHECKING: from pyhon import HonAPI diff --git a/pyhon/parameter.py b/pyhon/parameter.py deleted file mode 100644 index e5fba67..0000000 --- a/pyhon/parameter.py +++ /dev/null @@ -1,150 +0,0 @@ -from typing import Dict, Any, List, TYPE_CHECKING - -if TYPE_CHECKING: - from pyhon.commands import HonCommand - - -def str_to_float(string: str | float) -> float: - try: - return int(string) - except ValueError: - return float(str(string).replace(",", ".")) - - -class HonParameter: - def __init__(self, key: str, attributes: Dict[str, Any]) -> None: - self._key = key - self._category: str = attributes.get("category", "") - self._typology: str = attributes.get("typology", "") - self._mandatory: int = attributes.get("mandatory", 0) - self._value: str | float = "" - - @property - def key(self) -> str: - return self._key - - @property - def value(self) -> str | float: - return self._value if self._value is not None else "0" - - @property - def category(self) -> str: - return self._category - - @property - def typology(self) -> str: - return self._typology - - @property - def mandatory(self) -> int: - return self._mandatory - - -class HonParameterFixed(HonParameter): - def __init__(self, key: str, attributes: Dict[str, Any]) -> None: - super().__init__(key, attributes) - self._value = attributes.get("fixedValue", None) - - def __repr__(self) -> str: - return f"{self.__class__} (<{self.key}> fixed)" - - @property - def value(self) -> str | float: - return self._value if self._value is not None else "0" - - @value.setter - def value(self, value): - if not value == self._value: - raise ValueError("Can't change fixed value") - - -class HonParameterRange(HonParameter): - def __init__(self, key: str, attributes: Dict[str, Any]) -> None: - super().__init__(key, attributes) - 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._value: float = self._default - - def __repr__(self): - return f"{self.__class__} (<{self.key}> [{self._min} - {self._max}])" - - @property - def min(self) -> float: - return self._min - - @property - def max(self) -> float: - return self._max - - @property - def step(self) -> float: - return self._step - - @property - def value(self) -> float: - return self._value if self._value is not None else self._min - - @value.setter - def value(self, value: float) -> None: - value = str_to_float(value) - if self._min <= value <= self._max and not value % self._step: - self._value = value - else: - raise ValueError( - f"Allowed: min {self._min} max {self._max} step {self._step}" - ) - - -class HonParameterEnum(HonParameter): - def __init__(self, key: str, attributes: Dict[str, Any]) -> None: - super().__init__(key, attributes) - self._default = attributes.get("defaultValue") - self._value = self._default or "0" - self._values: List[str] = attributes.get("enumValues", []) - - def __repr__(self) -> str: - return f"{self.__class__} (<{self.key}> {self.values})" - - @property - def values(self) -> List[str]: - return [str(value) for value in self._values] - - @property - def value(self) -> str | float: - return self._value if self._value is not None else self.values[0] - - @value.setter - def value(self, value: str) -> None: - if value in self.values: - self._value = value - else: - raise ValueError(f"Allowed values {self._value}") - - -class HonParameterProgram(HonParameterEnum): - _FILTER = ["iot_recipe", "iot_guided"] - - def __init__(self, key: str, command: "HonCommand") -> None: - super().__init__(key, {}) - self._command = command - self._value: str = command.program - self._values: List[str] = list(command.programs) - self._typology: str = "enum" - - @property - def value(self) -> str | float: - return self._value - - @value.setter - def value(self, value: str) -> None: - if value in self.values: - self._command.program = value - else: - raise ValueError(f"Allowed values {self._values}") - - @property - def values(self) -> List[str]: - values = [v for v in self._values if all(f not in v for f in self._FILTER)] - return sorted(values) diff --git a/pyhon/parameter/__init__.py b/pyhon/parameter/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/pyhon/parameter/base.py b/pyhon/parameter/base.py new file mode 100644 index 0000000..c2c818a --- /dev/null +++ b/pyhon/parameter/base.py @@ -0,0 +1,30 @@ +from typing import Dict, Any + + +class HonParameter: + def __init__(self, key: str, attributes: Dict[str, Any]) -> None: + self._key = key + self._category: str = attributes.get("category", "") + self._typology: str = attributes.get("typology", "") + self._mandatory: int = attributes.get("mandatory", 0) + self._value: str | float = "" + + @property + def key(self) -> str: + return self._key + + @property + def value(self) -> str | float: + return self._value if self._value is not None else "0" + + @property + def category(self) -> str: + return self._category + + @property + def typology(self) -> str: + return self._typology + + @property + def mandatory(self) -> int: + return self._mandatory diff --git a/pyhon/parameter/enum.py b/pyhon/parameter/enum.py new file mode 100644 index 0000000..c5afcda --- /dev/null +++ b/pyhon/parameter/enum.py @@ -0,0 +1,29 @@ +from typing import Dict, Any, List + +from pyhon.parameter.base import HonParameter + + +class HonParameterEnum(HonParameter): + def __init__(self, key: str, attributes: Dict[str, Any]) -> None: + super().__init__(key, attributes) + self._default = attributes.get("defaultValue") + self._value = self._default or "0" + self._values: List[str] = attributes.get("enumValues", []) + + def __repr__(self) -> str: + return f"{self.__class__} (<{self.key}> {self.values})" + + @property + def values(self) -> List[str]: + return [str(value) for value in self._values] + + @property + def value(self) -> str | float: + return self._value if self._value is not None else self.values[0] + + @value.setter + def value(self, value: str) -> None: + if value in self.values: + self._value = value + else: + raise ValueError(f"Allowed values {self._value}") diff --git a/pyhon/parameter/fixed.py b/pyhon/parameter/fixed.py new file mode 100644 index 0000000..c3f60ec --- /dev/null +++ b/pyhon/parameter/fixed.py @@ -0,0 +1,21 @@ +from typing import Dict, Any + +from pyhon.parameter.base import HonParameter + + +class HonParameterFixed(HonParameter): + def __init__(self, key: str, attributes: Dict[str, Any]) -> None: + super().__init__(key, attributes) + self._value = attributes.get("fixedValue", None) + + def __repr__(self) -> str: + return f"{self.__class__} (<{self.key}> fixed)" + + @property + def value(self) -> str | float: + return self._value if self._value is not None else "0" + + @value.setter + def value(self, value): + if not value == self._value: + raise ValueError("Can't change fixed value") diff --git a/pyhon/parameter/program.py b/pyhon/parameter/program.py new file mode 100644 index 0000000..a41603c --- /dev/null +++ b/pyhon/parameter/program.py @@ -0,0 +1,33 @@ +from typing import List, TYPE_CHECKING + +from pyhon.parameter.enum import HonParameterEnum + +if TYPE_CHECKING: + from pyhon.commands import HonCommand + + +class HonParameterProgram(HonParameterEnum): + _FILTER = ["iot_recipe", "iot_guided"] + + def __init__(self, key: str, command: "HonCommand") -> None: + super().__init__(key, {}) + self._command = command + self._value: str = command.program + self._values: List[str] = list(command.programs) + self._typology: str = "enum" + + @property + def value(self) -> str | float: + return self._value + + @value.setter + def value(self, value: str) -> None: + if value in self.values: + self._command.program = value + else: + raise ValueError(f"Allowed values {self._values}") + + @property + def values(self) -> List[str]: + values = [v for v in self._values if all(f not in v for f in self._FILTER)] + return sorted(values) diff --git a/pyhon/parameter/range.py b/pyhon/parameter/range.py new file mode 100644 index 0000000..d0d26d9 --- /dev/null +++ b/pyhon/parameter/range.py @@ -0,0 +1,49 @@ +from typing import Dict, Any + +from pyhon.parameter.base import HonParameter + + +def str_to_float(string: str | float) -> float: + try: + return int(string) + except ValueError: + return float(str(string).replace(",", ".")) + + +class HonParameterRange(HonParameter): + def __init__(self, key: str, attributes: Dict[str, Any]) -> None: + super().__init__(key, attributes) + 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._value: float = self._default + + def __repr__(self): + return f"{self.__class__} (<{self.key}> [{self._min} - {self._max}])" + + @property + def min(self) -> float: + return self._min + + @property + def max(self) -> float: + return self._max + + @property + def step(self) -> float: + return self._step + + @property + def value(self) -> float: + return self._value if self._value is not None else self._min + + @value.setter + def value(self, value: float) -> None: + value = str_to_float(value) + if self._min <= value <= self._max and not value % self._step: + self._value = value + else: + raise ValueError( + f"Allowed: min {self._min} max {self._max} step {self._step}" + )