Coverage for /Users/les/Projects/crackerjack/crackerjack/plugins/base.py: 0%

124 statements  

« prev     ^ index     » next       coverage.py v7.10.6, created at 2025-09-03 15:39 -0700

1import abc 

2import typing as t 

3from dataclasses import dataclass, field 

4from enum import Enum 

5 

6 

7class PluginType(Enum): 

8 HOOK = "hook" 

9 WORKFLOW = "workflow" 

10 INTEGRATION = "integration" 

11 FORMATTER = "formatter" 

12 ANALYZER = "analyzer" 

13 PUBLISHER = "publisher" 

14 

15 

16@dataclass 

17class PluginMetadata: 

18 name: str 

19 version: str 

20 plugin_type: PluginType 

21 description: str 

22 author: str = "" 

23 license: str = "" 

24 requires_python: str = "> = 3.11" 

25 dependencies: list[str] = field(default_factory=list) 

26 entry_point: str = "" 

27 config_schema: dict[str, t.Any] = field(default_factory=dict) 

28 

29 def to_dict(self) -> dict[str, t.Any]: 

30 return { 

31 "name": self.name, 

32 "version": self.version, 

33 "plugin_type": self.plugin_type.value, 

34 "description": self.description, 

35 "author": self.author, 

36 "license": self.license, 

37 "requires_python": self.requires_python, 

38 "dependencies": self.dependencies, 

39 "entry_point": self.entry_point, 

40 "config_schema": self.config_schema, 

41 } 

42 

43 

44class PluginBase(abc.ABC): 

45 def __init__(self, metadata: PluginMetadata) -> None: 

46 self.metadata = metadata 

47 self._enabled = True 

48 self._config: dict[str, t.Any] = {} 

49 

50 @property 

51 def name(self) -> str: 

52 return self.metadata.name 

53 

54 @property 

55 def version(self) -> str: 

56 return self.metadata.version 

57 

58 @property 

59 def plugin_type(self) -> PluginType: 

60 return self.metadata.plugin_type 

61 

62 @property 

63 def enabled(self) -> bool: 

64 return self._enabled 

65 

66 def enable(self) -> None: 

67 self._enabled = True 

68 

69 def disable(self) -> None: 

70 self._enabled = False 

71 

72 def configure(self, config: dict[str, t.Any]) -> None: 

73 self._config = config.copy() 

74 self.validate_config(config) 

75 

76 @abc.abstractmethod 

77 def activate(self) -> bool: 

78 pass 

79 

80 @abc.abstractmethod 

81 def deactivate(self) -> bool: 

82 pass 

83 

84 def validate_config(self, config: dict[str, t.Any]) -> None: 

85 schema = self.metadata.config_schema 

86 

87 if not schema: 

88 return 

89 

90 required_keys = schema.get("required", []) 

91 for key in required_keys: 

92 if key not in config: 

93 msg = f"Required config key '{key}' missing for plugin {self.name}" 

94 raise ValueError( 

95 msg, 

96 ) 

97 

98 def get_config(self, key: str, default: t.Any = None) -> t.Any: 

99 return self._config.get(key, default) 

100 

101 def get_info(self) -> dict[str, t.Any]: 

102 return { 

103 "metadata": self.metadata.to_dict(), 

104 "enabled": self.enabled, 

105 "config": self._config, 

106 } 

107 

108 

109class PluginRegistry: 

110 def __init__(self) -> None: 

111 self._plugins: dict[str, PluginBase] = {} 

112 self._plugins_by_type: dict[PluginType, list[PluginBase]] = {} 

113 

114 def register(self, plugin: PluginBase) -> bool: 

115 if plugin.name in self._plugins: 

116 return False 

117 

118 self._plugins[plugin.name] = plugin 

119 

120 plugin_type = plugin.plugin_type 

121 if plugin_type not in self._plugins_by_type: 

122 self._plugins_by_type[plugin_type] = [] 

123 self._plugins_by_type[plugin_type].append(plugin) 

124 

125 return True 

126 

127 def unregister(self, plugin_name: str) -> bool: 

128 if plugin_name not in self._plugins: 

129 return False 

130 

131 plugin = self._plugins.pop(plugin_name) 

132 

133 plugin_type = plugin.plugin_type 

134 if plugin_type in self._plugins_by_type: 

135 self._plugins_by_type[plugin_type] = [ 

136 p for p in self._plugins_by_type[plugin_type] if p.name != plugin_name 

137 ] 

138 

139 return True 

140 

141 def get(self, plugin_name: str) -> PluginBase | None: 

142 return self._plugins.get(plugin_name) 

143 

144 def get_by_type(self, plugin_type: PluginType) -> list[PluginBase]: 

145 return self._plugins_by_type.get(plugin_type, []).copy() 

146 

147 def get_enabled(self, plugin_type: PluginType | None = None) -> list[PluginBase]: 

148 if plugin_type: 

149 plugins = self.get_by_type(plugin_type) 

150 else: 

151 plugins = list(self._plugins.values()) 

152 

153 return [p for p in plugins if p.enabled] 

154 

155 def list_all(self) -> dict[str, PluginBase]: 

156 return self._plugins.copy() 

157 

158 def activate_all(self) -> dict[str, bool]: 

159 results = {} 

160 for plugin in self._plugins.values(): 

161 if plugin.enabled: 

162 try: 

163 results[plugin.name] = plugin.activate() 

164 except Exception: 

165 results[plugin.name] = False 

166 

167 return results 

168 

169 def deactivate_all(self) -> dict[str, bool]: 

170 results = {} 

171 for plugin in self._plugins.values(): 

172 try: 

173 results[plugin.name] = plugin.deactivate() 

174 except Exception: 

175 results[plugin.name] = False 

176 

177 return results 

178 

179 def get_stats(self) -> dict[str, t.Any]: 

180 by_type = {} 

181 for plugin_type in PluginType: 

182 plugins = self.get_by_type(plugin_type) 

183 by_type[plugin_type.value] = { 

184 "total": len(plugins), 

185 "enabled": len([p for p in plugins if p.enabled]), 

186 "disabled": len([p for p in plugins if not p.enabled]), 

187 } 

188 

189 return { 

190 "total_plugins": len(self._plugins), 

191 "enabled_plugins": len([p for p in self._plugins.values() if p.enabled]), 

192 "by_type": by_type, 

193 } 

194 

195 

196_registry = PluginRegistry() 

197 

198 

199def get_plugin_registry() -> PluginRegistry: 

200 return _registry