Skip to content

Commit

Permalink
Refactor arguments to renderers
Browse files Browse the repository at this point in the history
Just pass the run instead of the members of the run.

Signed-off-by: Eric Brown <[email protected]>
  • Loading branch information
ericwb committed Feb 5, 2024
1 parent a1d2bb1 commit 879144a
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 33 deletions.
5 changes: 2 additions & 3 deletions precli/renderers/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,13 @@
from abc import ABC
from abc import abstractmethod

from precli.core.metrics import Metrics
from precli.core.result import Result
from precli.core.run import Run


class Renderer(ABC):
def __init__(self, no_color: bool = False):
self._no_color = no_color

@abstractmethod
def render(self, results: list[Result], metrics: Metrics):
def render(self, run: Run):
pass
25 changes: 12 additions & 13 deletions precli/renderers/detailed.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@

from precli.core.level import Level
from precli.core.linecache import LineCache
from precli.core.metrics import Metrics
from precli.core.result import Result
from precli.core.run import Run
from precli.renderers import Renderer
from precli.rules import Rule

Expand All @@ -20,8 +19,8 @@ def __init__(self, no_color: bool = False):
else:
self.console = console.Console(highlight=False)

def render(self, results: list[Result], metrics: Metrics):
for result in results:
def render(self, run: Run):
for result in run.results:
match result.level:
case Level.ERROR:
emoji = ":no_entry-emoji:"
Expand Down Expand Up @@ -142,28 +141,28 @@ def render(self, results: list[Result], metrics: Metrics):
table.add_column(justify="right")
table.add_row(
"Files analyzed",
f"{metrics.files:,}",
f"{run.metrics.files:,}",
"Lines analyzed",
f"{metrics.lines:,}",
f"{run.metrics.lines:,}",
)
table.add_row(
"Files skipped",
f"{metrics.files_skipped:,}",
f"{run.metrics.files_skipped:,}",
end_section=True,
)
table.add_row(
"Errors",
f"{metrics.errors:,}",
style="red" if metrics.errors else "",
f"{run.metrics.errors:,}",
style="red" if run.metrics.errors else "",
)
table.add_row(
"Warnings",
f"{metrics.warnings:,}",
style="yellow" if metrics.warnings else "",
f"{run.metrics.warnings:,}",
style="yellow" if run.metrics.warnings else "",
)
table.add_row(
"Notes",
f"{metrics.notes:,}",
style="blue" if metrics.notes else "",
f"{run.metrics.notes:,}",
style="blue" if run.metrics.notes else "",
)
self.console.print(table)
19 changes: 9 additions & 10 deletions precli/renderers/json.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@

from rich import console

from precli.core.metrics import Metrics
from precli.core.result import Result
from precli.core.run import Run
from precli.renderers import Renderer
from precli.rules import Rule

Expand All @@ -14,9 +13,9 @@ def __init__(self, no_color: bool = False):
super().__init__(no_color=no_color)
self.console = console.Console(highlight=False)

def render(self, results: list[Result], metrics: Metrics):
def render(self, run: Run):
results_json = {"results": []}
for result in results:
for result in run.results:
rule = Rule.get_by_id(result.rule_id)

if result.artifact.uri is not None:
Expand All @@ -41,11 +40,11 @@ def render(self, results: list[Result], metrics: Metrics):
}
)
results_json["metrics"] = {
"files": metrics.files,
"files_skipped": metrics.files_skipped,
"lines": metrics.lines,
"errors": metrics.errors,
"warnings": metrics.warnings,
"notes": metrics.notes,
"files": run.metrics.files,
"files_skipped": run.metrics.files_skipped,
"lines": run.metrics.lines,
"errors": run.metrics.errors,
"warnings": run.metrics.warnings,
"notes": run.metrics.notes,
}
self.console.print_json(json.dumps(results_json))
13 changes: 6 additions & 7 deletions precli/renderers/plain.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
from rich.padding import Padding

from precli.core.level import Level
from precli.core.metrics import Metrics
from precli.core.result import Result
from precli.core.run import Run
from precli.renderers import Renderer
from precli.rules import Rule

Expand All @@ -14,8 +13,8 @@ def __init__(self, no_color: bool = False):
super().__init__(no_color=no_color)
self.console = console.Console(highlight=False)

def render(self, results: list[Result], metrics: Metrics):
for result in results:
def render(self, run: Run):
for result in run.results:
rule = Rule.get_by_id(result.rule_id)

if self._no_color is True:
Expand Down Expand Up @@ -66,7 +65,7 @@ def render(self, results: list[Result], metrics: Metrics):
)
self.console.print()
self.console.print(
f"Found {metrics.errors} errors, {metrics.warnings} warnings, "
f"and {metrics.notes} notes in {metrics.files} files and "
f"{metrics.lines} lines of code."
f"Found {run.metrics.errors} errors, {run.metrics.warnings} "
f"warnings, and {run.metrics.notes} notes in {run.metrics.files} "
f"files and {run.metrics.lines} lines of code."
)

0 comments on commit 879144a

Please sign in to comment.