跳转至

插件

zoo_framework.plugin.Plugin

Bases: ABC

Plugin base class.

Every plugin must inherit from this class and implement the abstract methods.

属性:

名称 类型 描述
name str

Plugin name; must be unique.

version str

Plugin version, following semantic versioning.

description str

Plugin description.

author str

Plugin author.

dependencies list[str]

Other plugins this one depends on.

源代码位于: zoo_framework/plugin/__init__.py
class Plugin(ABC):
    """Plugin base class.

    Every plugin must inherit from this class and implement the abstract
    methods.

    Attributes:
        name: Plugin name; must be unique.
        version: Plugin version, following semantic versioning.
        description: Plugin description.
        author: Plugin author.
        dependencies: Other plugins this one depends on.
    """

    name: str = ""
    version: str = "0.1.0"
    description: str = ""
    author: str = ""
    dependencies: list[str] = []

    def __init__(self):
        """Initialize the plugin."""
        self._initialized = False
        self._context: Any | None = None

    @abstractmethod
    def initialize(self, context: Any) -> None:
        """Initialize the plugin.

        Called when the plugin is loaded.

        Args:
            context: Application context with shared resources and configuration.
        """
        pass

    @abstractmethod
    def destroy(self) -> None:
        """Destroy the plugin.

        Called when the plugin is unloaded or the application shuts down.
        Release resources here.
        """
        pass

    @property
    def is_initialized(self) -> bool:
        """Whether the plugin has been initialized."""
        return self._initialized

    def _do_initialize(self, context: Any) -> None:
        """Internal initialization."""
        if not self._initialized:
            self._context = context
            self.initialize(context)
            self._initialized = True
            logger.info(f"✅ Plugin '{self.name}' v{self.version} initialized")

    def _do_destroy(self) -> None:
        """Internal teardown."""
        if self._initialized:
            self.destroy()
            self._initialized = False
            self._context = None
            logger.info(f"🛑 Plugin '{self.name}' destroyed")

is_initialized property

is_initialized: bool

Whether the plugin has been initialized.

initialize abstractmethod

initialize(context: Any) -> None

Initialize the plugin.

Called when the plugin is loaded.

参数:

名称 类型 描述 默认
context Any

Application context with shared resources and configuration.

必需
源代码位于: zoo_framework/plugin/__init__.py
@abstractmethod
def initialize(self, context: Any) -> None:
    """Initialize the plugin.

    Called when the plugin is loaded.

    Args:
        context: Application context with shared resources and configuration.
    """
    pass

destroy abstractmethod

destroy() -> None

Destroy the plugin.

Called when the plugin is unloaded or the application shuts down. Release resources here.

源代码位于: zoo_framework/plugin/__init__.py
@abstractmethod
def destroy(self) -> None:
    """Destroy the plugin.

    Called when the plugin is unloaded or the application shuts down.
    Release resources here.
    """
    pass

zoo_framework.plugin.PluginManager

Plugin manager.

Manages the register / load / unload lifecycle of plugins.

Provides: - register: register a plugin class - load / load_all: load plugins - unload / unload_all: unload plugins

Maintains internally: - _plugins: mapping of registered plugins - _loaded_plugins: mapping of currently loaded plugin instances

