Coverage for fastblocks / adapters / templates / _registration.py: 100%
49 statements
« prev ^ index » next coverage.py v7.13.1, created at 2026-01-02 20:00 -0800
« prev ^ index » next coverage.py v7.13.1, created at 2026-01-02 20:00 -0800
1"""Template filter registration system for FastBlocks adapters."""
3from contextlib import suppress
4from typing import Any
6from oneiric.core.resolution import Resolver
8from ._filters import FASTBLOCKS_FILTERS
10# Migration from ACB to Oneiric
11depends = Resolver()
14def register_fastblocks_filters(template_env: Any) -> None:
15 """Register all FastBlocks adapter filters with a Jinja2 environment.
17 Args:
18 template_env: Jinja2 Environment instance
19 """
20 for filter_name, filter_func in FASTBLOCKS_FILTERS.items():
21 template_env.filters[filter_name] = filter_func
24def register_async_fastblocks_filters(template_env: Any) -> None:
25 """Register async versions of FastBlocks adapter filters.
27 Args:
28 template_env: Jinja2 AsyncEnvironment instance
29 """
30 from ._async_filters import FASTBLOCKS_ASYNC_FILTERS
32 # Register both sync and async filters
33 register_fastblocks_filters(template_env)
35 # Add async-specific filters
36 for filter_name, filter_func in FASTBLOCKS_ASYNC_FILTERS.items():
37 template_env.filters[filter_name] = filter_func
40def get_global_template_context() -> dict[str, Any]:
41 """Get global template context with adapter instances.
43 Returns:
44 Dict of global template variables
45 """
46 context = {}
48 # Add adapter instances to global context
49 with suppress(Exception):
50 images = depends.get_sync("images")
51 if images:
52 context["images_adapter"] = images
54 with suppress(Exception):
55 styles = depends.get_sync("styles")
56 if styles:
57 context["styles_adapter"] = styles
59 with suppress(Exception):
60 icons = depends.get_sync("icons")
61 if icons:
62 context["icons_adapter"] = icons
64 with suppress(Exception):
65 fonts = depends.get_sync("fonts")
66 if fonts:
67 context["fonts_adapter"] = fonts
69 return context
72def register_template_globals(template_env: Any) -> None:
73 """Register global template variables and functions.
75 Args:
76 template_env: Jinja2 Environment instance
77 """
78 globals_dict = get_global_template_context()
79 template_env.globals.update(globals_dict)
82def setup_fastblocks_template_environment(
83 template_env: Any, async_mode: bool = False
84) -> None:
85 """Complete setup of FastBlocks template environment.
87 Args:
88 template_env: Jinja2 Environment instance
89 async_mode: Whether to register async filters
90 """
91 # Register filters
92 if async_mode:
93 register_async_fastblocks_filters(template_env)
94 else:
95 register_fastblocks_filters(template_env)
97 # Register globals
98 register_template_globals(template_env)
100 # Configure template environment for FastBlocks
101 template_env.trim_blocks = True
102 template_env.lstrip_blocks = True
104 # Set custom delimiters if not already set
105 if not hasattr(template_env, "_fastblocks_delimiters_set"):
106 template_env.variable_start_string = "[["
107 template_env.variable_end_string = "]]"
108 template_env.block_start_string = "[%"
109 template_env.block_end_string = "%]"
110 template_env._fastblocks_delimiters_set = True
113# Migration status indicator
114_using_oneiric = True