main
Colby Knox 0.2.66 source, docs: the README names every verb (generated by tools/readme_verbs.py, release.py refuses a stale list);… e8a8876
#!/usr/bin/env python3
"""Write the README's full verb list from the bridge's own catalog, so the page names every verb.

The groups and their order come from KNOWN in bridge-rs/src/verbs.rs (its comment headings), each verb's line from
the first sentence of its verb_doc summary. The list lives between the two markers below in README.md.

  python3 tools/readme_verbs.py           rewrite README.md
  python3 tools/readme_verbs.py --check   exit 1 if README.md is stale (scripts/release.py runs this)
"""
import re
import sys
from pathlib import Path

ROOT = Path(__file__).resolve().parent.parent
VERBS = ROOT / "bridge-rs" / "src" / "verbs.rs"
README = ROOT / "README.md"
START, END = "<!-- verbs:start (tools/readme_verbs.py writes this list) -->", "<!-- verbs:end -->"

# KNOWN's comment headings, as a reader would say them
HEADINGS = {
    "health / lifecycle": "Running Altium: launch, close, health, dialogs",
    "outputs / project": "Checks, outputs, BOM, schematic editing and project",
    "library authoring": "Libraries: symbols, footprints, 3D bodies, parts",
    "licence / account": "Licence and account",
    "Altium 365": "Altium 365 cloud",
    "agent / REPL": "Scripting: DelphiScript, Altium commands, documents",
    "board / schematic reads": "Reading and setting up the design",
    "the live-board lane AI Flow drives": "Placing and routing a live board",
    "surfaces": "Showing it: views, frames, 3D",
}


def catalog(src: str):
    known = src[src.index("pub const KNOWN"):]
    known = known[:known.index("];")]
    groups, current = [], None
    for line in known.splitlines():
        m = re.match(r"\s*//\s*(.+?)\s*$", line)
        if m:
            current = (m.group(1), [])
            groups.append(current)
            continue
        if current is not None:
            current[1].extend(re.findall(r'"([a-z0-9_]+)"', line))
    docs = {}
    for m in re.finditer(r'verb_doc\(\s*"([a-z0-9_]+)",\s*"((?:[^"\\]|\\.)*)"', src):
        docs[m.group(1)] = m.group(2).replace('\\"', '"').replace("\\'", "'")
    return groups, docs


def first_sentence(s: str) -> str:
    s = s.strip()
    m = re.search(r"(?<!e\.g)(?<!i\.e)\.\s", s)
    if m:
        s = s[:m.start() + 1]
    return s


def render(src: str) -> str:
    groups, docs = catalog(src)
    total = sum(len(v) for _, v in groups)
    out = [START, "", f"All {total} verbs, by area (prefix `altium_`). `altium_describe` returns the live catalog with "
           "every input, output, hint and example.", ""]
    for head, verbs in groups:
        out.append(f"### {HEADINGS.get(head, head[:1].upper() + head[1:])}")
        out.append("")
        for v in verbs:
            d = first_sentence(docs.get(v, ""))
            out.append(f"- `altium_{v}`" + (f": {d}" if d else ""))
        out.append("")
    out.append(END)
    return "\n".join(out)


def main():
    src = VERBS.read_text(encoding="utf-8")
    readme = README.read_text(encoding="utf-8")
    if START not in readme or END not in readme:
        sys.exit(f"README.md has no verb-list markers ({START} ... {END})")
    new = readme[:readme.index(START)] + render(src) + readme[readme.index(END) + len(END):]
    # the counts the prose quotes ("174 verbs", "174 commands", "174 `altium_*` verbs") follow the catalog too
    total = sum(len(v) for _, v in catalog(src)[0])
    new = re.sub(r"(?<![\d,])\d{3}(?= (?:verbs|commands|`altium_\*` verbs)\b)", str(total), new)
    if "--check" in sys.argv:
        if new != readme:
            sys.exit("README.md's verb list is stale: run python3 tools/readme_verbs.py")
        print("readme verb list ok")
        return
    README.write_text(new, encoding="utf-8")
    print("README.md verb list written")


if __name__ == "__main__":
    main()