Best Python code snippet using autotest_python
monitors_util.py
Source:monitors_util.py
...60 formatted_msg = msg_type + "\t" + msg_template % params61 timestamped_msg = prepend_timestamp(formatted_msg, timestamp_format)62 print >> warnfile, timestamped_msg63 return alert64def _assert_is_all_blank_lines(lines, source_file):65 if sum(len(line.strip()) for line in lines) > 0:66 raise ValueError('warning patterns are not separated by blank lines '67 'in %s' % source_file)68def _read_overrides(overrides_file):69 """70 Read pattern overrides from overrides_file, which may be None. Overrides71 files are expected to have the format:72 <old regex> <newline> <new regex> <newline> <newline>73 old regex = a regex from the patterns file74 new regex = the regex to replace it75 Lines beginning with # are ignored.76 Returns a dict mapping old regexes to their replacements.77 """78 if not overrides_file:79 return {}80 overrides_lines = [line for line in overrides_file.readlines()81 if not line.startswith('#')]82 overrides_pairs = zip(overrides_lines[0::3], overrides_lines[1::3])83 _assert_is_all_blank_lines(overrides_lines[2::3], overrides_file)84 return dict(overrides_pairs)85def build_alert_hooks(patterns_file, warnfile, overrides_file=None):86 """Parse data in patterns file and transform into alert_hook list.87 Args:88 patterns_file: file; File to read alert pattern definitions from.89 warnfile: file; File to configure alert function to write warning to.90 Returns:91 list; Regex to alert function mapping.92 [(regex, alert_function), ...]93 """94 pattern_lines = patterns_file.readlines()95 # expected pattern format:96 # <msgtype> <newline> <regex> <newline> <alert> <newline> <newline>97 # msgtype = a string categorizing the type of the message - used for98 # enabling/disabling specific categories of warnings99 # regex = a python regular expression100 # alert = a string describing the alert message101 # if the regex matches the line, this displayed warning will102 # be the result of (alert % match.groups())103 patterns = zip(pattern_lines[0::4], pattern_lines[1::4],104 pattern_lines[2::4])105 _assert_is_all_blank_lines(pattern_lines[3::4], patterns_file)106 overrides_map = _read_overrides(overrides_file)107 hooks = []108 for msgtype, regex, alert in patterns:109 regex = overrides_map.get(regex, regex)110 regex = re.compile(regex.rstrip('\n'))111 alert_function = make_alert(warnfile, msgtype.rstrip('\n'),112 alert.rstrip('\n'))113 hooks.append((regex, alert_function))114 return hooks115def build_alert_hooks_from_path(patterns_path, warnfile):116 """117 Same as build_alert_hooks, but accepts a path to a patterns file and118 automatically finds the corresponding site overrides file if one exists.119 """...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!