Changeset View
Changeset View
Standalone View
Standalone View
scap/plugins/__init__.py
# -*- coding: utf-8 -*- | |||||
""" | |||||
scap.plugins | |||||
~~~~~~~~~~~~ | |||||
Scap plugin architecture | |||||
.. function:: find_plugins(plugin_dirs) | |||||
Get a list of all plugins found in in plugin_dirs | |||||
:param list plugin_dirs: directories to search for plugins | |||||
:return: list of all plugin commands found in plugin_dirs | |||||
mobrovac: Could we explicitly list those locations here? | |||||
.. function:: load_plugins([plugin_dir]) | |||||
load scap plugin modules. | |||||
:type plugin_dir: str or None | |||||
:param str plugin_dir: an additional location to search | |||||
""" | |||||
import importlib | import importlib | ||||
import logging | import logging | ||||
import os | import os | ||||
import sys | import sys | ||||
from ..cli import Application | from ..cli import Application | ||||
from .say import Say | |||||
this_module = sys.modules[__name__] | this_module = sys.modules[__name__] | ||||
loaded_plugins = {} | loaded_plugins = {} | ||||
__all__ = [] | __all__ = ['Say'] | ||||
def find_plugins(plugin_dirs): | def find_plugins(plugin_dirs): | ||||
""" | """ | ||||
Returns a list of all plugin commands found in plugin_dir | returns a list of all plugin commands found in plugin_dirs | ||||
Returns an empty list if no commands are defined. | |||||
""" | """ | ||||
plugins = [] | plugins = [] | ||||
for d in plugin_dirs: | for d in plugin_dirs: | ||||
Not Done Inline Actionsbuilt-in. mobrovac: `built-in`. | |||||
if d is None or not os.path.exists(d): | if d is None or not os.path.exists(d): | ||||
continue | continue | ||||
file_list = os.listdir(os.path.realpath(d)) | file_list = os.listdir(os.path.realpath(d)) | ||||
if len(file_list) < 1: | if len(file_list) < 1: | ||||
continue | continue | ||||
Show All 30 Lines | def load_plugins(plugin_dir=None): | ||||
plugins = find_plugins(plugin_dirs) | plugins = find_plugins(plugin_dirs) | ||||
if len(plugins) < 1: | if len(plugins) < 1: | ||||
return | return | ||||
# import each of the plugin modules | # import each of the plugin modules | ||||
for plugin in plugins: | for plugin in plugins: | ||||
# module path relative to scap.plugins: | # module path relative to scap.plugins: | ||||
plugin_module = ".%s" % plugin | plugin_module = ".%s" % plugin | ||||
try: | |||||
mod = importlib.import_module(plugin_module, "scap.plugins") | mod = importlib.import_module(plugin_module, "scap.plugins") | ||||
# find classes in mod which extend scap.cli.Application | # find classes in mod which extend scap.cli.Application | ||||
for objname in dir(mod): | for objname in dir(mod): | ||||
obj = getattr(mod, objname) | obj = getattr(mod, objname) | ||||
if type(obj) is type and issubclass(obj, Application): | if type(obj) is type and issubclass(obj, Application): | ||||
if objname in loaded_plugins: | if objname in loaded_plugins: | ||||
# duplicate: another plugin already used the same name | # duplicate: another plugin already used the same name | ||||
msg = 'Duplicate plugin named %s, skipping.' % objname | msg = 'Duplicate plugin named %s, skipping.' | ||||
logging.getLogger().warning(msg) | logging.getLogger().warning(msg, objname) | ||||
continue | continue | ||||
# copy the class into the scap.plugins namespace | # copy the class into the scap.plugins namespace | ||||
setattr(this_module, objname, obj) | setattr(this_module, objname, obj) | ||||
loaded_plugins[objname] = obj | loaded_plugins[objname] = obj | ||||
__all__.append(objname) | __all__.append(objname) | ||||
except Exception: | |||||
msg = 'Problem loading plugins from module: scap.plugins.%s ' | |||||
logger = logging.getLogger() | |||||
logger.warning(msg % plugin, exc_info=sys.exc_info()) | |||||
Not Done Inline Actionsthe only change here is added error handling: an exception while evaluating a plugin shouldn't cause the entire scap application to fail mmodell: the only change here is added error handling: an exception while evaluating a plugin shouldn't… |
Content licensed under Creative Commons Attribution-ShareAlike 3.0 (CC-BY-SA) unless otherwise noted; code licensed under GNU General Public License (GPL) or other open source licenses. By using this site, you agree to the Terms of Use, Privacy Policy, and Code of Conduct. · Wikimedia Foundation · Privacy Policy · Code of Conduct · Terms of Use · Disclaimer · CC-BY-SA · GPL
Could we explicitly list those locations here?