Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
from importlib import import_module, resources
PLUGINS = {}
def register_plugin(f):
PLUGINS[f.__name__] = f
return f
def __getattr__(name):
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__():
_import_plugins()
return list(PLUGINS.keys())
def _import_plugins():
for name in resources.contents(__name__):
if name.endswith(".py"):
import_module(f"{__name__}.{name[:-3]})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.