From fc1de1ba8aed7797b1a28ecd1289a24497468230 Mon Sep 17 00:00:00 2001 From: Kate Case Date: Mon, 23 Sep 2024 10:07:59 -0400 Subject: [PATCH] Carry (potentially modified) template_data with src and dest --- src/ansible_creator/subcommands/init.py | 1 - src/ansible_creator/utils.py | 18 ++++++++++-------- tests/units/test_utils.py | 1 - 3 files changed, 10 insertions(+), 10 deletions(-) diff --git a/src/ansible_creator/subcommands/init.py b/src/ansible_creator/subcommands/init.py index c1e0d781..a952d172 100644 --- a/src/ansible_creator/subcommands/init.py +++ b/src/ansible_creator/subcommands/init.py @@ -137,7 +137,6 @@ def _scaffold(self) -> None: copier = Copier( output=self.output, templar=self._templar, - template_data=template_data, ) copier.copy_containers(paths) diff --git a/src/ansible_creator/utils.py b/src/ansible_creator/utils.py index 8d882ef6..145a2330 100644 --- a/src/ansible_creator/utils.py +++ b/src/ansible_creator/utils.py @@ -70,10 +70,12 @@ class DestinationFile: Attributes: source: The path of the original copy. dest: The path the file will be written to. + template_data: A dictionary containing the customized data to render templates with. """ source: Traversable dest: Path + template_data: TemplateData def __str__(self) -> str: """Supports str() on DestinationFile. @@ -191,7 +193,11 @@ def each_obj( dest_name = dest_name.replace(key, repl_val) dest_name = dest_name.removesuffix(".j2") - dest_path = DestinationFile(dest=self.dest / dest_name, source=obj) + dest_path = DestinationFile( + dest=self.dest / dest_name, + source=obj, + template_data=template_data, + ) self.output.debug(f"Working on {dest_path}") if dest_path.conflicts: @@ -282,24 +288,20 @@ class Copier: Attributes: output: An instance of the Output class. - template_data: A dictionary containing the original data to render templates with. templar: An instance of the Templar class. """ output: Output - template_data: TemplateData templar: Templar | None = None def _copy_file( self, dest_path: DestinationFile, - template_data: TemplateData, ) -> None: """Copy a file to destination. Args: dest_path: The destination path to copy the file to. - template_data: A dictionary containing current data to render templates with. """ # remove .j2 suffix at destination self.output.debug(msg=f"dest file is {dest_path}") @@ -307,10 +309,10 @@ def _copy_file( content = dest_path.source.read_text(encoding="utf-8") # only render as templates if both of these are provided, # and original file suffix was j2 - if self.templar and template_data and dest_path.needs_templating: + if self.templar and dest_path.template_data and dest_path.needs_templating: content = self.templar.render_from_content( template=content, - data=template_data, + data=dest_path.template_data, ) with dest_path.dest.open("w", encoding="utf-8") as df_handle: df_handle.write(content) @@ -329,4 +331,4 @@ def copy_containers(self: Copier, paths: list[DestinationFile]) -> None: path.dest.mkdir(parents=True, exist_ok=True) elif path.source.is_file(): - self._copy_file(path, self.template_data) + self._copy_file(path) diff --git a/tests/units/test_utils.py b/tests/units/test_utils.py index 486f00f0..2f5b8959 100644 --- a/tests/units/test_utils.py +++ b/tests/units/test_utils.py @@ -47,7 +47,6 @@ def test_skip_dirs(tmp_path: Path, monkeypatch: pytest.MonkeyPatch, output: Outp copier = Copier( output=output, - template_data=TemplateData(), ) copier.copy_containers(paths) assert (tmp_path / ".devcontainer" / "podman").exists()