def _import(package: str, plugin: str) -> None: """Import the given plugin file from a package""" if package in _PLUGINS and plugin in _PLUGINS[package]: return plugin_module = f"{package}.{plugin}" try: importlib.import_module(plugin_module) except ImportError as err: if repr(plugin_module) in err.msg: # type: ignore raise _exceptions.UnknownPluginError( f"Plugin {plugin!r} not found in {package!r}") from None elif repr(package) in err.msg: # type: ignore raise _exceptions.UnknownPackageError( f"Package {package!r} does not exist") from None raise
def _import_all(package: str) -> None: """Import all plugins in a package""" try: all_resources = resources.contents(package) # type: ignore except ImportError as err: raise _exceptions.UnknownPackageError(err) from None # Note that we have tried to import the package by adding it to _PLUGINS _PLUGINS.setdefault(package, {}) # Loop through all Python files in the directories of the package plugins = [ r[:-3] for r in all_resources if r.endswith(".py") and not r.startswith("_") ] for plugin in plugins: try: _import(package, plugin) except ImportError: pass # Don't let errors in one plugin, affect the others