MCPcopy Create free account
hub / github.com/aws/aws-cli / Keyword

Class Keyword

awscli/customizations/logs/ui.py:100–175  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

98
99
100class 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)

Callers 15

test_textMethod · 0.90
test_string_to_printMethod · 0.90
test_highlightMethod · 0.90
test_color_log_eventMethod · 0.90
test_print_log_eventsMethod · 0.90
test_h_key_bindingMethod · 0.90
test_c_key_bindingMethod · 0.90
test_metadata_updateMethod · 0.90

Calls

no outgoing calls

Tested by 14

test_textMethod · 0.72
test_string_to_printMethod · 0.72
test_highlightMethod · 0.72
test_color_log_eventMethod · 0.72
test_print_log_eventsMethod · 0.72
test_h_key_bindingMethod · 0.72
test_c_key_bindingMethod · 0.72
test_metadata_updateMethod · 0.72