template.py 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775
  1. from __future__ import annotations
  2. from .variable import Variable
  3. from .collection import VariableCollection
  4. from .exceptions import (
  5. TemplateError,
  6. TemplateLoadError,
  7. TemplateSyntaxError,
  8. TemplateValidationError,
  9. TemplateRenderError,
  10. YAMLParseError,
  11. ModuleLoadError
  12. )
  13. from pathlib import Path
  14. from typing import Any, Dict, List, Set, Optional, Literal
  15. from dataclasses import dataclass, field
  16. from functools import lru_cache
  17. import logging
  18. import os
  19. import yaml
  20. from jinja2 import Environment, FileSystemLoader, meta
  21. from jinja2.sandbox import SandboxedEnvironment
  22. from jinja2 import nodes
  23. from jinja2.visitor import NodeVisitor
  24. from jinja2.exceptions import (
  25. TemplateSyntaxError as Jinja2TemplateSyntaxError,
  26. UndefinedError,
  27. TemplateError as Jinja2TemplateError,
  28. TemplateNotFound as Jinja2TemplateNotFound
  29. )
  30. logger = logging.getLogger(__name__)
  31. def _extract_error_context(
  32. file_path: Path,
  33. line_number: Optional[int],
  34. context_size: int = 3
  35. ) -> List[str]:
  36. """Extract lines of context around an error location.
  37. Args:
  38. file_path: Path to the file with the error
  39. line_number: Line number where error occurred (1-indexed)
  40. context_size: Number of lines to show before and after
  41. Returns:
  42. List of context lines with line numbers
  43. """
  44. if not line_number or not file_path.exists():
  45. return []
  46. try:
  47. with open(file_path, 'r', encoding='utf-8') as f:
  48. lines = f.readlines()
  49. start_line = max(0, line_number - context_size - 1)
  50. end_line = min(len(lines), line_number + context_size)
  51. context = []
  52. for i in range(start_line, end_line):
  53. line_num = i + 1
  54. marker = '>>>' if line_num == line_number else ' '
  55. context.append(f"{marker} {line_num:4d} | {lines[i].rstrip()}")
  56. return context
  57. except (IOError, OSError):
  58. return []
  59. def _get_common_jinja_suggestions(error_msg: str, available_vars: set) -> List[str]:
  60. """Generate helpful suggestions based on common Jinja2 errors.
  61. Args:
  62. error_msg: The error message from Jinja2
  63. available_vars: Set of available variable names
  64. Returns:
  65. List of actionable suggestions
  66. """
  67. suggestions = []
  68. error_lower = error_msg.lower()
  69. # Undefined variable errors
  70. if 'undefined' in error_lower or 'is not defined' in error_lower:
  71. # Try to extract variable name from error message
  72. import re
  73. var_match = re.search(r"'([^']+)'.*is undefined", error_msg)
  74. if not var_match:
  75. var_match = re.search(r"'([^']+)'.*is not defined", error_msg)
  76. if var_match:
  77. undefined_var = var_match.group(1)
  78. suggestions.append(f"Variable '{undefined_var}' is not defined in the template spec")
  79. # Suggest similar variable names (basic fuzzy matching)
  80. similar = [v for v in available_vars if undefined_var.lower() in v.lower() or v.lower() in undefined_var.lower()]
  81. if similar:
  82. suggestions.append(f"Did you mean one of these? {', '.join(sorted(similar)[:5])}")
  83. suggestions.append(f"Add '{undefined_var}' to your template.yaml spec with a default value")
  84. suggestions.append("Or use the Jinja2 default filter: {{ " + undefined_var + " | default('value') }}")
  85. else:
  86. suggestions.append("Check that all variables used in templates are defined in template.yaml")
  87. suggestions.append("Use the Jinja2 default filter for optional variables: {{ var | default('value') }}")
  88. # Syntax errors
  89. elif 'unexpected' in error_lower or 'expected' in error_lower:
  90. suggestions.append("Check for syntax errors in your Jinja2 template")
  91. suggestions.append("Common issues: missing {% endfor %}, {% endif %}, or {% endblock %}")
  92. suggestions.append("Make sure all {{ }} and {% %} tags are properly closed")
  93. # Filter errors
  94. elif 'filter' in error_lower:
  95. suggestions.append("Check that the filter name is spelled correctly")
  96. suggestions.append("Verify the filter exists in Jinja2 built-in filters")
  97. suggestions.append("Make sure filter arguments are properly formatted")
  98. # Template not found
  99. elif 'not found' in error_lower or 'does not exist' in error_lower:
  100. suggestions.append("Check that the included/imported template file exists")
  101. suggestions.append("Verify the template path is relative to the template directory")
  102. suggestions.append("Make sure the file has the .j2 extension if it's a Jinja2 template")
  103. # Type errors
  104. elif 'type' in error_lower and ('int' in error_lower or 'str' in error_lower or 'bool' in error_lower):
  105. suggestions.append("Check that variable values have the correct type")
  106. suggestions.append("Use Jinja2 filters to convert types: {{ var | int }}, {{ var | string }}")
  107. # Add generic helpful tip
  108. if not suggestions:
  109. suggestions.append("Check the Jinja2 template syntax and variable usage")
  110. suggestions.append("Enable --debug mode for more detailed rendering information")
  111. return suggestions
  112. def _parse_jinja_error(
  113. error: Exception,
  114. template_file: TemplateFile,
  115. template_dir: Path,
  116. available_vars: set
  117. ) -> tuple[str, Optional[int], Optional[int], List[str], List[str]]:
  118. """Parse a Jinja2 exception to extract detailed error information.
  119. Args:
  120. error: The Jinja2 exception
  121. template_file: The TemplateFile being rendered
  122. template_dir: Template directory path
  123. available_vars: Set of available variable names
  124. Returns:
  125. Tuple of (error_message, line_number, column, context_lines, suggestions)
  126. """
  127. error_msg = str(error)
  128. line_number = None
  129. column = None
  130. context_lines = []
  131. suggestions = []
  132. # Extract line number from Jinja2 errors
  133. if hasattr(error, 'lineno'):
  134. line_number = error.lineno
  135. # Extract file path and get context
  136. file_path = template_dir / template_file.relative_path
  137. if line_number and file_path.exists():
  138. context_lines = _extract_error_context(file_path, line_number)
  139. # Generate suggestions based on error type
  140. if isinstance(error, UndefinedError):
  141. error_msg = f"Undefined variable: {error}"
  142. suggestions = _get_common_jinja_suggestions(str(error), available_vars)
  143. elif isinstance(error, Jinja2TemplateSyntaxError):
  144. error_msg = f"Template syntax error: {error}"
  145. suggestions = _get_common_jinja_suggestions(str(error), available_vars)
  146. elif isinstance(error, Jinja2TemplateNotFound):
  147. error_msg = f"Template file not found: {error}"
  148. suggestions = _get_common_jinja_suggestions(str(error), available_vars)
  149. else:
  150. # Generic Jinja2 error
  151. suggestions = _get_common_jinja_suggestions(error_msg, available_vars)
  152. return error_msg, line_number, column, context_lines, suggestions
  153. @dataclass
  154. class TemplateFile:
  155. """Represents a single file within a template directory."""
  156. relative_path: Path
  157. file_type: Literal['j2', 'static']
  158. output_path: Path # The path it will have in the output directory
  159. @dataclass
  160. class TemplateMetadata:
  161. """Represents template metadata with proper typing."""
  162. name: str
  163. description: str
  164. author: str
  165. date: str
  166. version: str
  167. module: str = ""
  168. tags: List[str] = field(default_factory=list)
  169. library: str = "unknown"
  170. next_steps: str = ""
  171. draft: bool = False
  172. def __init__(self, template_data: dict, library_name: str | None = None) -> None:
  173. """Initialize TemplateMetadata from parsed YAML template data.
  174. Args:
  175. template_data: Parsed YAML data from template.yaml
  176. library_name: Name of the library this template belongs to
  177. """
  178. # Validate metadata format first
  179. self._validate_metadata(template_data)
  180. # Extract metadata section
  181. metadata_section = template_data.get("metadata", {})
  182. self.name = metadata_section.get("name", "")
  183. # YAML block scalar (|) preserves a trailing newline. Remove only trailing newlines
  184. # while preserving internal newlines/formatting.
  185. raw_description = metadata_section.get("description", "")
  186. if isinstance(raw_description, str):
  187. description = raw_description.rstrip("\n")
  188. else:
  189. description = str(raw_description)
  190. self.description = description or "No description available"
  191. self.author = metadata_section.get("author", "")
  192. self.date = metadata_section.get("date", "")
  193. self.version = metadata_section.get("version", "")
  194. self.module = metadata_section.get("module", "")
  195. self.tags = metadata_section.get("tags", []) or []
  196. self.library = library_name or "unknown"
  197. self.draft = metadata_section.get("draft", False)
  198. # Extract next_steps (optional)
  199. raw_next_steps = metadata_section.get("next_steps", "")
  200. if isinstance(raw_next_steps, str):
  201. next_steps = raw_next_steps.rstrip("\n")
  202. else:
  203. next_steps = str(raw_next_steps) if raw_next_steps else ""
  204. self.next_steps = next_steps
  205. @staticmethod
  206. def _validate_metadata(template_data: dict) -> None:
  207. """Validate that template has required 'metadata' section with all required fields.
  208. Args:
  209. template_data: Parsed YAML data from template.yaml
  210. Raises:
  211. ValueError: If metadata section is missing or incomplete
  212. """
  213. metadata_section = template_data.get("metadata")
  214. if metadata_section is None:
  215. raise ValueError("Template format error: missing 'metadata' section")
  216. # Validate that metadata section has all required fields
  217. required_fields = ["name", "author", "version", "date", "description"]
  218. missing_fields = [field for field in required_fields if not metadata_section.get(field)]
  219. if missing_fields:
  220. raise ValueError(f"Template format error: missing required metadata fields: {missing_fields}")
  221. @dataclass
  222. class Template:
  223. """Represents a template directory."""
  224. def __init__(self, template_dir: Path, library_name: str) -> None:
  225. """Create a Template instance from a directory path."""
  226. logger.debug(f"Loading template from directory: {template_dir}")
  227. self.template_dir = template_dir
  228. self.id = template_dir.name
  229. self.library_name = library_name
  230. # Initialize caches for lazy loading
  231. self.__module_specs: Optional[dict] = None
  232. self.__merged_specs: Optional[dict] = None
  233. self.__jinja_env: Optional[Environment] = None
  234. self.__used_variables: Optional[Set[str]] = None
  235. self.__variables: Optional[VariableCollection] = None
  236. self.__template_files: Optional[List[TemplateFile]] = None # New attribute
  237. try:
  238. # Find and parse the main template file (template.yaml or template.yml)
  239. main_template_path = self._find_main_template_file()
  240. with open(main_template_path, "r", encoding="utf-8") as f:
  241. # Load all YAML documents (handles templates with empty lines before ---)
  242. documents = list(yaml.safe_load_all(f))
  243. # Filter out None/empty documents and get the first non-empty one
  244. valid_docs = [doc for doc in documents if doc is not None]
  245. if not valid_docs:
  246. raise ValueError("Template file contains no valid YAML data")
  247. if len(valid_docs) > 1:
  248. logger.warning(f"Template file contains multiple YAML documents, using the first one")
  249. self._template_data = valid_docs[0]
  250. # Validate template data
  251. if not isinstance(self._template_data, dict):
  252. raise ValueError("Template file must contain a valid YAML dictionary")
  253. # Load metadata (always needed)
  254. self.metadata = TemplateMetadata(self._template_data, library_name)
  255. logger.debug(f"Loaded metadata: {self.metadata}")
  256. # Validate 'kind' field (always needed)
  257. self._validate_kind(self._template_data)
  258. # NOTE: File collection is now lazy-loaded via the template_files property
  259. # This significantly improves performance when listing many templates
  260. logger.info(f"Loaded template '{self.id}' (v{self.metadata.version})")
  261. except (ValueError, FileNotFoundError) as e:
  262. logger.error(f"Error loading template from {template_dir}: {e}")
  263. raise TemplateLoadError(f"Error loading template from {template_dir}: {e}")
  264. except yaml.YAMLError as e:
  265. logger.error(f"YAML parsing error in template {template_dir}: {e}")
  266. raise YAMLParseError(str(template_dir / "template.y*ml"), e)
  267. except (IOError, OSError) as e:
  268. logger.error(f"File I/O error loading template {template_dir}: {e}")
  269. raise TemplateLoadError(f"File I/O error loading template from {template_dir}: {e}")
  270. def _find_main_template_file(self) -> Path:
  271. """Find the main template file (template.yaml or template.yml)."""
  272. for filename in ["template.yaml", "template.yml"]:
  273. path = self.template_dir / filename
  274. if path.exists():
  275. return path
  276. raise FileNotFoundError(f"Main template file (template.yaml or template.yml) not found in {self.template_dir}")
  277. @staticmethod
  278. @lru_cache(maxsize=32)
  279. def _load_module_specs(kind: str) -> dict:
  280. """Load specifications from the corresponding module with caching.
  281. Uses LRU cache to avoid re-loading the same module spec multiple times.
  282. This significantly improves performance when listing many templates of the same kind.
  283. Args:
  284. kind: The module kind (e.g., 'compose', 'terraform')
  285. Returns:
  286. Dictionary containing the module's spec, or empty dict if kind is empty
  287. Raises:
  288. ValueError: If module cannot be loaded or spec is invalid
  289. """
  290. if not kind:
  291. return {}
  292. try:
  293. import importlib
  294. module = importlib.import_module(f"cli.modules.{kind}")
  295. spec = getattr(module, 'spec', {})
  296. logger.debug(f"Loaded and cached module spec for kind '{kind}'")
  297. return spec
  298. except Exception as e:
  299. raise ValueError(f"Error loading module specifications for kind '{kind}': {e}")
  300. def _merge_specs(self, module_specs: dict, template_specs: dict) -> dict:
  301. """Deep merge template specs with module specs using VariableCollection.
  302. Uses VariableCollection's native merge() method for consistent merging logic.
  303. Module specs are base, template specs override with origin tracking.
  304. """
  305. # Create VariableCollection from module specs (base)
  306. module_collection = VariableCollection(module_specs) if module_specs else VariableCollection({})
  307. # Set origin for module variables
  308. for section in module_collection.get_sections().values():
  309. for variable in section.variables.values():
  310. if not variable.origin:
  311. variable.origin = "module"
  312. # Merge template specs into module specs (template overrides)
  313. if template_specs:
  314. merged_collection = module_collection.merge(template_specs, origin="template")
  315. else:
  316. merged_collection = module_collection
  317. # Convert back to dict format
  318. merged_spec = {}
  319. for section_key, section in merged_collection.get_sections().items():
  320. merged_spec[section_key] = section.to_dict()
  321. return merged_spec
  322. def _collect_template_files(self) -> None:
  323. """Collects all TemplateFile objects in the template directory."""
  324. template_files: List[TemplateFile] = []
  325. for root, _, files in os.walk(self.template_dir):
  326. for filename in files:
  327. file_path = Path(root) / filename
  328. relative_path = file_path.relative_to(self.template_dir)
  329. # Skip the main template file
  330. if filename in ["template.yaml", "template.yml"]:
  331. continue
  332. if filename.endswith(".j2"):
  333. file_type: Literal['j2', 'static'] = 'j2'
  334. output_path = relative_path.with_suffix('') # Remove .j2 suffix
  335. else:
  336. file_type = 'static'
  337. output_path = relative_path # Static files keep their name
  338. template_files.append(TemplateFile(relative_path=relative_path, file_type=file_type, output_path=output_path))
  339. self.__template_files = template_files
  340. def _extract_all_used_variables(self) -> Set[str]:
  341. """Extract all undeclared variables from all .j2 files in the template directory.
  342. Raises:
  343. ValueError: If any Jinja2 template has syntax errors
  344. """
  345. used_variables: Set[str] = set()
  346. syntax_errors = []
  347. for template_file in self.template_files: # Iterate over TemplateFile objects
  348. if template_file.file_type == 'j2':
  349. file_path = self.template_dir / template_file.relative_path
  350. try:
  351. with open(file_path, "r", encoding="utf-8") as f:
  352. content = f.read()
  353. ast = self.jinja_env.parse(content) # Use lazy-loaded jinja_env
  354. used_variables.update(meta.find_undeclared_variables(ast))
  355. except (IOError, OSError) as e:
  356. relative_path = file_path.relative_to(self.template_dir)
  357. syntax_errors.append(f" - {relative_path}: File I/O error: {e}")
  358. except Exception as e:
  359. # Collect syntax errors for Jinja2 issues
  360. relative_path = file_path.relative_to(self.template_dir)
  361. syntax_errors.append(f" - {relative_path}: {e}")
  362. # Raise error if any syntax errors were found
  363. if syntax_errors:
  364. logger.error(f"Jinja2 syntax errors found in template '{self.id}'")
  365. raise TemplateSyntaxError(self.id, syntax_errors)
  366. return used_variables
  367. def _extract_jinja_default_values(self) -> dict[str, object]:
  368. """Scan all .j2 files and extract literal arguments to the `default` filter.
  369. Returns a mapping var_name -> literal_value for simple cases like
  370. {{ var | default("value") }} or {{ var | default(123) }}.
  371. This does not attempt to evaluate complex expressions.
  372. """
  373. defaults: dict[str, object] = {}
  374. class _DefaultVisitor(NodeVisitor):
  375. def __init__(self):
  376. self.found: dict[str, object] = {}
  377. def visit_Filter(self, node: nodes.Filter) -> None: # type: ignore[override]
  378. try:
  379. if getattr(node, 'name', None) == 'default' and node.args:
  380. # target variable name when filter is applied directly to a Name
  381. target = None
  382. if isinstance(node.node, nodes.Name):
  383. target = node.node.name
  384. # first arg literal
  385. first = node.args[0]
  386. if isinstance(first, nodes.Const) and target:
  387. self.found[target] = first.value
  388. except Exception:
  389. # Be resilient to unexpected node shapes
  390. pass
  391. # continue traversal
  392. self.generic_visit(node)
  393. visitor = _DefaultVisitor()
  394. for template_file in self.template_files:
  395. if template_file.file_type != 'j2':
  396. continue
  397. file_path = self.template_dir / template_file.relative_path
  398. try:
  399. with open(file_path, 'r', encoding='utf-8') as f:
  400. content = f.read()
  401. ast = self.jinja_env.parse(content)
  402. visitor.visit(ast)
  403. except (IOError, OSError, yaml.YAMLError):
  404. # Skip failures - this extraction is best-effort only
  405. continue
  406. return visitor.found
  407. def _filter_specs_to_used(self, used_variables: set, merged_specs: dict, module_specs: dict, template_specs: dict) -> dict:
  408. """Filter specs to only include variables used in templates using VariableCollection.
  409. Uses VariableCollection's native filter_to_used() method.
  410. Keeps sensitive variables only if they're defined in the template spec or actually used.
  411. """
  412. # Build set of variables explicitly defined in template spec
  413. template_defined_vars = set()
  414. for section_data in (template_specs or {}).values():
  415. if isinstance(section_data, dict) and 'vars' in section_data:
  416. template_defined_vars.update(section_data['vars'].keys())
  417. # Create VariableCollection from merged specs
  418. merged_collection = VariableCollection(merged_specs)
  419. # Filter to only used variables (and sensitive ones that are template-defined)
  420. # We keep sensitive variables that are either:
  421. # 1. Actually used in template files, OR
  422. # 2. Explicitly defined in the template spec (even if not yet used)
  423. variables_to_keep = used_variables | template_defined_vars
  424. filtered_collection = merged_collection.filter_to_used(variables_to_keep, keep_sensitive=False)
  425. # Convert back to dict format
  426. filtered_specs = {}
  427. for section_key, section in filtered_collection.get_sections().items():
  428. filtered_specs[section_key] = section.to_dict()
  429. return filtered_specs
  430. @staticmethod
  431. def _validate_kind(template_data: dict) -> None:
  432. """Validate that template has required 'kind' field.
  433. Args:
  434. template_data: Parsed YAML data from template.yaml
  435. Raises:
  436. ValueError: If 'kind' field is missing
  437. """
  438. if not template_data.get("kind"):
  439. raise TemplateValidationError("Template format error: missing 'kind' field")
  440. def _validate_variable_definitions(self, used_variables: set[str], merged_specs: dict[str, Any]) -> None:
  441. """Validate that all variables used in Jinja2 content are defined in the spec."""
  442. defined_variables = set()
  443. for section_data in merged_specs.values():
  444. if "vars" in section_data and isinstance(section_data["vars"], dict):
  445. defined_variables.update(section_data["vars"].keys())
  446. undefined_variables = used_variables - defined_variables
  447. if undefined_variables:
  448. undefined_list = sorted(undefined_variables)
  449. error_msg = (
  450. f"Template validation error in '{self.id}': "
  451. f"Variables used in template content but not defined in spec: {undefined_list}\n\n"
  452. f"Please add these variables to your template's template.yaml spec. "
  453. f"Each variable must have a default value.\n\n"
  454. f"Example:\n"
  455. f"spec:\n"
  456. f" general:\n"
  457. f" vars:\n"
  458. )
  459. for var_name in undefined_list:
  460. error_msg += (
  461. f" {var_name}:\n"
  462. f" type: str\n"
  463. f" description: Description for {var_name}\n"
  464. f" default: <your_default_value_here>\n"
  465. )
  466. logger.error(error_msg)
  467. raise TemplateValidationError(error_msg)
  468. @staticmethod
  469. def _create_jinja_env(searchpath: Path) -> Environment:
  470. """Create sandboxed Jinja2 environment for secure template processing.
  471. Uses SandboxedEnvironment to prevent code injection vulnerabilities
  472. when processing untrusted templates. This restricts access to dangerous
  473. operations while still allowing safe template rendering.
  474. Returns:
  475. SandboxedEnvironment configured for template processing.
  476. """
  477. # NOTE Use SandboxedEnvironment for security - prevents arbitrary code execution
  478. return SandboxedEnvironment(
  479. loader=FileSystemLoader(searchpath),
  480. trim_blocks=True,
  481. lstrip_blocks=True,
  482. keep_trailing_newline=False,
  483. )
  484. def render(self, variables: VariableCollection, debug: bool = False) -> tuple[Dict[str, str], Dict[str, Any]]:
  485. """Render all .j2 files in the template directory.
  486. Args:
  487. variables: VariableCollection with values to use for rendering
  488. debug: Enable debug mode with verbose output
  489. Returns:
  490. Tuple of (rendered_files, variable_values) where variable_values includes autogenerated values
  491. """
  492. # Use get_satisfied_values() to exclude variables from sections with unsatisfied dependencies
  493. variable_values = variables.get_satisfied_values()
  494. # Auto-generate values for autogenerated variables that are empty
  495. import secrets
  496. import string
  497. for section in variables.get_sections().values():
  498. for var_name, variable in section.variables.items():
  499. if variable.autogenerated and (variable.value is None or variable.value == ""):
  500. # Generate a secure random string (32 characters by default)
  501. alphabet = string.ascii_letters + string.digits
  502. generated_value = ''.join(secrets.choice(alphabet) for _ in range(32))
  503. variable_values[var_name] = generated_value
  504. logger.debug(f"Auto-generated value for variable '{var_name}'")
  505. if debug:
  506. logger.info(f"Rendering template '{self.id}' in debug mode")
  507. logger.info(f"Available variables: {sorted(variable_values.keys())}")
  508. logger.info(f"Variable values: {variable_values}")
  509. else:
  510. logger.debug(f"Rendering template '{self.id}' with variables: {variable_values}")
  511. rendered_files = {}
  512. available_vars = set(variable_values.keys())
  513. for template_file in self.template_files: # Iterate over TemplateFile objects
  514. if template_file.file_type == 'j2':
  515. try:
  516. if debug:
  517. logger.info(f"Rendering Jinja2 template: {template_file.relative_path}")
  518. template = self.jinja_env.get_template(str(template_file.relative_path)) # Use lazy-loaded jinja_env
  519. rendered_content = template.render(**variable_values)
  520. # Sanitize the rendered content to remove excessive blank lines
  521. rendered_content = self._sanitize_content(rendered_content, template_file.output_path)
  522. rendered_files[str(template_file.output_path)] = rendered_content
  523. if debug:
  524. logger.info(f"Successfully rendered: {template_file.relative_path} -> {template_file.output_path}")
  525. except (UndefinedError, Jinja2TemplateSyntaxError, Jinja2TemplateNotFound, Jinja2TemplateError) as e:
  526. # Parse Jinja2 error to extract detailed information
  527. error_msg, line_num, col, context_lines, suggestions = _parse_jinja_error(
  528. e, template_file, self.template_dir, available_vars
  529. )
  530. logger.error(f"Error rendering template file {template_file.relative_path}: {error_msg}")
  531. # Create enhanced TemplateRenderError with all context
  532. raise TemplateRenderError(
  533. message=error_msg,
  534. file_path=str(template_file.relative_path),
  535. line_number=line_num,
  536. column=col,
  537. context_lines=context_lines,
  538. variable_context={k: str(v) for k, v in variable_values.items()} if debug else {},
  539. suggestions=suggestions,
  540. original_error=e
  541. )
  542. except Exception as e:
  543. # Catch any other unexpected errors
  544. logger.error(f"Unexpected error rendering template file {template_file.relative_path}: {e}")
  545. raise TemplateRenderError(
  546. message=f"Unexpected rendering error: {e}",
  547. file_path=str(template_file.relative_path),
  548. suggestions=["This is an unexpected error. Please check the template for issues."],
  549. original_error=e
  550. )
  551. elif template_file.file_type == 'static':
  552. # For static files, just read their content and add to rendered_files
  553. # This ensures static files are also part of the output dictionary
  554. file_path = self.template_dir / template_file.relative_path
  555. try:
  556. if debug:
  557. logger.info(f"Copying static file: {template_file.relative_path}")
  558. with open(file_path, "r", encoding="utf-8") as f:
  559. content = f.read()
  560. rendered_files[str(template_file.output_path)] = content
  561. except (IOError, OSError) as e:
  562. logger.error(f"Error reading static file {file_path}: {e}")
  563. raise TemplateRenderError(
  564. message=f"Error reading static file: {e}",
  565. file_path=str(template_file.relative_path),
  566. suggestions=["Check that the file exists and has read permissions"],
  567. original_error=e
  568. )
  569. return rendered_files, variable_values
  570. def _sanitize_content(self, content: str, file_path: Path) -> str:
  571. """Sanitize rendered content by removing excessive blank lines and trailing whitespace."""
  572. if not content:
  573. return content
  574. lines = [line.rstrip() for line in content.split('\n')]
  575. sanitized = []
  576. prev_blank = False
  577. for line in lines:
  578. is_blank = not line
  579. if is_blank and prev_blank:
  580. continue # Skip consecutive blank lines
  581. sanitized.append(line)
  582. prev_blank = is_blank
  583. # Remove leading blanks and ensure single trailing newline
  584. return '\n'.join(sanitized).lstrip('\n').rstrip('\n') + '\n'
  585. @property
  586. def template_files(self) -> List[TemplateFile]:
  587. if self.__template_files is None:
  588. self._collect_template_files() # Populate self.__template_files
  589. return self.__template_files
  590. @property
  591. def template_specs(self) -> dict:
  592. """Get the spec section from template YAML data."""
  593. return self._template_data.get("spec", {})
  594. @property
  595. def module_specs(self) -> dict:
  596. """Get the spec from the module definition."""
  597. if self.__module_specs is None:
  598. kind = self._template_data.get("kind")
  599. self.__module_specs = self._load_module_specs(kind)
  600. return self.__module_specs
  601. @property
  602. def merged_specs(self) -> dict:
  603. if self.__merged_specs is None:
  604. self.__merged_specs = self._merge_specs(self.module_specs, self.template_specs)
  605. return self.__merged_specs
  606. @property
  607. def jinja_env(self) -> Environment:
  608. if self.__jinja_env is None:
  609. self.__jinja_env = self._create_jinja_env(self.template_dir)
  610. return self.__jinja_env
  611. @property
  612. def used_variables(self) -> Set[str]:
  613. if self.__used_variables is None:
  614. self.__used_variables = self._extract_all_used_variables()
  615. return self.__used_variables
  616. @property
  617. def variables(self) -> VariableCollection:
  618. if self.__variables is None:
  619. # Validate that all used variables are defined
  620. self._validate_variable_definitions(self.used_variables, self.merged_specs)
  621. # Filter specs to only used variables
  622. filtered_specs = self._filter_specs_to_used(self.used_variables, self.merged_specs, self.module_specs, self.template_specs)
  623. # Best-effort: extract literal defaults from Jinja `default()` filter and
  624. # merge them into the filtered_specs when no default exists there.
  625. try:
  626. jinja_defaults = self._extract_jinja_default_values()
  627. for section_key, section_data in filtered_specs.items():
  628. # Guard against None from empty YAML sections
  629. vars_dict = section_data.get('vars') or {}
  630. for var_name, var_data in vars_dict.items():
  631. if 'default' not in var_data or var_data.get('default') in (None, ''):
  632. if var_name in jinja_defaults:
  633. var_data['default'] = jinja_defaults[var_name]
  634. except (KeyError, TypeError, AttributeError):
  635. # Keep behavior stable on any extraction errors
  636. pass
  637. self.__variables = VariableCollection(filtered_specs)
  638. # Sort sections: required first, then enabled, then disabled
  639. self.__variables.sort_sections()
  640. return self.__variables