pyhOn/pyhon/parameter/enum.py

41 lines
1.3 KiB
Python
Raw Normal View History

2023-04-15 17:43:37 -06:00
from typing import Dict, Any, List
from pyhon.parameter.base import HonParameter
2023-05-21 17:07:55 -06:00
def clean_value(value):
return str(value).strip("[]").replace("|", "_").lower()
2023-04-15 17:43:37 -06:00
class HonParameterEnum(HonParameter):
2023-05-06 08:07:28 -06:00
def __init__(self, key: str, attributes: Dict[str, Any], group: str) -> None:
super().__init__(key, attributes, group)
2023-04-15 17:43:37 -06:00
self._default = attributes.get("defaultValue")
self._value = self._default or "0"
self._values: List[str] = attributes.get("enumValues", [])
2023-05-21 17:07:55 -06:00
if self._default and clean_value(self._default.strip("[]")) not in self.values:
2023-05-15 11:29:42 -06:00
self._values.append(self._default)
2023-04-15 17:43:37 -06:00
def __repr__(self) -> str:
return f"{self.__class__} (<{self.key}> {self.values})"
@property
def values(self) -> List[str]:
2023-05-21 17:07:55 -06:00
return [clean_value(value) for value in self._values]
2023-04-15 17:43:37 -06:00
2023-05-20 18:25:43 -06:00
@values.setter
def values(self, values) -> None:
self._values = values
2023-04-15 17:43:37 -06:00
@property
def value(self) -> str | float:
2023-05-21 17:07:55 -06:00
return clean_value(self._value) if self._value is not None else self.values[0]
2023-04-15 17:43:37 -06:00
@value.setter
def value(self, value: str) -> None:
if value in self.values:
self._value = value
2023-05-20 18:25:43 -06:00
self.check_trigger(value)
2023-04-15 17:43:37 -06:00
else:
2023-05-20 18:25:43 -06:00
raise ValueError(f"Allowed values {self._values}")