From d744dceab36b29e06c0ff4926d56c38e5e595497 Mon Sep 17 00:00:00 2001 From: angrybayblade Date: Mon, 11 Mar 2024 21:44:26 +0530 Subject: [PATCH] feat: add support for dry runs --- ph7/django/engine.py | 27 +++++++++++++++++++++++---- 1 file changed, 23 insertions(+), 4 deletions(-) diff --git a/ph7/django/engine.py b/ph7/django/engine.py index a690908..716570c 100644 --- a/ph7/django/engine.py +++ b/ph7/django/engine.py @@ -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 @@ -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( @@ -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" @@ -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