-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerate_schemas.py
60 lines (45 loc) · 1.72 KB
/
generate_schemas.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
""" Module for generating schemas frpm pydantic models. """
# standard imports
from pathlib import Path
from typing import List
# third-party imports
from pydantic import BaseModel
import pydantic
# internal imports
from interface import (
ProgrammingLanguage,
Licenses,
Organization,
ToolCategory,
SoftwareTool,
)
from util import read_file, write_file
# pylint:disable=missing-class-docstring
# pylint:disable=missing-function-docstring
class SchemaItemModel(BaseModel):
"""Interface for schema item."""
name: str
model: pydantic._internal._model_construction.ModelMetaclass
class Config:
arbitrary_types_allowed = True
class SchemaManager:
def __init__(self, schema_folder: str = ".vscode"):
self.schema_folder = Path(schema_folder)
self.schemas: List[SchemaItemModel] = []
def add_schema(self, name: str, model: BaseModel):
self.schemas.append(SchemaItemModel(name=name, model=model))
def generate_and_save_schemas(self):
if not self.schema_folder.exists():
self.schema_folder.mkdir()
for schema in self.schemas:
json_schema = schema.model.model_json_schema()
schema_file = self.schema_folder / f"{schema.name}.json"
write_file(json_schema, schema_file)
if __name__ == "__main__":
schema_manager = SchemaManager(schema_folder="../schemas")
schema_manager.add_schema("programming_languages", ProgrammingLanguage)
schema_manager.add_schema("licenses", Licenses)
schema_manager.add_schema("category", ToolCategory)
schema_manager.add_schema("software_tool", SoftwareTool)
schema_manager.add_schema("organization", Organization)
schema_manager.generate_and_save_schemas()