From 83f4e86329acbc7ded4770fe73adc95e0fdbff48 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C4=B0smail=20Ba=C5=9Faran?= Date: Tue, 1 Mar 2016 18:43:26 +0200 Subject: [PATCH] implementing plugin manager (in progress) --- .../base/messaging/MessageResponseQueue.py | 29 +++++++++++++++++++ opt/ahenk/base/plugin/AbstractCommand.py | 9 +++++- opt/ahenk/base/plugin/Plugin.py | 23 +++++++++++---- opt/ahenk/base/plugin/PluginManager.py | 19 ++++++++---- opt/ahenk/plugins/plugin1/command1.py | 22 ++++++++++++++ opt/ahenk/plugins/plugin1/command2.py | 0 opt/ahenk/plugins/plugin1/main.py | 4 +-- 7 files changed, 92 insertions(+), 14 deletions(-) create mode 100644 opt/ahenk/base/messaging/MessageResponseQueue.py create mode 100644 opt/ahenk/plugins/plugin1/command1.py create mode 100644 opt/ahenk/plugins/plugin1/command2.py diff --git a/opt/ahenk/base/messaging/MessageResponseQueue.py b/opt/ahenk/base/messaging/MessageResponseQueue.py new file mode 100644 index 0000000..d382b71 --- /dev/null +++ b/opt/ahenk/base/messaging/MessageResponseQueue.py @@ -0,0 +1,29 @@ +#!/usr/bin/python3 +# -*- coding: utf-8 -*- +# Author: İsmail BAŞARAN + +import threading +from base.Scope import Scope + +class MessageResponseQueue(threading.Thread): + """ + This class handles responses and sends it to lider server. + """ + def __init__(self,outQueue): + super(MessageResponseQueue, self).__init__() + scope = Scope.getInstance() + self.logger = scope.getLogger() + self.messageManager = scope.getMessageManager() + self.outQueue = outQueue + + def run(self): + try: + # This item will send response to lider. + # item must be response message. Response message may be generic message type + responseMessage = self.outQueue.get() + print(item) + # Call message manager for response + self.messageManager.sendResponse(responseMessage) + self.outQueue.task_done() + except: + pass diff --git a/opt/ahenk/base/plugin/AbstractCommand.py b/opt/ahenk/base/plugin/AbstractCommand.py index c0cdc16..f0fdd98 100644 --- a/opt/ahenk/base/plugin/AbstractCommand.py +++ b/opt/ahenk/base/plugin/AbstractCommand.py @@ -9,7 +9,7 @@ class AbstractCommand(object): super(AbstractCommand, self).__init__() self.scope = Scope.getInstance() - def run(): + def handle_task(task): # implement this metot from command pass @@ -20,6 +20,13 @@ class AbstractCommand(object): print('Logger did not found') return None + def configurationManager(self): + try: + return self.scope.getConfigurationManager() + except Exception as e: + print('ConfigurationManager did not found') + return None + def addMessageResponseQueue(self): # TODO implement response queue pass diff --git a/opt/ahenk/base/plugin/Plugin.py b/opt/ahenk/base/plugin/Plugin.py index a7186c6..99387da 100644 --- a/opt/ahenk/base/plugin/Plugin.py +++ b/opt/ahenk/base/plugin/Plugin.py @@ -1,16 +1,27 @@ #!/usr/bin/python3 # -*- coding: utf-8 -*- # Author: İsmail BAŞARAN +import threading +from base.Scope import Scope -class Plugin(object): +class Plugin(threading.Thread): """docstring for Plugin""" - def __init__(self, name,mainModule): + def __init__(self, name,InQueue): super(Plugin, self).__init__() self.name = name - self.mainModule = mainModule + self.InQueue = InQueue + self.scope=Scope.getInstance() + self.pluginManager = self.scope.getPluginManager() + + def run(): + try: + task=self.InQueue.get() + command = self.pluginManager.findCommand(task.getCommandId()) + command.handle_task(task) + # TODO add result to response queue + + except Exception as e: + print("exception occured when executing plugin") def getName(self): return self.name - - def getMainModule(self): - return self.mainModule diff --git a/opt/ahenk/base/plugin/PluginManager.py b/opt/ahenk/base/plugin/PluginManager.py index db0be7d..cb16d96 100644 --- a/opt/ahenk/base/plugin/PluginManager.py +++ b/opt/ahenk/base/plugin/PluginManager.py @@ -20,11 +20,20 @@ class PluginManager(object): if not os.path.isdir(location) or not self.configManager.get("PLUGIN", "mainModuleName") + ".py" in os.listdir(location): continue info = imp.find_module(self.configManager.get("PLUGIN", "mainModuleName"), [location]) - mainModule = self.loadSinglePlugin(info): + #mainModule = self.loadSinglePlugin(info): self.plugins.append(Plugin(pname,mainModule)) - def loadSinglePlugin(pluginInfo): - return imp.load_module(self.configManager.get("PLUGIN", "mainModuleName"), pluginInfo) + def createQueueForPlugin(self): - def findSinglePlugin(pluginName): - pass # Not implemented yet + + def loadSinglePlugin(self,pluginInfo): + return imp.load_module(self.configManager.get("PLUGIN", "mainModuleName"), *pluginInfo) + + def findSinglePlugin(self,pluginName): + for plugin in self.getPlugins(): + if plugin["name"] == self.plugins: + return self.loadSinglePlugin(plugin) + + + def findCommand(self,comamndName): + pass diff --git a/opt/ahenk/plugins/plugin1/command1.py b/opt/ahenk/plugins/plugin1/command1.py new file mode 100644 index 0000000..7410508 --- /dev/null +++ b/opt/ahenk/plugins/plugin1/command1.py @@ -0,0 +1,22 @@ +#!/usr/bin/python3 +# -*- coding: utf-8 -*- +# Author: İsmail BAŞARAN + +from base.plugin.AbstractCommand import AbstractCommand + +class MySamplePlugin(AbstractCommand): + """docstring for MySamplePlugin""" + def __init__(self, task): + super(MySamplePlugin, self).__init__() + self.task = task + + def handle_task(self): + print("This is command 1 ") + + +def handle_task(task): + # Do what ever you want here + # You can create command class but it is not necessary + # You can use directly this method. + myPlugin = MySamplePlugin(task) + myPlugin.handle_task() diff --git a/opt/ahenk/plugins/plugin1/command2.py b/opt/ahenk/plugins/plugin1/command2.py new file mode 100644 index 0000000..e69de29 diff --git a/opt/ahenk/plugins/plugin1/main.py b/opt/ahenk/plugins/plugin1/main.py index 859693f..59d591a 100644 --- a/opt/ahenk/plugins/plugin1/main.py +++ b/opt/ahenk/plugins/plugin1/main.py @@ -4,5 +4,5 @@ -def run(val): - print "oo yeah plugin1 " + str(val) +def info(): + return None