Lab 13: Plugin Framework
Overview
Prerequisites
pip install pluggyStep 1: pluggy Basics — HookSpec and HookImpl
pluggy Basics — HookSpec and HookImplimport pluggy
hookspec = pluggy.HookspecMarker('myapp')
hookimpl = pluggy.HookimplMarker('myapp')
class MySpec:
@hookspec
def process_request(self, request: dict) -> dict:
"""Process a request. Called for every registered plugin."""
@hookspec(firstresult=True)
def authenticate(self, token: str) -> dict | None:
"""Authenticate a token. Returns first non-None result."""
@hookspec
def on_startup(self) -> None:
"""Called when the application starts."""
print("Hook spec defined with 3 hooks.")
print(" process_request: all plugins called, results collected")
print(" authenticate: firstresult — stops at first non-None")
print(" on_startup: all plugins called")Step 2: Implementing Plugins
Step 3: Plugin Manager Setup and Hook Calls
Step 4: Processing Requests Through Plugin Chain
Step 5: Plugin Discovery via __init_subclass__
__init_subclass__Step 6: Plugin Versioning and Dependency Management
Step 7: Plugin Sandboxing for Test Isolation
Step 8: Capstone — Complete Plugin Framework
Summary
Concept
API
Use Case
Last updated
