GeezerVisonThe World Wide Web, restored to 800 pixels of glory. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Home|About|Gaming cabinet|Archives|How it works|Starter sites | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
GeezerVison is showing github.com — BasePlugin BasePlugin
Asterios Raptis edited this page
Called when the plugin is loaded. Receives both the global application config and the plugin-specific config from YAML. The default implementation stores both: def init(self, app_config: dict, plugin_config: dict) -> None: self.app_config = app_config self.config = plugin_config Override to perform custom initialization: def init(self, app_config: dict, plugin_config: dict) -> None: super().init(app_config, plugin_config) self.db_url = app_config.get("database", {}).get("url") self.output_dir = self.config.get("output_dir", "/tmp") Called after def activate(self) -> None: self.connection = create_connection(self.db_url) Called when the plugin is being shut down. Release resources here. def deactivate(self) -> None: if self.connection: self.connection.close() Called by def on_config_changed(self, old: dict, new: dict) -> None: new_log_level = new.get("logging", {}).get("level") if new_log_level != old.get("logging", {}).get("level"): self.logger.setLevel(new_log_level) The new Return a list of FastAPI from fastapi import APIRouter def get_routes(self) -> list: router = APIRouter() @router.get("/hello") def hello(): return {"message": self.config.get("greeting", "Hello")} return [router] Return a dict describing frontend UI components. Useful for apps with plugin-driven UIs. def get_frontend_manifest(self) -> dict | None: return { "components": ["ExportDialog", "FormatSelector"], "css": "/static/plugins/export/style.css", } Returns Return plugin health status. Override to check external dependencies (APIs, databases, etc.). def health(self) -> dict: try: self.api_client.ping() return {"status": "ok", "latency_ms": 12} except Exception as e: return {"status": "error", "error": str(e)} Returns Return the path to Alembic migration scripts. Only relevant when using Alembic integration. def get_migrations_dir(self) -> str | None: return str(Path(__file__).parent / "migrations") Plugins can declare expected config types via class ExportPlugin(BasePlugin): name = "export" config_schema = { "pandoc_path": str, "toc_depth": int, "default_format": str, } If a config value has the wrong type, the plugin is filtered with When the host declares class BibliogonPlugin(BasePlugin): name = "comics" target_application = "bibliogon" # v0.7.0
Hard-filter transition (shipped v0.9.0). Plugins without
Hosts that have not adopted For the full state of a plugin (lifecycle state, config snapshot, health, hooks, routes, identity), use inspection = pm.inspect_plugin("export") # inspection.state.activated, inspection.state.activated_at, # inspection.config, inspection.health, inspection.hooks, ... See PluginManager#inspect_plugin for the full surface. import pluggy from pluginforge import BasePlugin hookimpl = pluggy.HookimplMarker("myapp") class ExportPlugin(BasePlugin): name = "export" version = "1.0.0" target_application = "myapp" # v0.7.0: declare which host this targets min_app_version = "1.2.0" # v0.6.0: minimum host version description = "Export documents to various formats" author = "Asterios Raptis" depends_on = ["storage"] config_schema = {"formats": list, "pandoc_path": str} def init(self, app_config: dict, plugin_config: dict) -> None: super().init(app_config, plugin_config) self.formats = self.config.get("formats", ["pdf"]) def activate(self) -> None: self.engine = self._create_engine() def deactivate(self) -> None: self.engine = None def on_config_changed(self, old: dict, new: dict) -> None: # v0.6.0: react to live config changes new_formats = new.get("export", {}).get("formats") if new_formats and new_formats != old.get("export", {}).get("formats"): self.formats = new_formats def health(self) -> dict: return {"status": "ok", "formats": self.formats} @hookimpl def on_document_save(self, document: dict) -> None: for fmt in self.formats: self.engine.export(document, fmt) A plugin is both:
PluginForge manages the lifecycle around the plugin. pluggy manages the hooks. These are complementary, not competing. Clone this wiki locally | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| You are visitor 01402520 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |