Skip to content

Commit 5b301a2

Browse files
committed
chore: improve code readability and less repeatative
Signed-off-by: Demolus13 <parth.govale@oracle.com>
1 parent 64960c2 commit 5b301a2

File tree

1 file changed

+64
-109
lines changed

1 file changed

+64
-109
lines changed

src/macaron/console.py

Lines changed: 64 additions & 109 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,58 @@
1717
from rich.table import Table
1818

1919

20-
class Dependency:
20+
class TableBuilder:
21+
"""Builder to provide common table-building utilities for console classes."""
22+
23+
@staticmethod
24+
def _make_table(content: dict, columns: list[str]) -> Table:
25+
table = Table(show_header=False, box=None)
26+
for col in columns:
27+
table.add_column(col, justify="left")
28+
for field, value in content.items():
29+
table.add_row(field, value)
30+
return table
31+
32+
@staticmethod
33+
def _make_checks_table(checks: dict[str, str]) -> Table:
34+
table = Table(show_header=False, box=None)
35+
table.add_column("Status", justify="left")
36+
table.add_column("Check", justify="left")
37+
for check_name, check_status in checks.items():
38+
if check_status == "RUNNING":
39+
table.add_row(Status("[bold green]RUNNING[/]"), check_name)
40+
return table
41+
42+
@staticmethod
43+
def _make_failed_checks_table(failed_checks: list) -> Table:
44+
table = Table(show_header=False, box=None)
45+
table.add_column("Status", justify="left")
46+
table.add_column("Check ID", justify="left")
47+
table.add_column("Description", justify="left")
48+
for check in failed_checks:
49+
table.add_row("[bold red]FAILED[/]", check.check.check_id, check.check.check_description)
50+
return table
51+
52+
@staticmethod
53+
def _make_summary_table(checks_summary: dict, total_checks: int) -> Table:
54+
table = Table(show_header=False, box=None)
55+
table.add_column("Check Result Type", justify="left")
56+
table.add_column("Count", justify="left")
57+
table.add_row("Total Checks", str(total_checks), style="white")
58+
color_map = {
59+
"PASSED": "green",
60+
"FAILED": "red",
61+
"SKIPPED": "yellow",
62+
"DISABLED": "bright_blue",
63+
"UNKNOWN": "white",
64+
}
65+
for check_result_type, checks in checks_summary.items():
66+
if check_result_type in color_map:
67+
table.add_row(check_result_type, str(len(checks)), style=color_map[check_result_type])
68+
return table
69+
70+
71+
class Dependency(TableBuilder):
2172
"""A class to manage the display of dependency analysis in the console."""
2273

2374
def __init__(self) -> None:
@@ -56,13 +107,7 @@ def add_description_table_content(self, key: str, value: str | Status) -> None:
56107
The value associated with the key.
57108
"""
58109
self.description_table_content[key] = value
59-
description_table = Table(show_header=False, box=None)
60-
description_table.add_column("Details", justify="left")
61-
description_table.add_column("Value", justify="left")
62-
for field, content in self.description_table_content.items():
63-
description_table.add_row(field, content)
64-
65-
self.description_table = description_table
110+
self.description_table = self._make_table(self.description_table_content, ["Details", "Value"])
66111

67112
def no_of_checks(self, value: int) -> None:
68113
"""
@@ -91,16 +136,7 @@ def update_checks(self, check_id: str, status: str = "RUNNING") -> None:
91136
The new status of the check, by default "RUNNING"
92137
"""
93138
self.checks[check_id] = status
94-
95-
progress_table = Table(show_header=False, box=None)
96-
progress_table.add_column("Status", justify="left")
97-
progress_table.add_column("Check", justify="left")
98-
99-
for check_name, check_status in self.checks.items():
100-
if check_status == "RUNNING":
101-
progress_table.add_row(Status("[bold green]RUNNING[/]"), check_name)
102-
self.progress_table = progress_table
103-
139+
self.progress_table = self._make_checks_table(self.checks)
104140
if self.task_id is not None and status != "RUNNING":
105141
self.progress.update(self.task_id, advance=1)
106142

@@ -115,39 +151,8 @@ def update_checks_summary(self, checks_summary: dict, total_checks: int) -> None
115151
total_checks : int
116152
The total number of checks.
117153
"""
118-
failed_checks_table = Table(show_header=False, box=None)
119-
failed_checks_table.add_column("Status", justify="left")
120-
failed_checks_table.add_column("Check ID", justify="left")
121-
failed_checks_table.add_column("Description", justify="left")
122-
123-
failed_checks = checks_summary["FAILED"]
124-
for check in failed_checks:
125-
failed_checks_table.add_row(
126-
"[bold red]FAILED[/]",
127-
check.check.check_id,
128-
check.check.check_description,
129-
)
130-
131-
self.failed_checks_table = failed_checks_table
132-
133-
summary_table = Table(show_header=False, box=None)
134-
summary_table.add_column("Check Result Type", justify="left")
135-
summary_table.add_column("Count", justify="left")
136-
summary_table.add_row("Total Checks", str(total_checks), style="white")
137-
138-
for check_result_type, checks in checks_summary.items():
139-
if check_result_type == "PASSED":
140-
summary_table.add_row("PASSED", str(len(checks)), style="green")
141-
if check_result_type == "FAILED":
142-
summary_table.add_row("FAILED", str(len(checks)), style="red")
143-
if check_result_type == "SKIPPED":
144-
summary_table.add_row("SKIPPED", str(len(checks)), style="yellow")
145-
if check_result_type == "DISABLED":
146-
summary_table.add_row("DISABLED", str(len(checks)), style="bright_blue")
147-
if check_result_type == "UNKNOWN":
148-
summary_table.add_row("UNKNOWN", str(len(checks)), style="white")
149-
150-
self.summary_table = summary_table
154+
self.failed_checks_table = self._make_failed_checks_table(checks_summary.get("FAILED", []))
155+
self.summary_table = self._make_summary_table(checks_summary, total_checks)
151156

