# ruff: noqa: INP001 r"""Write what chapter 6 of the report reads: numbers, tables and figure data from the result files. Reads the eight files in ``data/resultater/`` and writes to ``rapport/generert/``. Each module in ``rapport/resultater/`` has ``lag(data: Path, ut: Path) -> dict[str, Tall]``: it writes its own tables (``.tex``) and figure data (``.dat``) to ``ut`` and returns every number it shows, under keys that start with the module's name and a full stop. This script collects them and writes ``tall.tex`` (``\tall{key}`` in the prose) and ``tall.json`` (key, shown string, file, path, raw value: the list a reader checks against the files). Standard library only. Same result files, same bytes. Run from anywhere: ``python3 rapport/lag_resultater.py`` (all modules) or ``python3 rapport/lag_resultater.py --bare dommer`` (one module, while it is being written; then ``tall-dommer.tex`` and ``tall-dommer.json`` are written instead). """ from __future__ import annotations import importlib import json import re import sys from pathlib import Path HER = Path(__file__).resolve().parent ROT = HER.parent DATA = ROT / "data" / "resultater" UT = HER / "generert" MODULER = ("dommer", "tidslinje", "miljo", "eierformer", "terskler", "folsomheter") NOKKEL = re.compile(r"^[a-z0-9]+(?:[.-][A-Za-z0-9]+)*$") def main(argv: list[str]) -> int: """Run the modules and write the two lists of numbers.""" sys.path.insert(0, str(HER)) from resultater.felles import OMREGNINGER, Tall # noqa: PLC0415 moduler = MODULER endelse = "" if len(argv) == 2 and argv[0] == "--bare" and argv[1] in MODULER: # noqa: PLR2004 moduler = (argv[1],) endelse = "-" + argv[1] elif argv: sys.stderr.write(f"bruk: lag_resultater.py [--bare {'|'.join(MODULER)}]\n") return 2 UT.mkdir(exist_ok=True) alle: dict[str, Tall] = {} for navn in moduler: modul = importlib.import_module(f"resultater.{navn}") for nokkel, tall in modul.lag(DATA, UT).items(): if not nokkel.startswith(navn + ".") or not NOKKEL.match(nokkel): sys.stderr.write(f"FEIL: ugyldig nokkel {nokkel!r} fra {navn}\n") return 1 if nokkel in alle: sys.stderr.write(f"FEIL: nokkelen {nokkel!r} finnes to ganger\n") return 1 if tall.omregning not in OMREGNINGER or (tall.omregning == "avledet") != (not tall.sti): sys.stderr.write(f"FEIL: {nokkel!r} har ugyldig omregning eller sti\n") return 1 alle[nokkel] = tall linjer = [ f"\\expandafter\\def\\csname tall@{nokkel}\\endcsname{{{tall.vist}}}%" for nokkel, tall in sorted(alle.items()) ] (UT / f"tall{endelse}.tex").write_text("\n".join(linjer) + "\n", encoding="utf-8") liste = { nokkel: { "vist": tall.vist, "fil": tall.fil, "sti": [list(s) if isinstance(s, tuple) else s for s in tall.sti], "raa": tall.raa, "omregning": tall.omregning, "desimaler": tall.desimaler, "forklaring": tall.forklaring, } for nokkel, tall in sorted(alle.items()) } (UT / f"tall{endelse}.json").write_text( json.dumps(liste, ensure_ascii=False, indent=1) + "\n", encoding="utf-8" ) sys.stdout.write(f"OK: {len(alle)} tall fra {', '.join(moduler)} skrevet til {UT}\n") return 0 if __name__ == "__main__": sys.exit(main(sys.argv[1:]))