summaryrefslogtreecommitdiffstats
path: root/pyload
diff options
context:
space:
mode:
authorGravatar Walter Purcaro <vuolter@gmail.com> 2014-11-16 00:17:49 +0100
committerGravatar Walter Purcaro <vuolter@gmail.com> 2014-11-16 00:17:49 +0100
commitf975a1b5dd34b3fd6420f68d6d26eedd4d4cf83b (patch)
tree766f1eecd1b9d86a7334039937d9ac54401a6889 /pyload
parentSpare code cosmetics (diff)
downloadpyload-f975a1b5dd34b3fd6420f68d6d26eedd4d4cf83b.tar.xz
[PluginManager] Improve getPlugin
Diffstat (limited to 'pyload')
-rw-r--r--pyload/api/__init__.py16
-rw-r--r--pyload/database/FileDatabase.py4
-rw-r--r--pyload/datatype/PyFile.py17
-rw-r--r--pyload/manager/PluginManager.py100
-rw-r--r--pyload/manager/thread/InfoThread.py33
-rw-r--r--pyload/plugins/internal/MultiHoster.py4
6 files changed, 100 insertions, 74 deletions
diff --git a/pyload/api/__init__.py b/pyload/api/__init__.py
index 570dc5eac..017a9a939 100644
--- a/pyload/api/__init__.py
+++ b/pyload/api/__init__.py
@@ -27,7 +27,7 @@ from pyload.datatype.PyFile import PyFile
from pyload.utils.packagetools import parseNames
from network.RequestFactory import getURL
from remote import activated
-from utils import compare_time, freeSpace, html_unescape, save_path
+from utils import compare_time, freeSpace, html_unescape, save_filename
if activated:
try:
@@ -322,7 +322,7 @@ class Api(Iface):
else:
folder = ""
- folder = save_path(folder)
+ folder = save_filename(folder)
pid = self.core.files.addPackage(name, folder, dest)
@@ -364,11 +364,11 @@ class Api(Iface):
data = self.core.pluginManager.parseUrls(urls)
plugins = {}
- for url, plugin in data:
- if plugin in plugins:
- plugins[plugin].append(url)
- else:
- plugins[plugin] = [url]
+ for url, plugintype, pluginname in data:
+ try:
+ plugins[plugintype][pluginname].append(url)
+ except:
+ plugins[plugintype][pluginname] = [url]
return plugins
@@ -383,7 +383,7 @@ class Api(Iface):
rid = self.core.threadManager.createResultThread(data, False)
- tmp = [(url, (url, OnlineStatus(url, pluginname, "unknown", 3, 0))) for url, pluginname in data]
+ tmp = [(url, (url, OnlineStatus(url, (plugintype, pluginname), "unknown", 3, 0))) for url, plugintype, pluginname in data]
data = parseNames(tmp)
result = {}
diff --git a/pyload/database/FileDatabase.py b/pyload/database/FileDatabase.py
index fdffe3fe7..5bd1e5a5f 100644
--- a/pyload/database/FileDatabase.py
+++ b/pyload/database/FileDatabase.py
@@ -612,7 +612,7 @@ class FileMethods:
@style.queue
def addLink(self, url, name, plugin, package):
order = self._nextFileOrder(package)
- self.c.execute('INSERT INTO links(url, name, plugin, package, linkorder) VALUES(?,?,?,?,?)', (url, name, plugin, package, order))
+ self.c.execute('INSERT INTO links(url, name, plugin, package, linkorder) VALUES(?,?,?,?,?)', (url, name, (plugintype, pluginname), package, order))
return self.c.lastrowid
@style.queue
@@ -620,7 +620,7 @@ class FileMethods:
""" links is a list of tupels (url, plugin)"""
order = self._nextFileOrder(package)
orders = [order + x for x in range(len(links))]
- links = [(x[0], x[0], x[1], package, o) for x, o in zip(links, orders)]
+ links = [(x[0], x[0], (x[1], x[2]), package, o) for x, o in zip(links, orders)]
self.c.executemany('INSERT INTO links(url, name, plugin, package, linkorder) VALUES(?,?,?,?,?)', links)
@style.queue
diff --git a/pyload/datatype/PyFile.py b/pyload/datatype/PyFile.py
index be3129681..e8916a921 100644
--- a/pyload/datatype/PyFile.py
+++ b/pyload/datatype/PyFile.py
@@ -49,11 +49,12 @@ class PyFile(object):
"""
Represents a file object at runtime
"""
- __slots__ = ("m", "id", "url", "name", "size", "_size", "status", "pluginname", "packageid",
- "error", "order", "lock", "plugin", "waitUntil", "active", "abort", "statusname",
- "reconnected", "progress", "maxprogress", "pluginmodule", "pluginclass")
+ __slots__ = ("m", "id", "url", "name", "size", "_size", "status", "plugin",
+ "packageid", "error", "order", "lock", "plugin", "waitUntil",
+ "active", "abort", "statusname", "reconnected", "progress",
+ "maxprogress", "pluginmodule", "pluginclass")
- def __init__(self, manager, id, url, name, size, status, error, pluginname, package, order):
+ def __init__(self, manager, id, url, name, size, status, error, plugin, package, order):
self.m = manager
self.id = int(id)
@@ -61,7 +62,7 @@ class PyFile(object):
self.name = name
self.size = size
self.status = status
- self.pluginname = pluginname
+ self.plugin = self.plugintype, self.pluginname = plugin
self.packageid = package #should not be used, use package() instead
self.error = error
self.order = order
@@ -97,9 +98,9 @@ class PyFile(object):
def initPlugin(self):
""" inits plugin instance """
if not self.plugin:
- self.pluginmodule = self.m.core.pluginManager.getPlugin(self.pluginname)
- self.pluginclass = getattr(self.pluginmodule, self.m.core.pluginManager.getPluginName(self.pluginname))
- self.plugin = self.pluginclass(self)
+ self.pluginmodule = self.m.core.pluginManager.getPlugin(self.plugintype, self.pluginname)
+ self.pluginclass = getattr(self.pluginmodule, self.m.core.pluginManager.getPluginName(self.plugintype, self.pluginname))
+ self.plugin = self.pluginclass(self)
@lock
def hasPlugin(self):
diff --git a/pyload/manager/PluginManager.py b/pyload/manager/PluginManager.py
index 8f837587a..79c3d6be6 100644
--- a/pyload/manager/PluginManager.py
+++ b/pyload/manager/PluginManager.py
@@ -5,7 +5,7 @@ import sys
from itertools import chain
from os import listdir, makedirs
-from os.path import isfile, join, exists, abspath
+from os.path import isdir, isfile, join, exists, abspath
from sys import version_info
from traceback import print_exc
@@ -15,7 +15,7 @@ from SafeEval import const_eval as literal_eval
class PluginManager:
ROOT = "pyload.plugins."
USERROOT = "userplugins."
- TYPES = ("account", "addon", "container", "crypter", "hook", "hoster", "internal", "ocr")
+ TYPES = []
PATTERN = re.compile(r'__pattern__\s*=\s*u?r("|\')([^"\']+)')
VERSION = re.compile(r'__version__\s*=\s*("|\')([\d.]+)')
@@ -33,11 +33,27 @@ class PluginManager:
sys.meta_path.append(self)
+ def initTYPES(self):
+ rootdir = join(pypath, "pyload", "plugins")
+ userdir = "userplugins"
+
+ tmpset = set()
+ for p in (rootdir, userdir):
+ tmpset += set([d for d in listdir(p) if isdir(join(p, d))])
+
+ self.TYPES = list(tmpset)
+
+
def createIndex(self):
"""create information for all plugins available"""
sys.path.append(abspath(""))
+ self.initTYPES()
+
+ if not self.TYPES:
+ self.log.critical(_("No TYPES, no fun!"))
+
for type in set(self.TYPES):
self.plugins[type] = self.parse(type)
setattr(self, "%sPlugins" % type, self.plugins[type])
@@ -177,61 +193,70 @@ class PluginManager:
"""parse plugins for given list of urls"""
last = None
- res = [] # tupels of (url, plugin)
+ res = [] #: tupels of (url, plugintype, pluginname)
for url in urls:
- if type(url) not in (str, unicode, buffer): continue
- found = False
+ if type(url) not in (str, unicode, buffer):
+ continue
- if last and last[1]['re'].match(url):
- res.append((url, last[0]))
+ if last and last[2]['re'].match(url):
+ res.append((url, last[0], last[1]))
continue
- for name, value in (self.crypterPlugins.iteritems(),
- self.hosterPlugins.iteritems(),
- self.containerPlugins.iteritems()):
- try:
- m = value['re'].match(url)
- except KeyError:
- self.core.log.error("Plugin %s skipped due broken pattern" % name)
+ for type in self.TYPES:
+ for name, plugin in self.plugins[type]:
+
m = None
+ try:
+ if 'pattern' in plugin:
+ m = plugin['re'].match(url)
- if m:
- res.append((url, name))
- last = (name, value)
- found = True
- break
+ except KeyError:
+ self.core.log.error(_("Plugin [%(type)s] %(name)s skipped due broken pattern")
+ % {'name': name, 'type': type})
- if not found:
- res.append((url, "BasePlugin"))
+ if m:
+ res.append((url, type, name))
+ last = (type, name, plugin)
+ break
+ else:
+ res.append((url, "internal", "BasePlugin"))
return res
- def findPlugin(self, name, pluginlist=("hoster", "crypter", "container")):
- for ptype in pluginlist:
- if name in self.plugins[ptype]:
- return self.plugins[ptype][name], ptype
- return None, None
+ def findPlugin(self, type, name):
+ if type not in self.plugins:
+ return None
+
+ elif name not in self.plugins[type]:
+ self.core.log.warning(_("Plugin [%(type)s] %(name)s not found | Using plugin: [internal] BasePlugin")
+ % {'name': name, 'type': type})
+ return self.internalPlugins["BasePlugin"]
+
+ else:
+ return self.plugins[type][name]
- def getPlugin(self, name, original=False):
+ def getPlugin(self, type, name, original=False):
"""return plugin module from hoster|decrypter|container"""
- plugin, type = self.findPlugin(name)
+ plugin = self.findPlugin(type, name)
- if not plugin:
- self.core.log.warning("Plugin %s not found" % name)
- plugin = self.internalPlugins['BasePlugin']
+ if plugin is None:
+ return {}
if "new_module" in plugin and not original:
return plugin['new_module']
-
- return self.loadModule(type, name)
+ else:
+ return self.loadModule(type, name)
- def getPluginName(self, name):
+ def getPluginName(self, type, name):
""" used to obtain new name if other plugin was injected"""
- plugin, type = self.findPlugin(name)
+ plugin = self.findPlugin(type, name)
+
+ if plugin is None:
+ return ""
if "new_name" in plugin:
return plugin['new_name']
@@ -246,6 +271,7 @@ class PluginManager:
:param name:
"""
plugins = self.plugins[type]
+
if name in plugins:
if "module" in plugins[name]:
return plugins[name]['module']
@@ -256,7 +282,7 @@ class PluginManager:
except Exception, e:
self.core.log.error(_("Error importing plugin: [%(type)s] %(name)s (v%(version).2f) | %(errmsg)s")
- % {'name': name, 'type': type, 'version': plugins[name]['version'], "errmsg": str(e)})
+ % {'name': name, 'type': type, 'version': plugins[name]['version'], "errmsg": str(e)})
if self.core.debug:
print_exc()
@@ -264,7 +290,7 @@ class PluginManager:
plugins[name]['module'] = module #: cache import, maybe unneeded
self.core.log.debug(_("Loaded plugin: [%(type)s] %(name)s (v%(version).2f)")
- % {'name': name, 'type': type, 'version': plugins[name]['version']})
+ % {'name': name, 'type': type, 'version': plugins[name]['version']})
return module
diff --git a/pyload/manager/thread/InfoThread.py b/pyload/manager/thread/InfoThread.py
index 1916b4b36..1775e2a86 100644
--- a/pyload/manager/thread/InfoThread.py
+++ b/pyload/manager/thread/InfoThread.py
@@ -61,12 +61,11 @@ class InfoThread(PluginThread):
plugins = {}
container = []
- for url, plugin in self.data:
- if plugin in plugins:
- plugins[plugin].append(url)
- else:
- plugins[plugin] = [url]
-
+ for url, plugintype, pluginname in data:
+ try:
+ plugins[plugintype][pluginname].append(url)
+ except:
+ plugins[plugintype][pluginname] = [url]
# filter out container plugins
for name in self.m.core.pluginManager.containerPlugins:
@@ -77,15 +76,15 @@ class InfoThread(PluginThread):
#directly write to database
if self.pid > -1:
- for pluginname, urls in plugins.iteritems():
- plugin = self.m.core.pluginManager.getPlugin(pluginname, True)
+ for plugintype, pluginname, urls in plugins.iteritems():
+ plugin = self.m.core.pluginManager.getPlugin(plugintype, pluginname, True)
if hasattr(plugin, "getInfo"):
self.fetchForPlugin(pluginname, plugin, urls, self.updateDB)
self.m.core.files.save()
elif self.add:
- for pluginname, urls in plugins.iteritems():
- plugin = self.m.core.pluginManager.getPlugin(pluginname, True)
+ for plugintype, pluginname, urls in plugins.iteritems():
+ plugin = self.m.core.pluginManager.getPlugin(plugintype, pluginname, True)
if hasattr(plugin, "getInfo"):
self.fetchForPlugin(pluginname, plugin, urls, self.updateCache, True)
@@ -117,16 +116,16 @@ class InfoThread(PluginThread):
self.m.log.error("Could not decrypt container.")
data = []
- for url, plugin in data:
- if plugin in plugins:
- plugins[plugin].append(url)
- else:
- plugins[plugin] = [url]
+ for url, plugintype, pluginname in data:
+ try:
+ plugins[plugintype][pluginname].append(url)
+ except:
+ plugins[plugintype][pluginname] = [url]
self.m.infoResults[self.rid] = {}
- for pluginname, urls in plugins.iteritems():
- plugin = self.m.core.pluginManager.getPlugin(pluginname, True)
+ for plugintype, pluginname, urls in plugins.iteritems():
+ plugin = self.m.core.pluginManager.getPlugin(plugintype, pluginname, True)
if hasattr(plugin, "getInfo"):
self.fetchForPlugin(pluginname, plugin, urls, self.updateResult, True)
diff --git a/pyload/plugins/internal/MultiHoster.py b/pyload/plugins/internal/MultiHoster.py
index 02594e04e..4eb4a6f31 100644
--- a/pyload/plugins/internal/MultiHoster.py
+++ b/pyload/plugins/internal/MultiHoster.py
@@ -145,7 +145,7 @@ class MultiHoster(Addon):
self.logError(_("No Hoster loaded"))
return
- module = self.core.pluginManager.getPlugin(self.__name__)
+ module = self.core.pluginManager.getPlugin(self.__type__, self.__name__)
klass = getattr(module, self.__name__)
# inject plugin plugin
@@ -189,7 +189,7 @@ class MultiHoster(Addon):
self.unloadHoster(hoster)
# reset pattern
- klass = getattr(self.core.pluginManager.getPlugin(self.__name__), self.__name__)
+ klass = getattr(self.core.pluginManager.getPlugin(self.__type__, self.__name__), self.__name__)
dict = self.core.pluginManager.hosterPlugins[self.__name__]
dict['pattern'] = getattr(klass, "__pattern__", r'^unmatchable$')
dict['re'] = re.compile(dict['pattern'])