152157
def make_layout(self) -> list[RenderableType]:
153158
"""
@@ -185,7 +190,7 @@ def make_layout(self) -> list[RenderableType]:
185190
return layout
186191

187192

188-
class RichConsoleHandler(RichHandler):
193+
class RichConsoleHandler(RichHandler, TableBuilder):
189194
"""A rich console handler for logging with rich formatting and live updates."""
190195

191196
def __init__(self, *args: Any, verbose: bool = False, **kwargs: Any) -> None:
@@ -309,18 +314,10 @@ def add_description_table_content(self, key: str, value: str | Status) -> None:
309314
The value associated with the key.
310315
"""
311316
if self.if_dependency and self.dependency_analysis_list:
312-
dependency = self.dependency_analysis_list[-1]
313-
dependency.add_description_table_content(key, value)
317+
self.dependency_analysis_list[-1].add_description_table_content(key, value)
314318
return
315-
316319
self.description_table_content[key] = value
317-
description_table = Table(show_header=False, box=None)
318-
description_table.add_column("Details", justify="left")
319-
description_table.add_column("Value", justify="left")
320-
for field, content in self.description_table_content.items():
321-
description_table.add_row(field, content)
322-
323-
self.description_table = description_table
320+
self.description_table = self._make_table(self.description_table_content, ["Details", "Value"])
324321

325322
def no_of_checks(self, value: int) -> None:
326323
"""
@@ -357,20 +354,10 @@ def update_checks(self, check_id: str, status: str = "RUNNING") -> None:
357354
The new status of the check, by default "RUNNING"
358355
"""
359356
if self.if_dependency and self.dependency_analysis_list:
360-
dependency = self.dependency_analysis_list[-1]
361-
dependency.update_checks(check_id, status)
357+
self.dependency_analysis_list[-1].update_checks(check_id, status)
362358
return
363359
self.checks[check_id] = status
364-
365-
progress_table = Table(show_header=False, box=None)
366-
progress_table.add_column("Status", justify="left")
367-
progress_table.add_column("Check", justify="left")
368-
369-
for check_name, check_status in self.checks.items():
370-
if check_status == "RUNNING":
371-
progress_table.add_row(Status("[bold green]RUNNING[/]"), check_name)
372-
self.progress_table = progress_table
373-
360+
self.progress_table = self._make_checks_table(self.checks)
374361
if self.task_id is not None and status != "RUNNING":
375362
self.progress.update(self.task_id, advance=1)
376363

@@ -386,42 +373,10 @@ def update_checks_summary(self, checks_summary: dict, total_checks: int) -> None
386373
The total number of checks.
387374
"""
388375
if self.if_dependency and self.dependency_analysis_list:
389-
dependency = self.dependency_analysis_list[-1]
390-
dependency.update_checks_summary(checks_summary, total_checks)
376+
self.dependency_analysis_list[-1].update_checks_summary(checks_summary, total_checks)
391377
return
392-
failed_checks_table = Table(show_header=False, box=None)
393-
failed_checks_table.add_column("Status", justify="left")
394-
failed_checks_table.add_column("Check ID", justify="left")
395-
failed_checks_table.add_column("Description", justify="left")
396-
397-
failed_checks = checks_summary["FAILED"]
398-
for check in failed_checks:
399-
failed_checks_table.add_row(
400-
"[bold red]FAILED[/]",
401-
check.check.check_id,
402-
check.check.check_description,
403-
)
404-
405-
self.failed_checks_table = failed_checks_table
406-
407-
summary_table = Table(show_header=False, box=None)
408-
summary_table.add_column("Check Result Type", justify="left")
409-
summary_table.add_column("Count", justify="left")
410-
summary_table.add_row("Total Checks", str(total_checks), style="white")
411-
412-
for check_result_type, checks in checks_summary.items():
413-
if check_result_type == "PASSED":
414-
summary_table.add_row("PASSED", str(len(checks)), style="green")
415-
if check_result_type == "FAILED":
416-
summary_table.add_row("FAILED", str(len(checks)), style="red")
417-
if check_result_type == "SKIPPED":
418-
summary_table.add_row("SKIPPED", str(len(checks)), style="yellow")
419-
if check_result_type == "DISABLED":
420-
summary_table.add_row("DISABLED", str(len(checks)), style="bright_blue")
421-
if check_result_type == "UNKNOWN":
422-
summary_table.add_row("UNKNOWN", str(len(checks)), style="white")
423-
424-
self.summary_table = summary_table
378+
self.failed_checks_table = self._make_failed_checks_table(checks_summary.get("FAILED", []))
379+
self.summary_table = self._make_summary_table(checks_summary, total_checks)
425380

426381
def update_report_table(self, report_type: str, report_path: str) -> None:
427382
"""

0 commit comments

Comments
 (0)