Skip to content

Commit

Permalink
♻️ Add exclude_extend to parameters.
Browse files Browse the repository at this point in the history
  • Loading branch information
Raphael Krupinski committed Feb 9, 2024
1 parent 0ce0b57 commit ed14872
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 10 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@

- Wrap render errors in `RenderError`.
- Renamed engine adapters from ${engine}Renderer to ${engine}Adapter.
- Rename parameter `excluded` to `exclude` and set default to `__pycache__`.

### Added

- Add `remove_suffixes` optional parameter to `render()`.
- Add `remove_suffixes` and `exclude_extend` optional parameters to `render()`.

### Fixed

Expand Down
11 changes: 7 additions & 4 deletions src/rybak/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
'render',
]

from itertools import chain
from pathlib import Path, PurePath
from typing import Iterable, Union

Expand All @@ -17,23 +18,25 @@ def render(
data: TemplateData,
target_root: Path,
*,
excluded: Union[Iterable[Path], Iterable[str]] = (),
exclude: Union[Iterable[Path], Iterable[str]] = ('__pycache__',),
exclude_extend: Union[Iterable[Path], Iterable[str]] = (),
remove_suffixes: Iterable[str] = (),
) -> None:
"""Render a directory-tree from a template and a data dictionary
:param target_root: render target root directory (filesystem)
:param adapter: template engine adapter (jinja, mako)
:param data: template data
:param excluded: paths within the template root directory, which are not templates
:param exclude: paths within the template root directory, which are not templates. Defaults to '__pycache__'
:param exclude_extend: paths to be added to the default exclude list.
:param remove_suffixes: filename suffixes to be removed when rendering file names, in `.suffix` format
"""
exclude_paths = {Path(path) for path in excluded}
exclude_paths = {Path(path) for path in chain(exclude, exclude_extend)}
TreeRenderer(
RenderContext(
adapter=adapter,
target_root=target_root,
excluded=exclude_paths,
exclude=exclude_paths,
remove_suffixes=remove_suffixes,
),
PurePath(),
Expand Down
4 changes: 2 additions & 2 deletions src/rybak/tree_renderer.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def loop_over(items: Iterable) -> NoReturn:
class RenderContext:
target_root: Path
adapter: RendererAdapter
excluded: Iterable[PurePath]
exclude: Iterable[PurePath]
remove_suffixes: Iterable[str]


Expand All @@ -52,7 +52,7 @@ def _render(self, file_name: str, data: TemplateData) -> None:
"""Dispatcher method that calls another render method depending on whether the path is a directory or a file"""

rel_path = self._template_path / file_name
if rel_path in self._context.excluded:
if rel_path in self._context.exclude:
logger.debug('Excluded %s', rel_path)
return

Expand Down
6 changes: 3 additions & 3 deletions tests/test_render.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,14 +86,14 @@ class TestData(NamedTuple):
]


@pytest.mark.parametrize('adapter_name,adapter,test_name,data,error,excluded', adapter_test_data)
@pytest.mark.parametrize('adapter_name,adapter,test_name,data,error,exclude', adapter_test_data)
def test_render(
adapter_name: str,
adapter: Callable[[Path], RendererAdapter],
test_name: str,
data: Mapping,
error: bool,
excluded: Iterable,
exclude: Iterable[str],
tmp_path: Path,
) -> None:
if adapter_name == 'mako' and sys.platform == 'win32':
Expand All @@ -108,7 +108,7 @@ def fn():
adapter(root / 'templates' / adapter_name / test_name),
data,
target_path,
excluded=[Path(item) for item in excluded] + [Path('__pycache__')],
exclude_extend=exclude,
remove_suffixes=['.jinja', '.mako'],
)

Expand Down

0 comments on commit ed14872

Please sign in to comment.