Reference¶
Generic Python class loader for robust plugin infrastructure.
Loader delivers plugins from one or many plugin packages.
Loader¶
Loader is the dict-like singleton providing the following services:
- plugin initialization and fetching.
- plugins enumeration.
Plugins are not dependent on the loader and do not need any registration process. The loaders are lazy by nature, meaning the plugin will be imported and initialized just in time when the user code requests the plugin.
Plugins¶
Plugins are named entities dedicated to the given task. Each plugin is defined in its python module. Depending on the loader settings plugins can be:
- Instances: Singleton instances having the class as the ancestor.
- Subclasses: Classes having the common ancestor.
- Protocols: Classes following the set of methods.
Plugin Packages¶
Plugin packages are plain Python packages: the directory containing
python files with plugins and the empty __init__.py file.
Plugin name must match the module name. For example, module
my_plugin.py will define the plugin my_plugin.
Import Path Resolver¶
ImportPathResolver resolves a dotted import string ("package.module.attr")
into the actual Python object using importlib. It is useful for resolving
handlers, classes, or any other symbols from configuration at runtime.
Example
Resolve a callable by path:
resolver = ImportPathResolver[Callable]()
func = resolver("myapp.handlers.main")
func()
Pass a resolved object directly — resolver returns it unchanged:
resolver(resolved_handler) # returned as-is
Failed lookups may be cached to avoid repeated import overhead.
Attributes:
| Name | Type | Description |
|---|---|---|
__version__ |
str
|
Current version |
ImportPathResolver
¶
Bases: Generic[T]
Resolve Python objects by import path.
This resolver converts dot-separated import paths into Python objects using importlib, with optional caching and support for already resolved values.
Both successful and (optionally) failed resolutions are cached:
- Successful imports cache the resolved object.
- Failed attribute lookups may be cached as negative results
(controlled by cache_negative) to avoid repeated import attempts
for missing symbols.
Typical use case is dynamic resolution of callables, handlers, classes, or other runtime-imported symbols in configuration-driven systems.
Example
resolver = ImportPathResolverCallable func = resolver("package.module.func") func()
Notes
This resolver is idempotent for string inputs.
Negative caching is optional and controlled by cache_negative.
When enabled, missing attributes are cached to prevent repeated
import attempts for unresolved symbols.
__call__(path)
¶
Resolve item.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str | T
|
Dot-separated import path (e.g. "package.module.attr") or already resolved object. If value is not a string, it is returned as-is. |
required |
Returns:
| Type | Description |
|---|---|
T
|
Resolved object of type T. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the provided import path is malformed or empty. |
ImportError
|
If module cannot be imported or attribute is missing. |
Loader
¶
Bases: Generic[T]
Generic loader. Used as singleton instantiated from generic.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
base
|
str | None
|
Plugins package name. |
None
|
bases
|
Iterable[str] | None
|
Iterable of plugin package names. |
None
|
attribute
|
str | None
|
Name of the module attribute to load. If specified, only this attribute is considered. |
None
|
strict
|
bool
|
Ignore missed plugin packages if set to False, Fail otherwise. |
False
|
exclude
|
Iterable[str] | None
|
Iterable of names to be excluded from plugins lists. |
None
|
module_policy
|
ModulePolicy
|
Policy for matching members to their origin module.
Defaults to |
STRICT
|
Note
base and bases parameters are mutually exclusive.
Either base or bases must be provided.
The public surface (get(), __getitem__, keys(), exclude)
operates in key space: public keys passed by the user are mapped
to module names with encode_key() and back with decode_key().
The internal state (item cache, module discovery) works in module
space. Both hooks are identity by default, so public keys and
module names coincide.
__getitem__(name)
¶
Get plugin by name.
Returns plugin item depending on generic type.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Name of plugin. |
required |
Returns:
| Type | Description |
|---|---|
T
|
Plugin item depending on generic type. |
Raises:
| Type | Description |
|---|---|
KeyError
|
if plugin is missed. |
Note
name is a public key. It is mapped to a module name
with encode_key() before the lookup.
__iter__()
¶
decode_key(name)
¶
Map a module name to a public key.
Reverse mapping of encode_key(), used by keys() to report
public keys instead of module names. By default this method is
an identity, so keys() reports module names.
If decode_key() is not overridden (identity) or is overridden
irreversibly, keys(), values() and items() operate with
module names as public keys, which is acceptable as long as
encode_key() is idempotent on module names.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Module name. |
required |
Returns:
| Type | Description |
|---|---|
str
|
Public key. |
encode_key(name)
¶
Map a public key to a module name.
Public keys are the names users pass to get() and __getitem__().
Module names are the names of python modules in plugin packages.
By default they coincide, so this method is an identity.
Override this method in subclasses to support public keys that differ from module names, e.g. dot-separated logical names ("Cisco.IOS") mapped to flat module names ("cisco__ios").
Contract:
get()and__getitem__()call this method exactly once, before the cache lookup. Everything downstream (item cache, module import) works in module space.- The item cache is indexed by module names. Two distinct public keys encoding to the same module name correctly share the same cache slot and return the same item.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Public key. |
required |
Returns:
| Type | Description |
|---|---|
str
|
Module name. |
get(name, default=None)
¶
Get plugin by name.
Return default value if plugin is missed.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Name of plugin. |
required |
default
|
T | None
|
Default value, if plugin is missed. |
None
|
Returns:
| Type | Description |
|---|---|
T | None
|
Plugin item if found, otherwise the explicit default value. |
T | None
|
If default is omitted, returns None when plugin is missing. |
Note
name is a public key. It is mapped to a module name
with encode_key() before the lookup.
items()
¶
Iterate the (name, item) tuples for all plugin items.
Return
Iterable of tuples of (name, item)
Note
items() will force plugin module loading and instantiation.
iter_members(module)
¶
Iterate over members of a module.
Override this method to implement custom member discovery or filtering.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
module
|
ModuleType
|
Module to inspect. |
required |
Yields:
| Type | Description |
|---|---|
Iterable[tuple[str, object]]
|
Tuples containing member names and their corresponding objects. |
keys()
¶
Iterate over plugin names.
Iterable yielding all existing plugin names as public keys.
Module names are deduplicated in module space and then
mapped to public keys with decode_key().
Returns:
| Type | Description |
|---|---|
Iterable[str]
|
Iterable of strings with all plugin public keys. |
Note
keys() do not force plugin module loading and instantiation.
values()
¶
Iterate all found plugin items.
Returns:
| Type | Description |
|---|---|
Iterable[T]
|
Iterable of plugin items. |
Note
values() will force plugin module loading and instantiation.
ModulePolicy
¶
Bases: Enum
Define policies for matching members to their origin module.
Attributes:
| Name | Type | Description |
|---|---|---|
STRICT |
Only accept members defined in the module being inspected. |
|
RELAXED |
Allow members imported from other modules. |