源代码位于: zoo_framework/plugin/__init__.py
class PluginManager:
    """Plugin manager.

    Manages the register / load / unload lifecycle of plugins.

    Provides:
    - register: register a plugin class
    - load / load_all: load plugins
    - unload / unload_all: unload plugins

    Maintains internally:
    - _plugins: mapping of registered plugins
    - _loaded_plugins: mapping of currently loaded plugin instances
    """

    def __init__(self):
        self._plugins: dict[str, type[Plugin]] = {}
        self._loaded_plugins: dict[str, Plugin] = {}
        self._context: dict[str, Any] = {}
        self._delay_manager = WorkerDelayManager()

    @property
    def delay_manager(self) -> WorkerDelayManager:
        """The delay-time manager."""
        return self._delay_manager

    def register(self, plugin_class: type[Plugin]) -> None:
        """Register a plugin.

        Args:
            plugin_class: The plugin class; must inherit from Plugin.

        Raises:
            ValueError: The plugin class is invalid or the name already exists.
        """
        if not issubclass(plugin_class, Plugin):
            raise ValueError(f"Plugin class must inherit from Plugin: {plugin_class}")

        if not plugin_class.name:
            raise ValueError(f"Plugin must have a name: {plugin_class}")

        if plugin_class.name in self._plugins:
            logger.warning(f"Plugin '{plugin_class.name}' already registered, overwriting")

        self._plugins[plugin_class.name] = plugin_class
        logger.info(f"📦 Plugin '{plugin_class.name}' registered")

    def unregister(self, plugin_name: str) -> None:
        """Unregister a plugin.

        Args:
            plugin_name: Plugin name.
        """
        if plugin_name in self._loaded_plugins:
            self.unload(plugin_name)

        self._plugins.pop(plugin_name, None)
        logger.info(f"🗑️ Plugin '{plugin_name}' unregistered")

    def load(self, plugin_name: str, context: Any | None = None) -> None:
        """Load a single plugin.

        Args:
            plugin_name: Plugin name.
            context: Application context.

        Raises:
            KeyError: The plugin is not registered.
            RuntimeError: A dependency plugin is not loaded.
        """
        if plugin_name in self._loaded_plugins:
            logger.debug(f"Plugin '{plugin_name}' already loaded")
            return

        if plugin_name not in self._plugins:
            raise KeyError(f"Plugin not registered: {plugin_name}")

        plugin_class = self._plugins[plugin_name]

        # Check dependencies
        for dep in plugin_class.dependencies:
            if dep not in self._loaded_plugins:
                raise RuntimeError(f"Plugin '{plugin_name}' requires '{dep}' but it's not loaded")

        # Instantiate and initialize
        plugin = plugin_class()
        ctx = context or self._context
        plugin._do_initialize(ctx)

        self._loaded_plugins[plugin_name] = plugin
        logger.info(f"✅ Plugin '{plugin_name}' loaded")

    def load_all(self, context: Any | None = None) -> None:
        """Load all registered plugins.

        Plugin dependencies are resolved automatically.

        Args:
            context: Application context.
        """
        # Order by dependencies
        loaded = set(self._loaded_plugins.keys())
        to_load = set(self._plugins.keys()) - loaded

        while to_load:
            progress = False
            for name in list(to_load):
                plugin_class = self._plugins[name]
                deps = set(plugin_class.dependencies)

                if deps <= loaded:
                    self.load(name, context)
                    loaded.add(name)
                    to_load.remove(name)
                    progress = True

            if not progress and to_load:
                # Circular dependency
                raise RuntimeError(f"Circular dependency detected: {to_load}")

    def unload(self, plugin_name: str) -> None:
        """Unload a plugin.

        Args:
            plugin_name: Plugin name.
        """
        if plugin_name not in self._loaded_plugins:
            return

        # Check whether other plugins depend on this one
        for name, plugin in self._loaded_plugins.items():
            if name != plugin_name and plugin_name in self._plugins[name].dependencies:
                raise RuntimeError(f"Cannot unload '{plugin_name}', '{name}' depends on it")

        plugin = self._loaded_plugins.pop(plugin_name)
        plugin._do_destroy()
        logger.info(f"🛑 Plugin '{plugin_name}' unloaded")

    def unload_all(self) -> None:
        """Unload all plugins."""
        # Unload in reverse dependency order
        for name in list(self._loaded_plugins.keys()):
            self.unload(name)

    def get_plugin(self, plugin_name: str) -> Plugin | None:
        """Get a loaded plugin instance.

        Args:
            plugin_name: Plugin name.

        Returns:
            The plugin instance, or None if not loaded.
        """
        return self._loaded_plugins.get(plugin_name)

    def get_registered_plugins(self) -> list[str]:
        """Get all registered plugin names."""
        return list(self._plugins.keys())

    def get_loaded_plugins(self) -> list[str]:
        """Get all loaded plugin names."""
        return list(self._loaded_plugins.keys())

    def set_context(self, key: str, value: Any) -> None:
        """Set a global context value."""
        self._context[key] = value

    def get_context(self, key: str, default: Any = None) -> Any:
        """Get a global context value."""
        return self._context.get(key, default)

