2016-03-20 14:13:24 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
2014-01-05 15:50:57 +00:00
|
|
|
# Copyright (C) 2012-2014 Bastian Kleineidam
|
2019-03-08 22:46:50 +00:00
|
|
|
# Copyright (C) 2016-2019 Tobias Gruetzmacher
|
2016-03-20 14:13:24 +00:00
|
|
|
|
2012-10-11 16:02:29 +00:00
|
|
|
"""
|
|
|
|
Functions to load plugin modules.
|
2013-12-11 16:54:39 +00:00
|
|
|
|
|
|
|
Example usage:
|
|
|
|
modules = loader.get_modules('plugins')
|
|
|
|
plugins = loader.get_plugins(modules, PluginClass)
|
2012-10-11 16:02:29 +00:00
|
|
|
"""
|
2016-03-20 14:13:24 +00:00
|
|
|
|
|
|
|
from __future__ import absolute_import, division, print_function
|
2012-11-19 20:20:50 +00:00
|
|
|
import importlib
|
2019-03-08 22:46:50 +00:00
|
|
|
import pkgutil
|
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
|
|
|
|
2013-12-11 16:54:39 +00:00
|
|
|
def get_modules(folder):
|
2016-03-20 14:13:24 +00:00
|
|
|
"""Find (and import) all valid modules in the given submodule of this file.
|
2012-10-11 10:03:12 +00:00
|
|
|
@return: all loaded valid modules
|
|
|
|
@rtype: iterator of module
|
|
|
|
"""
|
2016-03-20 14:13:24 +00:00
|
|
|
mod = importlib.import_module(".." + folder, __name__)
|
|
|
|
prefix = mod.__name__ + "."
|
|
|
|
modules = [m[1] for m in pkgutil.iter_modules(mod.__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):
|
|
|
|
modules.append(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
|
|
|
|
|
|
|
|
|
2012-10-11 10:03:12 +00:00
|
|
|
def get_plugins(modules, classobj):
|
2013-12-11 16:54:39 +00:00
|
|
|
"""Find all class objects in all modules.
|
2012-10-11 10:03:12 +00:00
|
|
|
@param modules: the modules to search
|
|
|
|
@ptype modules: iterator of modules
|
2013-12-11 16:54:39 +00:00
|
|
|
@return: found classes
|
2012-10-11 10:03:12 +00:00
|
|
|
@rytpe: iterator of class objects
|
|
|
|
"""
|
|
|
|
for module in modules:
|
|
|
|
for plugin in get_module_plugins(module, classobj):
|
|
|
|
yield plugin
|
|
|
|
|
|
|
|
|
|
|
|
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
|