summaryrefslogtreecommitdiffstats
path: root/module/database
diff options
context:
space:
mode:
Diffstat (limited to 'module/database')
-rw-r--r--module/database/DatabaseBackend.py164
-rw-r--r--module/database/File.py (renamed from module/database/FileDatabase.py)209
-rw-r--r--module/database/Storage.py (renamed from module/database/StorageDatabase.py)29
-rw-r--r--module/database/User.py (renamed from module/database/UserDatabase.py)31
-rw-r--r--module/database/__init__.py11
5 files changed, 142 insertions, 302 deletions
diff --git a/module/database/DatabaseBackend.py b/module/database/DatabaseBackend.py
index 9530390c3..4b63dd284 100644
--- a/module/database/DatabaseBackend.py
+++ b/module/database/DatabaseBackend.py
@@ -1,23 +1,7 @@
-#!/usr/bin/env python
-"""
- 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: RaNaN
- @author: mkaay
-"""
-from threading import Thread
-from threading import Event
+# -*- coding: utf-8 -*-
+# @author: RaNaN, mkaay
+
+from threading import Event, Thread
from os import remove
from os.path import exists
from shutil import move
@@ -25,22 +9,22 @@ from shutil import move
from Queue import Queue
from traceback import print_exc
-from module.utils import chmod
+from pyload.utils import chmod
try:
from pysqlite2 import dbapi2 as sqlite3
-except:
+except Exception:
import sqlite3
DB_VERSION = 4
-class style():
+class style(object):
db = None
-
+
@classmethod
def setDB(cls, db):
cls.db = db
-
+
@classmethod
def inner(cls, f):
@staticmethod
@@ -48,7 +32,7 @@ class style():
if cls.db:
return f(cls.db, *args, **kwargs)
return x
-
+
@classmethod
def queue(cls, f):
@staticmethod
@@ -56,7 +40,7 @@ class style():
if cls.db:
return cls.db.queue(f, *args, **kwargs)
return x
-
+
@classmethod
def async(cls, f):
@staticmethod
@@ -65,14 +49,14 @@ class style():
return cls.db.async(f, *args, **kwargs)
return x
-class DatabaseJob():
+class DatabaseJob(object):
def __init__(self, f, *args, **kwargs):
self.done = Event()
-
+
self.f = f
self.args = args
self.kwargs = kwargs
-
+
self.result = None
self.exception = False
@@ -98,13 +82,13 @@ class DatabaseJob():
print_exc()
try:
print "Database Error @", self.f.__name__, self.args[1:], self.kwargs, e
- except:
+ except Exception:
pass
self.exception = e
finally:
self.done.set()
-
+
def wait(self):
self.done.wait()
@@ -116,34 +100,34 @@ class DatabaseBackend(Thread):
self.core = core
self.jobs = Queue()
-
+
self.setuplock = Event()
-
+
style.setDB(self)
-
+
def setup(self):
self.start()
self.setuplock.wait()
-
+
def run(self):
"""main loop, which executes commands"""
convert = self._checkVersion() #returns None or current version
-
+
self.conn = sqlite3.connect("files.db")
chmod("files.db", 0600)
self.c = self.conn.cursor() #compatibility
-
+
if convert is not None:
self._convertDB(convert)
-
+
self._createTables()
self._migrateUser()
self.conn.commit()
-
+
self.setuplock.set()
-
+
while True:
j = self.jobs.get()
if j == "quit":
@@ -164,7 +148,7 @@ class DatabaseBackend(Thread):
f.write(str(DB_VERSION))
f.close()
return
-
+
f = open("files.version", "rb")
v = int(f.read().strip())
f.close()
@@ -172,7 +156,7 @@ class DatabaseBackend(Thread):
if v < 2:
try:
self.manager.core.log.warning(_("Filedatabase was deleted due to incompatible version."))
- except:
+ except Exception:
print "Filedatabase was deleted due to incompatible version."
remove("files.version")
move("files.db", "files.backup.db")
@@ -180,35 +164,35 @@ class DatabaseBackend(Thread):
f.write(str(DB_VERSION))
f.close()
return v
-
+
def _convertDB(self, v):
try:
getattr(self, "_convertV%i" % v)()
- except:
+ except Exception:
try:
self.core.log.error(_("Filedatabase could NOT be converted."))
- except:
+ except Exception:
print "Filedatabase could NOT be converted."
-
- #--convert scripts start
-
+
+ #convert scripts start-----------------------------------------------------
+
def _convertV2(self):
self.c.execute('CREATE TABLE IF NOT EXISTS "storage" ("id" INTEGER PRIMARY KEY AUTOINCREMENT, "identifier" TEXT NOT NULL, "key" TEXT NOT NULL, "value" TEXT DEFAULT "")')
try:
self.manager.core.log.info(_("Database was converted from v2 to v3."))
- except:
+ except Exception:
print "Database was converted from v2 to v3."
self._convertV3()
-
+
def _convertV3(self):
self.c.execute('CREATE TABLE IF NOT EXISTS "users" ("id" INTEGER PRIMARY KEY AUTOINCREMENT, "name" TEXT NOT NULL, "email" TEXT DEFAULT "" NOT NULL, "password" TEXT NOT NULL, "role" INTEGER DEFAULT 0 NOT NULL, "permission" INTEGER DEFAULT 0 NOT NULL, "template" TEXT DEFAULT "default" NOT NULL)')
try:
self.manager.core.log.info(_("Database was converted from v3 to v4."))
- except:
+ except Exception:
print "Database was converted from v3 to v4."
-
- #--convert scripts end
-
+
+ #convert scripts end-------------------------------------------------------
+
def _createTables(self):
"""create tables for database"""
@@ -222,7 +206,7 @@ class DatabaseBackend(Thread):
SELECT p.id AS id, SUM(l.size) AS sizetotal, COUNT(l.id) AS linkstotal, linksdone, sizedone\
FROM packages p JOIN links l ON p.id = l.package LEFT OUTER JOIN\
(SELECT p.id AS id, COUNT(*) AS linksdone, SUM(l.size) AS sizedone \
- FROM packages p JOIN links l ON p.id = l.package AND l.status in (0,4,13) GROUP BY p.id) s ON s.id = p.id \
+ FROM packages p JOIN links l ON p.id = l.package AND l.status in (0, 4, 13) GROUP BY p.id) s ON s.id = p.id \
GROUP BY p.id')
#try to lower ids
@@ -250,7 +234,7 @@ class DatabaseBackend(Thread):
if exists("pyload.db"):
try:
self.core.log.info(_("Converting old Django DB"))
- except:
+ except Exception:
print "Converting old Django DB"
conn = sqlite3.connect('pyload.db')
c = conn.cursor()
@@ -264,10 +248,10 @@ class DatabaseBackend(Thread):
self.c.executemany("INSERT INTO users(name, password, email) VALUES (?, ?, ?)", users)
move("pyload.db", "pyload.old.db")
-
+
def createCursor(self):
return self.conn.cursor()
-
+
@style.async
def commit(self):
self.conn.commit()
@@ -275,78 +259,32 @@ class DatabaseBackend(Thread):
@style.queue
def syncSave(self):
self.conn.commit()
-
+
@style.async
def rollback(self):
self.conn.rollback()
-
+
def async(self, f, *args, **kwargs):
- args = (self, ) + args
+ args = (self,) + args
job = DatabaseJob(f, *args, **kwargs)
self.jobs.put(job)
-
+
def queue(self, f, *args, **kwargs):
- args = (self, ) + args
+ args = (self,) + args
job = DatabaseJob(f, *args, **kwargs)
self.jobs.put(job)
job.wait()
return job.result
-
+
@classmethod
def registerSub(cls, klass):
cls.subs.append(klass)
-
+
@classmethod
def unregisterSub(cls, klass):
cls.subs.remove(klass)
-
+
def __getattr__(self, attr):
for sub in DatabaseBackend.subs:
if hasattr(sub, attr):
return getattr(sub, attr)
-
-if __name__ == "__main__":
- db = DatabaseBackend()
- db.setup()
-
- class Test():
- @style.queue
- def insert(db):
- c = db.createCursor()
- for i in range(1000):
- c.execute("INSERT INTO storage (identifier, key, value) VALUES (?, ?, ?)", ("foo", i, "bar"))
- @style.async
- def insert2(db):
- c = db.createCursor()
- for i in range(1000*1000):
- c.execute("INSERT INTO storage (identifier, key, value) VALUES (?, ?, ?)", ("foo", i, "bar"))
-
- @style.queue
- def select(db):
- c = db.createCursor()
- for i in range(10):
- res = c.execute("SELECT value FROM storage WHERE identifier=? AND key=?", ("foo", i))
- print res.fetchone()
-
- @style.queue
- def error(db):
- c = db.createCursor()
- print "a"
- c.execute("SELECT myerror FROM storage WHERE identifier=? AND key=?", ("foo", i))
- print "e"
-
- db.registerSub(Test)
- from time import time
- start = time()
- for i in range(100):
- db.insert()
- end = time()
- print end-start
-
- start = time()
- db.insert2()
- end = time()
- print end-start
-
- db.error()
-
diff --git a/module/database/FileDatabase.py b/module/database/File.py
index 7e7efb028..2b7c6cad9 100644
--- a/module/database/FileDatabase.py
+++ b/module/database/File.py
@@ -1,39 +1,22 @@
-#!/usr/bin/env python
-"""
- 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: RaNaN
- @author: mkaay
-"""
-
+# -*- coding: utf-8 -*-
+# @author: RaNaN, mkaay
from threading import RLock
from time import time
-from module.utils import formatSize, lock
-from module.PullEvents import InsertEvent, ReloadAllEvent, RemoveEvent, UpdateEvent
-from module.PyPackage import PyPackage
-from module.PyFile import PyFile
-from module.database import style, DatabaseBackend
+from pyload.utils import formatSize, lock
+from pyload.manager.Event import InsertEvent, ReloadAllEvent, RemoveEvent, UpdateEvent
+from pyload.datatype.Package import PyPackage
+from pyload.datatype.File import PyFile
+from pyload.database import style, DatabaseBackend
try:
from pysqlite2 import dbapi2 as sqlite3
-except:
+except Exception:
import sqlite3
-class FileHandler:
+class FileHandler(object):
"""Handles all request made to obtain information,
modify status or other request for links or packages"""
@@ -68,12 +51,12 @@ class FileHandler:
return func(*args)
return new
- #----------------------------------------------------------------------
+ #--------------------------------------------------------------------------
def save(self):
"""saves all data to backend"""
self.db.commit()
- #----------------------------------------------------------------------
+ #--------------------------------------------------------------------------
def syncSave(self):
"""saves all data to backend and waits until all data are written"""
pyfiles = self.cache.values()
@@ -121,7 +104,7 @@ class FileHandler:
def addLinks(self, urls, package):
"""adds links"""
- self.core.hookManager.dispatchEvent("linksAdded", urls, package)
+ self.core.addonManager.dispatchEvent("links-added", urls, package)
data = self.core.pluginManager.parseUrls(urls)
@@ -131,7 +114,7 @@ class FileHandler:
#@TODO change from reloadAll event to package update event
self.core.pullManager.addEvent(ReloadAllEvent("collector"))
- #----------------------------------------------------------------------
+ #--------------------------------------------------------------------------
@lock
@change
def addPackage(self, name, folder, queue=0):
@@ -142,7 +125,7 @@ class FileHandler:
self.core.pullManager.addEvent(e)
return lastID
- #----------------------------------------------------------------------
+ #--------------------------------------------------------------------------
@lock
@change
def deletePackage(self, id):
@@ -167,7 +150,7 @@ class FileHandler:
self.db.deletePackage(p)
self.core.pullManager.addEvent(e)
- self.core.hookManager.dispatchEvent("packageDeleted", id)
+ self.core.addonManager.dispatchEvent("package-deleted", id)
if id in self.packageCache:
del self.packageCache[id]
@@ -178,7 +161,7 @@ class FileHandler:
pack.order -= 1
pack.notifyChange()
- #----------------------------------------------------------------------
+ #--------------------------------------------------------------------------
@lock
@change
def deleteLink(self, id):
@@ -190,7 +173,7 @@ class FileHandler:
pid = f.packageid
e = RemoveEvent("file", id, "collector" if not f.package().queue else "queue")
-
+
oldorder = f.order
if id in self.core.threadManager.processingIds():
@@ -206,26 +189,26 @@ class FileHandler:
p = self.getPackage(pid)
if not len(p.getChildren()):
p.delete()
-
+
pyfiles = self.cache.values()
for pyfile in pyfiles:
if pyfile.packageid == pid and pyfile.order > oldorder:
pyfile.order -= 1
pyfile.notifyChange()
- #----------------------------------------------------------------------
+ #--------------------------------------------------------------------------
def releaseLink(self, id):
"""removes pyfile from cache"""
if id in self.cache:
del self.cache[id]
- #----------------------------------------------------------------------
+ #--------------------------------------------------------------------------
def releasePackage(self, id):
"""removes package from cache"""
if id in self.packageCache:
del self.packageCache[id]
- #----------------------------------------------------------------------
+ #--------------------------------------------------------------------------
def updateLink(self, pyfile):
"""updates link"""
self.db.updateLink(pyfile)
@@ -233,7 +216,7 @@ class FileHandler:
e = UpdateEvent("file", pyfile.id, "collector" if not pyfile.package().queue else "queue")
self.core.pullManager.addEvent(e)
- #----------------------------------------------------------------------
+ #--------------------------------------------------------------------------
def updatePackage(self, pypack):
"""updates a package"""
self.db.updatePackage(pypack)
@@ -241,7 +224,7 @@ class FileHandler:
e = UpdateEvent("pack", pypack.id, "collector" if not pypack.queue else "queue")
self.core.pullManager.addEvent(e)
- #----------------------------------------------------------------------
+ #--------------------------------------------------------------------------
def getPackage(self, id):
"""return package instance"""
@@ -250,7 +233,7 @@ class FileHandler:
else:
return self.db.getPackage(id)
- #----------------------------------------------------------------------
+ #--------------------------------------------------------------------------
def getPackageData(self, id):
"""returns dict with package information"""
pack = self.getPackage(id)
@@ -274,7 +257,7 @@ class FileHandler:
return pack
- #----------------------------------------------------------------------
+ #--------------------------------------------------------------------------
def getFileData(self, id):
"""returns dict with file information"""
if id in self.cache:
@@ -282,7 +265,7 @@ class FileHandler:
return self.db.getLinkData(id)
- #----------------------------------------------------------------------
+ #--------------------------------------------------------------------------
def getFile(self, id):
"""returns pyfile instance"""
if id in self.cache:
@@ -290,7 +273,7 @@ class FileHandler:
else:
return self.db.getFile(id)
- #----------------------------------------------------------------------
+ #--------------------------------------------------------------------------
@lock
def getJob(self, occ):
"""get suitable job"""
@@ -369,7 +352,7 @@ class FileHandler:
"""checks if all files are finished and dispatch event"""
if not self.getQueueCount(True):
- self.core.hookManager.dispatchEvent("allDownloadsFinished")
+ self.core.addonManager.dispatchEvent("all_downloads-finished")
self.core.log.debug("All downloads finished")
return True
@@ -382,7 +365,7 @@ class FileHandler:
self.resetCount()
if not self.db.processcount(1, fid):
- self.core.hookManager.dispatchEvent("allDownloadsProcessed")
+ self.core.addonManager.dispatchEvent("all_downloads-processed")
self.core.log.debug("All downloads processed")
return True
@@ -434,7 +417,7 @@ class FileHandler:
e = RemoveEvent("pack", id, "collector" if not p.queue else "queue")
self.core.pullManager.addEvent(e)
-
+
self.db.clearPackageOrder(p)
p = self.db.getPackage(id)
@@ -443,7 +426,7 @@ class FileHandler:
self.db.updatePackage(p)
self.db.reorderPackage(p, -1, True)
-
+
packs = self.packageCache.values()
for pack in packs:
if pack.queue != queue and pack.order > oldorder:
@@ -453,7 +436,7 @@ class FileHandler:
self.db.commit()
self.releasePackage(id)
p = self.getPackage(id)
-
+
e = InsertEvent("pack", id, p.order, "collector" if not p.queue else "queue")
self.core.pullManager.addEvent(e)
@@ -523,13 +506,13 @@ class FileHandler:
self.core.pullManager.addEvent(e)
def checkPackageFinished(self, pyfile):
- """ checks if package is finished and calls hookmanager """
+ """ checks if package is finished and calls AddonManager """
ids = self.db.getUnfinished(pyfile.packageid)
if not ids or (pyfile.id in ids and len(ids) == 1):
if not pyfile.package().setFinished:
self.core.log.info(_("Package finished: %s") % pyfile.package().name)
- self.core.hookManager.packageFinished(pyfile.package())
+ self.core.addonManager.packageFinished(pyfile.package())
pyfile.package().setFinished = True
@@ -540,7 +523,7 @@ class FileHandler:
urls = []
for pyfile in data.itervalues():
- if pyfile["status"] not in (0, 12, 13):
+ if pyfile["status"] not in (0, 12, 13):
urls.append((pyfile["url"], pyfile["plugin"]))
self.core.threadManager.createInfoThread(urls, pid)
@@ -573,23 +556,23 @@ class FileHandler:
""" restart all failed links """
self.db.restartFailed()
-class FileMethods():
+class FileMethods(object):
@style.queue
def filecount(self, queue):
"""returns number of files in queue"""
- self.c.execute("SELECT COUNT(*) FROM links as l INNER JOIN packages as p ON l.package=p.id WHERE p.queue=?", (queue, ))
+ self.c.execute("SELECT COUNT(*) FROM links as l INNER JOIN packages as p ON l.package=p.id WHERE p.queue=?", (queue,))
return self.c.fetchone()[0]
@style.queue
def queuecount(self, queue):
""" number of files in queue not finished yet"""
- self.c.execute("SELECT COUNT(*) FROM links as l INNER JOIN packages as p ON l.package=p.id WHERE p.queue=? AND l.status NOT IN (0,4)", (queue, ))
+ self.c.execute("SELECT COUNT(*) FROM links as l INNER JOIN packages as p ON l.package=p.id WHERE p.queue=? AND l.status NOT IN (0, 4)", (queue,))
return self.c.fetchone()[0]
@style.queue
def processcount(self, queue, fid):
""" number of files which have to be proccessed """
- self.c.execute("SELECT COUNT(*) FROM links as l INNER JOIN packages as p ON l.package=p.id WHERE p.queue=? AND l.status IN (2,3,5,7,12) AND l.id != ?", (queue, str(fid)))
+ self.c.execute("SELECT COUNT(*) FROM links as l INNER JOIN packages as p ON l.package=p.id WHERE p.queue=? AND l.status IN (2, 3, 5, 7, 12) AND l.id != ?", (queue, str(fid)))
return self.c.fetchone()[0]
@style.inner
@@ -600,7 +583,7 @@ class FileMethods():
return max + 1
else:
return 0
-
+
@style.inner
def _nextFileOrder(self, package):
self.c.execute('SELECT MAX(linkorder) FROM links WHERE package=?', (package,))
@@ -609,19 +592,19 @@ class FileMethods():
return max + 1
else:
return 0
-
+
@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
def addLinks(self, links, package):
- """ links is a list of tupels (url,plugin)"""
+ """ 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
@@ -658,7 +641,7 @@ class FileMethods():
}
"""
- self.c.execute('SELECT l.id,l.url,l.name,l.size,l.status,l.error,l.plugin,l.package,l.linkorder FROM links as l INNER JOIN packages as p ON l.package=p.id WHERE p.queue=? ORDER BY l.linkorder', (q,))
+ self.c.execute('SELECT l.id, l.url, l.name, l.size, l.status, l.error, l.plugin, l.package, l.linkorder FROM links as l INNER JOIN packages as p ON l.package=p.id WHERE p.queue=? ORDER BY l.linkorder', (q,))
data = {}
for r in self.c:
data[r[0]] = {
@@ -688,7 +671,7 @@ class FileMethods():
format:
{
- id: {'name': name ... 'links': {} }, ...
+ id: {'name': name ... 'links': {}}, ...
}
"""
self.c.execute('SELECT p.id, p.name, p.folder, p.site, p.password, p.queue, p.packageorder, s.sizetotal, s.sizedone, s.linksdone, s.linkstotal \
@@ -713,11 +696,11 @@ class FileMethods():
}
return data
-
+
@style.queue
def getLinkData(self, id):
"""get link information as dict"""
- self.c.execute('SELECT id,url,name,size,status,error,plugin,package,linkorder FROM links WHERE id=?', (str(id), ))
+ self.c.execute('SELECT id, url, name, size, status, error, plugin, package, linkorder FROM links WHERE id=?', (str(id),))
data = {}
r = self.c.fetchone()
if not r:
@@ -741,7 +724,7 @@ class FileMethods():
@style.queue
def getPackageData(self, id):
"""get data about links for a package"""
- self.c.execute('SELECT id,url,name,size,status,error,plugin,package,linkorder FROM links WHERE package=? ORDER BY linkorder', (str(id), ))
+ self.c.execute('SELECT id, url, name, size, status, error, plugin, package, linkorder FROM links WHERE package=? ORDER BY linkorder', (str(id),))
data = {}
for r in self.c:
@@ -764,22 +747,22 @@ class FileMethods():
@style.async
def updateLink(self, f):
- self.c.execute('UPDATE links SET url=?,name=?,size=?,status=?,error=?,package=? WHERE id=?', (f.url, f.name, f.size, f.status, f.error, str(f.packageid), str(f.id)))
+ self.c.execute('UPDATE links SET url=?, name=?, size=?, status=?, error=?, package=? WHERE id=?', (f.url, f.name, f.size, f.status, f.error, str(f.packageid), str(f.id)))
@style.queue
def updatePackage(self, p):
- self.c.execute('UPDATE packages SET name=?,folder=?,site=?,password=?,queue=? WHERE id=?', (p.name, p.folder, p.site, p.password, p.queue, str(p.id)))
-
- @style.queue
+ self.c.execute('UPDATE packages SET name=?, folder=?, site=?, password=?, queue=? WHERE id=?', (p.name, p.folder, p.site, p.password, p.queue, str(p.id)))
+
+ @style.queue
def updateLinkInfo(self, data):
""" data is list of tupels (name, size, status, url) """
- self.c.executemany('UPDATE links SET name=?, size=?, status=? WHERE url=? AND status IN (1,2,3,14)', data)
+ self.c.executemany('UPDATE links SET name=?, size=?, status=? WHERE url=? AND status IN (1, 2, 3, 14)', data)
ids = []
self.c.execute('SELECT id FROM links WHERE url IN (\'%s\')' % "','".join([x[3] for x in data]))
for r in self.c:
ids.append(int(r[0]))
return ids
-
+
@style.queue
def reorderPackage(self, p, position, noMove=False):
if position == -1:
@@ -791,7 +774,7 @@ class FileMethods():
self.c.execute('UPDATE packages SET packageorder=packageorder-1 WHERE packageorder <= ? AND packageorder > ? AND queue=? AND packageorder >= 0', (position, p.order, p.queue))
self.c.execute('UPDATE packages SET packageorder=? WHERE id=?', (position, str(p.id)))
-
+
@style.queue
def reorderLink(self, f, position):
""" reorder link with f as dict for pyfile """
@@ -801,34 +784,33 @@ class FileMethods():
self.c.execute('UPDATE links SET linkorder=linkorder-1 WHERE linkorder <= ? AND linkorder > ? AND package=?', (position, f["order"], f["package"]))
self.c.execute('UPDATE links SET linkorder=? WHERE id=?', (position, f["id"]))
-
-
+
@style.queue
def clearPackageOrder(self, p):
self.c.execute('UPDATE packages SET packageorder=? WHERE id=?', (-1, str(p.id)))
self.c.execute('UPDATE packages SET packageorder=packageorder-1 WHERE packageorder > ? AND queue=? AND id != ?', (p.order, p.queue, str(p.id)))
-
+
@style.async
def restartFile(self, id):
- self.c.execute('UPDATE links SET status=3,error="" WHERE id=?', (str(id),))
+ self.c.execute('UPDATE links SET status=3, error="" WHERE id=?', (str(id),))
@style.async
def restartPackage(self, id):
self.c.execute('UPDATE links SET status=3 WHERE package=?', (str(id),))
-
+
@style.queue
def getPackage(self, id):
"""return package instance from id"""
- self.c.execute("SELECT name,folder,site,password,queue,packageorder FROM packages WHERE id=?", (str(id), ))
+ self.c.execute("SELECT name, folder, site, password, queue, packageorder FROM packages WHERE id=?", (str(id),))
r = self.c.fetchone()
if not r: return None
return PyPackage(self.manager, id, * r)
- #----------------------------------------------------------------------
+ #--------------------------------------------------------------------------
@style.queue
def getFile(self, id):
"""return link instance from id"""
- self.c.execute("SELECT url, name, size, status, error, plugin, package, linkorder FROM links WHERE id=?", (str(id), ))
+ self.c.execute("SELECT url, name, size, status, error, plugin, package, linkorder FROM links WHERE id=?", (str(id),))
r = self.c.fetchone()
if not r: return None
return PyFile(self.manager, id, * r)
@@ -845,11 +827,11 @@ class FileMethods():
for i, item in enumerate(occ):
if i: cmd += ", "
cmd += "'%s'" % item
-
+
cmd += ")"
- cmd = "SELECT l.id FROM links as l INNER JOIN packages as p ON l.package=p.id WHERE ((p.queue=1 AND l.plugin NOT IN %s) OR l.plugin IN %s) AND l.status IN (2,3,14) ORDER BY p.packageorder ASC, l.linkorder ASC LIMIT 5" % (cmd, pre)
-
+ cmd = "SELECT l.id FROM links as l INNER JOIN packages as p ON l.package=p.id WHERE ((p.queue=1 AND l.plugin NOT IN %s) OR l.plugin IN %s) AND l.status IN (2, 3, 14) ORDER BY p.packageorder ASC, l.linkorder ASC LIMIT 5" % (cmd, pre)
+
self.c.execute(cmd) # very bad!
return [x[0] for x in self.c]
@@ -857,7 +839,7 @@ class FileMethods():
@style.queue
def getPluginJob(self, plugins):
"""returns pyfile ids with suited plugins"""
- cmd = "SELECT l.id FROM links as l INNER JOIN packages as p ON l.package=p.id WHERE l.plugin IN %s AND l.status IN (2,3,14) ORDER BY p.packageorder ASC, l.linkorder ASC LIMIT 5" % plugins
+ cmd = "SELECT l.id FROM links as l INNER JOIN packages as p ON l.package=p.id WHERE l.plugin IN %s AND l.status IN (2, 3, 14) ORDER BY p.packageorder ASC, l.linkorder ASC LIMIT 5" % plugins
self.c.execute(cmd) # very bad!
@@ -866,18 +848,18 @@ class FileMethods():
@style.queue
def getUnfinished(self, pid):
"""return list of max length 3 ids with pyfiles in package not finished or processed"""
-
+
self.c.execute("SELECT id FROM links WHERE package=? AND status NOT IN (0, 4, 13) LIMIT 3", (str(pid),))
return [r[0] for r in self.c]
@style.queue
def deleteFinished(self):
- self.c.execute("DELETE FROM links WHERE status IN (0,4)")
+ self.c.execute("DELETE FROM links WHERE status IN (0, 4)")
self.c.execute("DELETE FROM packages WHERE NOT EXISTS(SELECT 1 FROM links WHERE packages.id=links.package)")
@style.queue
def restartFailed(self):
- self.c.execute("UPDATE links SET status=3,error='' WHERE status IN (6, 8, 9)")
+ self.c.execute("UPDATE links SET status=3, error='' WHERE status IN (6, 8, 9)")
@style.queue
def findDuplicates(self, id, folder, filename):
@@ -891,54 +873,3 @@ class FileMethods():
self.c.execute("DELETE FROM packages;")
DatabaseBackend.registerSub(FileMethods)
-
-if __name__ == "__main__":
-
- pypath = "."
- _ = lambda x: x
-
- db = FileHandler(None)
-
- #p = PyFile(db, 5)
- #sleep(0.1)
-
- a = time()
-
- #print db.addPackage("package", "folder" , 1)
-
- pack = db.db.addPackage("package", "folder", 1)
-
- updates = []
-
-
- for x in range(0, 200):
- x = str(x)
- db.db.addLink("http://somehost.com/hoster/file/download?file_id=" + x, x, "BasePlugin", pack)
- updates.append(("new name" + x, 0, 3, "http://somehost.com/hoster/file/download?file_id=" + x))
-
-
- for x in range(0, 100):
- updates.append(("unimportant%s" % x, 0, 3, "a really long non existent url%s" % x))
-
- db.db.commit()
-
- b = time()
- print "adding 200 links, single sql execs, no commit", b-a
-
- print db.getCompleteData(1)
-
- c = time()
-
-
- db.db.updateLinkInfo(updates)
-
- d = time()
-
- print "updates", d-c
-
- print db.getCompleteData(1)
-
-
- e = time()
-
- print "complete data", e-d
diff --git a/module/database/StorageDatabase.py b/module/database/Storage.py
index 3ed29625f..75e166d39 100644
--- a/module/database/StorageDatabase.py
+++ b/module/database/Storage.py
@@ -1,25 +1,10 @@
# -*- 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.
+# @author: mkaay
- 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.
+from pyload.database import style
+from pyload.database import DatabaseBackend
- 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
-"""
-
-from module.database import style
-from module.database import DatabaseBackend
-
-class StorageMethods():
+class StorageMethods(object):
@style.queue
def setStorage(db, identifier, key, value):
db.c.execute("SELECT id FROM storage WHERE identifier=? AND key=?", (identifier, key))
@@ -27,7 +12,7 @@ class StorageMethods():
db.c.execute("UPDATE storage SET value=? WHERE identifier=? AND key=?", (value, identifier, key))
else:
db.c.execute("INSERT INTO storage (identifier, key, value) VALUES (?, ?, ?)", (identifier, key, value))
-
+
@style.queue
def getStorage(db, identifier, key=None):
if key is not None:
@@ -36,12 +21,12 @@ class StorageMethods():
if row is not None:
return row[0]
else:
- db.c.execute("SELECT key, value FROM storage WHERE identifier=?", (identifier, ))
+ db.c.execute("SELECT key, value FROM storage WHERE identifier=?", (identifier,))
d = {}
for row in db.c:
d[row[0]] = row[1]
return d
-
+
@style.queue
def delStorage(db, identifier, key):
db.c.execute("DELETE FROM storage WHERE identifier=? AND key=?", (identifier, key))
diff --git a/module/database/UserDatabase.py b/module/database/User.py
index 0c781057d..67cb62ab9 100644
--- a/module/database/UserDatabase.py
+++ b/module/database/User.py
@@ -1,32 +1,17 @@
# -*- 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
-"""
+# @author: mkaay
from hashlib import sha1
import random
-from DatabaseBackend import DatabaseBackend
-from DatabaseBackend import style
+from pyload.database.DatabaseBackend import DatabaseBackend
+from pyload.database.DatabaseBackend import style
-class UserMethods():
+class UserMethods(object):
@style.queue
def checkAuth(db, user, password):
c = db.c
- c.execute('SELECT id, name, password, role, permission, template, email FROM "users" WHERE name=?', (user, ))
+ c.execute('SELECT id, name, password, role, permission, template, email FROM "users" WHERE name=?', (user,))
r = c.fetchone()
if not r:
return {}
@@ -47,7 +32,7 @@ class UserMethods():
password = salt + h.hexdigest()
c = db.c
- c.execute('SELECT name FROM users WHERE name=?', (user, ))
+ c.execute('SELECT name FROM users WHERE name=?', (user,))
if c.fetchone() is not None:
c.execute('UPDATE users SET password=? WHERE name=?', (password, user))
else:
@@ -56,7 +41,7 @@ class UserMethods():
@style.queue
def changePassword(db, user, oldpw, newpw):
- db.c.execute('SELECT id, name, password FROM users WHERE name=?', (user, ))
+ db.c.execute('SELECT id, name, password FROM users WHERE name=?', (user,))
r = db.c.fetchone()
if not r:
return False
@@ -103,6 +88,6 @@ class UserMethods():
@style.queue
def removeUser(db, user):
- db.c.execute('DELETE FROM users WHERE name=?', (user, ))
+ db.c.execute('DELETE FROM users WHERE name=?', (user,))
DatabaseBackend.registerSub(UserMethods)
diff --git a/module/database/__init__.py b/module/database/__init__.py
index 545789c0c..64f049be1 100644
--- a/module/database/__init__.py
+++ b/module/database/__init__.py
@@ -1,6 +1,7 @@
-from DatabaseBackend import DatabaseBackend
-from DatabaseBackend import style
+# -*- coding: utf-8 -*-
-from FileDatabase import FileHandler
-from UserDatabase import UserMethods
-from StorageDatabase import StorageMethods \ No newline at end of file
+from pyload.database.DatabaseBackend import DatabaseBackend, style
+
+from pyload.database.File import FileHandler
+from pyload.database.User import UserMethods
+from pyload.database.Storage import StorageMethods