pyhOn/pyhon/parameter.py

129 lines
3.4 KiB
Python
Raw Normal View History

2023-02-12 17:41:38 -07:00
class HonParameter:
def __init__(self, key, attributes):
self._key = key
self._category = attributes.get("category")
self._typology = attributes.get("typology")
self._mandatory = attributes.get("mandatory")
self._value = ""
2023-02-18 14:25:51 -07:00
@property
def key(self):
return self._key
2023-02-12 17:41:38 -07:00
@property
def value(self):
return self._value if self._value is not None else "0"
2023-03-04 14:19:48 -07:00
@property
def category(self):
return self._category
@property
def typology(self):
return self._typology
@property
def mandatory(self):
return self._mandatory
2023-02-12 17:41:38 -07:00
class HonParameterFixed(HonParameter):
def __init__(self, key, attributes):
super().__init__(key, attributes)
self._value = attributes.get("fixedValue", None)
2023-02-12 17:41:38 -07:00
2023-02-19 11:43:41 -07:00
def __repr__(self):
return f"{self.__class__} (<{self.key}> fixed)"
2023-02-18 14:25:51 -07:00
@property
def value(self):
2023-03-03 10:24:19 -07:00
return self._value if self._value is not None else "0"
2023-02-18 14:25:51 -07:00
@value.setter
def value(self, value):
2023-02-18 19:30:48 -07:00
if not value == self._value:
raise ValueError("Can't change fixed value")
2023-02-18 14:25:51 -07:00
2023-02-12 17:41:38 -07:00
class HonParameterRange(HonParameter):
def __init__(self, key, attributes):
super().__init__(key, attributes)
2023-02-18 14:25:51 -07:00
self._min = int(attributes["minimumValue"])
self._max = int(attributes["maximumValue"])
self._step = int(attributes["incrementValue"])
self._default = int(attributes.get("defaultValue", self._min))
self._value = self._default
def __repr__(self):
2023-02-19 11:43:41 -07:00
return f"{self.__class__} (<{self.key}> [{self._min} - {self._max}])"
2023-02-18 14:25:51 -07:00
2023-02-18 19:30:48 -07:00
@property
def min(self):
return self._min
@property
def max(self):
return self._max
@property
def step(self):
return self._step
2023-02-18 14:25:51 -07:00
@property
def value(self):
return self._value if self._value is not None else self._min
@value.setter
def value(self, value):
value = int(value)
2023-02-18 19:30:48 -07:00
if self._min <= value <= self._max and not value % self._step:
2023-03-05 11:03:03 -07:00
self._value = value
2023-02-18 19:30:48 -07:00
else:
raise ValueError(f"Allowed: min {self._min} max {self._max} step {self._step}")
2023-02-12 17:41:38 -07:00
class HonParameterEnum(HonParameter):
def __init__(self, key, attributes):
super().__init__(key, attributes)
2023-02-18 14:25:51 -07:00
self._default = attributes.get("defaultValue")
2023-02-18 19:30:48 -07:00
self._value = self._default or "0"
2023-02-18 14:25:51 -07:00
self._values = attributes.get("enumValues")
def __repr__(self):
2023-02-19 11:43:41 -07:00
return f"{self.__class__} (<{self.key}> {self.values})"
2023-02-18 14:25:51 -07:00
@property
def values(self):
2023-03-10 17:10:27 -07:00
return sorted([str(value) for value in self._values])
2023-02-18 14:25:51 -07:00
@property
def value(self):
2023-03-03 10:24:19 -07:00
return self._value if self._value is not None else self.values[0]
2023-02-18 14:25:51 -07:00
@value.setter
def value(self, value):
2023-02-18 19:30:48 -07:00
if value in self.values:
2023-03-05 11:03:03 -07:00
self._value = value
2023-02-18 19:30:48 -07:00
else:
raise ValueError(f"Allowed values {self._value}")
2023-02-18 14:25:51 -07:00
class HonParameterProgram(HonParameterEnum):
2023-02-18 19:30:48 -07:00
def __init__(self, key, command):
super().__init__(key, {})
self._command = command
2023-03-10 17:10:27 -07:00
self._value = command._program
2023-02-18 19:30:48 -07:00
self._values = command._multi
2023-03-06 11:45:46 -07:00
self._typology = "enum"
2023-02-18 19:30:48 -07:00
@property
def value(self):
return self._value
@value.setter
def value(self, value):
if value in self.values:
self._command.set_program(value)
else:
2023-03-05 11:03:03 -07:00
raise ValueError(f"Allowed values {self._values}")