|
| 1 | +import json |
| 2 | +from collections.abc import Iterable |
| 3 | +from dataclasses import dataclass |
| 4 | +from inspect import cleandoc |
| 5 | +from pathlib import Path |
| 6 | + |
| 7 | +import typer |
| 8 | + |
| 9 | +CLI = typer.Typer() |
| 10 | + |
| 11 | + |
| 12 | +@dataclass(frozen=True) |
| 13 | +class Finding: |
| 14 | + type: str |
| 15 | + module: str |
| 16 | + obj: str |
| 17 | + line: int |
| 18 | + column: int |
| 19 | + endLine: int |
| 20 | + endColumn: int |
| 21 | + path: str |
| 22 | + symbol: str |
| 23 | + message: str |
| 24 | + message_id: str |
| 25 | + |
| 26 | + |
| 27 | +def lint_issue_from_json(data: str) -> Iterable[Finding]: |
| 28 | + issues = json.loads(data) |
| 29 | + for issue in issues: |
| 30 | + yield Finding( |
| 31 | + type=issue["type"], |
| 32 | + module=issue["module"], |
| 33 | + obj=issue["obj"], |
| 34 | + line=issue["line"], |
| 35 | + column=issue["column"], |
| 36 | + endLine=issue["endLine"], |
| 37 | + endColumn=issue["endColumn"], |
| 38 | + path=issue["path"], |
| 39 | + symbol=issue["symbol"], |
| 40 | + message=issue["message"], |
| 41 | + message_id=issue["message-id"], |
| 42 | + ) |
| 43 | + |
| 44 | + |
| 45 | +def lint_issue_to_markdown(lint_issues: Iterable[Finding]) -> str: |
| 46 | + def _header() -> str: |
| 47 | + header = "# Static Code Analysis\n\n" |
| 48 | + header += "|File|line/<br>column|id|message|\n" |
| 49 | + header += "|---|:-:|:-:|---|\n" |
| 50 | + return header |
| 51 | + |
| 52 | + def _rows(findings: Iterable[Finding]) -> str: |
| 53 | + rows = "" |
| 54 | + for finding in findings: |
| 55 | + rows += f"|{finding.path}" |
| 56 | + rows += f"|line: {finding.line}/<br>column: {finding.column}" |
| 57 | + rows += f"|{finding.message_id}" |
| 58 | + rows += f"|{finding.message}|\n" |
| 59 | + return rows |
| 60 | + |
| 61 | + template = cleandoc( |
| 62 | + """ |
| 63 | + {header}{rows} |
| 64 | + """ |
| 65 | + ) |
| 66 | + lint_issues = sorted(lint_issues, key=lambda i: (i.path, i.message_id, i.line)) |
| 67 | + return template.format(header=_header(), rows=_rows(lint_issues)) |
| 68 | + |
| 69 | + |
| 70 | +@CLI.command(name="pretty-print") |
| 71 | +def lint_json_to_markdown( |
| 72 | + path: Path = typer.Argument(default=Path(".lint.json"), help="path to lint.json"), |
| 73 | +) -> None: |
| 74 | + """converts the lint json to a Markdown table""" |
| 75 | + issues = lint_issue_from_json(path.read_text()) |
| 76 | + print(lint_issue_to_markdown(issues)) |
| 77 | + |
| 78 | + |
| 79 | +if __name__ == "__main__": |
| 80 | + CLI() |
0 commit comments