show how to call methods on plugins
This commit is contained in:
@@ -5,6 +5,7 @@ from pkgutil import iter_modules
|
|||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
from os.path import dirname
|
from os.path import dirname
|
||||||
from importlib import import_module
|
from importlib import import_module
|
||||||
|
from inspect import signature
|
||||||
from raven.pluginbase import PluginBase
|
from raven.pluginbase import PluginBase
|
||||||
|
|
||||||
|
|
||||||
@@ -29,27 +30,38 @@ class PluginRegistry():
|
|||||||
@staticmethod
|
@staticmethod
|
||||||
def load_module(module_name: str) -> ModuleType:
|
def load_module(module_name: str) -> ModuleType:
|
||||||
module_name = f"plugins.{module_name}"
|
module_name = f"plugins.{module_name}"
|
||||||
# import the module and iterate through its attributes
|
|
||||||
module = import_module(module_name)
|
module = import_module(module_name)
|
||||||
PluginRegistry.modules.append(module)
|
PluginRegistry.modules.append(module)
|
||||||
return module
|
return module
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def load_plugin(plugin_name: str):
|
def load_plugin(plugin_name: str) -> PluginBase:
|
||||||
module_name = f"plugins.{plugin_name.lower()}"
|
module_name = f"plugins.{plugin_name.lower()}"
|
||||||
module = import_module(module_name)
|
module = import_module(module_name)
|
||||||
if plugin_name in dir(module):
|
if plugin_name in dir(module):
|
||||||
plugin_class = getattr(module, plugin_name)
|
plugin_class = getattr(module, plugin_name)
|
||||||
|
|
||||||
if isclass(plugin_class) and issubclass(plugin_class, PluginBase):
|
if isclass(plugin_class) and issubclass(plugin_class, PluginBase):
|
||||||
PluginRegistry.register_plugin(plugin_name, plugin_class)
|
PluginRegistry.register_plugin(plugin_name, plugin_class())
|
||||||
print("%s -> %s :: %s in %s" % (plugin_name, plugin_class, module_name, module))
|
|
||||||
return plugin_class
|
return plugin_class
|
||||||
|
raise RuntimeError("plugin %s not found" % plugin_name)
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def get_modules() -> [ModuleType]:
|
def get_modules() -> [ModuleType]:
|
||||||
return PluginRegistry.modules.copy()
|
return PluginRegistry.modules.copy()
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def get_plugins() -> [ModuleType]:
|
def get_plugins() -> [PluginBase]:
|
||||||
return PluginRegistry.modules.copy()
|
return PluginRegistry.modules.copy()
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def execute(function_name: str, *args):
|
||||||
|
for plugin in PluginRegistry.plugins.values():
|
||||||
|
fun = getattr(plugin, function_name, None)
|
||||||
|
|
||||||
|
sig = signature(fun)
|
||||||
|
if callable(fun):
|
||||||
|
if len(sig.parameters) != len(args):
|
||||||
|
raise RuntimeError("method %s.%s has wrong number of arguments. expected %s but was %s " % (
|
||||||
|
plugin, function_name, len(args), len(sig.parameters)))
|
||||||
|
fun(args)
|
||||||
|
|||||||
@@ -6,6 +6,9 @@ class FilterPlugin(PluginBase):
|
|||||||
super(FilterPlugin, self).__init__()
|
super(FilterPlugin, self).__init__()
|
||||||
print("init FilterPlugin")
|
print("init FilterPlugin")
|
||||||
|
|
||||||
|
def say_hello(self, arg: str):
|
||||||
|
print("FilterPlugin says hello %s" % arg)
|
||||||
|
|
||||||
|
|
||||||
def init():
|
def init():
|
||||||
print("initializing filter plugin")
|
print("initializing filter plugin")
|
||||||
|
|||||||
@@ -5,3 +5,6 @@ class LogFileViewerPlugin(PluginBase):
|
|||||||
def __init__(self):
|
def __init__(self):
|
||||||
super(LogFileViewerPlugin, self).__init__()
|
super(LogFileViewerPlugin, self).__init__()
|
||||||
print("init LogFileViewerPlugin")
|
print("init LogFileViewerPlugin")
|
||||||
|
|
||||||
|
def say_hello(self, *args):
|
||||||
|
print("LogFileViewerPlugin says hello")
|
||||||
|
|||||||
@@ -63,10 +63,9 @@ def set_window_icon(app: QApplication):
|
|||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
filterplugin = PluginRegistry.load_plugin("FilterPlugin")
|
filterplugin = PluginRegistry.load_plugin("FilterPlugin")
|
||||||
filterplugin()
|
|
||||||
|
|
||||||
logfileviewerplugin = PluginRegistry.load_plugin("LogFileViewerPlugin")
|
logfileviewerplugin = PluginRegistry.load_plugin("LogFileViewerPlugin")
|
||||||
logfileviewerplugin()
|
|
||||||
|
PluginRegistry.execute("say_hello", "World")
|
||||||
|
|
||||||
app = QApplication(sys.argv)
|
app = QApplication(sys.argv)
|
||||||
set_window_icon(app)
|
set_window_icon(app)
|
||||||
|
|||||||
Reference in New Issue
Block a user