Skip to content

Commit 8cbf559

Browse files
authored
refactor(sanity): add to_str() function to Report (#245)
* refactor: add to_str() functino to Report Signed-off-by: ktro2828 <kotaro.uetake@tier4.jp> * perf: avoid inefficient string concatenation Signed-off-by: ktro2828 <kotaro.uetake@tier4.jp> --------- Signed-off-by: ktro2828 <kotaro.uetake@tier4.jp>
1 parent 5ab8fd9 commit 8cbf559

1 file changed

Lines changed: 29 additions & 17 deletions

File tree

t4_devkit/sanity/result.py

Lines changed: 29 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,32 @@ def is_skipped(self) -> bool:
6969
"""Check if the status is skipped."""
7070
return self.status == Status.SKIPPED
7171

72+
def to_str(self, *, strict: bool = False) -> str:
73+
"""Return a string representation of the report.
74+
75+
Args:
76+
strict (bool): Whether to consider warnings as failures.
77+
78+
Returns:
79+
A string representation of the report.
80+
"""
81+
parts = []
82+
if not self.is_passed(strict=strict):
83+
parts.append(f"\033[31m {self.id}:\033[0m\n")
84+
for reason in self.reasons or []:
85+
parts.append(f"\033[31m - {reason}\033[0m\n")
86+
elif self.is_skipped():
87+
parts.append(f"\033[36m {self.id}: [SKIPPED]\033[0m\n")
88+
for reason in self.reasons or []:
89+
parts.append(f"\033[36m - {reason}\033[0m\n")
90+
elif self.severity.is_warning() and self.reasons:
91+
parts.append(f"\033[33m {self.id}:\033[0m\n")
92+
for reason in self.reasons or []:
93+
parts.append(f"\033[33m - {reason}\033[0m\n")
94+
else:
95+
parts.append(f"\033[32m {self.id}: ✅\033[0m\n")
96+
return "".join(parts)
97+
7298

7399
def make_report(
74100
id: RuleID,
@@ -142,23 +168,9 @@ def to_str(self, *, strict: bool = False) -> str:
142168
Returns:
143169
A string representation of the result.
144170
"""
145-
string = f"=== DatasetID: {self.dataset_id} ===\n"
146-
for report in self.reports:
147-
if not report.is_passed(strict=strict):
148-
string += f"\033[31m {report.id}:\033[0m\n"
149-
for reason in report.reasons or []:
150-
string += f"\033[31m - {reason}\033[0m\n"
151-
elif report.is_skipped():
152-
string += f"\033[36m {report.id}: [SKIPPED]\033[0m\n"
153-
for reason in report.reasons or []:
154-
string += f"\033[36m - {reason}\033[0m\n"
155-
elif report.severity.is_warning() and report.reasons:
156-
string += f"\033[33m {report.id}:\033[0m\n"
157-
for reason in report.reasons or []:
158-
string += f"\033[33m - {reason}\033[0m\n"
159-
else:
160-
string += f"\033[32m {report.id}: ✅\033[0m\n"
161-
return string
171+
return f"=== DatasetID: {self.dataset_id} ===\n" + "".join(
172+
report.to_str(strict=strict) for report in self.reports
173+
)
162174

163175

164176
def print_sanity_result(result: SanityResult, *, strict: bool = False) -> None:

0 commit comments

Comments
 (0)