2023-04-16 05:13:37 +05:30
|
|
|
from typing import Dict, Any, List
|
|
|
|
|
|
|
|
from pyhon.parameter.base import HonParameter
|
|
|
|
|
|
|
|
|
2023-06-28 22:32:11 +05:30
|
|
|
def clean_value(value: str | float) -> str:
|
2023-05-22 04:37:55 +05:30
|
|
|
return str(value).strip("[]").replace("|", "_").lower()
|
|
|
|
|
|
|
|
|
2023-04-16 05:13:37 +05:30
|
|
|
class HonParameterEnum(HonParameter):
|
2023-05-06 19:37:28 +05:30
|
|
|
def __init__(self, key: str, attributes: Dict[str, Any], group: str) -> None:
|
|
|
|
super().__init__(key, attributes, group)
|
2023-07-12 23:06:32 +05:30
|
|
|
self._default: str | float = ""
|
|
|
|
self._value: str | float = ""
|
2023-07-12 03:35:27 +05:30
|
|
|
self._values: List[str] = []
|
|
|
|
self._set_attributes()
|
2023-05-22 04:37:55 +05:30
|
|
|
if self._default and clean_value(self._default.strip("[]")) not in self.values:
|
2023-05-15 22:59:42 +05:30
|
|
|
self._values.append(self._default)
|
2023-04-16 05:13:37 +05:30
|
|
|
|
2023-07-12 23:06:32 +05:30
|
|
|
def _set_attributes(self) -> None:
|
2023-07-12 03:35:27 +05:30
|
|
|
super()._set_attributes()
|
2023-07-12 23:06:32 +05:30
|
|
|
self._default = self._attributes.get("defaultValue", "")
|
2023-07-12 03:35:27 +05:30
|
|
|
self._value = self._default or "0"
|
2023-07-12 23:06:32 +05:30
|
|
|
self._values = self._attributes.get("enumValues", [])
|
2023-07-12 03:35:27 +05:30
|
|
|
|
2023-04-16 05:13:37 +05:30
|
|
|
def __repr__(self) -> str:
|
|
|
|
return f"{self.__class__} (<{self.key}> {self.values})"
|
|
|
|
|
|
|
|
@property
|
|
|
|
def values(self) -> List[str]:
|
2023-05-22 04:37:55 +05:30
|
|
|
return [clean_value(value) for value in self._values]
|
2023-04-16 05:13:37 +05:30
|
|
|
|
2023-05-21 05:55:43 +05:30
|
|
|
@values.setter
|
2023-06-28 22:32:11 +05:30
|
|
|
def values(self, values: List[str]) -> None:
|
2023-05-21 05:55:43 +05:30
|
|
|
self._values = values
|
|
|
|
|
2023-06-09 05:39:20 +05:30
|
|
|
@property
|
|
|
|
def intern_value(self) -> str:
|
|
|
|
return str(self._value) if self._value is not None else str(self.values[0])
|
|
|
|
|
2023-04-16 05:13:37 +05:30
|
|
|
@property
|
|
|
|
def value(self) -> str | float:
|
2023-05-22 04:37:55 +05:30
|
|
|
return clean_value(self._value) if self._value is not None else self.values[0]
|
2023-04-16 05:13:37 +05:30
|
|
|
|
|
|
|
@value.setter
|
|
|
|
def value(self, value: str) -> None:
|
|
|
|
if value in self.values:
|
|
|
|
self._value = value
|
2023-05-21 05:55:43 +05:30
|
|
|
self.check_trigger(value)
|
2023-04-16 05:13:37 +05:30
|
|
|
else:
|
2023-07-01 18:01:37 +05:30
|
|
|
raise ValueError(f"Allowed values: {self._values} But was: {value}")
|