Coverage for fastblocks/adapters/templates/_registration.py: 83%

47 statements  

« prev     ^ index     » next       coverage.py v7.10.7, created at 2025-10-09 00:47 -0700

1"""Template filter registration system for FastBlocks adapters.""" 

2 

3from contextlib import suppress 

4from typing import Any 

5 

6from acb.depends import depends 

7 

8from ._filters import FASTBLOCKS_FILTERS 

9 

10 

11def register_fastblocks_filters(template_env: Any) -> None: 

12 """Register all FastBlocks adapter filters with a Jinja2 environment. 

13 

14 Args: 

15 template_env: Jinja2 Environment instance 

16 """ 

17 for filter_name, filter_func in FASTBLOCKS_FILTERS.items(): 

18 template_env.filters[filter_name] = filter_func 

19 

20 

21def register_async_fastblocks_filters(template_env: Any) -> None: 

22 """Register async versions of FastBlocks adapter filters. 

23 

24 Args: 

25 template_env: Jinja2 AsyncEnvironment instance 

26 """ 

27 from ._async_filters import FASTBLOCKS_ASYNC_FILTERS 

28 

29 # Register both sync and async filters 

30 register_fastblocks_filters(template_env) 

31 

32 # Add async-specific filters 

33 for filter_name, filter_func in FASTBLOCKS_ASYNC_FILTERS.items(): 

34 template_env.filters[filter_name] = filter_func 

35 

36 

37def get_global_template_context() -> dict[str, Any]: 

38 """Get global template context with adapter instances. 

39 

40 Returns: 

41 Dict of global template variables 

42 """ 

43 context = {} 

44 

45 # Add adapter instances to global context 

46 with suppress(Exception): 

47 images = depends.get("images") 

48 if images: 

49 context["images_adapter"] = images 

50 

51 with suppress(Exception): 

52 styles = depends.get("styles") 

53 if styles: 

54 context["styles_adapter"] = styles 

55 

56 with suppress(Exception): 

57 icons = depends.get("icons") 

58 if icons: 

59 context["icons_adapter"] = icons 

60 

61 with suppress(Exception): 

62 fonts = depends.get("fonts") 

63 if fonts: 

64 context["fonts_adapter"] = fonts 

65 

66 return context 

67 

68 

69def register_template_globals(template_env: Any) -> None: 

70 """Register global template variables and functions. 

71 

72 Args: 

73 template_env: Jinja2 Environment instance 

74 """ 

75 globals_dict = get_global_template_context() 

76 template_env.globals.update(globals_dict) 

77 

78 

79def setup_fastblocks_template_environment( 

80 template_env: Any, async_mode: bool = False 

81) -> None: 

82 """Complete setup of FastBlocks template environment. 

83 

84 Args: 

85 template_env: Jinja2 Environment instance 

86 async_mode: Whether to register async filters 

87 """ 

88 # Register filters 

89 if async_mode: 

90 register_async_fastblocks_filters(template_env) 

91 else: 

92 register_fastblocks_filters(template_env) 

93 

94 # Register globals 

95 register_template_globals(template_env) 

96 

97 # Configure template environment for FastBlocks 

98 template_env.trim_blocks = True 

99 template_env.lstrip_blocks = True 

100 

101 # Set custom delimiters if not already set 

102 if not hasattr(template_env, "_fastblocks_delimiters_set"): 

103 template_env.variable_start_string = "[[" 

104 template_env.variable_end_string = "]]" 

105 template_env.block_start_string = "[%" 

106 template_env.block_end_string = "%]" 

107 template_env._fastblocks_delimiters_set = True