generated from Hochfrequenz/python_template_repository
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(generate): ✨Implement code generation for Python (#27)
Part of #1. The part with the `sqlmodel`-parser was done in co-development with @DeltaDaniel. --------- Co-authored-by: kevin <[email protected]>
- Loading branch information
1 parent
b4dbca3
commit cf659aa
Showing
35 changed files
with
1,712 additions
and
741 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 61 additions & 0 deletions
61
src/bo4e_cli/generate/python/custom_templates/BaseModel.jinja2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
{%- if SQL %} | ||
|
||
{%- for import in SQL.imports %} | ||
{%- if import.alias is not none %} | ||
from {{ import.from_ }} import {{ import.import_ }} as {{ import.alias }} | ||
{%- else %} | ||
from {{ import.from_ }} import {{ import.import_ }} | ||
{%- endif %} | ||
{%- endfor -%} | ||
|
||
{%- endif %} | ||
|
||
{% for decorator in decorators -%} | ||
{{ decorator }} | ||
{% endfor -%} | ||
class {{ class_name }}({% if base_class %}{{ base_class }}{% endif %}{% if SQL %}, table=True{% endif %}):{% if comment is defined %} # {{ comment }}{% endif %} | ||
{%- if description %} | ||
""" | ||
{{ description | indent(4) }} | ||
""" | ||
{%- endif %} | ||
{%- if not fields and not description %} | ||
pass | ||
{%- endif %} | ||
{%- if config %} | ||
{%- filter indent(4) %} | ||
{% include 'Config.jinja2' %} | ||
{%- endfilter %} | ||
{%- endif %} | ||
{%- for field in fields -%} | ||
{%- if not field.annotated and field.field %} | ||
{{ field.name }}: {{ field.type_hint }} = {{ field.field }} | ||
{%- else %} | ||
{%- if field.annotated %} | ||
{{ field.name }}: {{ field.annotated }} | ||
{%- else %} | ||
{{ field.name }}: {{ field.type_hint }} | ||
{%- endif %} | ||
{%- if not (field.required or (field.represented_default == 'None' and field.strip_default_none)) | ||
%} = {{ field.represented_default }} | ||
{%- endif -%} | ||
{%- endif %} | ||
{%- if field.docstring %} | ||
""" | ||
{{ field.docstring | indent(4) }} | ||
""" | ||
{%- endif %} | ||
{%- for method in methods -%} | ||
{{ method }} | ||
{%- endfor -%} | ||
{%- endfor -%} | ||
{%- if SQL and SQL.fields %} | ||
{%- for field_name, field in SQL.fields.items() %} | ||
{{ field_name }}: {{ field.annotation }} = {{ field.definition }} | ||
{%- if field.description %} | ||
""" | ||
{{ field.description | indent(4) }} | ||
""" | ||
{%- endif %} | ||
{%- endfor -%} | ||
{%- endif %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
model_config = SQLModelConfig( | ||
arbitrary_types_allowed=True, | ||
{%- for field_name, value in config.dict(exclude_unset=True).items() %} | ||
{{ field_name }}={{ value }}, | ||
{%- endfor %} | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
{% for decorator in decorators -%} | ||
{{ decorator }} | ||
{% endfor -%} | ||
class {{ class_name }}({{ base_class }}): | ||
{%- if description %} | ||
""" | ||
{{ description | indent(4) }} | ||
""" | ||
{%- endif %} | ||
{%- for field in fields %} | ||
{{ field.name[:63] }} = {{ field.default }} | ||
{%- if field.docstring %} | ||
""" | ||
{{ field.docstring | indent(4) }} | ||
""" | ||
{%- endif %} | ||
{%- endfor -%} |
19 changes: 19 additions & 0 deletions
19
src/bo4e_cli/generate/python/custom_templates/ManyLinks.jinja2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
""" | ||
File containing all linking classes for many-many relations in the BO4E version | ||
""" | ||
import uuid as uuid_pkg | ||
|
||
from sqlalchemy import Column, ForeignKey | ||
from sqlmodel import Field, SQLModel | ||
|
||
{%- for link in links %} | ||
class {{ link.table_name }}(SQLModel, table=True): | ||
""" | ||
class linking m-n relation of tables {{ link.cls1 }} and {{ link.cls2 }} for field {{ link.rel_field_name1 }}. | ||
""" | ||
{{ link.id_field_name1 }}: uuid_pkg.UUID = Field(..., primary_key=True, foreign_key="{{ link.cls1.lower() }}.id", ondelete="CASCADE") | ||
"""Id linking to {{ link.cls1 }}.""" | ||
{{ link.id_field_name2 }}: uuid_pkg.UUID = Field(..., primary_key=True, foreign_key="{{ link.cls2.lower() }}.id", ondelete="CASCADE") | ||
"""Id linking to {{ link.cls2 }}.""" | ||
|
||
{%- endfor -%} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.