Skip to content

Commit

Permalink
add pydantic as dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
xnuinside committed Jul 12, 2021
1 parent e8fbb1a commit f85c07a
Show file tree
Hide file tree
Showing 14 changed files with 101 additions and 69 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
**v0.8.2**
**v0.8.3**
1. Added fundamental concept of TableMetaModel - class that unifies metadata parsed from different classes/ORM models types/DDLs to one standard to allow easy way convert one models to another
in next releases it will be used for converter from one type of models to another.
2. Fixed issue: https://github.com/xnuinside/omymodels/issues/18 "NOW() not recognized as now()"
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ Please describe issue that you want to solve and open the PR, I will review it a
Any questions? Ping me in Telegram: https://t.me/xnuinside

## Changelog
**v0.8.2**
**v0.8.3**
1. Added fundamental concept of TableMetaModel - class that unifies metadata parsed from different classes/ORM models types/DDLs to one standard to allow easy way convert one models to another
in next releases it will be used for converter from one type of models to another.
2. Fixed issue: https://github.com/xnuinside/omymodels/issues/18 "NOW() not recognized as now()"
Expand Down
2 changes: 1 addition & 1 deletion docs/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ Any questions? Ping me in Telegram: https://t.me/xnuinside
Changelog
---------

**v0.8.2**
**v0.8.3**


#. Added fundamental concept of TableMetaModel - class that unifies metadata parsed from different classes/ORM models types/DDLs to one standard to allow easy way convert one models to another
Expand Down
27 changes: 13 additions & 14 deletions omymodels/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,13 @@

from typing import Optional, List, Dict

from typing_extensions import final
from simple_ddl_parser import DDLParser, parse_from_file
from omymodels.gino import core as g
from omymodels.pydantic import core as p
from omymodels.dataclass import core as d
from omymodels.sqlalchemy import core as s
from omymodels.sqlalchemy_core import core as sc
from omymodels.meta_model import TableMeta, Column, Type
from omymodels.meta_model import TableMeta, Type


