File size: 1,325 Bytes
497c3ef
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
"""Regenerate ``schema.json`` from the Pydantic models.

Usage::

    python scripts/generate_schema.py            # write schema.json
    python scripts/generate_schema.py --check     # exit 1 if schema.json is stale
"""

from __future__ import annotations

import argparse
import json
import sys
from pathlib import Path

from bl_ocr.domain.schema_gen import build_json_schema

PROJECT_ROOT = Path(__file__).resolve().parents[1]
SCHEMA_PATH = PROJECT_ROOT / "schema.json"


def render() -> str:
    return json.dumps(build_json_schema(), indent=2, ensure_ascii=False) + "\n"


def main(argv: list[str] | None = None) -> int:
    parser = argparse.ArgumentParser(description=__doc__)
    parser.add_argument("--check", action="store_true", help="Fail if schema.json is out of date.")
    args = parser.parse_args(argv)

    rendered = render()
    if args.check:
        current = SCHEMA_PATH.read_text(encoding="utf-8") if SCHEMA_PATH.exists() else ""
        if current != rendered:
            print("schema.json is out of date; run scripts/generate_schema.py", file=sys.stderr)
            return 1
        print("schema.json is up to date")
        return 0

    SCHEMA_PATH.write_text(rendered, encoding="utf-8")
    print(f"Wrote {SCHEMA_PATH}")
    return 0


if __name__ == "__main__":
    raise SystemExit(main())