#!/usr/bin/env python3 """Regenerate the QR code of the support link in ``src/ui/support.py``. The game draws the code from the module's rows, so the runtime needs no QR library. This tool needs ``segno`` (``pip install segno``) and is only run when ``SUPPORT_URL`` changes; ``--check`` verifies the stored rows. """ from __future__ import annotations import argparse from pathlib import Path import re import sys ROOT = Path(__file__).resolve().parents[1] MODULE = ROOT / "src" / "ui" / "support.py" BEGIN, END = "# --- generated by tools/gen_support_qr.py ---", "# --- end generated ---" def render_rows(url: str) -> list[str]: import segno code = segno.make(url, error="m", micro=False) return ["".join("1" if cell else "0" for cell in row) for row in code.matrix_iter(scale=1, border=0)] def block(rows: list[str]) -> str: body = "\n".join(f' "{row}",' for row in rows) return f"{BEGIN}\nQR_ROWS = (\n{body}\n)\n{END}" def main() -> int: parser = argparse.ArgumentParser(description=__doc__) parser.add_argument("--check", action="store_true") args = parser.parse_args() text = MODULE.read_text(encoding="utf-8") url = re.search(r'^SUPPORT_URL = "([^"]+)"$', text, re.M).group(1) start, stop = text.index(BEGIN), text.index(END) + len(END) updated = text[:start] + block(render_rows(url)) + text[stop:] if args.check: if updated != text: print("support QR is stale: run python tools/gen_support_qr.py", file=sys.stderr) return 1 print("support QR up to date") return 0 MODULE.write_text(updated, encoding="utf-8") print(f"wrote {MODULE.relative_to(ROOT)}") return 0 if __name__ == "__main__": raise SystemExit(main())