delay_manager property

delay_manager: WorkerDelayManager

The delay-time manager.

register

register(plugin_class: type[Plugin]) -> None

Register a plugin.

参数:

名称 类型 描述 默认
plugin_class type[Plugin]

The plugin class; must inherit from Plugin.

必需

引发:

类型 描述
ValueError

The plugin class is invalid or the name already exists.

源代码位于: zoo_framework/plugin/__init__.py
def register(self, plugin_class: type[Plugin]) -> None:
    """Register a plugin.

    Args:
        plugin_class: The plugin class; must inherit from Plugin.

    Raises:
        ValueError: The plugin class is invalid or the name already exists.
    """
    if not issubclass(plugin_class, Plugin):
        raise ValueError(f"Plugin class must inherit from Plugin: {plugin_class}")

    if not plugin_class.name:
        raise ValueError(f"Plugin must have a name: {plugin_class}")

    if plugin_class.name in self._plugins:
        logger.warning(f"Plugin '{plugin_class.name}' already registered, overwriting")

    self._plugins[plugin_class.name] = plugin_class
    logger.info(f"📦 Plugin '{plugin_class.name}' registered")

unregister

unregister(plugin_name: str) -> None

Unregister a plugin.

参数:

名称 类型 描述 默认
plugin_name str

Plugin name.

必需
源代码位于: zoo_framework/plugin/__init__.py
def unregister(self, plugin_name: str) -> None:
    """Unregister a plugin.

    Args:
        plugin_name: Plugin name.
    """
    if plugin_name in self._loaded_plugins:
        self.unload(plugin_name)

    self._plugins.pop(plugin_name, None)
    logger.info(f"🗑️ Plugin '{plugin_name}' unregistered")

load

load(plugin_name: str, context: Any | None = None) -> None

Load a single plugin.

参数:

名称 类型 描述 默认
plugin_name str

Plugin name.

必需
context Any | None

Application context.

None

引发:

类型 描述
KeyError

The plugin is not registered.

RuntimeError

A dependency plugin is not loaded.

源代码位于: zoo_framework/plugin/__init__.py
def load(self, plugin_name: str, context: Any | None = None) -> None:
    """Load a single plugin.

    Args:
        plugin_name: Plugin name.
        context: Application context.

    Raises:
        KeyError: The plugin is not registered.
        RuntimeError: A dependency plugin is not loaded.
    """
    if plugin_name in self._loaded_plugins:
        logger.debug(f"Plugin '{plugin_name}' already loaded")
        return

    if plugin_name not in self._plugins:
        raise KeyError(f"Plugin not registered: {plugin_name}")

    plugin_class = self._plugins[plugin_name]

    # Check dependencies
    for dep in plugin_class.dependencies:
        if dep not in self._loaded_plugins:
            raise RuntimeError(f"Plugin '{plugin_name}' requires '{dep}' but it's not loaded")

    # Instantiate and initialize
    plugin = plugin_class()
    ctx = context or self._context
    plugin._do_initialize(ctx)

    self._loaded_plugins[plugin_name] = plugin
    logger.info(f"✅ Plugin '{plugin_name}' loaded")

load_all

load_all(context: Any | None = None) -> None

Load all registered plugins.

Plugin dependencies are resolved automatically.

参数:

名称 类型 描述 默认
context Any | None

Application context.

None
源代码位于: zoo_framework/plugin/__init__.py
def load_all(self, context: Any | None = None) -> None:
    """Load all registered plugins.

    Plugin dependencies are resolved automatically.

    Args:
        context: Application context.
    """
    # Order by dependencies
    loaded = set(self._loaded_plugins.keys())
    to_load = set(self._plugins.keys()) - loaded

    while to_load:
        progress = False
        for name in list(to_load):
            plugin_class = self._plugins[name]
            deps = set(plugin_class.dependencies)

            if deps <= loaded:
                self.load(name, context)
                loaded.add(name)
                to_load.remove(name)
                progress = True

        if not progress and to_load:
            # Circular dependency
            raise RuntimeError(f"Circular dependency detected: {to_load}")

unload

unload(plugin_name: str) -> None

