Coverage for queue_cli/core/templates.py: 0.00%

33 statements  

« prev     ^ index     » next       coverage.py v7.10.6, created at 2026-04-13 00:07 +0000

1""" 

2Template management for message templates. 

3""" 

4 

5import json 

6from pathlib import Path 

7from typing import Any 

8 

9 

10class TemplateManager: 

11 """Manages message templates.""" 

12 

13 def __init__(self, templates_dir: str | None = None): 

14 """ 

15 Initialize template manager. 

16 

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") 

22 

23 self.templates_dir = Path(templates_dir) 

24 

25 def list_templates(self) -> list[str]: 

26 """ 

27 List available templates. 

28 

29 Returns: 

30 List of template names (without .json extension) 

31 """ 

32 if not self.templates_dir.exists(): 

33 return [] 

34 

35 templates = [] 

36 for file in self.templates_dir.glob("*.json"): 

37 templates.append(file.stem) 

38 

39 return sorted(templates) 

40 

41 def load_template(self, template_name: str) -> dict[str, Any] | None: 

42 """ 

43 Load a template by name. 

44 

45 Args: 

46 template_name: Template name (without .json extension) 

47 

48 Returns: 

49 Template data or None if not found 

50 """ 

51 template_path = self.templates_dir / f"{template_name}.json" 

52 

53 if not template_path.exists(): 

54 return None 

55 

56 with open(template_path) as f: 

57 return json.load(f) 

58 

59 def save_template(self, template_name: str, data: dict[str, Any]) -> bool: 

60 """ 

61 Save a template. 

62 

63 Args: 

64 template_name: Template name (without .json extension) 

65 data: Template data 

66 

67 Returns: 

68 True if saved successfully 

69 """ 

70 try: 

71 self.templates_dir.mkdir(parents=True, exist_ok=True) 

72 

73 template_path = self.templates_dir / f"{template_name}.json" 

74 

75 with open(template_path, "w") as f: 

76 json.dump(data, f, indent=2) 

77 

78 return True 

79 

80 except Exception: 

81 return False 

82 

83 def template_exists(self, template_name: str) -> bool: 

84 """ 

85 Check if a template exists. 

86 

87 Args: 

88 template_name: Template name (without .json extension) 

89 

90 Returns: 

91 True if template exists 

92 """ 

93 template_path = self.templates_dir / f"{template_name}.json" 

94 return template_path.exists()