def get_tables_information(
Expand Down Expand Up @@ -46,7 +45,7 @@ def create_models(
data = get_tables_information(ddl, ddl_path)
data = remove_quotes_from_strings(data)
data = convert_ddl_to_models(data)
if not data['tables']:
if not data["tables"]:
sys.exit(0)
# generate code
output = generate_models_file(
Expand All @@ -60,15 +59,15 @@ def create_models(


def convert_ddl_to_models(data):
final_data = {'tables': [], 'types': []}
final_data = {"tables": [], "types": []}
tables = []
for table in data['tables']:
for table in data["tables"]:
tables.append(TableMeta(**table))
final_data['tables'] = tables
_types = []
for _type in data['types']:
final_data["tables"] = tables
_types = []
for _type in data["types"]:
_types.append(Type(**_type))
final_data['types'] = _types
final_data["types"] = _types
return final_data


Expand Down Expand Up @@ -113,20 +112,20 @@ def generate_models_file(
schema_global=schema_global,
defaults_off=defaults_off,
)
header = model_generator.create_header(
data["tables"], schema=schema_global)
header = model_generator.create_header(data["tables"], schema=schema_global)
output = render_jinja2_template(models_type, models_str, header)
return output


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

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


Expand Down
14 changes: 7 additions & 7 deletions omymodels/dataclass/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def generate_attr(self, column: Dict, defaults_off: bool) -> str:
self.typing_imports.add(_type.split("[")[0])
elif "datetime" in _type:
self.datetime_import = True
self.additional_imports.add('field')
self.additional_imports.add("field")
elif "[" in column.type:
self.typing_imports.add("List")
_type = f"List[{_type}]"
Expand Down Expand Up @@ -75,14 +75,12 @@ def generate_model(
**kwargs,
) -> str:
model = ""

# mean one model one table
model += "\n\n"
model += (
dt.dataclass_class.format(
class_name=create_class_name(
table.name, singular, exceptions
),
class_name=create_class_name(table.name, singular, exceptions),
table_name=table.name,
)
) + "\n\n"
Expand Down Expand Up @@ -114,8 +112,10 @@ def create_header(self, *args, **kwargs) -> str:
if self.additional_imports:
self.additional_imports = f', {",".join(self.additional_imports)}'
else:
self.additional_imports = ''
header += dt.dataclass_imports.format(additional_imports=self.additional_imports)
self.additional_imports = ""
header += dt.dataclass_imports.format(
additional_imports=self.additional_imports
)
return header

def generate_type(
Expand Down
2 changes: 1 addition & 1 deletion omymodels/dataclass/templates.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,4 @@ class {class_name}:"""
enum_import = "from enum import {enums}"

uuid_import = "from uuid import UUID"
field_datetime_now = "field(default_factory=datetime.datetime.now)"
field_datetime_now = "field(default_factory=datetime.datetime.now)"
13 changes: 4 additions & 9 deletions omymodels/gino/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ def setup_column_attributes(
column = self.add_reference_to_the_column(
column_data.name, column, column_data.references
)
if not column_data.nullable and not column_data.name in table_pk:
if not column_data.nullable and column_data.name not in table_pk:
column += gt.required
if column_data.default is not None:
column = self.prepare_column_default(column_data, column)
Expand All @@ -104,13 +104,13 @@ def setup_column_attributes(
if "columns" in table_data.alter:
for alter_column in table_data.alter["columns"]:
if (
alter_column['name'] == column_data.name
alter_column["name"] == column_data.name
and not alter_column["constraint_name"]
and alter_column["references"]
):

column = self.add_reference_to_the_column(
alter_column['name'], column, alter_column["references"]
alter_column["name"], column, alter_column["references"]
)
return column

Expand Down Expand Up @@ -222,12 +222,7 @@ def generate_model(
)
for column in table.columns:
model += self.generate_column(column, table.primary_key, table)
if (
table.indexes
or table.alter
or table.checks
or not schema_global
):
if table.indexes or table.alter or table.checks or not schema_global:
model = self.add_table_args(model, table, schema_global)
# create sequence
return model
Expand Down
23 changes: 13 additions & 10 deletions omymodels/meta_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@


class TableProperties(BaseModel):

indexes: List


class Column(BaseModel):

name: str
type: str
size: Optional[Union[str, int, tuple]]
Expand All @@ -19,29 +20,31 @@ class Column(BaseModel):
generated_as: Optional[str]
other_properties: Optional[dict]
references: Optional[dict]
@validator('size')

@validator("size")
def size_must_contain_space(cls, v):
if isinstance(v, str) and v.isnumeric():
return int(v)
return v


class TableMeta(BaseModel):
name: str = Field(alias='table_name')
table_schema: Optional[str] = Field(alias='schema')
name: str = Field(alias="table_name")
table_schema: Optional[str] = Field(alias="schema")
columns: List[Column]
indexes: Optional[List[dict]] = Field(alias='index')
indexes: Optional[List[dict]] = Field(alias="index")
alter: Optional[dict]
checks: Optional[List[dict]]
properties: Optional[TableProperties]
primary_key: List

class Config:
""" pydantic class config """

arbitrary_types_allowed = True


class Type(BaseModel):
name: str = Field(alias='type_name')
name: str = Field(alias="type_name")
base_type: str
properties: Optional[dict]
properties: Optional[dict]
4 changes: 1 addition & 3 deletions omymodels/pydantic/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,7 @@ def generate_model(
model += "\n\n"
model += (
pt.pydantic_class.format(
class_name=create_class_name(
table.name, singular, exceptions
),
class_name=create_class_name(table.name, singular, exceptions),
table_name=table.name,
)
) + "\n\n"
Expand Down
18 changes: 7 additions & 11 deletions omymodels/sqlalchemy/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ def setup_column_attributes(
column = self.add_reference_to_the_column(
column_data.name, column, column_data.references
)
if not column_data.nullable and not column_data.name in table_pk:
if not column_data.nullable and column_data.name not in table_pk:
column += st.required
if column_data.default is not None:
column = self.prepare_column_default(column_data, column)
Expand All @@ -104,13 +104,13 @@ def setup_column_attributes(
if "columns" in table_data.alter:
for alter_column in table_data.alter["columns"]:
if (
alter_column['name'] == column_data.name
alter_column["name"] == column_data.name
and not alter_column["constraint_name"]
and alter_column["references"]
):

column = self.add_reference_to_the_column(
alter_column['name'], column, alter_column["references"]
alter_column["name"], column, alter_column["references"]
)
return column

Expand All @@ -133,7 +133,8 @@ def generate_column(
""" method to generate full column defention """
column_type = self.prepare_column_type(column_data)
column = self.setup_column_attributes(
column_data, table_pk, column_type, table_data)
column_data, table_pk, column_type, table_data
)
column += ")\n"
return column

Expand Down Expand Up @@ -214,19 +215,14 @@ def generate_model(
) -> str:
""" method to prepare one Model defention - name & tablename & columns """
model = ""

model = st.model_template.format(
model_name=create_class_name(table.name, singular, exceptions),
table_name=table.name,
)
for column in table.columns:
model += self.generate_column(column, table.primary_key, table)
if (
table.indexes
or table.alter
or table.checks
or not schema_global
):
if table.indexes or table.alter or table.checks or not schema_global:
model = self.add_table_args(model, table, schema_global)
return model

Expand Down
6 changes: 3 additions & 3 deletions omymodels/sqlalchemy_core/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ def get_column_attributes(
properties.append(
self.column_reference(column_data.name, column_data.references)
)
if not column_data.nullable and not column_data.name in table_pk:
if not column_data.nullable and column_data.name not in table_pk:
properties.append(st.required)
if column_data.default is not None:
properties.append(self.column_default(column_data))
Expand All @@ -103,14 +103,14 @@ def get_column_attributes(
if "columns" in table_data.alter:
for alter_column in table_data.alter["columns"]:
if (
alter_column['name'] == column_data.name
alter_column["name"] == column_data.name
and not alter_column["constraint_name"]
and alter_column["references"]
and not column_data.references
):
properties.append(
self.column_reference(
alter_column['name'], alter_column["references"]
alter_column["name"], alter_column["references"]
)
)
return properties
Expand Down
Loading

0 comments on commit f85c07a

Please sign in to comment.