-
Notifications
You must be signed in to change notification settings - Fork 76
Expand file tree
/
Copy pathtemplate_renderer.py
More file actions
62 lines (47 loc) · 2.09 KB
/
Copy pathtemplate_renderer.py
File metadata and controls
62 lines (47 loc) · 2.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
"""Jinja template rendering for resource generation."""
from pathlib import Path
from typing import Any
from jinja2 import DebugUndefined, Environment, FileSystemLoader, meta
from simple_logger.logger import get_logger
LOGGER = get_logger(name=__name__)
def render_jinja_template(template_dict: dict[Any, Any], template_dir: str, template_name: str) -> str:
"""
Render a Jinja template with the provided context.
Args:
template_dict: Dictionary of variables to pass to the template
template_dir: Directory containing the template
template_name: Name of the template file
Returns:
Rendered template as string
"""
env = Environment(
loader=FileSystemLoader(template_dir),
trim_blocks=True,
lstrip_blocks=True,
undefined=DebugUndefined,
)
template = env.get_template(name=template_name)
# Parse the template source to find undeclared variables
# Get template source in a way that's compatible with different Jinja2 versions
try:
# Try to get source directly (newer Jinja2 versions)
template_source = template.source
except AttributeError:
# Fallback: read the template file directly
template_path = Path(template_dir) / template_name
with open(template_path, "r", encoding="utf-8") as f:
template_source = f.read()
ast = env.parse(source=template_source)
undeclared_variables = meta.find_undeclared_variables(ast)
# Filter out variables that are present in template_dict
# We need to check all levels of the template_dict for nested access
provided_variables = set(template_dict.keys())
# Find truly undefined variables
undefined_variables = undeclared_variables - provided_variables
if undefined_variables:
error_msg = f"The following variables are undefined in template '{template_name}': {undefined_variables}. Available variables: {provided_variables}"
LOGGER.error(error_msg)
raise ValueError(error_msg)
# Now render the template
rendered = template.render(template_dict)
return rendered