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

149 statements  

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

1import logging 

2import typing as t 

3from pathlib import Path 

4 

5from rich.console import Console 

6 

7from crackerjack.models.protocols import OptionsProtocol 

8 

9from .base import PluginRegistry, PluginType, get_plugin_registry 

10from .hooks import HookPluginRegistry, get_hook_plugin_registry 

11from .loader import PluginDiscovery, PluginLoader 

12 

13 

14class PluginManager: 

15 def __init__( 

16 self, 

17 console: Console, 

18 project_path: Path, 

19 registry: PluginRegistry | None = None, 

20 hook_registry: HookPluginRegistry | None = None, 

21 ) -> None: 

22 self.console = console 

23 self.project_path = project_path 

24 self.registry = registry or get_plugin_registry() 

25 self.hook_registry = hook_registry or get_hook_plugin_registry() 

26 

27 self.loader = PluginLoader(self.registry) 

28 self.discovery = PluginDiscovery(self.loader) 

29 self.logger = logging.getLogger("crackerjack.plugin_manager") 

30 

31 self._initialized = False 

32 

33 def initialize(self) -> bool: 

34 if self._initialized: 

35 return True 

36 

37 self.logger.info("Initializing plugin system") 

38 

39 try: 

40 results = self.discovery.auto_discover_and_load(self.project_path) 

41 

42 loaded_count = sum(1 for success in results.values() if success) 

43 total_count = len(results) 

44 

45 if total_count > 0: 

46 self.console.print( 

47 f"[green]✅[/ green] Loaded {loaded_count} / {total_count} plugins", 

48 ) 

49 

50 activation_results = self.registry.activate_all() 

51 activated_count = sum( 

52 1 for success in activation_results.values() if success 

53 ) 

54 

55 if activated_count > 0: 

56 self.console.print( 

57 f"[green]✅[/ green] Activated {activated_count} plugins", 

58 ) 

59 

60 hook_plugins = self.registry.get_enabled(PluginType.HOOK) 

61 for plugin in hook_plugins: 

62 if hasattr(plugin, "initialize"): 

63 plugin.initialize(self.console, self.project_path) 

64 self.hook_registry.register_hook_plugin(plugin) 

65 

66 self._initialized = True 

67 return True 

68 

69 except Exception as e: 

70 self.logger.exception(f"Failed to initialize plugin system: {e}") 

71 self.console.print( 

72 f"[red]❌[/ red] Plugin system initialization failed: {e}", 

73 ) 

74 return False 

75 

76 def shutdown(self) -> None: 

77 if not self._initialized: 

78 return 

79 

80 self.logger.info("Shutting down plugin system") 

81 

82 try: 

83 results = self.registry.deactivate_all() 

84 deactivated_count = sum(1 for success in results.values() if success) 

85 

86 if deactivated_count > 0: 

87 self.console.print( 

88 f"[yellow]⏹️[/ yellow] Deactivated {deactivated_count} plugins", 

89 ) 

90 

91 self._initialized = False 

92 

93 except Exception as e: 

94 self.logger.exception(f"Error during plugin system shutdown: {e}") 

95 

96 def list_plugins(self, plugin_type: PluginType | None = None) -> dict[str, t.Any]: 

97 if plugin_type: 

98 plugins = self.registry.get_by_type(plugin_type) 

99 else: 

100 plugins = list(self.registry.list_all().values()) 

101 

102 plugin_info = [] 

103 for plugin in plugins: 

104 info = plugin.get_info() 

105 info["active"] = plugin.enabled 

106 plugin_info.append(info) 

107 

108 return { 

109 "plugins": plugin_info, 

110 "total": len(plugin_info), 

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

112 } 

113 

114 def get_plugin_stats(self) -> dict[str, t.Any]: 

115 stats = self.registry.get_stats() 

116 

117 hook_plugins = self.registry.get_enabled(PluginType.HOOK) 

118 custom_hooks = self.hook_registry.get_all_custom_hooks() 

119 

120 stats["hook_plugins"] = { 

121 "active_plugins": len(hook_plugins), 

122 "total_custom_hooks": len(custom_hooks), 

123 "hook_names": list(custom_hooks.keys()), 

124 } 

125 

126 return stats 

127 

128 def enable_plugin(self, plugin_name: str) -> bool: 

129 plugin = self.registry.get(plugin_name) 

130 if not plugin: 

