Skip to content

export_schemas

src.models.export_schemas

Export JSON Schema files from Pydantic models.

Generates one .schema.json file per model, matching the layout in the data-schemas repository. Run this after modifying any model in src/models/ to keep schemas in sync.

Usage

python -m src.models.export_schemas --output_dir ../data-schemas/schemas

export_all(output_dir: str) -> list[str]

Export all registered schemas. Returns list of written file paths.

Source code in src/models/export_schemas.py
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
def export_all(output_dir: str) -> list[str]:
    """Export all registered schemas. Returns list of written file paths."""
    os.makedirs(output_dir, exist_ok=True)
    written = []

    for filename, is_array, module_path, class_name in SCHEMA_REGISTRY:
        cls = _import_class(module_path, class_name)
        schema = cls.model_json_schema()
        schema_id = f"{BASE_URL}/{filename}"

        if is_array:
            title = schema.get("title", class_name)
            item_description = schema.get("description", "")
            # Use the class docstring first line if model description is empty
            if not item_description and cls.__doc__:
                item_description = cls.__doc__.strip().split("\n")[0]
            # Collection description summarises the array; item description stays on the $def.
            collection_description = f"Array of {title} records. Each element: {item_description}"
            final = _make_array_schema(schema, f"{title} Collection", collection_description, schema_id)
        else:
            final = _make_object_schema(schema, schema_id)

        path = os.path.join(output_dir, filename)
        with open(path, "w", encoding="utf-8") as f:
            json.dump(final, f, indent=2, ensure_ascii=False)
            f.write("\n")

        written.append(path)
        logger.info(f"  {filename}")

    return written

tag_schema_repo(schema_repo: str | Path) -> str | None

Create a git tag v{SCHEMA_VERSION} in the data-schemas repo.

If the tag already exists the function is a no-op. Returns the tag name on success, None when the tag was already present.

Source code in src/models/export_schemas.py
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
def tag_schema_repo(schema_repo: str | Path) -> str | None:
    """Create a git tag ``v{SCHEMA_VERSION}`` in the data-schemas repo.

    If the tag already exists the function is a no-op.  Returns the tag
    name on success, ``None`` when the tag was already present.
    """
    from src.models import SCHEMA_VERSION

    repo = Path(schema_repo).resolve()
    tag = f"v{SCHEMA_VERSION}"

    # Check if tag exists
    result = subprocess.run(
        ["git", "tag", "-l", tag],
        capture_output=True,
        text=True,
        cwd=repo,
        timeout=10,
    )
    if tag in result.stdout.strip().splitlines():
        logger.info("Tag %s already exists in %s", tag, repo)
        return None

    # Stage, commit, then tag
    subprocess.run(["git", "add", "-A"], cwd=repo, check=True, timeout=10)
    diff = subprocess.run(["git", "diff", "--cached", "--quiet"], cwd=repo, timeout=10)
    if diff.returncode != 0:
        subprocess.run(
            ["git", "commit", "-m", f"Schema {tag}"],
            cwd=repo,
            check=True,
            timeout=30,
        )
    subprocess.run(
        ["git", "tag", "-a", tag, "-m", f"Data schema version {SCHEMA_VERSION}"],
        cwd=repo,
        check=True,
        timeout=10,
    )
    logger.info("Created tag %s in %s", tag, repo)
    return tag