From 98c146134fc93f5701a9ff10c6cd1a275ad62586 Mon Sep 17 00:00:00 2001 From: Onkel Andy Date: Thu, 26 Sep 2024 01:05:36 +0200 Subject: [PATCH] stateengine plugin: introduce regex casting, used for conditionset comparison for actions --- stateengine/StateEngineAction.py | 8 ++++---- stateengine/StateEngineValue.py | 29 ++++++++++++++++++++++++++++- 2 files changed, 32 insertions(+), 5 deletions(-) diff --git a/stateengine/StateEngineAction.py b/stateengine/StateEngineAction.py index 69250609d..70099d79d 100755 --- a/stateengine/StateEngineAction.py +++ b/stateengine/StateEngineAction.py @@ -74,10 +74,10 @@ def __init__(self, abitem, name: str): self.__delay = StateEngineValue.SeValue(self._abitem, "delay") self.__repeat = None self.__instanteval = None - self.nextconditionset = StateEngineValue.SeValue(self._abitem, "nextconditionset", True, "str") - self.conditionset = StateEngineValue.SeValue(self._abitem, "conditionset", True, "str") - self.previousconditionset = StateEngineValue.SeValue(self._abitem, "previousconditionset", True, "str") - self.previousstate_conditionset = StateEngineValue.SeValue(self._abitem, "previousstate_conditionset", True, "str") + self.nextconditionset = StateEngineValue.SeValue(self._abitem, "nextconditionset", True, "regex") + self.conditionset = StateEngineValue.SeValue(self._abitem, "conditionset", True, "regex") + self.previousconditionset = StateEngineValue.SeValue(self._abitem, "previousconditionset", True, "regex") + self.previousstate_conditionset = StateEngineValue.SeValue(self._abitem, "previousstate_conditionset", True, "regex") self.__mode = StateEngineValue.SeValue(self._abitem, "mode", True, "str") self.__order = StateEngineValue.SeValue(self._abitem, "order", False, "num") self._minagedelta = StateEngineValue.SeValue(self._abitem, "minagedelta") diff --git a/stateengine/StateEngineValue.py b/stateengine/StateEngineValue.py index b4ff3a3bb..244689933 100755 --- a/stateengine/StateEngineValue.py +++ b/stateengine/StateEngineValue.py @@ -54,7 +54,7 @@ def __init__(self, abitem, name, allow_value_list=False, value_type=None): self.__varname = None self.__template = None self.__issues = [] - self.__get_issues = {'cast_item': [], 'eval': [], 'regex': [], 'struct': [], 'var': [], 'item': []} + self.__get_issues = {'cast_item': [], 'cast_regex': [], 'eval': [], 'regex': [], 'struct': [], 'var': [], 'item': []} self._additional_sources = [] self.itemsApi = Items.get_instance() self.__itemClass = Item @@ -64,6 +64,8 @@ def __init__(self, abitem, name, allow_value_list=False, value_type=None): self.__valid_valuetypes = ["value", "regex", "eval", "var", "item", "template", "struct"] if value_type == "str": self.__cast_func = StateEngineTools.cast_str + elif value_type == "regex": + self.__cast_func = self.cast_regex elif value_type == "num": self.__cast_func = StateEngineTools.cast_num elif value_type == "item": @@ -564,6 +566,31 @@ def get_text(self, prefix=None, suffix=None): value = value if suffix is None else value + suffix return value + # cast a value as regex. Throws ValueError if cast is not possible + # value: value to cast + # returns: value as regex + def cast_regex(self, value): + try: + _issue_dict = {} + _returnvalue = value + if isinstance(value, str): + try: + _returnvalue = re.compile(value, re.IGNORECASE) + except Exception as ex: + _issue = "Issue converting {} to regex: {}".format(value, ex) + _issue_dict = {str(value): _issue} + self._log_error(_issue) + if _issue_dict and _issue_dict not in self.__get_issues['cast_regex']: + self.__get_issues['cast_regex'].append(_issue_dict) + return _returnvalue + except Exception as ex: + _issue = "Can't cast {0} to regex! {1}".format(value, ex) + _issue_dict = {str(value): _issue} + if _issue_dict not in self.__get_issues['cast_regex']: + self.__get_issues['cast_regex'].append(_issue_dict) + self._log_error(_issue) + return value + # cast a value as item. Throws ValueError if cast is not possible # value: value to cast # returns: value as item or struct