Skip to content

Commit

Permalink
chore: minor improvements (#103)
Browse files Browse the repository at this point in the history
Minor fixes.
  • Loading branch information
jgadling authored Sep 20, 2024
1 parent 24d16f8 commit 5f51958
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 2 deletions.
15 changes: 14 additions & 1 deletion platformics/codegen/generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@

import logging
import os
import re

import jinja2.ext
from jinja2 import Environment, FileSystemLoader
from linkml_runtime.utils.schemaview import SchemaView

Expand Down Expand Up @@ -122,6 +124,16 @@ def generate_entity_import_files(
print(f"... wrote {filename}")


def regex_replace(txt, rgx, val, ignorecase=False, multiline=False):
flag = 0
if ignorecase:
flag |= re.I
if multiline:
flag |= re.M
compiled_rgx = re.compile(rgx, flag)
return compiled_rgx.sub(val, txt)


def generate(schemafile: str, output_prefix: str, render_files: bool, template_override_paths: tuple[str]) -> None:
"""
Launch code generation
Expand All @@ -130,7 +142,8 @@ def generate(schemafile: str, output_prefix: str, render_files: bool, template_o
template_paths.append(
os.path.join(os.path.abspath(os.path.dirname(__file__)), "templates/"),
) # default template path
environment = Environment(loader=FileSystemLoader(template_paths))
environment = Environment(loader=FileSystemLoader(template_paths), extensions=[jinja2.ext.loopcontrols])
environment.filters["regex_replace"] = regex_replace
view = SchemaView(schemafile)
view.imports_closure()
wrapped_view = ViewWrapper(view)
Expand Down
2 changes: 2 additions & 0 deletions platformics/codegen/templates/database/migrations/env.py.j2
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ def run_migrations_offline() -> None:
)

with context.begin_transaction():
context.get_context()._ensure_version_table() # pylint: disable=protected-access
connection.execute(sa.sql.text("LOCK TABLE alembic_version IN ACCESS EXCLUSIVE MODE"))
context.run_migrations()


Expand Down
3 changes: 2 additions & 1 deletion platformics/codegen/templates/support/enums.py.j2
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import enum
@strawberry.enum
class {{enum.name}}(enum.Enum):
{%- for value in enum.permissible_values %}
{{value}} = "{{value}}"
{#- SQLAlchemy freaks out about spaces in enum values :'( #}
{{value | regex_replace('[^0-9A-Za-z]', '_') }} = "{{value | regex_replace('[^0-9A-Za-z]', '_') }}"
{%- endfor %}
{%- endfor %}

0 comments on commit 5f51958

Please sign in to comment.