pyhOn/pyhon/rules.py

146 lines
5.6 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
from pyhon.typedefs import Parameter
2023-05-21 00:25:43 +00:00
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-07-16 03:53:23 +00:00
@property
def rules(self) -> Dict[str, List[HonRule]]:
return self._rules
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)
2023-10-01 23:38:40 +00:00
else:
param_data = {"typology": "fixed", "fixedValue": param_data}
self._create_rule(
param_key, trigger_key, trigger_value, param_data, extra
)
2023-06-10 04:41:37 +00:00
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)
def _extra_rules_matches(self, rule: HonRule) -> bool:
if rule.extras:
for key, value in rule.extras.items():
if not self._command.parameters.get(key):
return False
if str(self._command.parameters.get(key)) != str(value):
return False
return True
def _apply_fixed(self, param: Parameter, value: str | float) -> None:
if isinstance(param, HonParameterEnum) and set(param.values) != {str(value)}:
param.values = [str(value)]
param.value = str(value)
elif isinstance(param, HonParameterRange):
2023-10-01 23:38:40 +00:00
if float(value) < param.min:
param.min = float(value)
elif float(value) > param.max:
param.max = float(value)
param.value = float(value)
return
param.value = str(value)
def _apply_enum(self, param: Parameter, rule: HonRule) -> None:
if not isinstance(param, HonParameterEnum):
return
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
2023-06-28 17:02:11 +00:00
def _add_trigger(self, parameter: "HonParameter", data: HonRule) -> None:
def apply(rule: HonRule) -> None:
if not self._extra_rules_matches(rule):
return
if not (param := self._command.parameters.get(rule.param_key)):
return
if fixed_value := rule.param_data.get("fixedValue", ""):
self._apply_fixed(param, fixed_value)
elif rule.param_data.get("typology") == "enum":
self._apply_enum(param, rule)
2023-06-10 04:41:37 +00:00
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)