"""
The Plugin API.
Plugins allow third-party code (e.g. your own Python package) to add functionality
to Betty.
Read more at :doc:`/development/plugin`.
"""
from __future__ import annotations
from abc import ABC, abstractmethod
from typing import TypeVar, Generic, Self, overload, TYPE_CHECKING, TypeAlias
from betty.error import UserFacingError
from betty.locale.localizable import _
from betty.machine_name import MachineName
if TYPE_CHECKING:
from betty.locale.localizable import Localizable
from collections.abc import AsyncIterator, Sequence
[docs]
class PluginError(UserFacingError):
"""
Any error originating from the Plugin API.
"""
pass
[docs]
class Plugin(ABC):
"""
A plugin.
Plugins are identified by their :py:meth:`IDs <betty.plugin.Plugin.plugin_id>` as well as their types.
Each must be able to uniquely identify the plugin within a plugin repository.
To test your own subclasses, use :py:class:`betty.test_utils.plugin.PluginTestBase`.
"""
[docs]
@classmethod
@abstractmethod
def plugin_id(cls) -> MachineName:
"""
Get the plugin ID.
IDs are unique per plugin type:
- A plugin repository **MUST** at most have a single plugin for any ID.
- Different plugin repositories **MAY** each have a plugin with the same ID.
"""
pass
[docs]
@classmethod
@abstractmethod
def plugin_label(cls) -> Localizable:
"""
Get the human-readable short plugin label.
"""
pass
[docs]
@classmethod
def plugin_description(cls) -> Localizable | None:
"""
Get the human-readable long plugin description.
"""
return None
[docs]
class PluginRepository(Generic[_PluginT], ABC):
"""
Discover and manage plugins.
"""
[docs]
@abstractmethod
async def get(self, plugin_id: MachineName) -> type[_PluginT]:
"""
Get a single plugin by its ID.
:raises PluginNotFound: if no plugin can be found for the given ID.
"""
pass
[docs]
async def select(
self,
mixin_one: type[_PluginMixinOneT] | None = None,
mixin_two: type[_PluginMixinTwoT] | None = None,
mixin_three: type[_PluginMixinThreeT] | None = None,
) -> (
Sequence[type[_PluginT]]
| Sequence[type[_PluginT & _PluginMixinOneT]]
| Sequence[type[_PluginT & _PluginMixinOneT & _PluginMixinTwoT]]
| Sequence[
type[_PluginT & _PluginMixinOneT & _PluginMixinTwoT & _PluginMixinThreeT]
]
):
"""
Select a subset of the known plugins.
When called without arguments, this returns all known plugins.
When called with one or more arguments, this returns all known plugins
that are also subclasses or **all** of the given mixins.
This method is overloaded to provide for the majority use case of at most
three mixins, because when using ``*args: *Ts``, we cannot unpack ``Ts`` into an
:py:class:`basedtyping.Intersection` return type.
"""
return [
plugin
async for plugin in self
if self._select_plugin(plugin, mixin_one, mixin_two, mixin_three)
]
def _select_plugin(
self,
plugin: type[_PluginT],
mixin_one: type[_PluginMixinOneT] | None = None,
mixin_two: type[_PluginMixinTwoT] | None = None,
mixin_three: type[_PluginMixinThreeT] | None = None,
) -> bool:
for mixin in (mixin_one, mixin_two, mixin_three):
if mixin is None:
continue
if not issubclass(plugin, mixin):
return False
return True
@abstractmethod
def __aiter__(self) -> AsyncIterator[type[_PluginT]]:
pass