Files

69 lines
2.4 KiB
Python

"""Generate the JS data tables for uarite-js from the Python source.
Run from the repository root:
uv run scripts/port_tables.py
Writes uarite-js/src/tables.ts. That file is generated — edit the Python
tables (uarite/bots.py, uarite/clients.py) instead and re-run this script.
"""
import json
from pathlib import Path
from uarite.bots import BOTS, KIND_LABEL, LABELED, PRETTY_OVERRIDE, PROVIDER_OF
from uarite.clients import BROWSERS, SAMSUNG, SAMSUNG_SERIES
OUT = Path(__file__).resolve().parent.parent / "uarite-js" / "src" / "tables.ts"
HEADER = """\
// Generated by scripts/port_tables.py from uarite/bots.py and
// uarite/clients.py. Do not edit by hand; re-run the script.
"""
def js(obj: object) -> str:
return json.dumps(obj, indent=2, ensure_ascii=False)
def main() -> None:
src = [HEADER]
src.append(
"/** UA token -> display name, checked in order; first hit wins. */\n"
f"export const BROWSERS: ReadonlyArray<readonly [string, string]> = {js(list(map(list, BROWSERS)))};\n"
)
src.append(
"/** Lowercase UA substring -> [display name, kind]. First match wins. */\n"
f"export const BOTS: Readonly<Record<string, readonly [string, string]>> = {js({k: list(v) for k, v in BOTS.items()})};\n"
)
src.append(
"/** Pretty suffixes for the kinds more precise than a generic spider. */\n"
f"export const KIND_LABEL: Readonly<Record<string, string>> = {js(KIND_LABEL)};\n"
)
src.append(
"/** Bot display name -> provider. */\n"
f"export const PROVIDER_OF: Readonly<Record<string, string>> = {js(PROVIDER_OF)};\n"
)
src.append(
"/** Per-bot pretty overrides: the full display string. */\n"
f"export const PRETTY_OVERRIDE: Readonly<Record<string, string>> = {js(PRETTY_OVERRIDE)};\n"
)
src.append(
"/** Bot names whose kind label is displayed. */\n"
f"export const LABELED: ReadonlySet<string> = new Set({js(sorted(LABELED))});\n"
)
src.append(
"/** Samsung model code (without region/carrier letter) -> marketing name. */\n"
f"export const SAMSUNG: Readonly<Record<string, string>> = {js(SAMSUNG)};\n"
)
src.append(
"/** Samsung series for codes missing from SAMSUNG. */\n"
f"export const SAMSUNG_SERIES: Readonly<Record<string, string>> = {js(SAMSUNG_SERIES)};\n"
)
OUT.write_text("\n".join(src))
print(f"wrote {OUT}")
if __name__ == "__main__":
main()