#!/usr/bin/env python3 """Build MATRIX.md from TABLE.md, and check the table's shape. Read-only on everything but MATRIX.md in this folder. It runs on DooPlex against the git clone only. Usage: make-matrix.py write ../MATRIX.md, print the totals make-matrix.py --check write nothing; rc 1 when the table breaks a rule Rules checked (a broken rule is printed and makes rc 1): - an ending heading is `### — · · NAMED|MENTIONED|UNNAMED` - each ending has exactly eight answer rows, numbered 1..8, each with one mark A, D, K or N - an A row carries a backticked pointer; a K row an `R-`; an N row one class E1..E6 and a search S """ import os import re import sys HERE = os.path.dirname(os.path.abspath(__file__)) AUDIT = os.path.dirname(HERE) HEAD = re.compile(r"^### (E[CAH]\d+) — (.+?) · (?:App|Drive|Copies|Box|Household|Access|Versions|Outside) · (NAMED|MENTIONED|UNNAMED)\s*$") ROW = re.compile(r"^\| ([1-8]) \| (.*) \| ([ADKN]) \| (.*) \|\s*$") QUESTIONS = ["what ends, who starts it", "what the thing created", "what it removes, what it keeps on purpose", "what it leaves by accident", "can it be undone", "who is told", "the half-way state", "how do we know"] CLASS_ROW = {"E1": "R-1373", "E2": "R-1374", "E3": "R-1375", "E4": "R-1376", "E5": "R-1377", "E6": "R-1378"} def parse(path): places, errors, cur = [], [], None with open(path, encoding="utf-8") as fh: for ln, line in enumerate(fh, 1): if line.startswith("## TEST-ONLY") or line.startswith("## DOC-ONLY"): cur = None continue if line.startswith("### "): m = HEAD.match(line) if not m: errors.append(f"line {ln}: heading has the wrong shape: {line.strip()}") cur = None continue cur = {"id": m.group(1), "name": m.group(2), "named": m.group(3), "marks": {}, "n": {}, "k": {}} places.append(cur) continue m = ROW.match(line) if not m or cur is None: continue q, mark, ptr = int(m.group(1)), m.group(3), m.group(4) if q in cur["marks"]: errors.append(f"{cur['id']}: question {q} is answered twice") cur["marks"][q] = mark if mark == "A" and "`" not in ptr: errors.append(f"{cur['id']}-{q}: A with no pointer") if mark == "K": rows = re.findall(r"R-\d+", ptr) if not rows: errors.append(f"{cur['id']}-{q}: K with no row") cur["k"][q] = rows if mark == "N": cls = re.findall(r"\bE[1-6]\b", ptr) if len(cls) != 1: errors.append(f"{cur['id']}-{q}: N needs exactly one class, found {cls}") elif not re.search(r"\bS\d+\b", ptr) and "NOBODY KNOWS" not in m.group(2): errors.append(f"{cur['id']}-{q}: N with no search id") cur["n"][q] = cls[0] if cls else "?" for p in places: if sorted(p["marks"]) != list(range(1, 9)): errors.append(f"{p['id']}: has answers {sorted(p['marks'])}, wants 1..8") return places, errors def build(places): out = ["# The Endings list — the matrix", "", "Made by `tools/make-matrix.py` from `TABLE.md`. Do not edit it by hand.", "", "`A` answered · `D` decided, not built · `K` known gap (has a row) · `N` nobody asked. Columns are the eight", "questions: 1 what ends · 2 what it created · 3 removes and keeps · 4 left by accident · 5 undo · 6 who is told · 7 half-way · 8 how we know.", "", "| Ending | Named | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | N |", "|---|---|---|---|---|---|---|---|---|---|---|"] tot = {m: 0 for m in "ADKN"} perq = {q: {m: 0 for m in "ADKN"} for q in range(1, 9)} named = {"NAMED": 0, "MENTIONED": 0, "UNNAMED": 0} members, krows = {c: [] for c in CLASS_ROW}, {} for p in places: named[p["named"]] += 1 cells = [] for q in range(1, 9): mk = p["marks"].get(q, "?") cells.append(f"**{mk}**" if mk == "N" else mk) if mk in tot: tot[mk] += 1 perq[q][mk] += 1 for q, c in p["n"].items(): members.setdefault(c, []).append(f"{p['id']}-{q}") for q, rows in p["k"].items(): for r in rows: krows.setdefault(r, []).append(f"{p['id']}-{q}") out.append(f"| {p['id']} {p['name']} | {p['named'].lower()} | " + " | ".join(cells) + f" | {len(p['n'])} |") n = len(places) out += ["", "## Totals", "", f"- Endings: **{n}** in the table · named **{named['NAMED']}** · mentioned only **{named['MENTIONED']}** · " f"named by no architecture document **{named['UNNAMED']}**.", f"- Answers: **{n * 8}** · A **{tot['A']}** · D **{tot['D']}** · K **{tot['K']}** · N **{tot['N']}**.", f"- Endings with no N: **{sum(1 for p in places if not p['n'])}** of {n}.", "", "| Question | A | D | K | N |", "|---|---|---|---|---|"] for q in range(1, 9): out.append(f"| {q} {QUESTIONS[q - 1]} | {perq[q]['A']} | {perq[q]['D']} | {perq[q]['K']} | {perq[q]['N']} |") out += ["", "## The N answers, by class row", "", "| Class | Row | Count | Members (ending-question) |", "|---|---|---|---|"] for c in sorted(members): out.append(f"| {c} | {CLASS_ROW.get(c, '?')} | {len(members[c])} | {', '.join(members[c])} |") out += ["", "## The K answers, by existing row", "", "| Row | Count | Members |", "|---|---|---|"] for r in sorted(krows, key=lambda x: int(x[2:])): out.append(f"| {r} | {len(krows[r])} | {', '.join(krows[r])} |") return "\n".join(out) + "\n", tot, named, members def main() -> int: places, errors = parse(os.path.join(AUDIT, "TABLE.md")) for e in errors: print("BROKEN " + e) text, tot, named, members = build(places) if "--check" not in sys.argv[1:]: with open(os.path.join(AUDIT, "MATRIX.md"), "w", encoding="utf-8") as fh: fh.write(text) print(f"places {len(places)} named {named} marks {tot} " + " ".join(f"{c}={len(m)}" for c, m in sorted(members.items())) + f" broken {len(errors)}") return 1 if errors else 0 if __name__ == "__main__": sys.exit(main())