-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add script to generate JSON schema for hexdoc.toml
- Loading branch information
1 parent
fb416da
commit 7b3e7a3
Showing
12 changed files
with
271 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Internal hexdoc helper scripts. This package is not published to PyPI. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import json | ||
from pathlib import Path | ||
from typing import Annotated, Any, Optional | ||
|
||
import rich | ||
import typer | ||
from hexdoc.cli.utils import DefaultTyper | ||
from hexdoc.cli.utils.args import parse_import_class | ||
from pydantic import BaseModel, TypeAdapter | ||
from typer import Argument, Option | ||
|
||
app = DefaultTyper() | ||
|
||
|
||
@app.command() | ||
def main( | ||
model_type: Annotated[type[Any], Argument(parser=parse_import_class)], | ||
*, | ||
output_path: Annotated[Optional[Path], Option("--output", "-o")] = None, | ||
): | ||
if issubclass(model_type, BaseModel): | ||
schema = model_type.model_json_schema() | ||
else: | ||
schema = TypeAdapter(model_type).json_schema() | ||
|
||
if output_path: | ||
output_path.parent.mkdir(parents=True, exist_ok=True) | ||
with output_path.open("w", encoding="utf-8") as f: | ||
json.dump(schema, f, indent=2) | ||
typer.echo(f"Successfully wrote schema for {model_type} to {output_path}.") | ||
else: | ||
typer.echo(f"Schema for {model_type}:") | ||
rich.print(schema) | ||
|
||
|
||
if __name__ == "__main__": | ||
app() |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.