Source code for sfmapi.runtime
"""Runtime hooks for embedding sfmapi with backend plugins."""
from __future__ import annotations
from collections.abc import Callable
from typing import TYPE_CHECKING, Any, Protocol
if TYPE_CHECKING:
from fastapi import FastAPI
from app.adapters.backend import Backend
from app.adapters.registry import get_backend, list_backends, register_backend
from sfm_hub.discovery import load_backend_entry_points
BackendFactory = Callable[[], Backend]
class BackendPlugin(Protocol):
"""Plugin object shape accepted by :func:`register_plugin`."""
def register(self, register_backend: Callable[[str, BackendFactory], None]) -> None: ...
[docs]
def register_plugin(plugin: BackendPlugin) -> None:
"""Register one plugin object with the process-local backend registry."""
plugin.register(register_backend)
[docs]
def load_installed_plugins() -> list[Any]:
"""Load installed backend entry points into the process-local registry."""
return load_backend_entry_points(register_backend)
[docs]
def create_app() -> FastAPI:
"""Create the FastAPI app without side effects at ``sfmapi.runtime`` import time."""
from app.main import create_app as _create_app
return _create_app()
__all__ = [
"BackendFactory",
"create_app",
"get_backend",
"list_backends",
"load_installed_plugins",
"register_backend",
"register_plugin",
]