Skip to content

Commit 51fe994

Browse files
committed
refactor: do refactoring
Signed-off-by: ktro2828 <kotaro.uetake@tier4.jp>
1 parent a1b2329 commit 51fe994

2 files changed

Lines changed: 39 additions & 11 deletions

File tree

t4_devkit/sanity/checker.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,15 @@ def __call__(self, context: SanityContext, fix: bool = False) -> Report:
4949
fixed = self.fix(context) if fix and reasons else False
5050
return make_report(self.id, self.name, self.severity, self.description, reasons, fixed)
5151

52-
def can_skip(self, _: SanityContext) -> Maybe[Reason]:
53-
"""Return a skip reason if the checker should be skipped."""
52+
def can_skip(self, context: SanityContext) -> Maybe[Reason]:
53+
"""Return a skip reason if the checker should be skipped.
54+
55+
Args:
56+
context (SanityContext): The sanity context.
57+
58+
Returns:
59+
A skip reason if the checker should be skipped, or Nothing if it should not be skipped.
60+
"""
5461
return Nothing
5562

5663
@abstractmethod

t4_devkit/sanity/result.py

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from __future__ import annotations
22

33
from enum import Enum
4-
from typing import TYPE_CHECKING, NewType
4+
from typing import TYPE_CHECKING, ClassVar, NewType
55

66
from attrs import define, field
77
from tabulate import tabulate
@@ -47,6 +47,13 @@ class Report:
4747
reasons: list[Reason] | None = field(default=None)
4848
fixed: bool = False
4949

50+
PALETTE: ClassVar[dict[str, str]] = {
51+
"RED": "\033[31m",
52+
"GREEN": "\033[32m",
53+
"YELLOW": "\033[33m",
54+
"CYAN": "\033[36m",
55+
}
56+
5057
def __attrs_post_init__(self) -> None:
5158
if self.status == Status.PASSED:
5259
assert self.reasons is None, "Passed report cannot have reasons"
@@ -82,21 +89,35 @@ def to_str(self, *, strict: bool = False) -> str:
8289
A string representation of the report.
8390
"""
8491
parts = []
85-
is_fixed = " --> FIXED" if self.fixed else ""
8692
if not self.is_passed(strict=strict):
87-
parts.append(f"\033[31m {self.id}:\033[0m{is_fixed}\n")
93+
# print failure reasons
94+
color = self.PALETTE["RED"]
95+
parts.append(f"{color} {self.id}:\033[0m\n")
8896
for reason in self.reasons or []:
89-
parts.append(f"\033[31m - {reason}\033[0m\n")
97+
parts.append(f"{color} - {reason}\033[0m\n")
9098
elif self.is_skipped():
91-
parts.append(f"\033[36m {self.id}: [SKIPPED]\033[0m\n")
99+
# print skipped reasons
100+
color = self.PALETTE["CYAN"]
101+
parts.append(f"{color} {self.id}: [SKIPPED]\033[0m\n")
92102
for reason in self.reasons or []:
93-
parts.append(f"\033[36m - {reason}\033[0m\n")
103+
parts.append(f"{color} - {reason}\033[0m\n")
94104
elif self.severity.is_warning() and self.reasons:
95-
parts.append(f"\033[33m {self.id}:\033[0m{is_fixed}\n")
105+
# print warning reasons
106+
color = self.PALETTE["YELLOW"]
107+
parts.append(f"{color} {self.id}:\033[0m\n")
108+
for reason in self.reasons or []:
109+
parts.append(f"{color} - {reason}\033[0m\n")
110+
elif self.is_passed() and self.fixed:
111+
# print failure or warning but fixed reasons
112+
color1 = self.PALETTE["YELLOW"] if self.severity.is_warning() else self.PALETTE["RED"]
113+
color2 = self.PALETTE["GREEN"]
114+
parts.append(f"{color1} {self.id}:\033[0m {color2}--> FIXED🎉\033[0m\n")
96115
for reason in self.reasons or []:
97-
parts.append(f"\033[33m - {reason}\033[0m\n")
116+
parts.append(f"{color1} - {reason}\033[0m\n")
98117
else:
99-
parts.append(f"\033[32m {self.id}: ✅\033[0m\n")
118+
# print passed
119+
color = self.PALETTE["GREEN"]
120+
parts.append(f"{color} {self.id}: ✅\033[0m\n")
100121
return "".join(parts)
101122

102123

0 commit comments

Comments
 (0)