#!/usr/bin/env python3 """Dispatcher for Hanzi data scripts.""" from __future__ import annotations import argparse import subprocess import sys from pathlib import Path ROOT = Path(__file__).resolve().parents[1] PY = sys.executable def run_script(name: str, *args: str) -> None: subprocess.run([PY, str(ROOT / "scripts" / name), *args], check=True) def prompt_choice(label: str, choices: list[str]) -> str: while True: value = input(f"{label} ({', '.join(choices)}): ").strip() if value in choices: return value print(f"Choose one of: {', '.join(choices)}") def prompt_audio_args() -> tuple[str, str, str]: from build_audio import VALID_LOCALES locale = prompt_choice("Locale", VALID_LOCALES) dictionary = prompt_choice("Dict", ["character", "vocabulary"]) while True: value = input("Limit (0 = all) [0]: ").strip() or "0" try: limit = int(value) except ValueError: limit = -1 if limit >= 0: return locale, dictionary, str(limit) print("Enter a non-negative integer.") def main() -> None: parser = argparse.ArgumentParser(description=__doc__) sub = parser.add_subparsers(dest="command", required=True) p = sub.add_parser("build-data", help="Build all data assets; skip audio unless requested") p.add_argument("--prod", action="store_true") p.add_argument("--audio", action="store_true", help="Prompt for and generate audio before building") sub.add_parser("sort:dicts").add_argument("--lang") sub.add_parser("update:dicts") sub.add_parser("update:sentences") sub.add_parser("games:audio-matrix") args = parser.parse_args() prod = ["--prod"] if getattr(args, "prod", False) else [] if args.command == "build-data": if args.audio: audio_args = prompt_audio_args() print("=== build:audio ===", flush=True) run_script("build_audio.py", *audio_args) print("\n=== games:gen-audio:zhuyin ===", flush=True) run_script("games_gen_audio_zhuyin.py") print("=== build:app ===") run_script("build_app.py", *prod) print("\n=== build:progress ===") run_script("build_progress.py", *prod) print("\n=== build:licenses ===") run_script("build_licenses.py", *prod) print("\n=== games:build ===") run_script("games_build.py", *prod) elif args.command == "games:audio-matrix": run_script("games_audio_matrix.py") elif args.command == "sort:dicts": lang = [f"--lang={args.lang}"] if args.lang else [] run_script("sort_dicts.py", *lang) elif args.command == "update:dicts": run_script("update_dicts.py") elif args.command == "update:sentences": run_script("update_sentences.py") if __name__ == "__main__": main()