131 self.console.print(f"[red]❌[/ red] Plugin not found: {plugin_name}") 

132 return False 

133 

134 if plugin.enabled: 

135 self.console.print( 

136 f"[yellow]⚠️[/ yellow] Plugin already enabled: {plugin_name}", 

137 ) 

138 return True 

139 

140 try: 

141 plugin.enable() 

142 success = plugin.activate() 

143 

144 if success: 

145 self.console.print(f"[green]✅[/ green] Enabled plugin: {plugin_name}") 

146 

147 if plugin.plugin_type == PluginType.HOOK: 

148 self.hook_registry.register_hook_plugin(plugin) 

149 

150 return True 

151 plugin.disable() 

152 self.console.print( 

153 f"[red]❌[/ red] Failed to activate plugin: {plugin_name}", 

154 ) 

155 return False 

156 

157 except Exception as e: 

158 self.console.print( 

159 f"[red]❌[/ red] Error enabling plugin {plugin_name}: {e}", 

160 ) 

161 return False 

162 

163 def disable_plugin(self, plugin_name: str) -> bool: 

164 plugin = self.registry.get(plugin_name) 

165 if not plugin: 

166 self.console.print(f"[red]❌[/ red] Plugin not found: {plugin_name}") 

167 return False 

168 

169 if not plugin.enabled: 

170 self.console.print( 

171 f"[yellow]⚠️[/ yellow] Plugin already disabled: {plugin_name}", 

172 ) 

173 return True 

174 

175 try: 

176 success = plugin.deactivate() 

177 plugin.disable() 

178 

179 if plugin.plugin_type == PluginType.HOOK: 

180 self.hook_registry.unregister_hook_plugin(plugin_name) 

181 

182 if success: 

183 self.console.print( 

184 f"[yellow]⏹️[/ yellow] Disabled plugin: {plugin_name}" 

185 ) 

186 else: 

187 self.console.print( 

188 f"[yellow]⚠️[/ yellow] Plugin disabled with warnings: {plugin_name}", 

189 ) 

190 

191 return True 

192 

193 except Exception as e: 

194 self.console.print( 

195 f"[red]❌[/ red] Error disabling plugin {plugin_name}: {e}", 

196 ) 

197 return False 

198 

199 def reload_plugin(self, plugin_name: str) -> bool: 

200 if not self.disable_plugin(plugin_name): 

201 return False 

202 

203 return self.enable_plugin(plugin_name) 

204 

205 def configure_plugin(self, plugin_name: str, config: dict[str, t.Any]) -> bool: 

206 plugin = self.registry.get(plugin_name) 

207 if not plugin: 

208 self.console.print(f"[red]❌[/ red] Plugin not found: {plugin_name}") 

209 return False 

210 

211 try: 

212 plugin.configure(config) 

213 self.console.print(f"[green]✅[/ green] Configured plugin: {plugin_name}") 

214 return True 

215 except Exception as e: 

216 self.console.print( 

217 f"[red]❌[/ red] Error configuring plugin {plugin_name}: {e}", 

218 ) 

219 return False 

220 

221 def install_plugin_from_file(self, plugin_file: Path) -> bool: 

222 try: 

223 success = self.loader.load_and_register(plugin_file) 

224 

225 if success: 

226 self.console.print( 

227 f"[green]✅[/ green] Installed plugin from: {plugin_file}", 

228 ) 

229 

230 if self._initialized: 

231 plugins = self.registry.list_all() 

232 if plugins: 

233 latest_plugin_name = max( 

234 plugins.keys(), 

235 key=lambda k: id(plugins[k]), 

236 ) 

237 self.enable_plugin(latest_plugin_name) 

238 else: 

239 self.console.print( 

240 f"[red]❌[/ red] Failed to install plugin from: {plugin_file}", 

241 ) 

242 

243 return success 

244 

245 except Exception as e: 

246 self.console.print( 

247 f"[red]❌[/ red] Error installing plugin {plugin_file}: {e}", 

248 ) 

249 return False 

250 

251 def get_available_custom_hooks(self) -> list[str]: 

252 custom_hooks = self.hook_registry.get_all_custom_hooks() 

253 return list(custom_hooks.keys()) 

254 

255 def execute_custom_hook( 

256 self, 

257 hook_name: str, 

258 files: list[Path], 

259 options: OptionsProtocol, 

260 ) -> t.Any: 

261 return self.hook_registry.execute_custom_hook(hook_name, files, options)