|
| 1 | +"""Plugin system for heliosHarness extensions. |
| 2 | +
|
| 3 | +Defines the plugin protocol and extension points for modular code. |
| 4 | +""" |
| 5 | + |
| 6 | +from typing import Protocol, runtime_checkable |
| 7 | +from dataclasses import dataclass |
| 8 | +from enum import Enum |
| 9 | +from abc import ABC, abstractmethod |
| 10 | +from pathlib import Path |
| 11 | + |
| 12 | + |
| 13 | +class PluginType(Enum): |
| 14 | + """Types of plugins.""" |
| 15 | + HELIOS_CLI_EXTENSION = "helioscli_extension" |
| 16 | + HARBOR_PLUGIN = "harbor_plugin" |
| 17 | + PORTAGE_MODULE = "portage_module" |
| 18 | + ADAPTER = "adapter" |
| 19 | + TEMPLATE = "template" |
| 20 | + |
| 21 | + |
| 22 | +@dataclass |
| 23 | +class PluginMetadata: |
| 24 | + """Plugin metadata.""" |
| 25 | + name: str |
| 26 | + version: str |
| 27 | + plugin_type: PluginType |
| 28 | + source_project: str |
| 29 | + description: str = "" |
| 30 | + author: str = "" |
| 31 | + tags: list[str] = None |
| 32 | + |
| 33 | + def __post_init__(self): |
| 34 | + if self.tags is None: |
| 35 | + self.tags = [] |
| 36 | + |
| 37 | + |
| 38 | +@dataclass |
| 39 | +class PluginConfig: |
| 40 | + """Plugin configuration.""" |
| 41 | + enabled: bool = True |
| 42 | + priority: int = 100 |
| 43 | + settings: dict = None |
| 44 | + |
| 45 | + def __post_init__(self): |
| 46 | + if self.settings is None: |
| 47 | + self.settings = {} |
| 48 | + |
| 49 | + |
| 50 | +@runtime_checkable |
| 51 | +class Plugin(Protocol): |
| 52 | + """Base plugin protocol.""" |
| 53 | + |
| 54 | + @property |
| 55 | + def metadata(self) -> PluginMetadata: |
| 56 | + """Plugin metadata.""" |
| 57 | + ... |
| 58 | + |
| 59 | + def initialize(self, config: dict) -> None: |
| 60 | + """Initialize the plugin with configuration.""" |
| 61 | + ... |
| 62 | + |
| 63 | + def execute(self, *args, **kwargs): |
| 64 | + """Execute the plugin.""" |
| 65 | + ... |
| 66 | + |
| 67 | + def shutdown(self) -> None: |
| 68 | + """Cleanup resources.""" |
| 69 | + ... |
| 70 | + |
| 71 | + |
| 72 | + def validate(self) -> bool: |
| 73 | + """Validate plugin setup.""" |
| 74 | + return True |
| 75 | + |
| 76 | + |
| 77 | +class PluginLoader: |
| 78 | + """Loads and manages plugins.""" |
| 79 | + |
| 80 | + def __init__(self, plugin_dir: Path = None): |
| 81 | + self.plugin_dir = plugin_dir or Path("plugins") |
| 82 | + self._plugins: dict[str, Plugin] = {} |
| 83 | + self._metadata: dict[str, PluginMetadata] = {} |
| 84 | + |
| 85 | + def discover(self) -> list[PluginMetadata]: |
| 86 | + """Discover available plugins.""" |
| 87 | + discovered = [] |
| 88 | + |
| 89 | + if not self.plugin_dir.exists(): |
| 90 | + return discovered |
| 91 | + |
| 92 | + for plugin_path in self.plugin_dir.rglob("plugin.yaml"): |
| 93 | + with open(plugin_path) as f: |
| 94 | + # Parse YAML and create metadata |
| 95 | + pass |
| 96 | + |
| 97 | + return discovered |
| 98 | + |
| 99 | + def load(self, name: str) -> Plugin: |
| 100 | + """Load a plugin by name.""" |
| 101 | + if name in self._plugins: |
| 102 | + return self._plugins[name] |
| 103 | + |
| 104 | + # Dynamic import |
| 105 | + module = __import__(f"plugins.{name}", fromlist=["Plugin"]) |
| 106 | + plugin = module.Plugin() |
| 107 | + |
| 108 | + self._plugins[name] = plugin |
| 109 | + return plugin |
| 110 | + |
| 111 | + def unload(self, name: str) -> None: |
| 112 | + """Unload a plugin.""" |
| 113 | + if name in self._plugins: |
| 114 | + self._plugins[name].shutdown() |
| 115 | + del self._plugins[name] |
| 116 | + |
| 117 | + |
| 118 | +class ExtensionRegistry: |
| 119 | + """Registry for extension points.""" |
| 120 | + |
| 121 | + def __init__(self): |
| 122 | + self._extensions: dict[str, list[type] = {} |
| 123 | + |
| 124 | + def register(self, extension_point: str, extension: type): |
| 125 | + """Register an extension for a point.""" |
| 126 | + if extension_point not in self._extensions: |
| 127 | + self._extensions[extension_point] = [] |
| 128 | + self._extensions[extension_point].append(extension) |
| 129 | + |
| 130 | + def get_extensions(self, extension_point: str) -> list[type]: |
| 131 | + """Get all extensions for a point.""" |
| 132 | + return self._extensions.get(extension_point, []) |
| 133 | + |
| 134 | + |
| 135 | +# Extension points |
| 136 | +EXTENSION_POINTS = { |
| 137 | + "codex_command": "Codex CLI command extension", |
| 138 | + "harbor_adapter": "Harbor system adapter", |
| 139 | + "portage_module": "Portage module extension", |
| 140 | + "template_renderer": "Template renderer extension", |
| 141 | +} |
0 commit comments