-
Notifications
You must be signed in to change notification settings - Fork 89
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #416 from xxyzz/json_schema
Add a script to create all JSON schema files from pydantic models
- Loading branch information
Showing
4 changed files
with
52 additions
and
48 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import importlib | ||
import json | ||
from importlib.resources import files | ||
|
||
|
||
def main() -> None: | ||
""" | ||
Run this script at the project root folder to generate JSON schema files of | ||
each extractor that has pydantic model `WordEntry` defined in the | ||
`models.py` file. | ||
""" | ||
|
||
extractor_folder = files("wiktextract") / "extractor" | ||
for extractor_folder in filter( | ||
lambda p: p.is_dir(), (files("wiktextract") / "extractor").iterdir() | ||
): | ||
if (extractor_folder / "models.py").is_file(): | ||
lang_code = extractor_folder.stem | ||
model_module = importlib.import_module( | ||
f"wiktextract.extractor.{lang_code}.models" | ||
) | ||
model_schema = model_module.WordEntry.model_json_schema() | ||
model_schema["$id"] = f"https://kaikki.org/{lang_code}.json" | ||
model_schema[ | ||
"$schema" | ||
] = "https://json-schema.org/draft/2020-12/schema" | ||
with open( | ||
f"json_schema/{lang_code}.json", "w", encoding="utf-8" | ||
) as f: | ||
json.dump( | ||
model_schema, | ||
f, | ||
indent=2, | ||
ensure_ascii=False, | ||
sort_keys=True, | ||
) | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |
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 |
---|---|---|
@@ -1,4 +1,5 @@ | ||
import importlib | ||
import importlib.util | ||
import types | ||
|
||
|
||
|