diff options
author | mkaay <mkaay@mkaay.de> | 2010-08-25 16:48:55 +0200 |
---|---|---|
committer | mkaay <mkaay@mkaay.de> | 2010-08-25 16:48:55 +0200 |
commit | 3c9f55270a83b0e88ec0dc516f9d9921e4d7b6ea (patch) | |
tree | c5b2b1bfeb7eb8df2b97be118f6cbcec4e29cb3b /core/module/HookManager.py | |
parent | ul.to fetching, so.biz expire (diff) | |
download | pyload-3c9f55270a83b0e88ec0dc516f9d9921e4d7b6ea.tar.xz |
merged gui
Diffstat (limited to 'core/module/HookManager.py')
-rw-r--r-- | core/module/HookManager.py | 128 |
1 files changed, 128 insertions, 0 deletions
diff --git a/core/module/HookManager.py b/core/module/HookManager.py new file mode 100644 index 000000000..77a17b0aa --- /dev/null +++ b/core/module/HookManager.py @@ -0,0 +1,128 @@ +# -*- coding: utf-8 -*- + +""" + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 3 of the License, + or (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + See the GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, see <http://www.gnu.org/licenses/>. + + @author: mkaay + @interface-version: 0.1 +""" + +import traceback +from threading import RLock +from module.PluginThread import HookThread +from time import time + +class HookManager(): + def __init__(self, core): + self.core = core + + self.config = self.core.config + + self.log = self.core.log + self.plugins = [] + self.lock = RLock() + self.createIndex() + + def lock(func): + def new(*args): + args[0].lock.acquire() + res = func(*args) + args[0].lock.release() + return res + return new + + def try_catch(func): + def new(*args): + try: + return func(*args) + except Exception, e: + args[0].log.error(_("Error executing hooks: %s") % str(e)) + return new + + def createIndex(self): + + plugins = [] + for pluginClass in self.core.pluginManager.getHookPlugins(): + try: + #hookClass = getattr(plugin, plugin.__name__) + + if self.core.config.getPlugin(pluginClass.__name__, "load"): + #@TODO handle in pluginmanager + plugin = pluginClass(self.core) + plugins.append(plugin) + self.log.info(_("%s loaded, activated %s") % (pluginClass.__name__, plugin.isActivated() )) + except: + self.log.warning(_("Failed activating %(name)s") % {"name":pluginClass.__name__}) + if self.core.debug: + traceback.print_exc() + + self.plugins = plugins + + + @try_catch + def periodical(self): + for plugin in self.plugins: + if plugin.isActivated() and plugin.lastCall + plugin.interval < time(): + plugin.periodical() + plugin.lastCall = time() + + + @try_catch + def coreReady(self): + for plugin in self.plugins: + if plugin.isActivated(): + plugin.coreReady() + + @lock + def downloadStarts(self, pyfile): + + for plugin in self.plugins: + if plugin.isActivated(): + plugin.downloadStarts(pyfile) + + @lock + def downloadFinished(self, pyfile): + + for plugin in self.plugins: + if plugin.isActivated(): + if "downloadFinished" in plugin.__threaded__: + self.startThread(plugin.downloadFinished, pyfile) + else: + plugin.downloadFinished(pyfile) + + @lock + def packageFinished(self, package): + + for plugin in self.plugins: + if plugin.isActivated(): + if "packageFinished" in plugin.__threaded__: + self.startThread(plugin.packageFinished, package) + else: + plugin.packageFinished(package) + + @lock + def beforeReconnecting(self, ip): + + for plugin in self.plugins: + plugin.beforeReconnecting(ip) + + @lock + def afterReconnecting(self, ip): + + for plugin in self.plugins: + if plugin.isActivated(): + plugin.afterReconnecting(ip) + + def startThread(self, function, pyfile): + t = HookThread(self.core.threadManager, function, pyfile) |