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