#!/usr/bin/env python3 """Render a project map from its data.json, and the hub that links every map. render.py map writes index.html, artifact.html, state.json, publish.json render.py hub <.project-map dir> [--out index.html] data.json is the one source of a map. The schema is in README.md beside this file. Each render deletes proof media older than the proof life (30 days) and marks it expired, so a map never ships stale screenshots. """ import datetime as dt import html import json import os import sys from pathlib import Path HERE = Path(__file__).resolve().parent TEMPLATE = HERE / "template.html" LIFE_DAYS = 30 STATUSES = {"done", "prog", "idle", "stuck"} def today(): return dt.datetime.now(dt.timezone.utc).date() def expired(shot, life=LIFE_DAYS): if shot.get("expired"): return True captured = dt.date.fromisoformat(str(shot["captured"])[:10]) return today() >= captured + dt.timedelta(days=life) def fail(msg): sys.exit(f"render.py: {msg}") def validate(d): for key in ("project", "updated_at", "milestones", "parts"): if key not in d: fail(f"data.json has no '{key}'") codes = {m["code"] for m in d["milestones"]} ids = set() for p in d["parts"]: for key in ("id", "m", "name", "status", "summary"): if key not in p: fail(f"part {p.get('id', '?')} has no '{key}'") if p["id"] in ids: fail(f"part id {p['id']} is used twice") ids.add(p["id"]) if p["m"] not in codes: fail(f"part {p['id']} names milestone {p['m']}, which is not in milestones") if p["status"] not in STATUSES: fail(f"part {p['id']} has status {p['status']}; use one of {sorted(STATUSES)}") if p["status"] == "stuck" and not p.get("waiting_on"): fail(f"part {p['id']} is stuck but names nothing in waiting_on") if p.get("t") and p.get("d", 0) > p["t"]: fail(f"part {p['id']} has d > t") for c in d.get("needs_you", []): for key in ("id", "question", "options", "default"): if key not in c: fail(f"call {c.get('id', '?')} has no '{key}'") def prune_media(map_dir, d, dry=False): """Delete expired proof files and mark them expired. Returns the removed paths.""" life = d.get("proof_days", LIFE_DAYS) removed = [] for p in d["parts"]: for s in (p.get("proof") or {}).get("shots", []): if s.get("expired"): continue if expired(s, life): f = map_dir / s["src"] if not dry and f.is_file(): f.unlink() removed.append(s["src"]) if not dry: s["expired"] = True s.pop("bytes", None) return removed def check_media(map_dir, d): for p in d["parts"]: for s in (p.get("proof") or {}).get("shots", []): if s.get("expired"): continue if "captured" not in s: fail(f"shot {s.get('src')} in part {p['id']} has no captured date") f = map_dir / s["src"] if not f.is_file(): fail(f"shot {s['src']} in part {p['id']} is missing on disk") s["bytes"] = f.stat().st_size def page(d, inline=False): data = dict(d, inline=inline) blob = json.dumps(data, ensure_ascii=False, separators=(",", ":")).replace("{html.escape(s.get("title") or slug)}' f'{html.escape(s.get("epic") or "")}' f'{"" if pct is None else str(pct) + "%"}' f'{html.escape(s.get("next_step") or "")}' f'{html.escape((s.get("updated_at") or "")[:16].replace("T", " "))}' for _, slug, s, pct in rows ) or '

No maps yet.

' doc = f""" Project Maps

Project maps

{items}
""" (maps_dir / out_name).write_text(doc) print(f"hub: {maps_dir / out_name}, {len(rows)} maps") if __name__ == "__main__": if len(sys.argv) >= 3 and sys.argv[1] == "map": render_map(sys.argv[2]) elif len(sys.argv) >= 3 and sys.argv[1] == "hub": out = sys.argv[sys.argv.index("--out") + 1] if "--out" in sys.argv else "index.html" render_hub(sys.argv[2], out) else: sys.exit(__doc__)