From c49f322aec0d361a65dce125c52628ac0fdc075a Mon Sep 17 00:00:00 2001 From: Onkel Andy Date: Thu, 26 Sep 2024 01:07:09 +0200 Subject: [PATCH] stateengine plugin: correctly parse values in item, you can now also define regex, eval, etc. in an item(value)! --- stateengine/StateEngineValue.py | 338 +++++++++++++++++++------------- 1 file changed, 204 insertions(+), 134 deletions(-) diff --git a/stateengine/StateEngineValue.py b/stateengine/StateEngineValue.py index 244689933..4cb873448 100755 --- a/stateengine/StateEngineValue.py +++ b/stateengine/StateEngineValue.py @@ -164,11 +164,13 @@ def __resetvalue(self): # Set value # value: string indicating value or source of value # name: name of object ("time" is being handled differently) - def set(self, value, name="", reset=True, copyvalue=True): + def set(self, value, name="", reset=True, copyvalue=True, returnvalue=False): + if copyvalue is True: value = copy.copy(value) if reset: self.__resetvalue() + returnvalues = [] if isinstance(value, list): source = [] field_value = [] @@ -184,7 +186,8 @@ def set(self, value, name="", reset=True, copyvalue=True): f = val source.append(s) field_value.append(f) - self.__listorder.append("{}:{}".format(s, f)) + if not returnvalue: + self.__listorder.append("{}:{}".format(s, f)) if field_value[i] == "": source[i] = "value" field_value[i] = value[i] @@ -199,10 +202,11 @@ def set(self, value, name="", reset=True, copyvalue=True): "will be handled the same as the item type, e.g. string, bool, etc.", _issue, self.__valid_valuetypes, field_value[i]) source[i] = "value" - self.__type_listorder.append(source[i]) - self.__orig_listorder.append(val) - if source[i] == "value": - self.__listorder[i] = value[i] + if not returnvalue: + self.__type_listorder.append(source[i]) + self.__orig_listorder.append(val) + if source[i] == "value": + self.__listorder[i] = value[i] if source[i] == "template": if self.__template is None: self.__template = [] @@ -211,11 +215,14 @@ def set(self, value, name="", reset=True, copyvalue=True): if _template is not None: try: source[i], field_value[i] = StateEngineTools.partition_strip(_template, ":") - if val in self.__listorder and field_value[i] in self._abitem.templates: + if not returnvalue and val in self.__listorder and field_value[i] in self._abitem.templates: self.__listorder[self.__listorder.index(val)] = self._abitem.templates.get(field_value[i]) + elif returnvalue and val in returnvalues and field_value[i] in self._abitem.templates: + returnvalues[returnvalues.index(val)] = self._abitem.templates.get(field_value[i]) except Exception as ex: self._abitem.updatetemplates(field_value[i], None) - self.__listorder = [i for i in self.__listorder if i != val] + if not returnvalue: + self.__listorder = [i for i in self.__listorder if i != val] self._log_warning("Removing template {}: {}", field_value[i], ex) val, field_value[i], source[i] = None, None, None else: @@ -223,7 +230,8 @@ def set(self, value, name="", reset=True, copyvalue=True): if _issue not in self.__issues: self.__issues.append(_issue) self._log_warning(_issue) - self.__listorder = [i for i in self.__listorder if i != val] + if not returnvalue: + self.__listorder = [i for i in self.__listorder if i != val] source[i], field_value[i], val = None, None, None try: if isinstance(self.__template, list) and len(self.__template) == 1: @@ -233,17 +241,21 @@ def set(self, value, name="", reset=True, copyvalue=True): elif isinstance(value, str): source, field_value = StateEngineTools.partition_strip(value, ":") - self.__listorder.append("{}{}{}".format(source, ":" if field_value else "", field_value)) + if not returnvalue: + self.__listorder.append("{}{}{}".format(source, ":" if field_value else "", field_value)) if source == "template": self.__template = field_value _template = self._abitem.templates.get(self.__template) if _template is not None: try: source, field_value = StateEngineTools.partition_strip(_template, ":") - if value in self.__listorder and field_value in self._abitem.templates: + if not returnvalue and value in self.__listorder and field_value in self._abitem.templates: self.__listorder[self.__listorder.index(value)] = self._abitem.templates[self.__template] + elif returnvalue and value in returnvalues and field_value in self._abitem.templates: + returnvalues[returnvalues.index(value)] = self._abitem.templates[self.__template] except Exception as ex: - self.__listorder = [i for i in self.__listorder if i != value] + if not returnvalue: + self.__listorder = [i for i in self.__listorder if i != value] source, field_value, value = None, None, None self._abitem.updatetemplates(self.__template, None) self._log_warning("Removing template {}: {}", self.__template, ex) @@ -252,7 +264,8 @@ def set(self, value, name="", reset=True, copyvalue=True): if _issue not in self.__issues: self.__issues.append(_issue) self._log_warning(_issue) - self.__listorder = [i for i in self.__listorder if i != value] + if not returnvalue: + self.__listorder = [i for i in self.__listorder if i != value] source, field_value, value = None, None, None try: cond1 = source.lstrip('-').replace('.', '', 1).isdigit() @@ -274,10 +287,11 @@ def set(self, value, name="", reset=True, copyvalue=True): "will be handled the same as the item type, e.g. string, bool, etc.", _issue, self.__valid_valuetypes, field_value) source = "value" - if source == "value": - self.__listorder = [field_value] - self.__type_listorder.append(source) - self.__orig_listorder.append(value) + if not returnvalue: + if source == "value": + self.__listorder = [field_value] + self.__type_listorder.append(source) + self.__orig_listorder.append(value) else: source = "value" field_value = value @@ -316,8 +330,9 @@ def set(self, value, name="", reset=True, copyvalue=True): elif field_value[i] == "": field_value[i] = s s = "value" - self.__value = [] if self.__value is None else [self.__value] if not isinstance(self.__value, - list) else self.__value + if not returnvalue: + self.__value = [] if self.__value is None else [self.__value] if not isinstance(self.__value, + list) else self.__value if s == "value": cond3 = isinstance(field_value[i], str) and field_value[i].lstrip('-').replace('.', '', 1).isdigit() if cond3: @@ -330,10 +345,14 @@ def set(self, value, name="", reset=True, copyvalue=True): _value, _issue = self.__do_cast(field_value[i]) if _issue not in [[], None, [None], self.__issues]: self.__issues.append(_issue) - self.__value.append(_value) + if not returnvalue: + self.__value.append(_value) + else: + returnvalues.append(_value) else: self.__value.append(None) - self.__item = [] if self.__item is None else [self.__item] if not isinstance(self.__item, list) else self.__item + if not returnvalue: + self.__item = [] if self.__item is None else [self.__item] if not isinstance(self.__item, list) else self.__item if s == "item": _item, _issue = self._abitem.return_item(field_value[i]) if _issue not in [[], None, [None], self.__issues]: @@ -341,35 +360,47 @@ def set(self, value, name="", reset=True, copyvalue=True): if _issue_dict not in self.__get_issues['item']: self.__get_issues['item'].append(_issue_dict) self.__issues.append(_issue) - self.__item.append(None if s != "item" else self.__absolute_item(_item, field_value[i])) - self.__eval = [] if self.__eval is None else [self.__eval] if not isinstance(self.__eval, list) else self.__eval - self.__eval.append(None if s != "eval" else field_value[i]) - self.__regex = [] if self.__regex is None else [self.__regex] if not isinstance(self.__regex, list) else self.__regex - self.__regex.append(None if s != "regex" else field_value[i]) - self.__struct = [] if self.__struct is None else [self.__struct] if not isinstance(self.__struct, list) else self.__struct - self.__struct.append(None if s != "struct" else StateEngineStructs.create(self._abitem, field_value[i])) - self.__varname = [] if self.__varname is None else [self.__varname] if not isinstance(self.__varname, list) else self.__varname - self.__varname.append(None if s != "var" else field_value[i]) - - if self.__item: - self.__item = [i for i in self.__item if i is not None] - self.__item = self.__item[0] if len(self.__item) == 1 else None if len(self.__item) == 0 else self.__item - if self.__eval: - self.__eval = [i for i in self.__eval if i is not None] - self.__eval = self.__eval[0] if len(self.__eval) == 1 else None if len(self.__eval) == 0 else self.__eval - if self.__regex: - self.__regex = [i for i in self.__regex if i is not None] - self.__regex = self.__regex[0] if len(self.__regex) == 1 else None if len(self.__regex) == 0 else self.__regex - if self.__struct: - self.__struct = [i for i in self.__struct if i is not None] - self.__struct = None if len(self.__struct) == 0 else self.__struct - if self.__varname: - self.__varname = [i for i in self.__varname if i is not None] - self.__varname = self.__varname[0] if len(self.__varname) == 1 else None if len(self.__varname) == 0 else self.__varname - if self.__value: - self.__value = [i for i in self.__value if i is not None] - self.__value = self.__value[0] if len(self.__value) == 1 else None if len(self.__value) == 0 else self.__value - + returnvalues.append(_item) + if not returnvalue: + self.__item.append(None if s != "item" else self.__absolute_item(_item, field_value[i])) + self.__eval = [] if self.__eval is None else [self.__eval] if not isinstance(self.__eval, list) else self.__eval + self.__eval.append(None if s != "eval" else field_value[i]) + self.__regex = [] if self.__regex is None else [self.__regex] if not isinstance(self.__regex, list) else self.__regex + self.__regex.append(None if s != "regex" else field_value[i]) + self.__struct = [] if self.__struct is None else [self.__struct] if not isinstance(self.__struct, list) else self.__struct + self.__struct.append(None if s != "struct" else StateEngineStructs.create(self._abitem, field_value[i])) + self.__varname = [] if self.__varname is None else [self.__varname] if not isinstance(self.__varname, list) else self.__varname + self.__varname.append(None if s != "var" else field_value[i]) + else: + if s == "item": + returnvalues.append(self.__get_from_item(field_value[i])) + elif s == "struct": + returnvalues.append(self.__get_from_struct(field_value[i])) + elif s == "eval": + returnvalues.append(self.__get_eval(field_value[i])) + elif s == "regex": + returnvalues.append(self.__get_from_regex(field_value[i])) + elif s == "var": + returnvalues.append(self.__get_from_variable(field_value[i])) + if not returnvalue: + if self.__item: + self.__item = [i for i in self.__item if i is not None] + self.__item = self.__item[0] if len(self.__item) == 1 else None if len(self.__item) == 0 else self.__item + if self.__eval: + self.__eval = [i for i in self.__eval if i is not None] + self.__eval = self.__eval[0] if len(self.__eval) == 1 else None if len(self.__eval) == 0 else self.__eval + if self.__regex: + self.__regex = [i for i in self.__regex if i is not None] + self.__regex = self.__regex[0] if len(self.__regex) == 1 else None if len(self.__regex) == 0 else self.__regex + if self.__struct: + self.__struct = [i for i in self.__struct if i is not None] + self.__struct = None if len(self.__struct) == 0 else self.__struct + if self.__varname: + self.__varname = [i for i in self.__varname if i is not None] + self.__varname = self.__varname[0] if len(self.__varname) == 1 else None if len(self.__varname) == 0 else self.__varname + if self.__value: + self.__value = [i for i in self.__value if i is not None] + self.__value = self.__value[0] if len(self.__value) == 1 else None if len(self.__value) == 0 else self.__value else: if source == "item": _item, _issue = self._abitem.return_item(field_value) @@ -378,11 +409,24 @@ def set(self, value, name="", reset=True, copyvalue=True): if _issue_dict not in self.__get_issues['item']: self.__get_issues['item'].append(_issue_dict) self.__issues.append(_issue) - self.__item = None if source != "item" else self.__absolute_item(_item, field_value) - self.__eval = None if source != "eval" else field_value - self.__regex = None if source != "regex" else field_value - self.__struct = None if source != "struct" else StateEngineStructs.create(self._abitem, field_value) - self.__varname = None if source != "var" else field_value + returnvalues.append(_item) + if not returnvalue: + self.__item = None if source != "item" else self.__absolute_item(_item, field_value) + self.__eval = None if source != "eval" else field_value + self.__regex = None if source != "regex" else field_value + self.__struct = None if source != "struct" else StateEngineStructs.create(self._abitem, field_value) + self.__varname = None if source != "var" else field_value + else: + if source == "item": + returnvalues.append(self.__get_from_item(field_value)) + elif source == "struct": + returnvalues.append(self.__get_from_struct(field_value)) + elif source == "eval": + returnvalues.append(self.__get_eval(field_value)) + elif source == "regex": + returnvalues.append(self.__get_from_regex(field_value)) + elif source == "var": + returnvalues.append(self.__get_from_variable(field_value)) if source == "value": if isinstance(field_value, list) and not self.__allow_value_list: raise ValueError("{0}: value_in is not allowed, problem with {1}. Allowed = {2}".format( @@ -394,16 +438,24 @@ def set(self, value, name="", reset=True, copyvalue=True): field_value = True elif isinstance(field_value, str) and field_value.lower() in ['false', 'no']: field_value = False - self.__value, _issue = self.__do_cast(field_value) + if not returnvalue: + self.__value, _issue = self.__do_cast(field_value) + else: + val, _issue = self.__do_cast(field_value) + returnvalues.append(val) if _issue not in [[], None, [None], self.__issues]: self.__issues.append(_issue) else: self.__value = None self.__issues = StateEngineTools.flatten_list(self.__issues) + del value + if returnvalue: + returnvalues = StateEngineTools.flatten_list(returnvalues) + returnvalues = returnvalues[0] if len(returnvalues) == 1 else None if len(returnvalues) == 0 else returnvalues + return returnvalues, self.__issues self.__listorder = StateEngineTools.flatten_list(self.__listorder) self.__type_listorder = StateEngineTools.flatten_list(self.__type_listorder) self.__orig_listorder = StateEngineTools.flatten_list(self.__orig_listorder) - del value return self.__listorder, self.__type_listorder, self.__issues, self.__orig_listorder # Set cast function @@ -718,10 +770,12 @@ def __do_cast(self, value, item_id=None): return value, _issue # Determine value by using a struct - def __get_from_struct(self): + def __get_from_struct(self, struct_get=None): values = [] - if isinstance(self.__struct, list): - for val in self.__struct: + if struct_get is None: + struct_get = self.__struct + if isinstance(struct_get, list): + for val in struct_get: if val is not None: _newvalue, _issue = self.__do_cast(val) _issue_dict = {val: _issue} @@ -731,28 +785,28 @@ def __get_from_struct(self): if 'struct:{}'.format(val.property.path) in self.__listorder: self.__listorder[self.__listorder.index('struct:{}'.format(val.property.path))] = _newvalue else: - if self.__struct is not None: - _newvalue, _issue = self.__do_cast(self.__struct) - _issue_dict = {self.__struct: _issue} + if struct_get is not None: + _newvalue, _issue = self.__do_cast(struct_get) + _issue_dict = {struct_get: _issue} if _issue not in [[], None, [None]] and _issue_dict not in self.__get_issues['struct']: self.__get_issues['struct'].append(_issue_dict) - if 'struct:{}'.format(self.__regex) in self.__listorder: - self.__listorder[self.__listorder.index('struct:{}'.format(self.__struct))] = _newvalue + if 'struct:{}'.format(struct_get) in self.__listorder: + self.__listorder[self.__listorder.index('struct:{}'.format(struct_get))] = _newvalue values = _newvalue if values: return values try: - _newvalue, _issue = self.__do_cast(self.__struct) + _newvalue, _issue = self.__do_cast(struct_get) _issue_dict = {_newvalue: _issue} if _issue not in [[], None, [None]] and _issue_dict not in self.__get_issues['struct']: self.__get_issues['struct'].append(_issue_dict) - if 'struct:{}'.format(self.__struct) in self.__listorder: - self.__listorder[self.__listorder.index('struct:{}'.format(self.__struct))] = _newvalue + if 'struct:{}'.format(struct_get) in self.__listorder: + self.__listorder[self.__listorder.index('struct:{}'.format(struct_get))] = _newvalue values = _newvalue except Exception as ex: - values = self.__struct + values = struct_get _issue = "Problem while getting from struct '{0}': {1}.".format(values, ex) _issue_dict = {values: _issue} if _issue_dict not in self.__get_issues['struct']: @@ -761,28 +815,30 @@ def __get_from_struct(self): return values # Determine value by regular expression - def __get_from_regex(self): - if isinstance(self.__regex, list): + def __get_from_regex(self, regex_get=None): + if regex_get is None: + regex_get = self.__regex + if isinstance(regex_get, list): values = [] - for val in self.__regex: + for val in regex_get: _newvalue = re.compile(val, re.IGNORECASE) values.append(_newvalue) if 'regex:{}'.format(val) in self.__listorder: self.__listorder[self.__listorder.index('regex:{}'.format(val))] = _newvalue else: - _newvalue = re.compile(self.__regex, re.IGNORECASE) - if 'regex:{}'.format(self.__regex) in self.__listorder: - self.__listorder[self.__listorder.index('regex:{}'.format(self.__regex))] = _newvalue + _newvalue = re.compile(regex_get, re.IGNORECASE) + if 'regex:{}'.format(regex_get) in self.__listorder: + self.__listorder[self.__listorder.index('regex:{}'.format(regex_get))] = _newvalue values = _newvalue if values is not None: return values try: - _newvalue = re.compile(self.__regex, re.IGNORECASE) - if 'regex:{}'.format(self.__regex) in self.__listorder: - self.__listorder[self.__listorder.index('regex:{}'.format(self.__regex))] = _newvalue + _newvalue = re.compile(regex_get, re.IGNORECASE) + if 'regex:{}'.format(regex_get) in self.__listorder: + self.__listorder[self.__listorder.index('regex:{}'.format(regex_get))] = _newvalue values = _newvalue except Exception as ex: - values = self.__regex + values = regex_get _issue = "Problem while creating regex '{0}': {1}.".format(values, ex) _issue_dict = {values: _issue} if _issue_dict not in self.__get_issues['regex']: @@ -791,7 +847,7 @@ def __get_from_regex(self): return values # Determine value by executing eval-function - def __get_eval(self): + def __get_eval(self, eval_get=None): # noinspection PyUnusedLocal sh = self._sh # noinspection PyUnusedLocal @@ -802,37 +858,44 @@ def __get_eval(self): "get_variable('next.", 'get_variable("next.' ] - if isinstance(self.__eval, str): - self.__eval = StateEngineTools.parse_relative(self.__eval, 'sh.', ['()', '.property.']) - if "stateengine_eval" in self.__eval or "se_eval" in self.__eval: + set_eval = False + if eval_get is None: + eval_get = self.__eval + set_eval = True + if isinstance(eval_get, str): + if set_eval: + self.__eval = StateEngineTools.parse_relative(eval_get, 'sh.', ['()', '.property.']) + else: + eval_get = StateEngineTools.parse_relative(eval_get, 'sh.', ['()', '.property.']) + if "stateengine_eval" in eval_get or "se_eval" in eval_get: # noinspection PyUnusedLocal stateengine_eval = se_eval = StateEngineEval.SeEval(self._abitem) - self._log_debug("Checking eval: {0}", self.__eval) - if self.__eval in self._abitem.cache: + self._log_debug("Checking eval: {0}", eval_get) + if eval_get in self._abitem.cache: self._log_increase_indent() - result = self._abitem.cache.get(self.__eval) + result = self._abitem.cache.get(eval_get) self._log_debug("Loading eval from cache: {}", result) self._log_decrease_indent() - if 'eval:{}'.format(self.__eval) in self.__listorder: - self.__listorder[self.__listorder.index('eval:{}'.format(self.__eval))] = [result] + if 'eval:{}'.format(eval_get) in self.__listorder: + self.__listorder[self.__listorder.index('eval:{}'.format(eval_get))] = [result] return result self._log_increase_indent() try: - _newvalue, _issue = self.__do_cast(eval(self.__eval)) - _issue_dict = {StateEngineTools.get_eval_name(self.__eval): _issue} + _newvalue, _issue = self.__do_cast(eval(eval_get)) + _issue_dict = {StateEngineTools.get_eval_name(eval_get): _issue} if _issue not in [[], None, [None]] and _issue_dict not in self.__get_issues['eval']: self.__get_issues['eval'].append(_issue_dict) - if 'eval:{}'.format(self.__eval) in self.__listorder: - self.__listorder[self.__listorder.index('eval:{}'.format(self.__eval))] = [_newvalue] + if 'eval:{}'.format(eval_get) in self.__listorder: + self.__listorder[self.__listorder.index('eval:{}'.format(eval_get))] = [_newvalue] values = _newvalue self._log_decrease_indent() self._log_debug("Eval result: {0} ({1}).", values, type(values)) - if not any(pattern in self.__eval for pattern in patterns): - self._abitem.cache = {self.__eval: values} + if not any(pattern in eval_get for pattern in patterns): + self._abitem.cache = {eval_get: values} self._log_increase_indent() except Exception as ex: self._log_decrease_indent() - _name = StateEngineTools.get_eval_name(self.__eval) + _name = StateEngineTools.get_eval_name(eval_get) _issue = "Problem evaluating '{0}': {1}.".format(_name, ex) _issue_dict = {_name: _issue} if _issue_dict not in self.__get_issues['eval']: @@ -843,14 +906,14 @@ def __get_eval(self): finally: self._log_decrease_indent() else: - if isinstance(self.__eval, list): + if isinstance(eval_get, list): values = [] - for val in self.__eval: + for val in eval_get: try: val = val.replace("\n", "") except Exception: pass - self._log_debug("Checking eval {0} from list {1}.", val, self.__eval) + self._log_debug("Checking eval {0} from list {1}.", val, eval_get) self._log_increase_indent() if val in self._abitem.cache: result = self._abitem.cache.get(val) @@ -909,31 +972,31 @@ def __get_eval(self): self._abitem.cache = {val: value} self._log_decrease_indent() else: - self._log_debug("Checking eval (no str, no list): {0}.", self.__eval) - if self.__eval in self._abitem.cache: + self._log_debug("Checking eval (no str, no list): {0}.", eval_get) + if eval_get in self._abitem.cache: self._log_increase_indent() - result = self._abitem.cache.get(self.__eval) + result = self._abitem.cache.get(eval_get) self._log_debug("Loading eval (no str, no list) from cache: {}", result) self._log_decrease_indent() - if 'eval:{}'.format(self.__eval) in self.__listorder: - self.__listorder[self.__listorder.index('eval:{}'.format(self.__eval))] = [result] + if 'eval:{}'.format(eval_get) in self.__listorder: + self.__listorder[self.__listorder.index('eval:{}'.format(eval_get))] = [result] return result try: self._log_increase_indent() - _newvalue, _issue = self.__do_cast(self.__eval()) + _newvalue, _issue = self.__do_cast(eval_get()) _issue_dict = {_newvalue: _issue} if _issue not in [[], None, [None]] and _issue_dict not in self.__get_issues['eval']: self.__get_issues['eval'].append(_issue_dict) - if 'eval:{}'.format(self.__eval) in self.__listorder: - self.__listorder[self.__listorder.index('eval:{}'.format(self.__eval))] = [_newvalue] + if 'eval:{}'.format(eval_get) in self.__listorder: + self.__listorder[self.__listorder.index('eval:{}'.format(eval_get))] = [_newvalue] values = _newvalue self._log_decrease_indent() self._log_debug("Eval result (no str, no list): {0}.", values) - self._abitem.cache = {self.__eval: values} + self._abitem.cache = {eval_get: values} self._log_increase_indent() except Exception as ex: self._log_decrease_indent() - _name = StateEngineTools.get_eval_name(self.__eval) + _name = StateEngineTools.get_eval_name(eval_get) _issue = "Problem evaluating '{0}': {1}.".format(_name, ex) self._log_warning(_issue) self._log_increase_indent() @@ -944,10 +1007,12 @@ def __get_eval(self): return values # Determine value from item - def __get_from_item(self): - if isinstance(self.__item, list): + def __get_from_item(self, get_item=None): + if get_item is None: + get_item = self.__item + if isinstance(get_item, list): values = [] - for val in self.__item: + for val in get_item: _new_values = [] if val is None: _newvalue = None @@ -960,7 +1025,9 @@ def __get_from_item(self): checked_entry = checked_entry if isinstance(checked_entry, list) else [checked_entry] for entry in checked_entry: - _newvalue, _issue = self.__do_cast(entry) + # _newvalue, _issue = self.__do_cast(entry) + _newvalue, _issue = self.set(entry, reset=False, returnvalue=True) + self._log_develop("Return from set from item list: {}, issue {}", _newvalue, _issue) _issue_dict = {entry: _issue} if _issue not in [[], None, [None]] and _issue_dict not in self.__get_issues['item']: self.__get_issues['item'].append(_issue_dict) @@ -981,78 +1048,81 @@ def __get_from_item(self): if values is not None: return values else: - if self.__item is None: + if get_item is None: return None try: - checked_entry = StateEngineTools.convert_str_to_list(self.__item.property.value) + checked_entry = StateEngineTools.convert_str_to_list(get_item.property.value) except Exception as ex: self._log_warning("While getting from item: {}", ex) checked_entry = [] checked_entry = checked_entry if isinstance(checked_entry, list) else [checked_entry] _new_values = [] for entry in checked_entry: - _newvalue, _issue = self.__do_cast(entry) + #_newvalue, _issue = self.__do_cast(entry) + _newvalue, _issue = self.set(entry, reset=False, returnvalue=True) + self._log_develop("Return from set from item: {}, issue {}, listorder {}", _newvalue, _issue, self.__listorder) _issue_dict = {entry: _issue} if _issue not in [[], None, [None]] and _issue_dict not in self.__get_issues['item']: self.__get_issues['item'].append(_issue_dict) if _newvalue is not None: _new_values.append(_newvalue) _new_values = _new_values[0] if len(_new_values) == 1 else None if len(_new_values) == 0 else [_new_values] - search_item = 'item:{}'.format(self.__item) + search_item = 'item:{}'.format(get_item) if search_item in self.__listorder: index = self.__listorder.index(search_item) self.__listorder[index] = _new_values - if self.__item in self.__listorder: - index = self.__listorder.index(self.__item) + if get_item in self.__listorder: + index = self.__listorder.index(get_item) self.__listorder[index] = _new_values values = _new_values if values is not None: return values try: - _newvalue = self.__item.property.path - search_item = 'item:{}'.format(self.__item) + _newvalue = get_item.property.path + search_item = 'item:{}'.format(get_item) if search_item in self.__listorder: index = self.__listorder.index(search_item) self.__listorder[index] = _newvalue values = _newvalue except Exception as ex: - values = self.__item + values = get_item _issue = "Problem while reading item path '{0}': {1}.".format(values, ex) self._log_info(_issue) - _newvalue, _issue = self.__do_cast(values) + #_newvalue, _issue = self.__do_cast(values) + _newvalue, _issue = self.set(values, reset=False, returnvalue=True) + self._log_develop("Return from set from item end: {}, issue {}", _newvalue, _issue) _issue_dict = {_newvalue: _issue} if _issue not in [[], None, [None]] and _issue_dict not in self.__get_issues['item']: self.__get_issues['item'].append(_issue_dict) return _newvalue # Determine value from variable - def __get_from_variable(self): + def __get_from_variable(self, var_get=None): def update_value(varname): value = self._abitem.get_variable(varname) new_value, _issue = self.__do_cast(value) new_value = 'var:{}'.format(varname) if new_value == '' else new_value - if isinstance(new_value, str) and 'Unknown variable' in new_value: + if isinstance(new_value, str) and '(Unknown variable' in new_value: _issue = "There is a problem with your variable {}".format(new_value) - self._log_warning(_issue) new_value = '' _issue_dict = {varname: _issue} - if _issue not in [[], None, [None]] and _issue_dict not in self.__get_issues['var']: + if _issue_dict not in self.__get_issues['var']: self.__get_issues['var'].append(_issue_dict) - self._log_debug("Checking variable '{0}', value {1} from list {2}", - varname, new_value, self.__listorder) if 'var:{}'.format(varname) in self.__listorder: self.__listorder[self.__listorder.index('var:{}'.format(varname))] = new_value return new_value - + _issue = "" values = [] - - if isinstance(self.__varname, list): - for var in self.__varname: + if var_get is None: + var_get = self.__varname + if isinstance(var_get, list): + for var in var_get: self._log_debug("Checking variable in loop '{0}', value {1} from list {2}", var, values[-1], self.__listorder) values.append(update_value(var)) else: - values = update_value(self.__varname) - self._log_debug("Variable result: {0}", values) + values = update_value(var_get) + + self._log_debug("Variable result: '{}'.", values) return values