pyhOn/pyhon/parameter/base.py

66 lines
1.8 KiB
Python
Raw Normal View History

2023-05-21 02:25:43 +02:00
from typing import Dict, Any, List, Tuple, Callable, TYPE_CHECKING
if TYPE_CHECKING:
from pyhon.rules import HonRule
2023-04-16 01:43:37 +02:00
class HonParameter:
2023-05-06 16:07:28 +02:00
def __init__(self, key: str, attributes: Dict[str, Any], group: str) -> None:
2023-04-16 01:43:37 +02:00
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 = ""
2023-05-06 16:07:28 +02:00
self._group: str = group
2023-05-21 02:25:43 +02:00
self._triggers: Dict[str, List[Tuple[Callable, "HonRule"]]] = {}
2023-04-16 01:43:37 +02:00
@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"
2023-05-22 01:07:55 +02:00
@property
def intern_value(self) -> str | float:
return str(self._value) if self._value is not None else ""
2023-04-24 04:33:00 +02:00
@property
def values(self) -> List[str]:
2023-05-06 16:07:28 +02:00
return [str(self.value)]
2023-04-24 04:33:00 +02:00
2023-04-16 01:43:37 +02:00
@property
def category(self) -> str:
return self._category
@property
def typology(self) -> str:
return self._typology
@property
def mandatory(self) -> int:
return self._mandatory
2023-05-06 16:07:28 +02:00
@property
def group(self) -> str:
return self._group
2023-05-21 02:25:43 +02:00
def add_trigger(self, value, func, data):
if self._value == value:
func(data)
self._triggers.setdefault(value, []).append((func, data))
def check_trigger(self, value) -> None:
if str(value) in self._triggers:
for trigger in self._triggers[str(value)]:
func, args = trigger
func(args)
@property
def triggers(self):
result = {}
for value, rules in self._triggers.items():
result[value] = {rule.param_key: rule.param_value for _, rule in rules}
return result