diff --git a/opt/ahenk/base/plugin/PluginManager.py b/opt/ahenk/base/plugin/PluginManager.py index cb16d96..2e35deb 100644 --- a/opt/ahenk/base/plugin/PluginManager.py +++ b/opt/ahenk/base/plugin/PluginManager.py @@ -2,15 +2,20 @@ # -*- coding: utf-8 -*- # Author: İsmail BAŞARAN from base.plugin.Plugin import Plugin +from base.plugin.PluginQueue import PluginQueue +from base.Scope import Scope import imp,os class PluginManager(object): """docstring for PluginManager""" #implement logger - def __init__(self, configManager): + def __init__(self): super(PluginManager, self).__init__() - self.configManager = configManager + scope = Scope.getInstance() + self.configManager = scope.getConfigurationManager() self.plugins = [] + self.pluginQueueDict = dict() + self.logger = scope.getLogger() def loadPlugins(): self.plugins = [] @@ -18,22 +23,26 @@ class PluginManager(object): 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): + #TODO debug log here continue - info = imp.find_module(self.configManager.get("PLUGIN", "mainModuleName"), [location]) - #mainModule = self.loadSinglePlugin(info): - self.plugins.append(Plugin(pname,mainModule)) + try: + self.loadSinglePlugin(pname) + except Exception as e: + # TODO error log - def createQueueForPlugin(self): + 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 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 + def findCommand(self,pluginName,commandId): + location = os.path.join(self.configManager.get("PLUGIN", "pluginFolderPath"), pname) + 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: + self.logger.warning('Command id - %s - not found',commandId) + return None diff --git a/opt/ahenk/base/plugin/PluginQueue.py b/opt/ahenk/base/plugin/PluginQueue.py new file mode 100644 index 0000000..df7a961 --- /dev/null +++ b/opt/ahenk/base/plugin/PluginQueue.py @@ -0,0 +1,10 @@ +#!/usr/bin/python3 +# -*- coding: utf-8 -*- +# Author: İsmail BAŞARAN + +import Queue + +class PluginQueue(Queue.PriorityQueue): + def __contains__(self, item): + with self.mutex: + return item in self.queue