mirror of
https://github.com/Second-Hand-Friends/kleinanzeigen-bot.git
synced 2026-03-12 02:31:45 +01:00
feat: add type safe Config model
This commit is contained in:
committed by
Sebastian Thomschke
parent
e7a3d46d25
commit
1369da1c34
39
scripts/generate_schemas.py
Normal file
39
scripts/generate_schemas.py
Normal file
@@ -0,0 +1,39 @@
|
||||
# SPDX-FileCopyrightText: © Sebastian Thomschke and contributors
|
||||
# SPDX-License-Identifier: AGPL-3.0-or-later
|
||||
# SPDX-ArtifactOfProjectHomePage: https://github.com/Second-Hand-Friends/kleinanzeigen-bot/
|
||||
import json
|
||||
from pathlib import Path
|
||||
from typing import Type
|
||||
|
||||
from pydantic import BaseModel
|
||||
|
||||
from kleinanzeigen_bot.model.config_model import Config
|
||||
|
||||
|
||||
def generate_schema(model:Type[BaseModel], out_dir:Path) -> None:
|
||||
"""
|
||||
Generate and write JSON schema for the given model.
|
||||
"""
|
||||
name = model.__name__
|
||||
print(f"[+] Generating schema for model [{name}]...")
|
||||
|
||||
# Create JSON Schema dict
|
||||
schema = model.model_json_schema(mode = "validation")
|
||||
schema.setdefault("title", f"{name} Schema")
|
||||
schema.setdefault("description", f"Auto-generated JSON Schema for {name}")
|
||||
|
||||
# Write JSON
|
||||
json_path = out_dir / f"{name.lower()}.schema.json"
|
||||
with json_path.open("w", encoding = "utf-8") as f_json:
|
||||
json.dump(schema, f_json, indent = 2)
|
||||
f_json.write("\n")
|
||||
print(f"[✓] {json_path}")
|
||||
|
||||
|
||||
project_root = Path(__file__).parent.parent
|
||||
out_dir = project_root / "schemas"
|
||||
out_dir.mkdir(parents = True, exist_ok = True)
|
||||
|
||||
print(f"Generating schemas in: {out_dir.resolve()}")
|
||||
generate_schema(Config, out_dir)
|
||||
print("All schemas generated successfully.")
|
||||
Reference in New Issue
Block a user