Skip to content

Commit f85c07a

Browse files
committed
add pydantic as dependency
1 parent e8fbb1a commit f85c07a

File tree

14 files changed

+101
-69
lines changed

14 files changed

+101
-69
lines changed

CHANGELOG.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
**v0.8.2**
1+
**v0.8.3**
22
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
33
in next releases it will be used for converter from one type of models to another.
44
2. Fixed issue: https://github.com/xnuinside/omymodels/issues/18 "NOW() not recognized as now()"

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,7 @@ Please describe issue that you want to solve and open the PR, I will review it a
278278
Any questions? Ping me in Telegram: https://t.me/xnuinside
279279

280280
## Changelog
281-
**v0.8.2**
281+
**v0.8.3**
282282
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
283283
in next releases it will be used for converter from one type of models to another.
284284
2. Fixed issue: https://github.com/xnuinside/omymodels/issues/18 "NOW() not recognized as now()"

docs/README.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,7 @@ Any questions? Ping me in Telegram: https://t.me/xnuinside
293293
Changelog
294294
---------
295295

296-
**v0.8.2**
296+
**v0.8.3**
297297

298298

299299
#. 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

omymodels/common.py

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,13 @@
55

66
from typing import Optional, List, Dict
77

8-
from typing_extensions import final
98
from simple_ddl_parser import DDLParser, parse_from_file
109
from omymodels.gino import core as g
1110
from omymodels.pydantic import core as p
1211
from omymodels.dataclass import core as d
1312
from omymodels.sqlalchemy import core as s
1413
from omymodels.sqlalchemy_core import core as sc
15-
from omymodels.meta_model import TableMeta, Column, Type
14+
from omymodels.meta_model import TableMeta, Type
1615

1716

