|
| 1 | +"""Generate OED reference pages from ``oed.json`` (single source of truth). |
| 2 | +
|
| 3 | +The OED standard is defined by ``oed.json`` at the repo root — the same machine-readable |
| 4 | +spec that ods-tools consumes. Rather than hand-maintain reference tables, this extension |
| 5 | +reads ``oed.json`` at build time and writes MyST Markdown into ``reference/_generated/`` |
| 6 | +which the reference pages include. Edit ``oed.json`` (and its source CSVs), not the |
| 7 | +generated Markdown. |
| 8 | +
|
| 9 | +Runs on the Sphinx ``config-inited`` event; also runnable standalone for quick checks. |
| 10 | +""" |
| 11 | +import json |
| 12 | +import os |
| 13 | + |
| 14 | +HERE = os.path.dirname(os.path.abspath(__file__)) |
| 15 | +REPO_ROOT = os.path.abspath(os.path.join(HERE, os.pardir, os.pardir, os.pardir)) |
| 16 | +OED_JSON = os.path.join(REPO_ROOT, "oed.json") |
| 17 | +OUT_DIR = os.path.join(HERE, os.pardir, "reference", "_generated") |
| 18 | + |
| 19 | +# input-file groups, in exposure-modelling order |
| 20 | +FILE_ORDER = [("Loc", "Location"), ("Acc", "Account"), |
| 21 | + ("ReinsInfo", "Reinsurance Info"), ("ReinsScope", "Reinsurance Scope")] |
| 22 | + |
| 23 | + |
| 24 | +def _cell(text): |
| 25 | + return str(text).replace("|", "\\|").replace("\n", " ").replace("\r", " ").strip() |
| 26 | + |
| 27 | + |
| 28 | +def _pipe_table(columns, rows): |
| 29 | + out = ["| " + " | ".join(columns) + " |", |
| 30 | + "| " + " | ".join(["---"] * len(columns)) + " |"] |
| 31 | + for row in rows: |
| 32 | + out.append("| " + " | ".join(_cell(c) for c in row) + " |") |
| 33 | + return "\n".join(out) |
| 34 | + |
| 35 | + |
| 36 | +def _write(name, text): |
| 37 | + os.makedirs(OUT_DIR, exist_ok=True) |
| 38 | + with open(os.path.join(OUT_DIR, name), "w", encoding="utf-8") as fh: |
| 39 | + fh.write(text + "\n") |
| 40 | + |
| 41 | + |
| 42 | +def generate_fields(oed): |
| 43 | + """input_fields -> field reference, grouped by input file.""" |
| 44 | + fields = oed["input_fields"] |
| 45 | + parts = ["<!-- generated by _ext/gen_oed_reference.py from oed.json (input_fields) -->"] |
| 46 | + total = 0 |
| 47 | + for key, title in FILE_ORDER: |
| 48 | + recs = fields.get(key) |
| 49 | + if not recs: |
| 50 | + continue |
| 51 | + parts.append(f"\n## {title} (`{key}`)\n") |
| 52 | + rows = [] |
| 53 | + for rec in recs.values(): |
| 54 | + rows.append([ |
| 55 | + rec.get("Input Field Name", ""), |
| 56 | + rec.get("Type & Description", ""), |
| 57 | + rec.get("Data Type", ""), |
| 58 | + rec.get("Property field status", ""), |
| 59 | + rec.get("Default", ""), |
| 60 | + ]) |
| 61 | + total += 1 |
| 62 | + parts.append(_pipe_table( |
| 63 | + ["Field", "Description", "Data type", "Status", "Default"], rows)) |
| 64 | + _write("oed_fields.md", "\n".join(parts)) |
| 65 | + return total |
| 66 | + |
| 67 | + |
| 68 | +def generate_values(oed): |
| 69 | + """Coded value lists -> code reference tables.""" |
| 70 | + parts = ["<!-- generated by _ext/gen_oed_reference.py from oed.json (value lists) -->"] |
| 71 | + total = 0 |
| 72 | + |
| 73 | + # perils: nested under 'info' |
| 74 | + perils = oed.get("perils", {}).get("info", {}) |
| 75 | + if perils: |
| 76 | + parts.append("\n## Perils\n") |
| 77 | + rows = [[code, r.get("DB table PerilCode", ""), r.get("Peril Description", ""), |
| 78 | + r.get("Grouped PerilCode", "")] for code, r in perils.items()] |
| 79 | + parts.append(_pipe_table(["Code", "PerilCode", "Description", "Grouped"], rows)) |
| 80 | + total += len(rows) |
| 81 | + |
| 82 | + def simple(key, title, cols): |
| 83 | + nonlocal total |
| 84 | + v = oed.get(key) |
| 85 | + if not isinstance(v, dict): |
| 86 | + return |
| 87 | + parts.append(f"\n## {title}\n") |
| 88 | + rows = [[code] + [rec.get(c, "") for _, c in cols] for code, rec in v.items()] |
| 89 | + parts.append(_pipe_table(["Code"] + [h for h, _ in cols], rows)) |
| 90 | + total += len(rows) |
| 91 | + |
| 92 | + simple("occupancy", "Occupancy codes", |
| 93 | + [("Name", "Name"), ("Description", "Description"), ("Broad Category", "Broad Category")]) |
| 94 | + simple("construction", "Construction codes", |
| 95 | + [("Name", "Name"), ("Description", "Description"), ("Broad Category", "Broad Category")]) |
| 96 | + simple("country", "Country codes", [("Name", "Name")]) |
| 97 | + simple("CoverageValues", "Coverage types", |
| 98 | + [("CoverageID", "CoverageID"), ("Description", "Description"), ("Type", "Type")]) |
| 99 | + _write("oed_values.md", "\n".join(parts)) |
| 100 | + return total |
| 101 | + |
| 102 | + |
| 103 | +def _ensure_oed_json(): |
| 104 | + """oed.json is a generated artifact (utils/gen-json.py builds it from the CSVs) and is not |
| 105 | + committed. Generate it if missing so the docs build is self-sufficient in CI.""" |
| 106 | + if os.path.exists(OED_JSON): |
| 107 | + return |
| 108 | + import subprocess |
| 109 | + import sys |
| 110 | + gen = os.path.join(REPO_ROOT, "utils", "gen-json.py") |
| 111 | + subprocess.run([sys.executable, gen, "--output-path", OED_JSON], cwd=REPO_ROOT, check=True) |
| 112 | + |
| 113 | + |
| 114 | +def run(app=None, config=None): |
| 115 | + _ensure_oed_json() |
| 116 | + with open(OED_JSON, encoding="utf-8") as fh: |
| 117 | + oed = json.load(fh) |
| 118 | + n_fields = generate_fields(oed) |
| 119 | + n_values = generate_values(oed) |
| 120 | + msg = f"[gen_oed_reference] wrote {n_fields} fields, {n_values} coded values -> reference/_generated/" |
| 121 | + if app is not None: |
| 122 | + from sphinx.util import logging |
| 123 | + logging.getLogger(__name__).info(msg) |
| 124 | + else: |
| 125 | + print(msg) |
| 126 | + |
| 127 | + |
| 128 | +def setup(app): |
| 129 | + app.connect("config-inited", run) |
| 130 | + return {"parallel_read_safe": True, "parallel_write_safe": True} |
| 131 | + |
| 132 | + |
| 133 | +if __name__ == "__main__": |
| 134 | + run() |
0 commit comments