Unload a plugin.

参数:

名称 类型 描述 默认
plugin_name str

Plugin name.

必需
源代码位于: zoo_framework/plugin/__init__.py
def unload(self, plugin_name: str) -> None:
    """Unload a plugin.

    Args:
        plugin_name: Plugin name.
    """
    if plugin_name not in self._loaded_plugins:
        return

    # Check whether other plugins depend on this one
    for name, plugin in self._loaded_plugins.items():
        if name != plugin_name and plugin_name in self._plugins[name].dependencies:
            raise RuntimeError(f"Cannot unload '{plugin_name}', '{name}' depends on it")

    plugin = self._loaded_plugins.pop(plugin_name)
    plugin._do_destroy()
    logger.info(f"🛑 Plugin '{plugin_name}' unloaded")

unload_all

unload_all() -> None

Unload all plugins.

源代码位于: zoo_framework/plugin/__init__.py
def unload_all(self) -> None:
    """Unload all plugins."""
    # Unload in reverse dependency order
    for name in list(self._loaded_plugins.keys()):
        self.unload(name)

get_plugin

get_plugin(plugin_name: str) -> Plugin | None

Get a loaded plugin instance.

参数:

名称 类型 描述 默认
plugin_name str

Plugin name.

必需

返回:

类型 描述
Plugin | None

The plugin instance, or None if not loaded.

源代码位于: zoo_framework/plugin/__init__.py
def get_plugin(self, plugin_name: str) -> Plugin | None:
    """Get a loaded plugin instance.

    Args:
        plugin_name: Plugin name.

    Returns:
        The plugin instance, or None if not loaded.
    """
    return self._loaded_plugins.get(plugin_name)

get_registered_plugins

get_registered_plugins() -> list[str]

Get all registered plugin names.

源代码位于: zoo_framework/plugin/__init__.py
def get_registered_plugins(self) -> list[str]:
    """Get all registered plugin names."""
    return list(self._plugins.keys())

get_loaded_plugins

get_loaded_plugins() -> list[str]

Get all loaded plugin names.

源代码位于: zoo_framework/plugin/__init__.py
def get_loaded_plugins(self) -> list[str]:
    """Get all loaded plugin names."""
    return list(self._loaded_plugins.keys())

set_context

set_context(key: str, value: Any) -> None

Set a global context value.

源代码位于: zoo_framework/plugin/__init__.py
def set_context(self, key: str, value: Any) -> None:
    """Set a global context value."""
    self._context[key] = value

get_context

get_context(key: str, default: Any = None) -> Any

Get a global context value.

源代码位于: zoo_framework/plugin/__init__.py
def get_context(self, key: str, default: Any = None) -> Any:
    """Get a global context value."""
    return self._context.get(key, default)

zoo_framework.plugin.WorkerDelayManager

Worker delay-time manager.

Controls a Worker's delayed execution via a delay-strategy object. Supports fixed delay, exponential backoff, adaptive delay, and other strategies.

属性:

名称 类型 描述
default_delay

Default delay in seconds.

max_delay

Maximum delay in seconds.

min_delay

Minimum delay in seconds.

