pyhOn/pyhon/rules.py

118 lines
4.7 KiB
Python
Raw Permalink Normal View History

2023-05-21 00:25:43 +00:00
from dataclasses import dataclass
2023-06-10 04:41:37 +00:00
from typing import List, Dict, TYPE_CHECKING, Any, Optional
2023-05-21 00:25:43 +00:00
from pyhon.parameter.enum import HonParameterEnum
from pyhon.parameter.range import HonParameterRange
if TYPE_CHECKING:
from pyhon.commands import HonCommand
2023-06-28 17:02:11 +00:00
from pyhon.parameter.base import HonParameter
2023-05-21 00:25:43 +00:00
@dataclass
class HonRule:
trigger_key: str
trigger_value: str
param_key: str
2023-06-10 04:41:37 +00:00
param_data: Dict[str, Any]
extras: Optional[Dict[str, str]] = None
2023-05-21 00:25:43 +00:00
class HonRuleSet:
2023-06-28 17:02:11 +00:00
def __init__(self, command: "HonCommand", rule: Dict[str, Any]):
2023-05-21 00:25:43 +00:00
self._command: "HonCommand" = command
self._rules: Dict[str, List[HonRule]] = {}
self._parse_rule(rule)
2023-06-28 17:02:11 +00:00
def _parse_rule(self, rule: Dict[str, Any]) -> None:
2023-06-10 04:41:37 +00:00
for param_key, params in rule.items():
param_key = self._command.appliance.options.get(param_key, param_key)
for trigger_key, trigger_data in params.items():
self._parse_conditions(param_key, trigger_key, trigger_data)
2023-06-28 17:02:11 +00:00
def _parse_conditions(
self,
param_key: str,
trigger_key: str,
trigger_data: Dict[str, Any],
extra: Optional[Dict[str, str]] = None,
) -> None:
2023-06-10 04:41:37 +00:00
trigger_key = trigger_key.replace("@", "")
trigger_key = self._command.appliance.options.get(trigger_key, trigger_key)
for multi_trigger_value, param_data in trigger_data.items():
for trigger_value in multi_trigger_value.split("|"):
if isinstance(param_data, dict) and "typology" in param_data:
self._create_rule(
param_key, trigger_key, trigger_value, param_data, extra
)
elif isinstance(param_data, dict):
if extra is None:
extra = {}
extra[trigger_key] = trigger_value
for extra_key, extra_data in param_data.items():
self._parse_conditions(param_key, extra_key, extra_data, extra)
def _create_rule(
2023-06-28 17:02:11 +00:00
self,
param_key: str,
trigger_key: str,
trigger_value: str,
param_data: Dict[str, Any],
extras: Optional[Dict[str, str]] = None,
) -> None:
2023-06-10 04:41:37 +00:00
if param_data.get("fixedValue") == f"@{param_key}":
return
self._rules.setdefault(trigger_key, []).append(
HonRule(trigger_key, trigger_value, param_key, param_data, extras)
)
2023-06-28 17:02:11 +00:00
def _duplicate_for_extra_conditions(self) -> None:
new: Dict[str, List[HonRule]] = {}
2023-06-10 04:41:37 +00:00
for rules in self._rules.values():
for rule in rules:
if rule.extras is None:
continue
for key, value in rule.extras.items():
extras = rule.extras.copy()
extras.pop(key)
extras[rule.trigger_key] = rule.trigger_value
new.setdefault(key, []).append(
HonRule(key, value, rule.param_key, rule.param_data, extras)
2023-05-21 00:25:43 +00:00
)
2023-06-10 04:41:37 +00:00
for key, rules in new.items():
for rule in rules:
self._rules.setdefault(key, []).append(rule)
2023-06-28 17:02:11 +00:00
def _add_trigger(self, parameter: "HonParameter", data: HonRule) -> None:
def apply(rule: HonRule) -> None:
2023-06-10 04:41:37 +00:00
if rule.extras is not None:
for key, value in rule.extras.items():
if str(self._command.parameters.get(key)) != str(value):
return
if param := self._command.parameters.get(rule.param_key):
if value := rule.param_data.get("fixedValue", ""):
if isinstance(param, HonParameterEnum) and set(param.values) != {
str(value)
}:
param.values = [str(value)]
elif isinstance(param, HonParameterRange):
param.value = float(value)
return
param.value = str(value)
elif rule.param_data.get("typology") == "enum":
if isinstance(param, HonParameterEnum):
if enum_values := rule.param_data.get("enumValues"):
param.values = enum_values.split("|")
if default_value := rule.param_data.get("defaultValue"):
param.value = default_value
parameter.add_trigger(data.trigger_value, apply, data)
2023-05-21 00:25:43 +00:00
2023-06-28 17:02:11 +00:00
def patch(self) -> None:
2023-06-10 04:41:37 +00:00
self._duplicate_for_extra_conditions()
2023-05-21 00:25:43 +00:00
for name, parameter in self._command.parameters.items():
if name not in self._rules:
continue
2023-06-28 17:02:11 +00:00
for data in self._rules.get(name, []):
2023-06-10 04:41:37 +00:00
self._add_trigger(parameter, data)