Coverage for queue_cli/core/templates.py: 0.00%
33 statements
« prev ^ index » next coverage.py v7.10.6, created at 2026-04-12 14:12 +0000
« prev ^ index » next coverage.py v7.10.6, created at 2026-04-12 14:12 +0000
1"""
2Template management for message templates.
3"""
5import json
6from pathlib import Path
7from typing import Any
10class TemplateManager:
11 """Manages message templates."""
13 def __init__(self, templates_dir: str | None = None):
14 """
15 Initialize template manager.
17 Args:
18 templates_dir: Directory containing templates. Defaults to package templates.
19 """
20 if templates_dir is None:
21 templates_dir = str(Path(__file__).parent.parent / "templates")
23 self.templates_dir = Path(templates_dir)
25 def list_templates(self) -> list[str]:
26 """
27 List available templates.
29 Returns:
30 List of template names (without .json extension)
31 """
32 if not self.templates_dir.exists():
33 return []
35 templates = []
36 for file in self.templates_dir.glob("*.json"):
37 templates.append(file.stem)
39 return sorted(templates)
41 def load_template(self, template_name: str) -> dict[str, Any] | None:
42 """
43 Load a template by name.
45 Args:
46 template_name: Template name (without .json extension)
48 Returns:
49 Template data or None if not found
50 """
51 template_path = self.templates_dir / f"{template_name}.json"
53 if not template_path.exists():
54 return None
56 with open(template_path) as f:
57 return json.load(f)
59 def save_template(self, template_name: str, data: dict[str, Any]) -> bool:
60 """
61 Save a template.
63 Args:
64 template_name: Template name (without .json extension)
65 data: Template data
67 Returns:
68 True if saved successfully
69 """
70 try:
71 self.templates_dir.mkdir(parents=True, exist_ok=True)
73 template_path = self.templates_dir / f"{template_name}.json"
75 with open(template_path, "w") as f:
76 json.dump(data, f, indent=2)
78 return True
80 except Exception:
81 return False
83 def template_exists(self, template_name: str) -> bool:
84 """
85 Check if a template exists.
87 Args:
88 template_name: Template name (without .json extension)
90 Returns:
91 True if template exists
92 """
93 template_path = self.templates_dir / f"{template_name}.json"
94 return template_path.exists()