BL / scripts /generate_schema.py
youssefboutaleb's picture
Implement OCR pipeline enhancements: Pydantic schemas, validation gate, retry, and evaluation harness (excluding images)
497c3ef
Raw History Blame Contribute Delete
1.33 kB
"""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())