diff --git a/providers/generate/hooks/generate.py b/providers/generate/hooks/generate.py index b1fc52a0..e17c13c4 100644 --- a/providers/generate/hooks/generate.py +++ b/providers/generate/hooks/generate.py @@ -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