From 2f0775178859aac47236d2fb2f4e397b75f7d05b Mon Sep 17 00:00:00 2001 From: Mahmudul Alam Date: Thu, 12 Mar 2026 10:45:34 +0600 Subject: [PATCH] Added post index support to return single value in data collector --- .../BuiltInFunctionSharedResources.py | 49 +++++++++++++++++-- 1 file changed, 44 insertions(+), 5 deletions(-) diff --git a/Framework/Built_In_Automation/Shared_Resources/BuiltInFunctionSharedResources.py b/Framework/Built_In_Automation/Shared_Resources/BuiltInFunctionSharedResources.py index 2f0062841..103de7b0b 100644 --- a/Framework/Built_In_Automation/Shared_Resources/BuiltInFunctionSharedResources.py +++ b/Framework/Built_In_Automation/Shared_Resources/BuiltInFunctionSharedResources.py @@ -566,6 +566,7 @@ class a: # Data collector with pattern. # Match with the following pattern. # var_name{pattern1}{pattern2}{...} + # Optional post-indexing: var_name{pattern}[0] to get single value name = name[: name.find("{")] val = Get_Shared_Variables(name, log=False) @@ -573,16 +574,31 @@ class a: if val == "zeuz_failed": return "zeuz_failed" + # Separate {} collector patterns from trailing [] post-indices + rest = copy_of_name[copy_of_name.find("{"):] + last_brace = rest.rfind("}") + trailing = rest[last_brace + 1:] if last_brace >= 0 else "" + collector_patterns = re.findall(r"\{(.*?)\}", rest) + post_indices = re.findall(r"\[(.*?)\]", trailing) + result = [] - for idx in indices: + for idx in collector_patterns: result.append(data_collector.collect(idx, val, "pattern")) - if len(indices) > 1: + if len(collector_patterns) > 1: result = list(zip(*result)) - elif len(indices) == 1: + elif len(collector_patterns) == 1: result = result[0] + # Apply post-indexing if specified (e.g., var{pattern}[0] to get single value) + for idx in post_indices: + try: + result = result[int(idx)] + except (ValueError, IndexError, TypeError) as e: + CommonUtil.ExecLog(sModuleInfo, "Post-index [%s] could not be applied to data collector result: %s" % (idx, str(e)), 3) + return "zeuz_failed" + # send variable value in report logs and terminal if str(shared_variables['zeuz_enable_variable_logging']).lower() in {"on", "yes", "true", "1"}: CommonUtil.AddVariableToLog(sModuleInfo, copy_of_name, result) @@ -595,6 +611,7 @@ class a: # Data collector with keys. # Match with the following pattern. # var_name(pattern1)(pattern2)(...) + # Optional post-indexing: var_name(key)[0] to get single value name = name[: name.find("(")] val = Get_Shared_Variables(name, log=False) @@ -602,14 +619,36 @@ class a: if val == "zeuz_failed": return "zeuz_failed" + # Separate () collector patterns from trailing [] post-indices + rest = copy_of_name[copy_of_name.find("("):] + last_paren = rest.rfind(")") + trailing = rest[last_paren + 1:] if last_paren >= 0 else "" + collector_patterns = re.findall(r"\((.*?)\)", rest) + post_indices = re.findall(r"\[(.*?)\]", trailing) + result = [] - for idx in indices: + for idx in collector_patterns: result.append(data_collector.collect(idx, val, "key")) - if len(indices) == 1: + if len(collector_patterns) == 1: result = result[0] + # Apply post-indexing if specified (e.g., var(key)['line'][0] to get single value) + for idx in post_indices: + try: + result = result[int(idx)] + except ValueError: + # Try string key access (e.g., var(key)['line']) + try: + result = result[idx.strip("'\"")] + except (KeyError, TypeError, IndexError) as e: + CommonUtil.ExecLog(sModuleInfo, "Post-index [%s] could not be applied to key collector result: %s" % (idx, str(e)), 3) + return "zeuz_failed" + except (IndexError, TypeError) as e: + CommonUtil.ExecLog(sModuleInfo, "Post-index [%s] could not be applied to key collector result: %s" % (idx, str(e)), 3) + return "zeuz_failed" + if str(shared_variables['zeuz_enable_variable_logging']).lower() in {"on", "yes", "true", "1"}: CommonUtil.AddVariableToLog(sModuleInfo, copy_of_name, result)