36 lines
946 B
Python
36 lines
946 B
Python
|
|
from __future__ import annotations
|
||
|
|
|
||
|
|
import logging
|
||
|
|
|
||
|
|
logger = logging.getLogger(__name__)
|
||
|
|
|
||
|
|
|
||
|
|
class PluginRegistrationApi:
|
||
|
|
def __init__(self, plugin_id: str, config: dict | None = None):
|
||
|
|
self._plugin_id = plugin_id
|
||
|
|
self._config = config or {}
|
||
|
|
self._closed = False
|
||
|
|
|
||
|
|
@property
|
||
|
|
def is_closed(self) -> bool:
|
||
|
|
return self._closed
|
||
|
|
|
||
|
|
def close(self) -> None:
|
||
|
|
self._closed = True
|
||
|
|
|
||
|
|
@property
|
||
|
|
def config(self) -> dict:
|
||
|
|
if self._closed:
|
||
|
|
raise RuntimeError(f"registration API closed for plugin: {self._plugin_id}")
|
||
|
|
return self._config
|
||
|
|
|
||
|
|
|
||
|
|
class ApiGuard:
|
||
|
|
def create(
|
||
|
|
self, plugin_id: str, config: dict | None = None, logger_: logging.Logger | None = None
|
||
|
|
) -> PluginRegistrationApi:
|
||
|
|
api = PluginRegistrationApi(plugin_id, config)
|
||
|
|
if logger_:
|
||
|
|
logger_.debug("created registration API for plugin: %s", plugin_id)
|
||
|
|
return api
|