Skip to content

Commit

Permalink
Merge pull request #75 from cicekhayri/jinja-templates
Browse files Browse the repository at this point in the history
Refactor template rendering using Jinja2.
  • Loading branch information
cicekhayri authored Jan 22, 2024
2 parents 2e3834c + e029e8c commit 438d89e
Show file tree
Hide file tree
Showing 9 changed files with 44 additions and 29 deletions.
21 changes: 13 additions & 8 deletions inspira/cli/create_controller.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import os

import click
from jinja2 import Template

from inspira.cli.create_app import generate_project
from inspira.cli.init_file import create_init_file
Expand All @@ -22,7 +23,7 @@ def create_controller_file(name, is_websocket):
controller_directory = os.path.join(SRC_DIRECTORY, "controller")
singularize_name = singularize(name.lower())
controller_file_name = f"{singularize_name}_controller.py"
controller_template_file = "controller_template.txt"
controller_template_file = "controller_template.jinja2"

controller_file_path = os.path.join(controller_directory, controller_file_name)

Expand All @@ -31,7 +32,7 @@ def create_controller_file(name, is_websocket):
return

if is_websocket:
controller_template_file = "websocket_controller_template.txt"
controller_template_file = "websocket_controller_template.jinja2"

template_path = os.path.join(
os.path.dirname(__file__), "templates", controller_template_file
Expand All @@ -40,11 +41,15 @@ def create_controller_file(name, is_websocket):
with open(template_path, "r") as template_file, open(
controller_file_path, "w"
) as output_file:
content = (
template_file.read()
.replace("{{controller_name}}", singularize_name.capitalize())
.replace("{{root_path}}", pluralize_word(name.lower()))
)
template_content = template_file.read()
template = Template(template_content)

context = {
"controller_name": singularize_name.capitalize(),
"root_path": pluralize_word(name.lower()),
}

content = template.render(context)
output_file.write(content)

click.echo(f"Controller '{singularize_name}' created successfully.")
click.echo(f"Controller '{singularize_name}' created successfully.")
18 changes: 10 additions & 8 deletions inspira/cli/generate_model_file.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import os

import click
from jinja2 import Template

from inspira.constants import SRC_DIRECTORY
from inspira.utils import pluralize_word, singularize
Expand All @@ -10,7 +11,7 @@ def generate_model_file(module_name):
model_directory = os.path.join(SRC_DIRECTORY, "model")
model_file_name = f"{singularize(module_name.lower())}.py"
template_path = os.path.join(
os.path.dirname(__file__), "templates", "model_template.txt"
os.path.dirname(__file__), "templates", "model_template.jinja2"
)
model_file_path = os.path.join(model_directory, model_file_name)

Expand All @@ -21,13 +22,14 @@ def generate_model_file(module_name):
with open(template_path, "r") as template_file, open(
model_file_path, "w"
) as output_file:
content = (
template_file.read()
.replace(
"{{module_name_capitalize}}", singularize(module_name.capitalize())
)
.replace("{{module_name_plural}}", pluralize_word(module_name))
)
template_content = template_file.read()
template = Template(template_content)

context = {
"module_name_capitalize": singularize(module_name.capitalize()),
"module_name_plural": pluralize_word(module_name),
}
content = template.render(context)
output_file.write(content)

click.echo(f"Model '{model_file_name}' created successfully.")
Expand Down
14 changes: 10 additions & 4 deletions inspira/cli/generate_repository_file.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import os

import click
from jinja2 import Template

from inspira.constants import SRC_DIRECTORY
from inspira.utils import singularize
Expand All @@ -11,7 +12,7 @@ def generate_repository_file(module_name):
repository_file_name = f"{singularize(module_name.lower())}_repository.py"

template_path = os.path.join(
os.path.dirname(__file__), "templates", "repository_template.txt"
os.path.dirname(__file__), "templates", "repository_template.jinja2"
)
repository_file_path = os.path.join(model_directory, repository_file_name)

Expand All @@ -22,9 +23,14 @@ def generate_repository_file(module_name):
with open(template_path, "r") as template_file, open(
repository_file_path, "w"
) as output_file:
content = template_file.read().replace(
"{{model_name_upper}}", singularize(module_name.capitalize())
)
template_content = template_file.read()
template = Template(template_content)

context = {
"model_name_upper": singularize(module_name.capitalize())
}

content = template.render(context)
output_file.write(content)

click.echo(f"Repository '{repository_file_name}' created successfully.")
20 changes: 11 additions & 9 deletions inspira/cli/generate_service_file.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
import os

import click
from jinja2 import Template

from inspira.constants import SRC_DIRECTORY
from inspira.utils import pluralize_word, singularize
from inspira.utils import singularize


def generate_service_file(module_name):
model_directory = os.path.join(SRC_DIRECTORY, "service")
service_file_name = f"{singularize(module_name.lower())}_service.py"

template_path = os.path.join(
os.path.dirname(__file__), "templates", "service_template.txt"
os.path.dirname(__file__), "templates", "service_template.jinja2"
)
service_file_path = os.path.join(model_directory, service_file_name)

Expand All @@ -22,13 +23,14 @@ def generate_service_file(module_name):
with open(template_path, "r") as template_file, open(
service_file_path, "w"
) as output_file:
content = (
template_file.read()
.replace("{{model_name}}", pluralize_word(module_name))
.replace("{{model_name_upper}}", singularize(module_name.capitalize()))
.replace("{{module_name}}", module_name)
.replace("{{model_name_singular}}", singularize(module_name.lower()))
)
template_content = template_file.read()
template = Template(template_content)

context = {
"model_name_upper": singularize(module_name.capitalize()),
}

content = template.render(context)
output_file.write(content)

click.echo(f"Service '{service_file_name}' created successfully.")
File renamed without changes.
File renamed without changes.

0 comments on commit 438d89e

Please sign in to comment.