Grappler Bases¶
grappler.grapplers.bases
¶
This module contains base classes for Grapplers.
BasicGrappler
provides a very barebones base to build a grappler onto. It helps
to properly implement the
Grappler.find
context managed interface.
PluginPairGrapplerBase
¶
Bases: BasicGrappler[Dict[Plugin, T_Cache]]
, ABC
, Generic[T_Cache]
A grappler base that allows to pair arbitrary data with each iterated plugin, allowing for easier loading later.
See grappler.grapplers.CompositeGrappler for example usage.
To implement this, you must implement the
iter_plugins
and
load_with_pair
methods.
iter_plugins(topic: Optional[str], exit_stack: ExitStack) -> Iterable[Tuple[Plugin, T_Cache]]
abstractmethod
¶
Return an iterator for (plugin, pair) pairs.
It a topic is given, each iterated plugin must support it. If no topic is given, the any plugin may be iterated.
The value returned for pair
will be used with
load_with_pair
Source code in grappler/grapplers/bases/_plugin_pair.py
load_with_pair(plugin: Plugin, pair: T_Cache) -> Any
abstractmethod
¶
Load a plugin and return it.
The value for pair
is passed unmodifier from
iter_plugins
Source code in grappler/grapplers/bases/_plugin_pair.py
BasicGrappler() -> None
¶
Bases: ABC
, Generic[T_ItConfig]
An abstract base class for a Grappler
To properly implement this abstract class, both
create_iteration_context()
and
load_from_context
()
must be implemented.
In return, you will receive a class that is compliant with
the Grappler
protocol, which uses context variables
to isolate iterations.
It is implemented as a generic class that allows a subclass store typed
state before iteration, and receive it later when loading plugins.
See EntryPointGrappler
source
code for an example.
Source code in grappler/grapplers/bases/_basic.py
id() -> str
property
abstractmethod
¶
create_iteration_context(topic: Optional[str], exit_stack: ExitStack) -> Tuple[Iterable[Plugin], T_ItConfig]
abstractmethod
¶
Return an iteration context for the grappler.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
topic |
Optional[str]
|
Optionally a topic to which the iterated plugins
should be limited, or |
required |
exit_stack |
ExitStack
|
A |
required |
The return value is a pair of values:
- an iterable of plugins
- a value that can store config for when
load_from_context
() is called.
Source code in grappler/grapplers/bases/_basic.py
load_from_context(plugin: Plugin, context: T_ItConfig) -> Any
abstractmethod
¶
Load a plugin and return its value.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
plugin |
Plugin
|
Plugin to be loaded, which comes from the iterator
returned from
|
required |
context |
T_ItConfig
|
Context value which was returned from
|
required |
Source code in grappler/grapplers/bases/_basic.py
cleanup_iteration_context(context: T_ItConfig) -> None
¶
Cleanup an iteration context.
This method can be used to cleanup dangling resources that
were created in
create_iteration_context()
.
The default implementation does nothing.