Skip to content

Commit

Permalink
♻️ Move saving files out of adapters.
Browse files Browse the repository at this point in the history
  • Loading branch information
rafalkrupinski committed Jun 25, 2024
1 parent 7a1500f commit 106300f
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 17 deletions.
3 changes: 1 addition & 2 deletions src/rybak/adapter.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import abc
from pathlib import Path

from ._types import LoopOverFn, TemplateData
from .pycompat import Traversable
Expand All @@ -11,7 +10,7 @@ def render_str(self, template: str, data: TemplateData, loop_over: LoopOverFn) -
pass

@abc.abstractmethod
def render_file(self, template_path: str, target_file: Path, data: TemplateData) -> None:
def render_file(self, template_path: str, data: TemplateData) -> str:
pass

@property
Expand Down
7 changes: 3 additions & 4 deletions src/rybak/jinja.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,12 @@ def render_str(self, template: str, data: TemplateData, loop_over: Optional[Loop
except (jinja2.TemplateError, ValueError) as e:
raise RenderError(template) from e

def render_file(self, template_path: str, target_file: Path, data: TemplateData) -> None:
def render_file(self, template_path: str, data: TemplateData) -> str:
try:
template_obj = self._env.get_template(template_path)
text = template_obj.render(**data)
return template_obj.render(**data)
except (jinja2.TemplateError, ValueError) as e:
raise RenderError(template_path, target_file) from e
target_file.write_text(text)
raise RenderError(template_path) from e

@property
def template_root(self) -> Traversable:
Expand Down
7 changes: 3 additions & 4 deletions src/rybak/mako.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,12 @@ def render_str(self, template: str, data: TemplateData, loop_over: Optional[Loop
except (AttributeError, mako.exceptions.MakoException, ValueError, TypeError) as e:
raise RenderError(template) from e

def render_file(self, template_path: str, target_file: Path, data: TemplateData) -> None:
def render_file(self, template_path: str, data: TemplateData) -> str:
try:
template = self._loader.get_template(template_path)
text = template.render(**data)
return template.render(**data)
except (AttributeError, mako.exceptions.MakoException, ValueError) as e:
raise RenderError(template_path, target_file) from e
target_file.write_text(text)
raise RenderError(template_path) from e

@property
def template_root(self) -> Traversable:
Expand Down
7 changes: 2 additions & 5 deletions src/rybak/tree_renderer.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,11 +175,8 @@ def _render_file_name(self, template: str, data: TemplateData, loop_over: LoopOv
def _render_file(self, template_path: PurePath, target_path: Path, data: TemplateData) -> None:
target_path.parent.mkdir(parents=True, exist_ok=True)

self._adapter.render_file(
template_path.as_posix(),
target_path,
data,
)
text = self._adapter.render_file(template_path.as_posix(), data)
target_path.write_text(text)

@property
def template_root(self) -> PurePath:
Expand Down
14 changes: 12 additions & 2 deletions tests/test_render.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ def test_remove_stale(tmp_path: pathlib.Path):
remove_stale=True,
)

assert dir_content(root / 'output' / 'simple') == dir_content(tmp_path)
assert dir_content(tmp_path) == dir_content(root / 'output' / 'simple')


def test_no_remove_stale(tmp_path: pathlib.Path):
Expand All @@ -95,4 +95,14 @@ def test_no_remove_stale(tmp_path: pathlib.Path):
remove_stale=False,
)

assert dir_content(root / 'output' / 'simple') != dir_content(tmp_path)
assert set(dir_content(tmp_path)) == {
('file1.txt', 'foo'),
('stale_dir', None),
('stale_dir/stale_file.txt', 'another test file'),
('stale_file.txt', 'test file'),
('subdir', None),
('subdir/file3.txt', 'baz'),
('target_dir', None),
('target_dir/file2.txt', 'bar'),
('target_dir/suffixed.txt', 'test suffix removal\n'),
}

0 comments on commit 106300f

Please sign in to comment.