2020-04-18 11:45:44 +00:00
|
|
|
# SPDX-License-Identifier: MIT
|
2014-01-05 15:50:57 +00:00
|
|
|
# Copyright (C) 2012-2014 Bastian Kleineidam
|
2020-10-01 19:20:08 +00:00
|
|
|
# Copyright (C) 2016-2020 Tobias Gruetzmacher
|
2012-10-11 16:02:29 +00:00
|
|
|
"""
|
|
|
|
Functions to load plugin modules.
|
2013-12-11 16:54:39 +00:00
|
|
|
|
|
|
|
Example usage:
|
2020-10-01 19:54:30 +00:00
|
|
|
for module in loader.get_plugin_modules():
|
|
|
|
plugins.extend(loader.get_module_plugins(module, PluginClass))
|
2012-10-11 16:02:29 +00:00
|
|
|
"""
|
2012-11-19 20:20:50 +00:00
|
|
|
import importlib
|
2019-03-08 22:46:50 +00:00
|
|
|
import pkgutil
|
2020-10-01 19:54:30 +00:00
|
|
|
import sys
|
2020-10-01 19:20:08 +00:00
|
|
|
|
|
|
|
from .plugins import (__name__ as plugin_package, __path__ as plugin_path)
|
2013-04-03 18:31:10 +00:00
|
|
|
from .output import out
|
2012-10-11 10:03:12 +00:00
|
|
|
|
2012-10-11 16:02:29 +00:00
|
|
|
|
2020-10-01 19:20:08 +00:00
|
|
|
def get_plugin_modules():
|
|
|
|
"""Find (and import) all valid modules in the "plugins" package.
|
2012-10-11 10:03:12 +00:00
|
|
|
@return: all loaded valid modules
|
|
|
|
@rtype: iterator of module
|
|
|
|
"""
|
2020-10-01 19:20:08 +00:00
|
|
|
prefix = plugin_package + "."
|
2022-05-28 19:05:12 +00:00
|
|
|
modules = {m[1] for m in pkgutil.iter_modules(plugin_path, prefix)}
|
2012-10-11 10:03:12 +00:00
|
|
|
|
2019-03-08 22:46:50 +00:00
|
|
|
for elm in _get_all_modules_pyinstaller():
|
2016-03-20 14:13:24 +00:00
|
|
|
if elm.startswith(prefix):
|
2022-05-28 19:05:12 +00:00
|
|
|
modules.add(elm)
|
2012-10-11 10:03:12 +00:00
|
|
|
|
2016-03-20 14:13:24 +00:00
|
|
|
for name in modules:
|
|
|
|
try:
|
|
|
|
yield importlib.import_module(name)
|
|
|
|
except ImportError as msg:
|
|
|
|
out.error("could not load module %s: %s" % (name, msg))
|
2012-10-11 10:03:12 +00:00
|
|
|
|
|
|
|
|
2019-03-08 22:46:50 +00:00
|
|
|
def _get_all_modules_pyinstaller():
|
|
|
|
# Special handling for PyInstaller
|
|
|
|
toc = set()
|
|
|
|
importers = pkgutil.iter_importers(__package__)
|
|
|
|
for i in importers:
|
|
|
|
if hasattr(i, 'toc'):
|
|
|
|
toc |= i.toc
|
|
|
|
return toc
|
|
|
|
|
|
|
|
|
2020-10-01 19:54:30 +00:00
|
|
|
def get_plugin_modules_from_dir(path, prefix='user_'):
|
|
|
|
"""Load and import a directory of python files as if they were part of the
|
|
|
|
"plugins" package. (Mostly "stolen" from
|
|
|
|
https://docs.python.org/3/library/importlib.html#importing-a-source-file-directly)
|
2012-10-11 10:03:12 +00:00
|
|
|
"""
|
2020-10-01 19:54:30 +00:00
|
|
|
modules = []
|
|
|
|
for f in path.glob('*.py'):
|
|
|
|
name = plugin_package + "." + prefix + f.stem
|
|
|
|
# FIXME: Drop str() when this is Python 3.6+
|
|
|
|
spec = importlib.util.spec_from_file_location(name, str(f))
|
|
|
|
module = importlib.util.module_from_spec(spec)
|
|
|
|
sys.modules[name] = module
|
|
|
|
spec.loader.exec_module(module)
|
|
|
|
modules.append(module)
|
|
|
|
return modules
|
2012-10-11 10:03:12 +00:00
|
|
|
|
|
|
|
|
|
|
|
def get_module_plugins(module, classobj):
|
2013-12-11 16:54:39 +00:00
|
|
|
"""Return all subclasses of a class in the module.
|
2012-10-11 10:03:12 +00:00
|
|
|
If the module defines __all__, only those entries will be searched,
|
|
|
|
otherwise all objects not starting with '_' will be searched.
|
|
|
|
"""
|
|
|
|
try:
|
|
|
|
names = module.__all__
|
|
|
|
except AttributeError:
|
|
|
|
names = [x for x in vars(module) if not x.startswith('_')]
|
2013-02-18 19:40:35 +00:00
|
|
|
for name in names:
|
2012-10-11 10:03:12 +00:00
|
|
|
try:
|
|
|
|
obj = getattr(module, name)
|
|
|
|
except AttributeError:
|
|
|
|
continue
|
|
|
|
try:
|
|
|
|
if issubclass(obj, classobj):
|
|
|
|
yield obj
|
|
|
|
except TypeError:
|
|
|
|
continue
|