@@ -33,29 +33,65 @@ class Input:
33
33
34
34
35
35
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 ()
45
81
46
82
47
83
class Log :
48
84
@staticmethod
49
85
def ok (* args : str ):
50
- print (f" { Color .ok } [ OK ]{ Color . end } " , * args )
86
+ print (C .ok ( " [ OK ]" ) , * args )
51
87
52
88
@staticmethod
53
89
def warn (* args : str ):
54
- print (f" { Color .medium } [ WARN ]{ Color . end } " , * args )
90
+ print (C .medium ( " [ WARN ]" ) , * args )
55
91
56
92
@staticmethod
57
93
def err (* args : str ):
58
- print (f" { Color .critical } [ ERR ]{ Color . end } " , * args )
94
+ print (C .critical ( " [ ERR ]" ) , * args )
59
95
60
96
61
97
RunIndexToMessageMap = dict [int , list [str ]]
@@ -198,7 +234,7 @@ def print_by_index(self) -> None:
198
234
199
235
def _default_title_transform (self , fail_type : str ) -> str :
200
236
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 )
202
238
known_name_tranforms = {
203
239
"pci" : "PCI device difference" ,
204
240
"usb" : "USB device difference" ,
@@ -210,7 +246,7 @@ def _default_title_transform(self, fail_type: str) -> str:
210
246
else :
211
247
capitalized = fail_type_lower .capitalize ()
212
248
213
- transformed_str = f"{ color } { capitalized } errors:{ Color . end } "
249
+ transformed_str = color ( f"{ capitalized } errors:" )
214
250
return transformed_str
215
251
216
252
def _default_err_msg_transform (self , msg : str ) -> str :
@@ -245,7 +281,7 @@ def _default_print_by_err(
245
281
)
246
282
247
283
for err_msg in all_err_msg :
248
- print (SPACE , f" { Color .bold } { err_msg } { Color . end } " )
284
+ print (SPACE , C .bold ( err_msg ) )
249
285
250
286
buffer = {
251
287
err_msg : {
@@ -369,10 +405,10 @@ def _short_print(
369
405
370
406
for fail_type , results in boot_results .items ():
371
407
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:"
375
410
)
411
+ print (f"{ prefix } { colorized } " )
376
412
377
413
wrapped = textwrap .wrap (str (failed_runs ), width = 50 )
378
414
print (f"{ prefix } { SPACE } - Failed runs: { wrapped [0 ]} " )
@@ -425,8 +461,7 @@ class FwtsPrinter(TestResultPrinter):
425
461
def print_by_err (self ):
426
462
def title_transform (fail_type : str ):
427
463
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:' )} "
430
465
)
431
466
432
467
def err_msg_transform (msg : str ):
@@ -694,12 +729,7 @@ def parse_args() -> Input:
694
729
def main ():
695
730
args = parse_args ()
696
731
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
703
733
704
734
for filename in args .filenames :
705
735
reader = SubmissionTarReader (filename )
@@ -729,7 +759,7 @@ def main():
729
759
for test in printer_classes :
730
760
printer = printer_classes [test ](reader , args .expected_n_runs )
731
761
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 ") )
733
763
734
764
if (len (printer .cold_results ) + len (printer .warm_results )) == 0 :
735
765
Log .ok (f"No { printer .name } failures" )
0 commit comments