Skip to content

Commit

Permalink
Merge pull request #7 from angrybayblade/feat/dry-run
Browse files Browse the repository at this point in the history
Add support for dry runs
  • Loading branch information
angrybayblade authored Mar 11, 2024
2 parents 0bf000a + d744dce commit 99d62db
Showing 1 changed file with 23 additions and 4 deletions.
27 changes: 23 additions & 4 deletions ph7/django/engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from importlib.machinery import SourceFileLoader
from pathlib import Path

from typing_extensions import TypedDict
from typing_extensions import NotRequired, TypedDict

from ph7.base import node

Expand Down Expand Up @@ -45,14 +45,26 @@ class NotFound(Exception):
"""Template not found."""


class _MockContext(dict):
def __init__(self, name: str) -> None:
"""Mock context."""
self.name = name

def __getitem__(self, name: str) -> "_MockContext":
"""Get mock item."""
return _MockContext(f"{self.name}->{name=}")


class EngineOptions(TypedDict):
"""Engine options."""

mock_context: NotRequired[t.Any]


class Environment:
"""Templates environment."""

def __init__(self) -> None:
def __init__(self, mock_context: t.Any = None) -> None:
"""Initialize object."""
if not DJANGO_INSTALLED:
raise RuntimeError(
Expand Down Expand Up @@ -83,6 +95,12 @@ def __init__(self) -> None:
).load_module()
self.templates[".".join(path)] = module

# dry run
for name in dir(module):
obj = getattr(module, name)
if isinstance(obj, node):
obj.render(context=mock_context or _MockContext("mock"))

def get(self, name: str, cls: t.Optional[str] = None) -> "Template":
"""Get PH7 node for `name`"""
cls = cls or "template"
Expand All @@ -105,8 +123,9 @@ class PH7Templates(BaseEngine):
def __init__(self, params: t.Dict[str, t.Any]) -> None:
"""Initialize object."""
self.options = EngineOptions(params.pop("OPTIONS")) # type: ignore
self.environment = Environment()

self.environment = Environment(
mock_context=self.options.get("mock_context"),
)
super().__init__(params)

@property
Expand Down

0 comments on commit 99d62db

Please sign in to comment.