提交 d38323d3 编写于 作者: R Rudá Moura 提交者: Ruda Moura

Add support for loading external plugins from directory.

Create new option --plugins to pass a directory for loading external plugins.
Also, add some plugins examples in examples/avocado_example_plugins.py.

The external plugins are loaded from avocado_*.py modules and the plugins are
subclasses of Plugin().
Signed-off-by: NRuda Moura <rmoura@redhat.com>
上级 98122ea9
......@@ -26,17 +26,26 @@ class AvocadoApp(object):
self.arg_parser.add_argument('--loglevel', action='store',
help='Debug Level',
dest='log_level', default='')
self.arg_parser.add_argument('--plugins', action='store',
help='Load extra plugins from directory',
dest='plugins_dir', default='')
args, extra = self.arg_parser.parse_known_args()
subparsers = self.arg_parser.add_subparsers(title='subcommands',
description='valid subcommands',
help='subcommand help')
self.load_plugin_manager(subparsers)
self.load_plugin_manager(subparsers, args.plugins_dir)
self.args = self.arg_parser.parse_args()
def load_plugin_manager(self, parser):
def load_plugin_manager(self, parser, plugins_dir):
"""Load Plugin Manager.
:param parser: Main argument parser.
:param plugins_dir: Extra plugins directory.
"""
self.plugin_manager = get_plugin_manager()
self.plugin_manager.load_plugins()
self.plugin_manager.load_plugins(plugins_dir)
if self.external_plugins:
self.plugin_manager.add_plugins(self.external_plugins)
self.plugin_manager.configure(parser)
......
"""Plugin Managers."""
from avocado.plugins.builtin import load_builtins
from avocado.plugins.plugin import Plugin
DefaultPluginManager = None
......@@ -23,7 +24,8 @@ class PluginManager(object):
raise NotImplementedError('Managers must implement the method load_plugins')
def configure(self, parser):
raise NotImplementedError('Managers must implement the method configure')
for plugin in self.plugins:
plugin.configure(parser)
class BuiltinPluginManager(PluginManager):
......@@ -36,10 +38,6 @@ class BuiltinPluginManager(PluginManager):
for plugin in load_builtins():
self.add_plugin(plugin())
def configure(self, parser):
for plugin in self.plugins:
plugin.configure(parser)
class ExternalPluginManager(PluginManager):
......@@ -47,6 +45,21 @@ class ExternalPluginManager(PluginManager):
Load external plugins.
"""
def load_plugins(self, path, pattern='avocado_*.py'):
from glob import glob
import os
import imp
if path:
candidates = glob(os.path.join(path, pattern))
candidates = [(os.path.splitext(os.path.basename(x))[0], path) for x in candidates]
candidates = [(x[0], imp.find_module(x[0], [path])) for x in candidates]
for candidate in candidates:
mod = imp.load_module(candidate[0], *candidate[1])
for name in mod.__dict__:
x = getattr(mod, name)
if isinstance(x, type) and issubclass(x, Plugin):
self.add_plugin(x())
def add_plugins(self, plugins):
for plugin in plugins:
self.add_plugin(plugin)
......@@ -60,6 +73,13 @@ class AvocadoPluginManager(BuiltinPluginManager, ExternalPluginManager):
Load builtins and external plugins.
"""
def __init__(self):
BuiltinPluginManager.__init__(self)
ExternalPluginManager.__init__(self)
def load_plugins(self, path=None):
BuiltinPluginManager.load_plugins(self)
ExternalPluginManager.load_plugins(self, path)
def get_plugin_manager():
"""
......
from avocado.plugins import plugin
from avocado.plugins.manager import get_plugin_manager
from avocado.core import output
class HelloWorld(plugin.Plugin):
"""
Hello World! plugin example.
"""
def configure(self, parser):
myparser = parser.add_parser('hello',
help='Hello World! plugin example')
myparser.set_defaults(func=self.hello)
self.enabled = True
def hello(self, args):
print self.__doc__
class PluginsList(plugin.Plugin):
"""
List all plugins loaded.
"""
def configure(self, parser):
myparser = parser.add_parser('plugins',
help='List all plugins loaded')
myparser.set_defaults(func=self.list_plugins)
self.enabled = True
def list_plugins(self, args):
bcolors = output.colors
pipe = output.get_paginator()
pm = get_plugin_manager()
pipe.write(bcolors.header_str('Plugins loaded:'))
pipe.write('\n')
for plug in pm.plugins:
pipe.write(' %s - %s\n' % (plug, plug.__doc__))
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册