Created
November 23, 2021 22:00
-
-
Save chinghwayu/2e366e094c45d3ab7ec58c22884b07af to your computer and use it in GitHub Desktop.
Registers a stevedore plugin dynamically without needing to install as a package
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
try: | |
# For python 3.8 and later | |
import importlib.metadata as importlib_metadata | |
except ImportError: | |
# For everyone else | |
import importlib_metadata | |
from stevedore import ExtensionManager | |
def register_plugin(name, namespace, entry_point) -> None: | |
"""Registers a plugin dynamically without needing to install as a package. | |
Args: | |
name (str): Name of plugin to be referenced. | |
namespace (str): Name of plugin namespace. | |
entry_point (str): Entry point in the form: some.module:some.attr | |
""" | |
ep = importlib_metadata.EntryPoint(name, entry_point, namespace) | |
e = ExtensionManager(namespace) | |
if namespace in e.ENTRY_POINT_CACHE: | |
entry_points = e.ENTRY_POINT_CACHE.get(namespace) | |
if name not in [entry_point.name for entry_point in entry_points]: | |
entry_points.append(ep) | |
e.ENTRY_POINT_CACHE[namespace] = entry_points | |
else: | |
e.ENTRY_POINT_CACHE[namespace] = [ep] | |
ep.load() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment