-
Notifications
You must be signed in to change notification settings - Fork 147
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Separate error listing from formatting in format_lines
This allows to add more formaters based on the same error list.
- Loading branch information
Showing
2 changed files
with
32 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -150,3 +150,4 @@ Contributors | |
- rajivsunar07 <[email protected]> | ||
- Сергій <[email protected]> | ||
- Mersho <[email protected]> | ||
- Michal Čihař <[email protected]> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ | |
# SPDX-FileCopyrightText: 2022 Florian Snow <[email protected]> | ||
# SPDX-FileCopyrightText: 2023 DB Systel GmbH | ||
# SPDX-FileCopyrightText: 2024 Nico Rikken <[email protected]> | ||
# SPDX-FileCopyrightText: 2024 Michal Čihař <[email protected]> | ||
# | ||
# SPDX-License-Identifier: GPL-3.0-or-later | ||
|
||
|
@@ -16,7 +17,7 @@ | |
from io import StringIO | ||
from pathlib import Path | ||
from textwrap import TextWrapper | ||
from typing import IO, Any, Optional | ||
from typing import IO, Any, Iterable, Optional | ||
|
||
from . import __REUSE_version__ | ||
from .project import Project | ||
|
@@ -264,7 +265,9 @@ def custom_serializer(obj: Any) -> Any: | |
) | ||
|
||
|
||
def format_lines(report: ProjectReport) -> str: | ||
def get_errors( | ||
report: ProjectReport, | ||
) -> Iterable[tuple[Path | str | None, str]]: | ||
"""Formats data dictionary as plaintext strings to be printed to sys.stdout | ||
Sorting of output is not guaranteed. | ||
Symbolic links can result in multiple entries per file. | ||
|
@@ -275,7 +278,6 @@ def format_lines(report: ProjectReport) -> str: | |
Returns: | ||
String (in plaintext) that can be output to sys.stdout | ||
""" | ||
output = StringIO() | ||
|
||
def license_path(lic: str) -> Optional[Path]: | ||
"""Resolve a license identifier to a license path.""" | ||
|
@@ -285,55 +287,58 @@ def license_path(lic: str) -> Optional[Path]: | |
# Bad licenses | ||
for lic, files in sorted(report.bad_licenses.items()): | ||
for path in sorted(files): | ||
output.write( | ||
_("{path}: bad license {lic}\n").format(path=path, lic=lic) | ||
) | ||
yield (path, _("bad license {lic}").format(lic=lic)) | ||
|
||
# Deprecated licenses | ||
for lic in sorted(report.deprecated_licenses): | ||
lic_path = license_path(lic) | ||
output.write( | ||
_("{lic_path}: deprecated license\n").format(lic_path=lic_path) | ||
) | ||
yield (lic_path, _("deprecated license")) | ||
|
||
# Licenses without extension | ||
for lic in sorted(report.licenses_without_extension): | ||
lic_path = license_path(lic) | ||
output.write( | ||
_("{lic_path}: license without file extension\n").format( | ||
lic_path=lic_path | ||
) | ||
) | ||
yield (lic_path, _("license without file extension")) | ||
|
||
# Unused licenses | ||
for lic in sorted(report.unused_licenses): | ||
lic_path = license_path(lic) | ||
output.write( | ||
_("{lic_path}: unused license\n").format(lic_path=lic_path) | ||
) | ||
yield lic_path, _("unused license") | ||
|
||
# Missing licenses | ||
for lic, files in sorted(report.missing_licenses.items()): | ||
for path in sorted(files): | ||
output.write( | ||
_("{path}: missing license {lic}\n").format( | ||
path=path, lic=lic | ||
) | ||
) | ||
yield (path, _("missing license {lic}").format(lic=lic)) | ||
|
||
# Read errors | ||
for path in sorted(report.read_errors): | ||
output.write(_("{path}: read error\n").format(path=path)) | ||
yield (path, _("read error")) | ||
|
||
# Without licenses | ||
for path in report.files_without_licenses: | ||
output.write(_("{path}: no license identifier\n").format(path=path)) | ||
yield (path, _("no license identifier")) | ||
|
||
# Without copyright | ||
for path in report.files_without_copyright: | ||
output.write(_("{path}: no copyright notice\n").format(path=path)) | ||
yield (path, _("no copyright notice")) | ||
|
||
return output.getvalue() | ||
|
||
def format_lines(report: ProjectReport) -> str: | ||
"""Formats data dictionary as plaintext strings to be printed to sys.stdout | ||
Sorting of output is not guaranteed. | ||
Symbolic links can result in multiple entries per file. | ||
Args: | ||
report: ProjectReport data | ||
Returns: | ||
String (in plaintext) that can be output to sys.stdout | ||
""" | ||
if not report.is_compliant: | ||
return "".join( | ||
f"{path}: {error}\n" for path, error in get_errors(report) | ||
) | ||
|
||
return "" | ||
|
||
|
||
def run(args: Namespace, project: Project, out: IO[str] = sys.stdout) -> int: | ||
|