#!/usr/bin/env python # coding=utf-8 __author__ = "TrackMe Limited" __copyright__ = "Copyright 2022-2026, TrackMe Limited, U.K." __credits__ = "TrackMe Limited, U.K." __license__ = "TrackMe Limited, all rights reserved" __version__ = "0.1.0" __maintainer__ = "TrackMe Limited, U.K." __email__ = "support@trackme-solutions.com" __status__ = "PRODUCTION" # Standard library imports import os import sys import re import uuid # splunk home splunkhome = os.environ["SPLUNK_HOME"] # append lib sys.path.append(os.path.join(splunkhome, "etc", "apps", "trackme", "lib")) def get_uuid(): """ Function to return a unique uuid which is used to trace performance run_time of each subtask. """ return str(uuid.uuid4()) def remove_leading_spaces(text): """ Remove leading spaces from each line of a variable """ # split the text into lines, remove leading spaces from each line, and rejoin them cleaned_text = "\n".join([line.lstrip() for line in text.split("\n")]) return cleaned_text def decode_unicode(s, replace_with="?"): """ Decode strings with escaped bytes and clean non-printable characters, preserving UTF-8. """ def clean_text(text): """Remove or replace non-printable characters, preserving UTF-8.""" # This will preserve printable ASCII, extended ASCII (Latin-1 Supplement, etc.), and other Unicode characters # It will replace control characters (0x00-0x1F and 0x7F-0x9F) except newline (0x0A), carriage return (0x0D), and tab (0x09) return re.sub(r"[\x00-\x08\x0B\x0C\x0E-\x1F\x7F-\x9F]", replace_with, text) def replace_backslashes(text): """Replace backslashes with their Unicode representation, avoiding double encoding.""" return re.sub(r"(? รจ decoded = s.encode('latin-1').decode('unicode_escape') # Check if there are still any Unicode sequences that need processing if '\\u' in decoded: try: # Try to decode any remaining Unicode sequences final_decoded = decoded.encode('latin-1').decode('unicode_escape') return final_decoded except (UnicodeDecodeError, UnicodeEncodeError): # If that fails, use regex to handle remaining sequences final_decoded = re.sub(r'\\u([0-9a-fA-F]{4})', lambda m: chr(int(m.group(1), 16)), decoded) return final_decoded return decoded except (UnicodeDecodeError, UnicodeEncodeError): # If that fails, use our custom approach for any remaining sequences def restore_unicode_escapes(text): """Restore Unicode escape sequences to their original characters.""" # Handle other Unicode escape sequences text = re.sub(r'\\u([0-9a-fA-F]{4})', lambda m: chr(int(m.group(1), 16)), text) # Handle hex escape sequences text = re.sub(r'\\x([0-9a-fA-F]{2})', lambda m: chr(int(m.group(1), 16)), text) return text decoded = restore_unicode_escapes(s) return decoded def interpret_boolean(value): """ Function to interpret the boolean value: if the value is 1 or true (case insensitive), return True, otherwise return False """ if isinstance(value, bool): return value elif isinstance(value, str): if value.lower() == "true" or value == "1": return True else: return False elif isinstance(value, int): if value == 1: return True else: return False else: return False def strict_interpret_boolean(value): """ Standardize a value to a proper boolean. Accepts: - String 'true'/'True' or 'false'/'False' - String '0' or '1' - Integer 0 or 1 - Boolean True or False Returns: - Boolean True or False Raises: - ValueError if the input cannot be converted to a boolean """ if isinstance(value, bool): return value if isinstance(value, str): value = value.lower() if value in ("true", "1"): return True if value in ("false", "0"): return False if isinstance(value, int): return bool(value) raise ValueError("Value must be one of: true/True/1 or false/False/0") def update_wildcard(object_value): """ Update wildcard in the object value and replace it with '.*' so we interpret it as regex """ # This regex will find '*' that are not preceded by a dot pattern = r"(?