pymap.plugin

class pymap.plugin.PluginT

The plugin type variable.

alias of TypeVar(‘PluginT’)

class pymap.plugin.Plugin(group, *, default=None)[source]

Plugin system, typically loaded from importlib.metadata entry points.

>>> example: Plugin[Example] = Plugin('plugins.example')
>>> example.add('two', ExampleTwo)
>>> example.registered
{'one': <class 'examples.ExampleOne'>,
 'two': <class 'examples.ExampleTwo'>}

Note

Plugins registered from group entry points are lazy-loaded. This is to avoid cyclic imports.

Parameters:
  • group (str) – The entry point group to load.

  • default (str | None) – The name of the default plugin.

property registered: Mapping[str, type[PluginT]]

A mapping of the registered plugins, keyed by name.

property default: type[PluginT]

The default plugin implementation.

This property may also be assigned a new string value to change the name of the default plugin.

>>> example: Plugin[Example] = Plugin('plugins.example', default='one')
>>> example.default
<class 'examples.ExampleOne'>
>>> example.default = 'two'
>>> example.default
<class 'examples.ExampleTwo'>
Raises:

KeyError – The default plugin name was not registered.

add(name, plugin)[source]

Add a new plugin by name.

Parameters:
  • name (str) – The identifying name of the plugin.

  • plugin (type[PluginT]) – The plugin object.

Return type:

None

register(name)[source]

Decorates a plugin implementation.

Parameters:

name (str) – The identifying name of the plugin.

Return type:

Callable[[type[PluginT]], type[PluginT]]