Skip to content

Commit

Permalink
Carry (potentially modified) template_data with src and dest
Browse files Browse the repository at this point in the history
  • Loading branch information
Qalthos committed Sep 23, 2024
1 parent 6f1fc66 commit fc1de1b
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 10 deletions.
1 change: 0 additions & 1 deletion src/ansible_creator/subcommands/init.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,6 @@ def _scaffold(self) -> None:
copier = Copier(
output=self.output,
templar=self._templar,
template_data=template_data,
)
copier.copy_containers(paths)

Expand Down
18 changes: 10 additions & 8 deletions src/ansible_creator/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -282,35 +288,31 @@ 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}")

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)
Expand All @@ -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)
1 change: 0 additions & 1 deletion tests/units/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit fc1de1b

Please sign in to comment.