2023-04-15 23:43:37 +00:00
|
|
|
from typing import Dict, Any, List
|
|
|
|
|
|
|
|
from pyhon.parameter.base import HonParameter
|
|
|
|
|
|
|
|
|
|
|
|
class HonParameterEnum(HonParameter):
|
2023-05-06 14:07:28 +00:00
|
|
|
def __init__(self, key: str, attributes: Dict[str, Any], group: str) -> None:
|
|
|
|
super().__init__(key, attributes, group)
|
2023-04-15 23:43:37 +00:00
|
|
|
self._default = attributes.get("defaultValue")
|
|
|
|
self._value = self._default or "0"
|
|
|
|
self._values: List[str] = attributes.get("enumValues", [])
|
2023-05-16 19:26:15 +00:00
|
|
|
if self._default and str(self._default) not in self.values:
|
2023-05-15 17:29:42 +00:00
|
|
|
self._values.append(self._default)
|
2023-04-15 23:43:37 +00:00
|
|
|
|
|
|
|
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}")
|