Created
June 28, 2018 10:47
-
-
Save markph0204/6911ae3c3fa683ae9f6c9cf4df5f1b42 to your computer and use it in GitHub Desktop.
Python 3.7 Plugins
This file contains hidden or 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
from importlib import import_module | |
from importlib import resources | |
PLUGINS = dict() | |
def register_plugin(func): | |
"""Decorator to register plug-ins""" | |
name = func.__name__ | |
PLUGINS[name] = func | |
return func | |
def __getattr__(name): | |
"""Return a named plugin""" | |
try: | |
return PLUGINS[name] | |
except KeyError: | |
_import_plugins() | |
if name in PLUGINS: | |
return PLUGINS[name] | |
else: | |
raise AttributeError( | |
f"module {__name__!r} has no attribute {name!r}" | |
) from None | |
def __dir__(): | |
"""List available plug-ins""" | |
_import_plugins() | |
return list(PLUGINS.keys()) | |
def _import_plugins(): | |
"""Import all resources to register plug-ins""" | |
for name in resources.contents(__name__): | |
if name.endswith(".py"): | |
import_module(f"{__name__}.{name[:-3]}") | |
# in './plugins/' path | |
from . import register_plugin | |
@register_plugin | |
def hello_1(): | |
print("Hello from Plugin 1") | |
@register_plugin | |
def hello_2(): | |
print("Hello from Plugin 2") | |
@register_plugin | |
def goodbye(): | |
print("Plugin 2 says goodbye") | |
#Sample discover plugins: | |
>>> import plugins | |
>>> plugins.hello_1() | |
Hello from Plugin 1 | |
>>> dir(plugins) | |
['goodbye', 'hello_1', 'hello_2'] | |
>>> plugins.goodbye() | |
Plugin 2 says goodbye |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment