"""Base of plugin systems."""
# copied from https://github.com/deepmodeling/dpdata/blob/a3e76d75de53f6076254de82d18605a010dc3b00/dpdata/plugin.py
from abc import ABCMeta
from typing import Callable
[docs]class Plugin:
"""A class to register and restore plugins.
Attributes
----------
plugins : Dict[str, object]
plugins
Examples
--------
>>> plugin = Plugin()
>>> @plugin.register("xx")
def xxx():
pass
>>> print(plugin.plugins['xx'])
"""
def __init__(self):
self.plugins = {}
def __add__(self, other) -> "Plugin":
self.plugins.update(other.plugins)
return self
[docs] def register(self, key : str) -> Callable[[object], object]:
"""Register a plugin.
Parameter
---------
key : str
key of the plugin
Returns
-------
Callable[[object], object]
decorator
"""
def decorator(object : object) -> object:
self.plugins[key] = object
return object
return decorator
[docs] def get_plugin(self, key) -> object:
"""Visit a plugin by key.
Parameters
----------
key : str
key of the plugin
Returns
-------
object
the plugin
"""
return self.plugins[key]
[docs]class PluginVariant(metaclass=VariantABCMeta):
"""A class to remove `type` from input arguments."""
pass