Group findings by kind, then by file, and write `.doc-report`. Removes any stale report when there are no findings.
(report_path: Path, findings: list[Finding])
| 829 | |
| 830 | |
| 831 | def write_report(report_path: Path, findings: list[Finding]) -> None: |
| 832 | """Group findings by kind, then by file, and write `.doc-report`. |
| 833 | Removes any stale report when there are no findings.""" |
| 834 | if not findings: |
| 835 | if report_path.exists(): |
| 836 | report_path.unlink() |
| 837 | return |
| 838 | |
| 839 | by_kind: dict[str, list[Finding]] = {} |
| 840 | for f in findings: |
| 841 | by_kind.setdefault(f.kind, []).append(f) |
| 842 | |
| 843 | out: list[str] = [_REPORT_HEADER] |
| 844 | out.append(f"\n_Total findings: {len(findings)}_\n") |
| 845 | |
| 846 | for kind in sorted(by_kind): |
| 847 | entries = by_kind[kind] |
| 848 | out.append(f"\n### `{kind}` ({len(entries)})\n\n") |
| 849 | # Group by file within each kind for readability. |
| 850 | by_file: dict[Path, list[Finding]] = {} |
| 851 | for f in entries: |
| 852 | by_file.setdefault(f.path, []).append(f) |
| 853 | for fpath in sorted(by_file): |
| 854 | out.append(f"**`{fpath}`**\n\n") |
| 855 | for f in by_file[fpath]: |
| 856 | excerpt = f.excerpt.replace("`", "'") |
| 857 | out.append(f"- L{f.line}:{f.col} -- {f.message}\n") |
| 858 | if excerpt: |
| 859 | out.append(f" > {excerpt}\n") |
| 860 | out.append("\n") |
| 861 | |
| 862 | out.append("\n---\n") |
| 863 | report_path.write_text("".join(out), encoding="utf-8", newline="\n") |
| 864 | |
| 865 | |
| 866 | # --------------------------------------------------------------------------- |