Skip to content

Commit

Permalink
fix: update generate hook and add convert_template_filenames field
Browse files Browse the repository at this point in the history
robcxyz committed Nov 5, 2024

Verified

This commit was signed with the committer’s verified signature.
aversey Aleksey Veresov
1 parent 5e790d8 commit 9cf9e2d
Showing 1 changed file with 35 additions and 27 deletions.
62 changes: 35 additions & 27 deletions providers/generate/hooks/generate.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

import fnmatch
import os.path
import shutil
@@ -48,7 +50,7 @@ class GenerateHook(BaseHook):
None,
description="List of files to skip generating over if they exist."
)
render_context: dict = Field(
render_context: dict | None = Field(
None,
description="A render context that invalidates the default context."
)
@@ -65,6 +67,10 @@ class GenerateHook(BaseHook):
"from. [Docs](https://jinja.palletsprojects.com/en/3.0.x/api/#jinja2.FileSystemLoader)."
# noqa
)
convert_template_filenames: bool = Field(
True,
description="Convert filenames like foo.py.j2 to foo.py when rendering."
)

base_dir_: Path = None
file_path_separator_: str = None # / for mac / linux - \ for win
@@ -89,36 +95,34 @@ def _init_paths(self, context: Context):
if not self.output.startswith('/'):
self.output = os.path.join(context.path.calling.directory, self.output)

# def _init_context(self, context: Context):
# # Update the render_context that will be used
# if self.render_context is not None:
# return
#
# # fmt: off
# existing_context = context.data.existing if context.data.temporary is not None else {}
# temporary_context = context.data.temporary if context.data.temporary is not None else {}
# private_context = context.data.private if context.data.private is not None else {}
# public_context = context.data.public if context.data.public is not None else {}
# # fmt: on
#
# self.render_context = {
# **existing_context,
# **temporary_context,
# **private_context,
# **public_context,
# }
#
# if self.extra_context is not None:
# if isinstance(self.extra_context, list):
# for i in self.extra_context:
# self.render_context.update(i)
# else:
# self.render_context.update(self.extra_context)
def _init_context(self, context: Context):
# Update the render_context that will be used
if self.render_context is None:
# fmt: off
existing_context = context.data.existing if context.data.temporary is not None else {}
temporary_context = context.data.temporary if context.data.temporary is not None else {}
private_context = context.data.private if context.data.private is not None else {}
public_context = context.data.public if context.data.public is not None else {}
# fmt: on

self.render_context = {
**existing_context,
**temporary_context,
**private_context,
**public_context,
}

if self.extra_context is not None:
if isinstance(self.extra_context, list):
for i in self.extra_context:
self.render_context.update(i)
else:
self.render_context.update(self.extra_context)

def exec(self, context: Context):
"""Generate files / directories."""
self._init_paths(context=context)
# self._init_context(context=context)
self._init_context(context=context)
init_context(self=self, context=context)

# https://stackoverflow.com/questions/42368678/jinja-environment-is-not-supporting-absolute-paths
@@ -231,6 +235,10 @@ def generate_file(self, context: Context, input_file: str, output_path: str):
msg = f"The `generate` hook failed to render -> {e}"
raise UndefinedVariableInTemplate(msg, context=context) from None

if self.convert_template_filenames:
if output_path.endswith('.j2'):
output_path = output_path[:-3]

# Write contents
with open(output_path, 'w') as f:
# Will write an empty file if the contents are None otherwise write contents

0 comments on commit 9cf9e2d

Please sign in to comment.