Skip to content

Commit fd05c12

Browse files
committed
refactor: refactor colorization for summarize reboot checks too
1 parent 8dc6e57 commit fd05c12

File tree

1 file changed

+57
-27
lines changed

1 file changed

+57
-27
lines changed

Tools/PC/c3-submission-helpers/summarize-reboot-check-test.py

Lines changed: 57 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -33,29 +33,65 @@ class Input:
3333

3434

3535
class Color:
36-
high = "\033[94m"
37-
low = "\033[95m"
38-
medium = "\033[93m"
39-
critical = "\033[91m"
40-
other = "\033[96m"
41-
ok = "\033[92m"
42-
end = "\033[0m"
43-
bold = "\033[1m"
44-
gray = "\033[90m"
36+
def __init__(self, no_color=False) -> None:
37+
self.no_color = no_color
38+
39+
def critical(self, s: str):
40+
if self.no_color:
41+
return s
42+
return f"\033[91m{s}\033[0m"
43+
44+
def high(self, s: str):
45+
if self.no_color:
46+
return s
47+
return f"\033[94m{s}\033[0m"
48+
49+
def medium(self, s: str):
50+
if self.no_color:
51+
return s
52+
return f"\033[93m{s}\033[0m"
53+
54+
def low(self, s: str):
55+
if self.no_color:
56+
return s
57+
return f"\033[95m{s}\033[0m"
58+
59+
def other(self, s: str):
60+
if self.no_color:
61+
return s
62+
return f"\033[96m{s}\033[0m"
63+
64+
def ok(self, s: str):
65+
if self.no_color:
66+
return s
67+
return f"\033[92m{s}\033[0m"
68+
69+
def gray(self, s: str):
70+
if self.no_color:
71+
return s
72+
return f"\033[90m{s}\033[0m"
73+
74+
def bold(self, s: str):
75+
if self.no_color:
76+
return s
77+
return f"\033[1m{s}\033[0m"
78+
79+
80+
C = Color()
4581

4682

4783
class Log:
4884
@staticmethod
4985
def ok(*args: str):
50-
print(f"{Color.ok}[ OK ]{Color.end}", *args)
86+
print(C.ok("[ OK ]"), *args)
5187

5288
@staticmethod
5389
def warn(*args: str):
54-
print(f"{Color.medium}[ WARN ]{Color.end}", *args)
90+
print(C.medium("[ WARN ]"), *args)
5591

5692
@staticmethod
5793
def err(*args: str):
58-
print(f"{Color.critical}[ ERR ]{Color.end}", *args)
94+
print(C.critical("[ ERR ]"), *args)
5995

6096

6197
RunIndexToMessageMap = dict[int, list[str]]
@@ -198,7 +234,7 @@ def print_by_index(self) -> None:
198234

199235
def _default_title_transform(self, fail_type: str) -> str:
200236
fail_type_lower = fail_type.lower().replace("_", " ")
201-
color = getattr(Color, fail_type_lower, Color.medium)
237+
color = getattr(C, fail_type_lower, C.medium)
202238
known_name_tranforms = {
203239
"pci": "PCI device difference",
204240
"usb": "USB device difference",
@@ -210,7 +246,7 @@ def _default_title_transform(self, fail_type: str) -> str:
210246
else:
211247
capitalized = fail_type_lower.capitalize()
212248

213-
transformed_str = f"{color}{capitalized} errors:{Color.end}"
249+
transformed_str = color(f"{capitalized} errors:")
214250
return transformed_str
215251

216252
def _default_err_msg_transform(self, msg: str) -> str:
@@ -245,7 +281,7 @@ def _default_print_by_err(
245281
)
246282

247283
for err_msg in all_err_msg:
248-
print(SPACE, f"{Color.bold}{err_msg}{Color.end}")
284+
print(SPACE, C.bold(err_msg))
249285

250286
buffer = {
251287
err_msg: {
@@ -369,10 +405,10 @@ def _short_print(
369405

370406
for fail_type, results in boot_results.items():
371407
failed_runs = sorted(list(results.keys()))
372-
print(
373-
f"{prefix}{getattr(Color, fail_type.lower(), Color.medium)}"
374-
f"{fail_type.replace('_', ' ').title()} failures:{Color.end}"
408+
colorized: str = getattr(C, fail_type.lower(), C.medium)(
409+
f"{fail_type.replace('_', ' ').title()} failures:"
375410
)
411+
print(f"{prefix}{colorized}")
376412

377413
wrapped = textwrap.wrap(str(failed_runs), width=50)
378414
print(f"{prefix}{SPACE}- Failed runs: {wrapped[0]}")
@@ -425,8 +461,7 @@ class FwtsPrinter(TestResultPrinter):
425461
def print_by_err(self):
426462
def title_transform(fail_type: str):
427463
return (
428-
f"{getattr(Color, fail_type.lower())}"
429-
f"FWTS {fail_type} errors:{Color.end}"
464+
f"{getattr(C, fail_type.lower())(f'FWTS {fail_type} errors:')}"
430465
)
431466

432467
def err_msg_transform(msg: str):
@@ -694,12 +729,7 @@ def parse_args() -> Input:
694729
def main():
695730
args = parse_args()
696731

697-
if args.no_color:
698-
# if no color, just replace all the escape sequences with empty str
699-
for prop in dir(Color):
700-
if prop.startswith("__") or type(getattr(Color, prop)) is not str:
701-
continue
702-
setattr(Color, prop, "")
732+
C.no_color = args.no_color
703733

704734
for filename in args.filenames:
705735
reader = SubmissionTarReader(filename)
@@ -729,7 +759,7 @@ def main():
729759
for test in printer_classes:
730760
printer = printer_classes[test](reader, args.expected_n_runs)
731761
print(f"\n{f' {printer.name.capitalize()} failures ':-^80}")
732-
print(f"{Color.gray}In file {filename}{Color.end}\n")
762+
print(C.gray(f"In file {filename}\n"))
733763

734764
if (len(printer.cold_results) + len(printer.warm_results)) == 0:
735765
Log.ok(f"No {printer.name} failures")

0 commit comments

Comments
 (0)