diff options
author | RaNaN <Mast3rRaNaN@hotmail.de> | 2013-11-17 20:46:52 +0100 |
---|---|---|
committer | RaNaN <Mast3rRaNaN@hotmail.de> | 2013-11-17 20:46:52 +0100 |
commit | 07f061bf96992a447ad535460ad6053d15a86888 (patch) | |
tree | a3137c54187aaf93289b9016ca0f3b2598a8b1db | |
parent | Merge pull request #400 from vuolter/m/c_logging (diff) | |
download | pyload-07f061bf96992a447ad535460ad6053d15a86888.tar.xz |
adapted multihoster to new plugin manager
-rw-r--r-- | pyload/PluginManager.py | 47 | ||||
-rw-r--r-- | pyload/datatypes/PyFile.py | 2 | ||||
-rw-r--r-- | pyload/plugins/Account.py | 7 | ||||
-rw-r--r-- | pyload/plugins/Base.py | 2 | ||||
-rw-r--r-- | pyload/plugins/addons/MultiHoster.py | 42 | ||||
-rw-r--r-- | pyload/plugins/hoster/Premium4Me.py | 4 | ||||
-rw-r--r-- | pyload/threads/InfoThread.py | 2 | ||||
-rw-r--r-- | pyload/utils/PluginLoader.py | 3 | ||||
-rw-r--r-- | pyload/web/app/scripts/helpers/gettext.js | 2 | ||||
-rw-r--r-- | tests/CrypterPluginTester.py | 2 |
10 files changed, 69 insertions, 44 deletions
diff --git a/pyload/PluginManager.py b/pyload/PluginManager.py index 2e3c66e03..297c67823 100644 --- a/pyload/PluginManager.py +++ b/pyload/PluginManager.py @@ -25,10 +25,12 @@ class PluginMatcher(object): """ Abstract class that allows modify which plugins to match and to load """ def matchURL(self, url): - return None + """ Returns (type, name) of a plugin if a match is found """ + return - def getPlugin(self, plugin, name): - return False + def matchPlugin(self, plugin, name): + """ Returns (type, name) of the plugin that will be loaded instead """ + return None class PluginManager: @@ -88,6 +90,7 @@ class PluginManager: found = False + # search the history for ptype, name in self.history: if self.loader.getPlugin(ptype, name).re.match(url): res[ptype].append((url, name)) @@ -100,15 +103,30 @@ class PluginManager: self.history.insert(0, found) continue + + # matcher won't go to history + for m in self.matcher: + match = m.matchURL(url) + if match and match[0] in res: + ptype, name = match + res[ptype].append((url, name)) + found = True + break + + if found: + continue + for ptype in ("crypter", "hoster"): for loader in self.loader: for name, plugin in loader.getPlugins(ptype).iteritems(): if plugin.re.match(url): res[ptype].append((url, name)) self.history.insert(0, (ptype, name)) - del self.history[10:] # cut down to size of 10 + del self.history[self.MATCH_HISTORY:] # cut down to size of 10 found = True break + if found: break + if found: break if not found: res["hoster"].append((url, self.DEFAULT_PLUGIN)) @@ -122,23 +140,18 @@ class PluginManager: plugins.update(loader.getPlugins(plugin)) return plugins - def findPlugin(self, name, pluginlist=("hoster", "crypter")): - # TODO: use matcher - for loader in self.loader: - for plugin in pluginlist: - if loader.hasPlugin(plugin, name): - return plugin, loader.getPlugin(plugin, name) - - return None, None - - def getPluginClass(self, name, overwrite=True): + def getPluginClass(self, plugin, name, overwrite=True): """Gives the plugin class of a hoster or crypter plugin :param overwrite: allow the use of overwritten plugins """ - # TODO: use matcher - type, plugin = self.findPlugin(name) - return self.loadClass(type, name) + if overwrite: + for m in self.matcher: + match = m.matchPlugin(plugin, name) + if match: + plugin, name = match + + return self.loadClass(plugin, name) def loadAttributes(self, plugin, name): for loader in self.loader: diff --git a/pyload/datatypes/PyFile.py b/pyload/datatypes/PyFile.py index 3ce114beb..3068b8251 100644 --- a/pyload/datatypes/PyFile.py +++ b/pyload/datatypes/PyFile.py @@ -139,7 +139,7 @@ class PyFile(object): def initPlugin(self): """ inits plugin instance """ if not self.plugin: - self.pluginclass = self.m.core.pluginManager.getPluginClass(self.pluginname) + self.pluginclass = self.m.core.pluginManager.getPluginClass("hoster", self.pluginname) self.plugin = self.pluginclass(self) @read_lock diff --git a/pyload/plugins/Account.py b/pyload/plugins/Account.py index ed0769fb4..d62825cad 100644 --- a/pyload/plugins/Account.py +++ b/pyload/plugins/Account.py @@ -212,9 +212,6 @@ class Account(Base): finally: req.close() - self.logDebug("Account Info: %s" % str(infos)) - self.timestamp = time() - self.restoreDefaults() # reset to initial state if type(infos) == dict: # copy result from dict to class for k, v in infos.iteritems(): @@ -223,6 +220,10 @@ class Account(Base): else: self.logDebug("Unknown attribute %s=%s" % (k, v)) + self.logDebug("Account Info: %s" % str(infos)) + self.timestamp = time() + self.core.evm.dispatchEvent("account:loaded", self.toInfoData()) + #TODO: remove user def loadAccountInfo(self, req): """ Overwrite this method and set account attributes within this method. diff --git a/pyload/plugins/Base.py b/pyload/plugins/Base.py index abb59a7bc..ae8614b0d 100644 --- a/pyload/plugins/Base.py +++ b/pyload/plugins/Base.py @@ -73,7 +73,7 @@ class Base(object): #: Url to service (to buy premium) for accounts __ref_url__ = "" - __author_name__ = tuple() + __author__ = tuple() __author_mail__ = tuple() diff --git a/pyload/plugins/addons/MultiHoster.py b/pyload/plugins/addons/MultiHoster.py index 2d4029dd6..f3d88a50d 100644 --- a/pyload/plugins/addons/MultiHoster.py +++ b/pyload/plugins/addons/MultiHoster.py @@ -2,12 +2,13 @@ # -*- coding: utf-8 -*- import re -from types import MethodType +from random import choice from pyload.plugins.MultiHoster import MultiHoster as MultiHosterAccount, normalize from pyload.plugins.Addon import Addon, AddEventListener from pyload.PluginManager import PluginMatcher + class MultiHoster(Addon, PluginMatcher): __version__ = "0.1" __internal__ = True @@ -17,13 +18,26 @@ class MultiHoster(Addon, PluginMatcher): __author_mail__ = ("support@pyload.org",) #TODO: multiple accounts - multihoster / config options - # TODO: rewrite for new plugin manager def init(self): - - # overwritten plugins + # overwritten plugins mapped to list of multihoster self.plugins = {} + # multihoster mapped to new regexp + self.regexp = {} + + def matchURL(self, url): + """ Overwritten to include new plugin regexp """ + for hoster, regexp in self.regexp.iteritems(): + if regexp.search(url): + return "hoster", hoster + + def matchPlugin(self, plugin, name): + """ Overwritten to overwrite already supported plugins """ + # Chooses a random multi hoster plugin + if name in self.plugins: + return plugin, choice(self.plugins[name]) + def addHoster(self, account): self.logInfo(_("Activated %s") % account.__name__) @@ -47,12 +61,15 @@ class MultiHoster(Addon, PluginMatcher): account.logError(_("No Hoster loaded")) return - klass = self.core.pluginManager.getPluginClass(account.__name__) + klass = self.core.pluginManager.getPluginClass("hoster", account.__name__, overwrite=False) # inject plugin plugin account.logDebug("Overwritten Hosters: %s" % ", ".join(sorted(supported))) for hoster in supported: - self.plugins[hoster] = klass + if hoster in self.plugins: + self.plugins[hoster].append(klass.__name__) + else: + self.plugins[hoster] = [klass.__name__] account.logDebug("New Hosters: %s" % ", ".join(sorted(new_supported))) @@ -62,13 +79,7 @@ class MultiHoster(Addon, PluginMatcher): if klass.__pattern__: patterns.append(klass.__pattern__) - regexp = r".*(%s).*" % "|".join(patterns) - - # recreate plugin tuple for new regexp - hoster = self.core.pluginManager.getPlugins("hoster") - p = hoster[account.__name__] - new = PluginTuple(p.version, re.compile(regexp), p.deps, p.category, p.user, p.path) - hoster[account.__name__] = new + self.regexp[klass.__name__] = re.compile(r".*(%s).*" % "|".join(patterns)) @AddEventListener("account:deleted") @@ -80,7 +91,7 @@ class MultiHoster(Addon, PluginMatcher): if isinstance(account, MultiHosterAccount) and account.isUsable(): self.addHoster(account) - @AddEventListener("account:updated") + @AddEventListener("account:loaded") def refreshAccount(self, acc): account = self.core.accountManager.getAccount(acc.plugin, acc.loginname) @@ -89,9 +100,8 @@ class MultiHoster(Addon, PluginMatcher): def activate(self): self.refreshAccounts() - self.core.pluginManager.addMatcher(self) - def deactivate(self): + def deactivate(self): self.core.pluginManager.removeMatcher(self) diff --git a/pyload/plugins/hoster/Premium4Me.py b/pyload/plugins/hoster/Premium4Me.py index d6c154693..b3920192c 100644 --- a/pyload/plugins/hoster/Premium4Me.py +++ b/pyload/plugins/hoster/Premium4Me.py @@ -5,8 +5,8 @@ from urllib import quote from os.path import exists from os import remove -from module.plugins.Hoster import Hoster -from module.utils import fs_encode +from pyload.plugins.Hoster import Hoster +from pyload.utils.fs import fs_encode class Premium4Me(Hoster): diff --git a/pyload/threads/InfoThread.py b/pyload/threads/InfoThread.py index f39ac41f2..62e309020 100644 --- a/pyload/threads/InfoThread.py +++ b/pyload/threads/InfoThread.py @@ -57,7 +57,7 @@ class InfoThread(DecrypterThread): for pluginname, urls in plugins.iteritems(): plugin = self.m.core.pluginManager.loadModule("hoster", pluginname) - klass = self.m.core.pluginManager.getPluginClass(pluginname) + klass = self.m.core.pluginManager.getPluginClass("hoster", pluginname, overwrite=False) if has_method(klass, "getInfo"): self.fetchForPlugin(klass, urls, cb) # TODO: this branch can be removed in the future diff --git a/pyload/utils/PluginLoader.py b/pyload/utils/PluginLoader.py index 038ac9b23..cb1039443 100644 --- a/pyload/utils/PluginLoader.py +++ b/pyload/utils/PluginLoader.py @@ -164,7 +164,8 @@ class PluginLoader: self.log.debug(str(e)) if not hasattr(Base, "__%s__" % m[0]): - if m[0] != "type": #TODO remove type from all plugins, its not needed + #TODO remove type from all plugins, its not needed + if m[0] != "type" and m[0] != "author_name": self.logDebug(folder, name, "Unknown attribute '%s'" % m[0]) return attrs diff --git a/pyload/web/app/scripts/helpers/gettext.js b/pyload/web/app/scripts/helpers/gettext.js index d73b5e378..8a3fea5ed 100644 --- a/pyload/web/app/scripts/helpers/gettext.js +++ b/pyload/web/app/scripts/helpers/gettext.js @@ -1,4 +1,4 @@ -require(['underscore', 'handlebars', 'utils/i18n'], function(_, Handlebars, i18n) { +define(['underscore', 'handlebars', 'utils/i18n'], function(_, Handlebars, i18n) { 'use strict'; // These methods binds additional content directly to translated message function ngettext(single, plural, n) { diff --git a/tests/CrypterPluginTester.py b/tests/CrypterPluginTester.py index 42585939e..c63b173ef 100644 --- a/tests/CrypterPluginTester.py +++ b/tests/CrypterPluginTester.py @@ -18,7 +18,7 @@ class CrypterPluginTester(PluginTester): print "%s: %s" % (name, url.encode("utf8")) log(DEBUG, "%s: %s", name, url.encode("utf8")) - plugin = self.core.pluginManager.getPluginClass(name) + plugin = self.core.pluginManager.getPluginClass("crypter", name) p = plugin(self.core, None, "") self.thread.plugin = p |