| 98 | |
| 99 | |
| 100 | class Keyword: |
| 101 | def __init__(self, text: str, color=None): |
| 102 | self._text = text.lower().strip() |
| 103 | self._color = color or self._get_color() |
| 104 | self._occurrence_count = 0 |
| 105 | self._regex_pattern = re.compile(re.escape(self._text)) |
| 106 | self._removal_regex_pattern = re.compile( |
| 107 | re.escape(self._color + self._text + colorama.Style.RESET_ALL) |
| 108 | ) |
| 109 | |
| 110 | @property |
| 111 | def text(self): |
| 112 | return self._text |
| 113 | |
| 114 | @property |
| 115 | def color(self): |
| 116 | return self._color |
| 117 | |
| 118 | def _get_color(self): |
| 119 | if self._text == "error": |
| 120 | return colorama.Fore.RED |
| 121 | elif self._text == "info": |
| 122 | return colorama.Fore.GREEN |
| 123 | elif self._text == "warn": |
| 124 | return colorama.Fore.YELLOW |
| 125 | |
| 126 | def get_string_to_print(self): |
| 127 | text_to_print = self._text |
| 128 | if len(self._text) > 15: |
| 129 | text_to_print = self._text[:10] + "..." |
| 130 | return ( |
| 131 | self._color |
| 132 | + text_to_print |
| 133 | + colorama.Style.RESET_ALL |
| 134 | + ": " |
| 135 | + str(self._occurrence_count) |
| 136 | ) |
| 137 | |
| 138 | def _add_color_to_string(self, log_event: str, start, end): |
| 139 | string_to_replace = log_event[start:end] |
| 140 | return ( |
| 141 | log_event[:start] |
| 142 | + self._color |
| 143 | + string_to_replace |
| 144 | + colorama.Style.RESET_ALL |
| 145 | + log_event[end:] |
| 146 | ) |
| 147 | |
| 148 | def _remove_color_from_string(self, log_event: str, start, end): |
| 149 | string_with_color = log_event[start:end] |
| 150 | string_without_color = string_with_color.split(self._color)[-1].split( |
| 151 | colorama.Style.RESET_ALL |
| 152 | )[0] |
| 153 | return log_event[:start] + string_without_color + log_event[end:] |
| 154 | |
| 155 | def highlight(self, log_event: str): |
| 156 | matchings = list(self._regex_pattern.finditer(log_event.lower()))[::-1] |
| 157 | self._occurrence_count += len(matchings) |
no outgoing calls