源代码位于: zoo_framework/plugin/__init__.py
class WorkerDelayManager:
    """Worker delay-time manager.

    Controls a Worker's delayed execution via a delay-strategy object. Supports
    fixed delay, exponential backoff, adaptive delay, and other strategies.

    Attributes:
        default_delay: Default delay in seconds.
        max_delay: Maximum delay in seconds.
        min_delay: Minimum delay in seconds.
    """

    def __init__(
        self, default_delay: float = 1.0, max_delay: float = 60.0, min_delay: float = 0.01
    ):
        self.default_delay = default_delay
        self.max_delay = max_delay
        self.min_delay = min_delay
        self._delays: dict[str, float] = {}
        self._last_execute_time: dict[str, float] = {}
        self._execute_count: dict[str, int] = {}

    def get_delay(self, worker_name: str) -> float:
        """Get a Worker's delay.

        Args:
            worker_name: Worker name.

        Returns:
            The delay in seconds.
        """
        return self._delays.get(worker_name, self.default_delay)

    def set_delay(self, worker_name: str, delay: float) -> None:
        """Set a Worker's delay.

        Args:
            worker_name: Worker name.
            delay: The delay in seconds.
        """
        self._delays[worker_name] = max(self.min_delay, min(delay, self.max_delay))

    def record_execute(self, worker_name: str) -> None:
        """Record a Worker execution.

        Args:
            worker_name: Worker name.
        """
        import time

        self._last_execute_time[worker_name] = time.time()
        self._execute_count[worker_name] = self._execute_count.get(worker_name, 0) + 1

    def exponential_backoff(
        self, worker_name: str, base_delay: float = 1.0, max_retries: int = 5
    ) -> float:
        """Exponential backoff delay.

        When a Worker's execution fails, grow the delay exponentially.

        Args:
            worker_name: Worker name.
            base_delay: Base delay.
            max_retries: Maximum retry count.

        Returns:
            The computed delay.
        """
        retry_count = self._execute_count.get(worker_name, 0)
        if retry_count > max_retries:
            retry_count = max_retries

        # Explicit annotation: mypy infers `base_delay * (2**retry_count)` as `Any`
        # (located via reveal_type: the operands are float and int, only the
        # product falls to Any), so returning it from a function declared to
        # return float trips no-any-return. `delay: float` is arithmetically
        # true - this supplies the type to the checker rather than suppressing
        # the error.
        delay: float = base_delay * (2**retry_count)
        return min(delay, self.max_delay)

    def adaptive_delay(
        self, worker_name: str, execution_time: float, target_utilization: float = 0.8
    ) -> float:
        """Adaptive delay.

        Adjusts the delay based on the Worker's last execution time to approach
        the target CPU utilization.

        Args:
            worker_name: Worker name.
            execution_time: Duration of the last execution.
            target_utilization: Target CPU utilization.

        Returns:
            The adjusted delay.
        """
        if execution_time <= 0:
            return self.default_delay

        # Ideal delay for the target utilization
        ideal_delay = execution_time * (1 / target_utilization - 1)

        # Smooth adjustment
        current_delay = self.get_delay(worker_name)
        new_delay = (current_delay * 0.7) + (ideal_delay * 0.3)

        self.set_delay(worker_name, new_delay)
        return new_delay

    def reset(self, worker_name: str) -> None:
        """Reset a Worker's delay settings.

        Args:
            worker_name: Worker name.
        """
        self._delays.pop(worker_name, None)
        self._last_execute_time.pop(worker_name, None)
        self._execute_count.pop(worker_name, None)

get_delay

get_delay(worker_name: str) -> float

Get a Worker's delay.

参数:

名称 类型 描述 默认
worker_name str

Worker name.

必需

返回:

类型 描述
float

The delay in seconds.

源代码位于: zoo_framework/plugin/__init__.py
def get_delay(self, worker_name: str) -> float:
    """Get a Worker's delay.

    Args:
        worker_name: Worker name.

    Returns:
        The delay in seconds.
    """
    return self._delays.get(worker_name, self.default_delay)

set_delay

set_delay(worker_name: str, delay: float) -> None

Set a Worker's delay.

参数:

名称 类型 描述 默认
worker_name str

Worker name.

必需
delay float

The delay in seconds.

必需
源代码位于: zoo_framework/plugin/__init__.py
def set_delay(self, worker_name: str, delay: float) -> None:
    """Set a Worker's delay.

    Args:
        worker_name: Worker name.
        delay: The delay in seconds.
    """
    self._delays[worker_name] = max(self.min_delay, min(delay, self.max_delay))

record_execute

record_execute(worker_name: str) -> None

Record a Worker execution.

参数:

名称 类型 描述 默认
worker_name str

Worker name.

必需
源代码位于: zoo_framework/plugin/__init__.py
def record_execute(self, worker_name: str) -> None:
    """Record a Worker execution.

    Args:
        worker_name: Worker name.
    """
    import time

    self._last_execute_time[worker_name] = time.time()
    self._execute_count[worker_name] = self._execute_count.get(worker_name, 0) + 1

exponential_backoff

exponential_backoff(worker_name: str, base_delay: float = 1.0, max_retries: int = 5) -> float

