Skip to content

Commit

Permalink
fix: fix default console output not printing file path (#126)
Browse files Browse the repository at this point in the history
we want:
 - default console output adder to print file paths (used in CAL)
 - `lint` to not print it (it uses sections)
 - `lint --gha` to print it
  • Loading branch information
jnicoulaud-ledger authored Oct 29, 2024
1 parent 445f2f8 commit cf62076
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 5 deletions.
21 changes: 19 additions & 2 deletions src/erc7730/common/output.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,12 @@ def add(self, output: Output) -> None:
assert_never(output.level)

log = f"[{style}]{prefix}"
context = []
if output.file is not None:
context.append(f"{output.file}")
if output.line is not None:
log += f"line {output.line}: "
context.append(f"line {output.line}")
log += ", ".join(context)
if output.title is not None:
log += f"{output.title}: "
log += f"[/{style}]{output.message}"
Expand Down Expand Up @@ -176,7 +180,7 @@ def add(self, output: Output) -> None:
builtin_print(log)


class FileOutputAdder(OutputAdder):
class AddFileOutputAdder(OutputAdder):
"""An output adder wrapper that adds a specific file to all outputs."""

def __init__(self, delegate: OutputAdder, file: FilePath) -> None:
Expand All @@ -190,6 +194,19 @@ def add(self, output: Output) -> None:
self.delegate.add(output.model_copy(update={"file": self.file}))


class DropFileOutputAdder(OutputAdder):
"""An output adder wrapper that drops file information from all outputs."""

def __init__(self, delegate: OutputAdder) -> None:
super().__init__()
self.delegate: OutputAdder = delegate

@override
def add(self, output: Output) -> None:
super().add(output)
self.delegate.add(output.model_copy(update={"file": None}))


@final
class BufferAdder(AbstractContextManager[OutputAdder]):
"""A context manager that buffers outputs and outputs them all at once."""
Expand Down
13 changes: 10 additions & 3 deletions src/erc7730/lint/lint.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,14 @@
from rich import print

from erc7730 import ERC_7730_REGISTRY_CALLDATA_PREFIX, ERC_7730_REGISTRY_EIP712_PREFIX
from erc7730.common.output import BufferAdder, ConsoleOutputAdder, FileOutputAdder, GithubAnnotationsAdder, OutputAdder
from erc7730.common.output import (
AddFileOutputAdder,
BufferAdder,
ConsoleOutputAdder,
DropFileOutputAdder,
GithubAnnotationsAdder,
OutputAdder,
)
from erc7730.convert.resolved.convert_erc7730_input_to_resolved import ERC7730InputToResolved
from erc7730.lint import ERC7730Linter
from erc7730.lint.lint_base import MultiLinter
Expand All @@ -18,7 +25,7 @@


def lint_all_and_print_errors(paths: list[Path], gha: bool = False) -> bool:
out = GithubAnnotationsAdder() if gha else ConsoleOutputAdder()
out = GithubAnnotationsAdder() if gha else DropFileOutputAdder(delegate=ConsoleOutputAdder())

count = lint_all(paths, out)

Expand Down Expand Up @@ -91,7 +98,7 @@ def lint_file(path: Path, linter: ERC7730Linter, out: OutputAdder, show_as: Path
"""

label = path if show_as is None else show_as
file_out = FileOutputAdder(delegate=out, file=path)
file_out = AddFileOutputAdder(delegate=out, file=path)

with BufferAdder(file_out, prolog=f"➡️ checking [bold]{label}[/bold]…", epilog="") as out:
try:
Expand Down

0 comments on commit cf62076

Please sign in to comment.