README
liblaf.cherries.core.plugin
¶
Type Aliases:
-
MethodName– -
PluginName–
Classes:
-
ImplInfo–Ordering metadata attached to a plugin hook implementation.
-
Plugin–Base class for Cherries plugins.
-
PluginManager–Register plugins and delegate hook calls in dependency order.
Functions:
-
get_impl_info–Return hook metadata previously attached with
impl. -
impl–Mark a method as a plugin hook implementation.
ImplInfo
¶
Ordering metadata attached to a plugin hook implementation.
Parameters:
-
after(Iterable[PluginName], default:()) –Plugin names that should run before this implementation.
-
before(Iterable[PluginName], default:()) –Plugin names that should run after this implementation.
Attributes:
-
after(Iterable[PluginName]) –Plugin names that should run before this implementation.
-
before(Iterable[PluginName]) –Plugin names that should run after this implementation.
after
class-attribute
instance-attribute
¶
after: Iterable[PluginName] = ()
Plugin names that should run before this implementation.
before
class-attribute
instance-attribute
¶
before: Iterable[PluginName] = ()
Plugin names that should run after this implementation.
Plugin
¶
Base class for Cherries plugins.
Attributes:
Parameters:
-
name(PluginName, default:<dynamic>) –
name
class-attribute
instance-attribute
¶
name: PluginName = field(
default=Factory(_default_name, takes_self=True),
kw_only=True,
)
PluginManager
¶
Register plugins and delegate hook calls in dependency order.
Only methods decorated with impl are invoked.
Hook order is cached per method and recalculated whenever a plugin is
registered.
Parameters:
-
registry(dict[PluginName, Plugin], default:<class 'dict'>) –dict() -> new empty dictionary dict(mapping) -> new dictionary initialized from a mapping object's (key, value) pairs dict(iterable) -> new dictionary initialized as if via: d = {} for k, v in iterable: d[k] = v dict(**kwargs) -> new dictionary initialized with the name=value pairs in the keyword argument list. For example: dict(one=1, two=2)
Methods:
-
__getattr__–Return a callable that delegates hook
name. -
delegate–Call hook
methodon every plugin that implements it. -
register–Register or replace
plugin.
Attributes:
-
registry(dict[PluginName, Plugin]) –Plugins keyed by their unique plugin name.
registry
class-attribute
instance-attribute
¶
registry: dict[PluginName, Plugin] = field(
factory=dict, kw_only=True
)
Plugins keyed by their unique plugin name.
__getattr__
¶
Return a callable that delegates hook name.
This allows plugin delegates to satisfy protocol methods such as
log_metric() without defining each hook explicitly.
Source code in src/liblaf/cherries/core/plugin/_manager.py
delegate
¶
delegate(method: MethodName, *args, **kwargs) -> Any
Call hook method on every plugin that implements it.
Hook methods must be decorated with impl.
Exceptions are logged and later plugins still run.
Source code in src/liblaf/cherries/core/plugin/_manager.py
impl
¶
impl[F: Callable[..., Any]](
func: F,
/,
*,
after: Iterable[PluginName] = (),
before: Iterable[PluginName] = (),
) -> F
impl[F: Callable[..., Any]](
*,
after: Iterable[PluginName] = (),
before: Iterable[PluginName] = (),
) -> Callable[[F], F]
Mark a method as a plugin hook implementation.
Parameters:
-
func(Callable[..., Any] | None, default:None) –Method being decorated.
-
**kwargs(Any, default:{}) –Ordering metadata accepted by [
ImplInfo][liblaf.cherries.core.ImplInfo].
Examples:
>>> @impl(before=("Comet",))
... def log_metric():
... return None
>>> get_impl_info(log_metric).before
('Comet',)