2016-02-29 10:48:00 +02:00
|
|
|
#!/usr/bin/python3
|
2016-02-18 16:38:30 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
# Author: İsmail BAŞARAN <ismail.basaran@tubitak.gov.tr> <basaran.ismaill@gmail.com>
|
|
|
|
from base.plugin.Plugin import Plugin
|
2016-03-02 10:27:43 +02:00
|
|
|
from base.plugin.PluginQueue import PluginQueue
|
|
|
|
from base.Scope import Scope
|
2016-02-18 16:38:30 +02:00
|
|
|
import imp,os
|
|
|
|
|
|
|
|
class PluginManager(object):
|
|
|
|
"""docstring for PluginManager"""
|
|
|
|
#implement logger
|
2016-03-02 10:27:43 +02:00
|
|
|
def __init__(self):
|
2016-02-18 16:38:30 +02:00
|
|
|
super(PluginManager, self).__init__()
|
2016-03-07 17:13:03 +02:00
|
|
|
self.scope = Scope.getInstance()
|
|
|
|
self.configManager = self.scope.getConfigurationManager()
|
2016-02-18 16:38:30 +02:00
|
|
|
self.plugins = []
|
2016-03-02 10:27:43 +02:00
|
|
|
self.pluginQueueDict = dict()
|
2016-03-07 17:13:03 +02:00
|
|
|
self.logger = self.scope.getLogger()
|
2016-02-18 16:38:30 +02:00
|
|
|
|
2016-03-02 15:45:36 +02:00
|
|
|
def loadPlugins(self):
|
2016-03-02 17:20:42 +02:00
|
|
|
print("loading")
|
2016-02-18 16:38:30 +02:00
|
|
|
self.plugins = []
|
|
|
|
possibleplugins = os.listdir(self.configManager.get("PLUGIN", "pluginFolderPath"))
|
|
|
|
for pname in possibleplugins:
|
|
|
|
location = os.path.join(self.configManager.get("PLUGIN", "pluginFolderPath"), pname)
|
|
|
|
if not os.path.isdir(location) or not self.configManager.get("PLUGIN", "mainModuleName") + ".py" in os.listdir(location):
|
2016-03-02 15:45:36 +02:00
|
|
|
self.logger.debug('It is not a plugin location - - ')
|
2016-02-18 16:38:30 +02:00
|
|
|
continue
|
2016-03-02 10:27:43 +02:00
|
|
|
try:
|
|
|
|
self.loadSinglePlugin(pname)
|
|
|
|
except Exception as e:
|
|
|
|
# TODO error log
|
2016-03-02 15:45:36 +02:00
|
|
|
pass
|
2016-03-02 10:27:43 +02:00
|
|
|
|
|
|
|
def loadSinglePlugin(self,pluginName):
|
|
|
|
# TODO check already loaded plugin
|
|
|
|
self.pluginQueueDict[pluginName]=PluginQueue()
|
|
|
|
plugin = Plugin(pluginName,self.pluginQueueDict[pluginName])
|
|
|
|
plugin.setDaemon(True)
|
|
|
|
plugin.start()
|
|
|
|
self.plugins.append(plugin)
|
|
|
|
|
|
|
|
def findCommand(self,pluginName,commandId):
|
2016-03-02 11:59:19 +02:00
|
|
|
location = os.path.join(self.configManager.get("PLUGIN", "pluginFolderPath"), pluginName)
|
2016-03-02 10:27:43 +02:00
|
|
|
if os.path.dir(location) and commandId + ".py" in os.listdir(location):
|
|
|
|
info = imp.find_module(commandId, [location])
|
|
|
|
return imp.load_module(commandId, *info)
|
|
|
|
else:
|
2016-03-02 15:45:36 +02:00
|
|
|
self.logger.warning('Command id - - not found')
|
2016-03-02 10:27:43 +02:00
|
|
|
return None
|
2016-03-02 11:59:19 +02:00
|
|
|
|
|
|
|
def processTask(self,task):
|
|
|
|
try:
|
|
|
|
if task.getPluginId().lower() in self.pluginQueueDict :
|
|
|
|
self.pluginQueueDict[task.getPluginId().lower()].put(task,task.priority)
|
|
|
|
except Exception as e:
|
|
|
|
# TODO error log here
|
|
|
|
# TODO update task - status to not found command
|
2016-03-02 15:45:36 +02:00
|
|
|
pass
|
2016-03-02 11:59:19 +02:00
|
|
|
|
|
|
|
def reloadPlugins(self):
|
|
|
|
# Not implemented yet
|
|
|
|
pass
|
|
|
|
|
|
|
|
def checkPluginExists(self,pluginName):
|
|
|
|
# Not implemented yet
|
|
|
|
pass
|
|
|
|
|
|
|
|
def reloadSinglePlugin(self,pluginName):
|
|
|
|
# Not implemented yet
|
|
|
|
pass
|
|
|
|
|
|
|
|
def checkCommandExist(self,pluginName,commandId):
|
|
|
|
# Not implemented yet
|
|
|
|
pass
|
2016-03-02 17:20:42 +02:00
|
|
|
|
|
|
|
def printQueueSize(self):
|
|
|
|
print("size " + str(len(self.pluginQueueDict)))
|