implementing plugin manager (in progress)

This commit is contained in:
İsmail Başaran 2016-03-01 18:43:26 +02:00
parent 5e2a058412
commit 83f4e86329
7 changed files with 92 additions and 14 deletions

View file

@ -0,0 +1,29 @@
#!/usr/bin/python3
# -*- coding: utf-8 -*-
# Author: İsmail BAŞARAN <ismail.basaran@tubitak.gov.tr> <basaran.ismaill@gmail.com>
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

View file

@ -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

View file

@ -1,16 +1,27 @@
#!/usr/bin/python3
# -*- coding: utf-8 -*-
# Author: İsmail BAŞARAN <ismail.basaran@tubitak.gov.tr> <basaran.ismaill@gmail.com>
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

View file

@ -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

View file

@ -0,0 +1,22 @@
#!/usr/bin/python3
# -*- coding: utf-8 -*-
# Author: İsmail BAŞARAN <ismail.basaran@tubitak.gov.tr> <basaran.ismaill@gmail.com>
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()

View file

View file

@ -4,5 +4,5 @@
def run(val):
print "oo yeah plugin1 " + str(val)
def info():
return None