|
| 1 | +# ruff: noqa: INP001, D212 # planning/ is not a Python package; D212/D213 conflict differs from faststream-outbox |
| 2 | +""" |
| 3 | +Generate the planning change index from bundle frontmatter. |
| 4 | +
|
| 5 | +Run via ``just index``. Globs ``planning/changes/*/``, reads each bundle's |
| 6 | +``design.md`` (falling back to ``change.md``) frontmatter, and prints a |
| 7 | +Markdown listing grouped by lifecycle status to stdout. Never writes a file: |
| 8 | +the listing is a query over the bundles, not a committed artifact. |
| 9 | +""" |
| 10 | + |
| 11 | +import pathlib |
| 12 | +import sys |
| 13 | + |
| 14 | + |
| 15 | +CHANGES_DIR = pathlib.Path(__file__).parent / "changes" |
| 16 | +GROUPS: tuple[tuple[str, tuple[str, ...]], ...] = ( |
| 17 | + ("In progress", ("draft", "approved")), |
| 18 | + ("Shipped", ("shipped",)), |
| 19 | + ("Superseded", ("superseded",)), |
| 20 | +) |
| 21 | + |
| 22 | + |
| 23 | +def parse_frontmatter(text: str) -> dict[str, str]: |
| 24 | + """Parse a single-line-scalar YAML frontmatter block into a dict.""" |
| 25 | + lines = text.splitlines() |
| 26 | + if not lines or lines[0].strip() != "---": |
| 27 | + return {} |
| 28 | + fields: dict[str, str] = {} |
| 29 | + for line in lines[1:]: |
| 30 | + if line.strip() == "---": |
| 31 | + break |
| 32 | + key, sep, value = line.partition(": ") |
| 33 | + if not sep: |
| 34 | + continue |
| 35 | + cleaned = value.strip().strip('"').strip("'") |
| 36 | + fields[key.strip()] = "" if cleaned == "null" else cleaned |
| 37 | + return fields |
| 38 | + |
| 39 | + |
| 40 | +def load_bundles() -> list[dict[str, str]]: |
| 41 | + """Read every bundle's spec frontmatter under ``CHANGES_DIR``.""" |
| 42 | + bundles: list[dict[str, str]] = [] |
| 43 | + for bundle in sorted(CHANGES_DIR.iterdir()): |
| 44 | + if not bundle.is_dir(): |
| 45 | + continue |
| 46 | + spec = bundle / "design.md" |
| 47 | + if not spec.exists(): |
| 48 | + spec = bundle / "change.md" |
| 49 | + if not spec.exists(): |
| 50 | + continue |
| 51 | + fields = parse_frontmatter(spec.read_text(encoding="utf-8")) |
| 52 | + fields["path"] = f"changes/{bundle.name}/{spec.name}" |
| 53 | + fields["name"] = bundle.name |
| 54 | + bundles.append(fields) |
| 55 | + return bundles |
| 56 | + |
| 57 | + |
| 58 | +def format_row(bundle: dict[str, str]) -> str: |
| 59 | + """Render one bundle as a Markdown list item.""" |
| 60 | + slug = bundle.get("slug", "?") |
| 61 | + path = bundle.get("path", "") |
| 62 | + pr = bundle.get("pr") or "—" |
| 63 | + date = bundle.get("date", "") |
| 64 | + summary = bundle.get("summary") or "(no summary)" |
| 65 | + line = f"- **[{slug}]({path})** (#{pr}, {date}) — {summary}" |
| 66 | + if bundle.get("supersedes"): |
| 67 | + line += f" _(supersedes {bundle['supersedes']})_" |
| 68 | + if bundle.get("superseded_by"): |
| 69 | + line += f" _(superseded by {bundle['superseded_by']})_" |
| 70 | + return line |
| 71 | + |
| 72 | + |
| 73 | +def render(bundles: list[dict[str, str]]) -> str: |
| 74 | + """Render the full grouped Markdown listing.""" |
| 75 | + out = ["# Change index", "", "_Generated by `just index` — do not edit._", ""] |
| 76 | + for title, statuses in GROUPS: |
| 77 | + out += [f"## {title}", ""] |
| 78 | + rows = sorted( |
| 79 | + (b for b in bundles if b.get("status") in statuses), |
| 80 | + key=lambda b: b.get("name", ""), |
| 81 | + reverse=True, |
| 82 | + ) |
| 83 | + out += [format_row(b) for b in rows] if rows else ["_None._"] |
| 84 | + out.append("") |
| 85 | + return "\n".join(out).rstrip() + "\n" |
| 86 | + |
| 87 | + |
| 88 | +def main() -> int: |
| 89 | + """Print the listing to stdout.""" |
| 90 | + sys.stdout.write(render(load_bundles())) |
| 91 | + return 0 |
| 92 | + |
| 93 | + |
| 94 | +if __name__ == "__main__": |
| 95 | + raise SystemExit(main()) |
0 commit comments