Skip to content

Commit

Permalink
solve issue with kwargs in models
Browse files Browse the repository at this point in the history
  • Loading branch information
xnuinside committed Oct 11, 2022
1 parent 5be8f8c commit 1b445e3
Show file tree
Hide file tree
Showing 10 changed files with 445 additions and 269 deletions.
11 changes: 11 additions & 0 deletions CHANGELOG.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
**v0.12.0**
### Fixes

1. Now named arguments always went after positional. Fix for https://github.com/xnuinside/omymodels/issues/35

### New feature:
1. Availability to disable auto-name convertion - https://github.com/xnuinside/omymodels/issues/36.
Now, if you want to keep names 1-to-1 as in your DDL file, you can set argument `no_auto_snake_case=True` and O!MyModels will do nothing with the table or column names.



**v0.11.1**

### Improvements:
Expand Down
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,17 @@ If you see any bugs or have any suggestions - feel free to open the issue. Any h


## Changelog
**v0.12.0**
### Fixes

1. Now named arguments always went after positional. Fix for https://github.com/xnuinside/omymodels/issues/35

### New feature:
1. Availability to disable auto-name convertion - https://github.com/xnuinside/omymodels/issues/36.
Now, if you want to keep names 1-to-1 as in your DDL file, you can set argument `no_auto_snake_case=True` and O!MyModels will do nothing with the table or column names.



**v0.11.1**

### Improvements:
Expand Down
15 changes: 15 additions & 0 deletions docs/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,21 @@ If you see any bugs or have any suggestions - feel free to open the issue. Any h
Changelog
---------

**v0.12.0**

Fixes
^^^^^


#. Now named arguments always went after positional. Fix for https://github.com/xnuinside/omymodels/issues/35

New feature:
^^^^^^^^^^^^


#. Availability to disable auto-name convertion - https://github.com/xnuinside/omymodels/issues/36.
Now, if you want to keep names 1-to-1 as in your DDL file, you can set argument ``no_auto_snake_case=True`` and O!MyModels will do nothing with the table or column names.

**v0.11.1**

Improvements:
Expand Down
10 changes: 7 additions & 3 deletions omymodels/from_ddl.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,13 @@ def create_models(
schema_global: Optional[bool] = True,
defaults_off: Optional[bool] = False,
exit_silent: Optional[bool] = False,
no_auto_snake_case: Optional[bool] = False
):
""" models_type can be: "gino", "dataclass", "pydantic" """
# extract data from ddl file
data = get_tables_information(ddl, ddl_path)
data = prepare_data(data)
data = convert_ddl_to_models(data)
data = convert_ddl_to_models(data, no_auto_snake_case)
if not data["tables"] and not data["types"]:
if exit_silent:
sys.exit(0)
Expand All @@ -61,15 +62,18 @@ def create_models(


def snake_case(string: str) -> str:
if string.lower() in ['id']:
return string.lower()
return re.sub(r"(?<!^)(?=[A-Z])", "_", string).lower()


def convert_ddl_to_models(data):
def convert_ddl_to_models(data: Dict, no_auto_snake_case: bool) -> Dict[str, list]:
final_data = {"tables": [], "types": []}
tables = []
for table in data["tables"]:
for column in table["columns"]:
column["name"] = snake_case(column["name"])
if not no_auto_snake_case:
column["name"] = snake_case(column["name"])
tables.append(TableMeta(**table))
final_data["tables"] = tables
_types = []
Expand Down
24 changes: 13 additions & 11 deletions omymodels/logic.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,19 @@ def setup_column_attributes(
templates,
obj,
) -> str:
# foreign is a positional - so it should be before keyword args
if "columns" in table_data.alter:
for alter_column in table_data.alter["columns"]:
if (
alter_column["name"] == column_data.name
and not alter_column["constraint_name"]
and alter_column["references"]
):

column = add_reference_to_the_column(
alter_column["name"], column, alter_column["references"], templates
)
# keyword named args
if column_data.type.lower() == "serial" or column_data.type.lower() == "bigserial":
column += templates.autoincrement
if column_data.references:
Expand All @@ -41,17 +53,7 @@ def setup_column_attributes(
if column_data.unique:
column += templates.unique

if "columns" in table_data.alter:
for alter_column in table_data.alter["columns"]:
if (
alter_column["name"] == column_data.name
and not alter_column["constraint_name"]
and alter_column["references"]
):

column = add_reference_to_the_column(
alter_column["name"], column, alter_column["references"], templates
)

return column


Expand Down
3 changes: 3 additions & 0 deletions omymodels/models/dataclass/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,13 +80,16 @@ def generate_model(

# mean one model one table
model += "\n\n"
# generate class name
model += (
dt.dataclass_class.format(
class_name=create_class_name(table.name, singular, exceptions),
table_name=table.name,
)
) + "\n\n"
columns = {"default": [], "non_default": []}

# generate columns / attrs
for column in table.columns:
column = t.prepare_column_data(column)
column_str = self.generate_attr(column, defaults_off) + "\n"
Expand Down
Loading

0 comments on commit 1b445e3

Please sign in to comment.