1817
def get_tables_information(
@@ -46,7 +45,7 @@ def create_models(
4645
data = get_tables_information(ddl, ddl_path)
4746
data = remove_quotes_from_strings(data)
4847
data = convert_ddl_to_models(data)
49-
if not data['tables']:
48+
if not data["tables"]:
5049
sys.exit(0)
5150
# generate code
5251
output = generate_models_file(
@@ -60,15 +59,15 @@ def create_models(
6059

6160

6261
def convert_ddl_to_models(data):
63-
final_data = {'tables': [], 'types': []}
62+
final_data = {"tables": [], "types": []}
6463
tables = []
65-
for table in data['tables']:
64+
for table in data["tables"]:
6665
tables.append(TableMeta(**table))
67-
final_data['tables'] = tables
68-
_types = []
69-
for _type in data['types']:
66+
final_data["tables"] = tables
67+
_types = []
68+
for _type in data["types"]:
7069
_types.append(Type(**_type))
71-
final_data['types'] = _types
70+
final_data["types"] = _types
7271
return final_data
7372

7473

@@ -113,20 +112,20 @@ def generate_models_file(
113112
schema_global=schema_global,
114113
defaults_off=defaults_off,
115114
)
116-
header = model_generator.create_header(
117-
data["tables"], schema=schema_global)
115+
header = model_generator.create_header(data["tables"], schema=schema_global)
118116
output = render_jinja2_template(models_type, models_str, header)
119117
return output
120118

121119

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

125125
with open(template_file) as t:
126126
template = t.read()
127127
template = Template(template)
128-
params = {"models": models,
129-
"headers": headers}
128+
params = {"models": models, "headers": headers}
130129
return template.render(**params)
131130

132131

omymodels/dataclass/core.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ def generate_attr(self, column: Dict, defaults_off: bool) -> str:
3737
self.typing_imports.add(_type.split("[")[0])
3838
elif "datetime" in _type:
3939
self.datetime_import = True
40-
self.additional_imports.add('field')
40+
self.additional_imports.add("field")
4141
elif "[" in column.type:
4242
self.typing_imports.add("List")
4343
_type = f"List[{_type}]"
@@ -75,14 +75,12 @@ def generate_model(
7575
**kwargs,
7676
) -> str:
7777
model = ""
78-
78+
7979
# mean one model one table
8080
model += "\n\n"
8181
model += (
8282
dt.dataclass_class.format(
83-
class_name=create_class_name(
84-
table.name, singular, exceptions
85-
),
83+
class_name=create_class_name(table.name, singular, exceptions),
8684
table_name=table.name,
8785
)
8886
) + "\n\n"
@@ -114,8 +112,10 @@ def create_header(self, *args, **kwargs) -> str:
114112
if self.additional_imports:
115113
self.additional_imports = f', {",".join(self.additional_imports)}'
116114
else:
117-
self.additional_imports = ''
118-
header += dt.dataclass_imports.format(additional_imports=self.additional_imports)
115+
self.additional_imports = ""
116+
header += dt.dataclass_imports.format(
117+
additional_imports=self.additional_imports
118+
)
119119
return header
120120

121121
def generate_type(

omymodels/dataclass/templates.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,4 @@ class {class_name}:"""
1616
enum_import = "from enum import {enums}"
1717

1818
uuid_import = "from uuid import UUID"
19-
field_datetime_now = "field(default_factory=datetime.datetime.now)"
19+
field_datetime_now = "field(default_factory=datetime.datetime.now)"

omymodels/gino/core.py

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ def setup_column_attributes(
9292
column = self.add_reference_to_the_column(
9393
column_data.name, column, column_data.references
9494
)
95-
if not column_data.nullable and not column_data.name in table_pk:
95+
if not column_data.nullable and column_data.name not in table_pk:
9696
column += gt.required
9797
if column_data.default is not None:
9898
column = self.prepare_column_default(column_data, column)
@@ -104,13 +104,13 @@ def setup_column_attributes(
104104
if "columns" in table_data.alter:
105105
for alter_column in table_data.alter["columns"]:
106106
if (
107-
alter_column['name'] == column_data.name
107+
alter_column["name"] == column_data.name
108108
and not alter_column["constraint_name"]
109109
and alter_column["references"]
110110
):
111111

112112
column = self.add_reference_to_the_column(
113-
alter_column['name'], column, alter_column["references"]
113+
alter_column["name"], column, alter_column["references"]
114114
)
115115
return column
116116

@@ -222,12 +222,7 @@ def generate_model(
222222
)
223223
for column in table.columns:
224224
model += self.generate_column(column, table.primary_key, table)
225-
if (
226-
table.indexes
227-
or table.alter
228-
or table.checks
229-
or not schema_global
230-
):
225+
if table.indexes or table.alter or table.checks or not schema_global:
231226
model = self.add_table_args(model, table, schema_global)
232227
# create sequence
233228
return model

omymodels/meta_model.py

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,12 @@
33

44

55
class TableProperties(BaseModel):
6+
67
indexes: List
78

89

910
class Column(BaseModel):
10-
11+
1112
name: str
1213
type: str
1314
size: Optional[Union[str, int, tuple]]
@@ -19,29 +20,31 @@ class Column(BaseModel):
1920
generated_as: Optional[str]
2021
other_properties: Optional[dict]
2122
references: Optional[dict]
22-
23-
@validator('size')
23+
24+
@validator("size")
2425
def size_must_contain_space(cls, v):
2526
if isinstance(v, str) and v.isnumeric():
2627
return int(v)
2728
return v
2829

29-
30+
3031
class TableMeta(BaseModel):
31-
name: str = Field(alias='table_name')
32-
table_schema: Optional[str] = Field(alias='schema')
32+
name: str = Field(alias="table_name")
33+
table_schema: Optional[str] = Field(alias="schema")
3334
columns: List[Column]
34-
indexes: Optional[List[dict]] = Field(alias='index')
35+
indexes: Optional[List[dict]] = Field(alias="index")
3536
alter: Optional[dict]
3637
checks: Optional[List[dict]]
3738
properties: Optional[TableProperties]
3839
primary_key: List
39-
40+
4041
class Config:
42+
""" pydantic class config """
43+
4144
arbitrary_types_allowed = True
4245

4346

4447
class Type(BaseModel):
45-
name: str = Field(alias='type_name')
48+
name: str = Field(alias="type_name")
4649
base_type: str
47-
properties: Optional[dict]
50+
properties: Optional[dict]

omymodels/pydantic/core.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,9 +78,7 @@ def generate_model(
7878
model += "\n\n"
7979
model += (
8080
pt.pydantic_class.format(
81-
class_name=create_class_name(
82-
table.name, singular, exceptions
83-
),
81+
class_name=create_class_name(table.name, singular, exceptions),
8482
table_name=table.name,
8583
)
8684
) + "\n\n"

omymodels/sqlalchemy/core.py

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ def setup_column_attributes(
9292
column = self.add_reference_to_the_column(
9393
column_data.name, column, column_data.references
9494
)
95-
if not column_data.nullable and not column_data.name in table_pk:
95+
if not column_data.nullable and column_data.name not in table_pk:
9696
column += st.required
9797
if column_data.default is not None:
9898
column = self.prepare_column_default(column_data, column)
@@ -104,13 +104,13 @@ def setup_column_attributes(
104104
if "columns" in table_data.alter:
105105
for alter_column in table_data.alter["columns"]:
106106
if (
107-
alter_column['name'] == column_data.name
107+
alter_column["name"] == column_data.name
108108
and not alter_column["constraint_name"]
109109
and alter_column["references"]
110110
):
111111

112112
column = self.add_reference_to_the_column(
113-
alter_column['name'], column, alter_column["references"]
113+
alter_column["name"], column, alter_column["references"]
114114
)
115115
return column
116116

@@ -133,7 +133,8 @@ def generate_column(
133133
""" method to generate full column defention """
134134
column_type = self.prepare_column_type(column_data)
135135
column = self.setup_column_attributes(
136-
column_data, table_pk, column_type, table_data)
136+
column_data, table_pk, column_type, table_data
137+
)
137138
column += ")\n"
138139
return column
139140

@@ -214,19 +215,14 @@ def generate_model(
214215
) -> str:
215216
""" method to prepare one Model defention - name & tablename & columns """
216217
model = ""
217-
218+
218219
model = st.model_template.format(
219220
model_name=create_class_name(table.name, singular, exceptions),
220221
table_name=table.name,
221222
)
222223
for column in table.columns:
223224
model += self.generate_column(column, table.primary_key, table)
224-
if (
225-
table.indexes
226-
or table.alter
227-
or table.checks
228-
or not schema_global
229-
):
225+
if table.indexes or table.alter or table.checks or not schema_global:
230226
model = self.add_table_args(model, table, schema_global)
231227
return model
232228

0 commit comments

Comments
 (0)