Skip to content

Commit

Permalink
add generators.py
Browse files Browse the repository at this point in the history
  • Loading branch information
xnuinside committed Aug 22, 2021
1 parent 1c90e2a commit 320cf6d
Showing 1 changed file with 47 additions and 0 deletions.
47 changes: 47 additions & 0 deletions omymodels/generators.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import pathlib
from types import ModuleType
from jinja2 import Template

from omymodels.models.gino import core as g
from omymodels.models.pydantic import core as p
from omymodels.models.dataclass import core as d
from omymodels.models.sqlalchemy import core as s
from omymodels.models.sqlalchemy_core import core as sc


models = {
"gino": g,
"pydantic": p,
"dataclass": d,
"sqlalchemy": s,
"sqlalchemy_core": sc,
}


supported_models = list(models.keys())


def get_model(models_type: str) -> ModuleType:
model = models.get(models_type)
return model


def get_generator_by_type(models_type: str):
model = get_model(models_type)
if not model:
raise ValueError(
f"Unsupported models type {models_type}. Possible variants: {supported_models}"
)
return getattr(model, "ModelGenerator")()


def render_jinja2_template(models_type: str, models: str, headers: str) -> str:
template_file = (
pathlib.Path(__file__).parent / "models" / models_type / f"{models_type}.jinja2"
)

with open(template_file) as t:
template = t.read()
template = Template(template)
params = {"models": models, "headers": headers}
return template.render(**params)

0 comments on commit 320cf6d

Please sign in to comment.