pyhOn/pyhon/parameter/base.py

99 lines
2.9 KiB
Python
Raw Normal View History

2023-05-21 00:25:43 +00:00
from typing import Dict, Any, List, Tuple, Callable, TYPE_CHECKING
if TYPE_CHECKING:
from pyhon.rules import HonRule
2023-04-15 23:43:37 +00:00
class HonParameter:
2023-05-06 14:07:28 +00:00
def __init__(self, key: str, attributes: Dict[str, Any], group: str) -> None:
2023-04-15 23:43:37 +00:00
self._key = key
2023-07-11 22:05:27 +00:00
self._attributes = attributes
self._category: str = ""
self._typology: str = ""
self._mandatory: int = 0
2023-04-15 23:43:37 +00:00
self._value: str | float = ""
2023-05-06 14:07:28 +00:00
self._group: str = group
2023-06-28 17:02:11 +00:00
self._triggers: Dict[
str, List[Tuple[Callable[["HonRule"], None], "HonRule"]]
] = {}
2023-07-11 22:05:27 +00:00
self._set_attributes()
2023-07-12 17:36:32 +00:00
def _set_attributes(self) -> None:
2023-07-11 22:05:27 +00:00
self._category = self._attributes.get("category", "")
self._typology = self._attributes.get("typology", "")
self._mandatory = self._attributes.get("mandatory", 0)
2023-04-15 23:43:37 +00: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-28 04:17:43 +00:00
@value.setter
def value(self, value: str | float) -> None:
self._value = value
self.check_trigger(value)
2023-05-21 23:07:55 +00:00
@property
2023-06-09 00:09:20 +00:00
def intern_value(self) -> str:
return str(self.value)
2023-05-21 23:07:55 +00:00
2023-04-24 02:33:00 +00:00
@property
def values(self) -> List[str]:
2023-05-06 14:07:28 +00:00
return [str(self.value)]
2023-04-24 02:33:00 +00:00
2023-04-15 23:43:37 +00: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 14:07:28 +00:00
@property
def group(self) -> str:
return self._group
2023-05-21 00:25:43 +00:00
2023-06-28 17:02:11 +00:00
def add_trigger(
2023-07-12 17:36:32 +00:00
self, value: str, func: Callable[["HonRule"], None], data: "HonRule"
2023-06-28 17:02:11 +00:00
) -> None:
2023-05-21 00:25:43 +00:00
if self._value == value:
func(data)
self._triggers.setdefault(value, []).append((func, data))
2023-06-28 17:02:11 +00:00
def check_trigger(self, value: str | float) -> None:
2023-10-01 23:38:40 +00:00
triggers = {str(k).lower(): v for k, v in self._triggers.items()}
if str(value).lower() in triggers:
for trigger in triggers[str(value)]:
2023-05-21 00:25:43 +00:00
func, args = trigger
func(args)
@property
2023-06-28 17:02:11 +00:00
def triggers(self) -> Dict[str, Any]:
result: Dict[str, Any] = {}
2023-05-21 00:25:43 +00:00
for value, rules in self._triggers.items():
2023-06-10 04:41:37 +00:00
for _, rule in rules:
if rule.extras:
param = result.setdefault(value, {})
for extra_key, extra_value in rule.extras.items():
param = param.setdefault(extra_key, {}).setdefault(
extra_value, {}
)
else:
param = result.setdefault(value, {})
if fixed_value := rule.param_data.get("fixedValue"):
param[rule.param_key] = fixed_value
else:
param[rule.param_key] = rule.param_data.get("defaultValue", "")
2023-05-21 00:25:43 +00:00
return result
2023-07-11 22:05:27 +00:00
2023-07-12 17:36:32 +00:00
def reset(self) -> None:
2023-07-11 22:05:27 +00:00
self._set_attributes()