Skip to content

Commit

Permalink
stateengine plugin: introduce regex casting, used for conditionset co…
Browse files Browse the repository at this point in the history
…mparison for actions
  • Loading branch information
onkelandy committed Sep 25, 2024
1 parent 00a6737 commit 98c1461
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 5 deletions.
8 changes: 4 additions & 4 deletions stateengine/StateEngineAction.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
29 changes: 28 additions & 1 deletion stateengine/StateEngineValue.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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":
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 98c1461

Please sign in to comment.