Exponential backoff delay.

When a Worker's execution fails, grow the delay exponentially.

参数:

名称 类型 描述 默认
worker_name str

Worker name.

必需
base_delay float

Base delay.

1.0
max_retries int

Maximum retry count.

5

返回:

类型 描述
float

The computed delay.

源代码位于: zoo_framework/plugin/__init__.py
def exponential_backoff(
    self, worker_name: str, base_delay: float = 1.0, max_retries: int = 5
) -> float:
    """Exponential backoff delay.

    When a Worker's execution fails, grow the delay exponentially.

    Args:
        worker_name: Worker name.
        base_delay: Base delay.
        max_retries: Maximum retry count.

    Returns:
        The computed delay.
    """
    retry_count = self._execute_count.get(worker_name, 0)
    if retry_count > max_retries:
        retry_count = max_retries

    # Explicit annotation: mypy infers `base_delay * (2**retry_count)` as `Any`
    # (located via reveal_type: the operands are float and int, only the
    # product falls to Any), so returning it from a function declared to
    # return float trips no-any-return. `delay: float` is arithmetically
    # true - this supplies the type to the checker rather than suppressing
    # the error.
    delay: float = base_delay * (2**retry_count)
    return min(delay, self.max_delay)

adaptive_delay

adaptive_delay(worker_name: str, execution_time: float, target_utilization: float = 0.8) -> float

Adaptive delay.

Adjusts the delay based on the Worker's last execution time to approach the target CPU utilization.

参数:

名称 类型 描述 默认
worker_name str

Worker name.

必需
execution_time float

Duration of the last execution.

必需
target_utilization float

Target CPU utilization.

0.8

返回:

类型 描述
float

The adjusted delay.

源代码位于: zoo_framework/plugin/__init__.py
def adaptive_delay(
    self, worker_name: str, execution_time: float, target_utilization: float = 0.8
) -> float:
    """Adaptive delay.

    Adjusts the delay based on the Worker's last execution time to approach
    the target CPU utilization.

    Args:
        worker_name: Worker name.
        execution_time: Duration of the last execution.
        target_utilization: Target CPU utilization.

    Returns:
        The adjusted delay.
    """
    if execution_time <= 0:
        return self.default_delay

    # Ideal delay for the target utilization
    ideal_delay = execution_time * (1 / target_utilization - 1)

    # Smooth adjustment
    current_delay = self.get_delay(worker_name)
    new_delay = (current_delay * 0.7) + (ideal_delay * 0.3)

    self.set_delay(worker_name, new_delay)
    return new_delay

reset

reset(worker_name: str) -> None

Reset a Worker's delay settings.

参数:

名称 类型 描述 默认
worker_name str

Worker name.

必需
源代码位于: zoo_framework/plugin/__init__.py
def reset(self, worker_name: str) -> None:
    """Reset a Worker's delay settings.

    Args:
        worker_name: Worker name.
    """
    self._delays.pop(worker_name, None)
    self._last_execute_time.pop(worker_name, None)
    self._execute_count.pop(worker_name, None)

zoo_framework.plugin.register_plugin

register_plugin(plugin_class: type[Plugin]) -> None

Convenience wrapper: register a plugin with the global manager.

源代码位于: zoo_framework/plugin/__init__.py
def register_plugin(plugin_class: type[Plugin]) -> None:
    """Convenience wrapper: register a plugin with the global manager."""
    get_plugin_manager().register(plugin_class)

zoo_framework.plugin.get_plugin_manager

get_plugin_manager() -> PluginManager

Get the global plugin manager instance.

源代码位于: zoo_framework/plugin/__init__.py
def get_plugin_manager() -> PluginManager:
    """Get the global plugin manager instance."""
    global _plugin_manager
    if _plugin_manager is None:
        _plugin_manager = PluginManager()
    return _plugin_manager

zoo_framework.plugin.load_plugins

load_plugins(context: Any | None = None) -> None

Convenience wrapper: load all registered plugins.

源代码位于: zoo_framework/plugin/__init__.py
def load_plugins(context: Any | None = None) -> None:
    """Convenience wrapper: load all registered plugins."""
    get_plugin_manager().load_all(context)