diff options
author | fedeG <federicogonzalez761@gmail.com> | 2015-04-19 17:37:58 +0200 |
---|---|---|
committer | Walter Purcaro <vuolter@users.noreply.github.com> | 2015-04-19 17:37:58 +0200 |
commit | ed7ba3c1f7552106148b014cbbf4a2642dcf4adc (patch) | |
tree | 8484a362d179640168e7a7d02b41447d39100c71 | |
parent | Use with statement (diff) | |
download | pyload-ed7ba3c1f7552106148b014cbbf4a2642dcf4adc.tar.xz |
PEP-8, Python Zen, refactor and reduce code (part 7 in master module/database)
Conflicts:
module/database/__init__.py
pyload/database/Backend.py
pyload/database/File.py
pyload/database/Storage.py
pyload/database/User.py
-rw-r--r-- | pyload/database/Backend.py | 47 | ||||
-rw-r--r-- | pyload/database/File.py | 99 | ||||
-rw-r--r-- | pyload/database/Storage.py | 6 | ||||
-rw-r--r-- | pyload/database/User.py | 12 |
4 files changed, 92 insertions, 72 deletions
diff --git a/pyload/database/Backend.py b/pyload/database/Backend.py index b105723cc..8b537511e 100644 --- a/pyload/database/Backend.py +++ b/pyload/database/Backend.py @@ -7,7 +7,6 @@ from threading import Event, Thread from os import remove from os.path import exists from shutil import move - from Queue import Queue from traceback import print_exc @@ -38,6 +37,7 @@ class style(object): def x(*args, **kwargs): if cls.db: return f(cls.db, *args, **kwargs) + return x @@ -49,6 +49,7 @@ class style(object): def x(*args, **kwargs): if cls.db: return cls.db.queue(f, *args, **kwargs) + return x @@ -75,12 +76,13 @@ class DatabaseJob(object): self.result = None self.exception = False -# import inspect -# self.frame = inspect.currentframe() + # import inspect + # self.frame = inspect.currentframe() def __repr__(self): from os.path import basename + frame = self.frame.f_back output = "" for _i in range(5): @@ -196,11 +198,12 @@ class DatabaseBackend(Thread): 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 "")') + 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 Exception: @@ -209,47 +212,45 @@ class DatabaseBackend(Thread): 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)') + 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 Exception: print "Database was converted from v3 to v4." - #convert scripts end------------------------------------------------------- + # convert scripts end ----------------------------------------------------- def _createTables(self): """create tables for database""" - self.c.execute('CREATE TABLE IF NOT EXISTS "packages" ("id" INTEGER PRIMARY KEY AUTOINCREMENT, "name" TEXT NOT NULL, "folder" TEXT, "password" TEXT DEFAULT "", "site" TEXT DEFAULT "", "queue" INTEGER DEFAULT 0 NOT NULL, "packageorder" INTEGER DEFAULT 0 NOT NULL)') - self.c.execute('CREATE TABLE IF NOT EXISTS "links" ("id" INTEGER PRIMARY KEY AUTOINCREMENT, "url" TEXT NOT NULL, "name" TEXT, "size" INTEGER DEFAULT 0 NOT NULL, "status" INTEGER DEFAULT 3 NOT NULL, "plugin" TEXT DEFAULT "BasePlugin" NOT NULL, "error" TEXT DEFAULT "", "linkorder" INTEGER DEFAULT 0 NOT NULL, "package" INTEGER DEFAULT 0 NOT NULL, FOREIGN KEY(package) REFERENCES packages(id))') + self.c.execute( + 'CREATE TABLE IF NOT EXISTS "packages" ("id" INTEGER PRIMARY KEY AUTOINCREMENT, "name" TEXT NOT NULL, "folder" TEXT, "password" TEXT DEFAULT "", "site" TEXT DEFAULT "", "queue" INTEGER DEFAULT 0 NOT NULL, "packageorder" INTEGER DEFAULT 0 NOT NULL)') + self.c.execute( + 'CREATE TABLE IF NOT EXISTS "links" ("id" INTEGER PRIMARY KEY AUTOINCREMENT, "url" TEXT NOT NULL, "name" TEXT, "size" INTEGER DEFAULT 0 NOT NULL, "status" INTEGER DEFAULT 3 NOT NULL, "plugin" TEXT DEFAULT "BasePlugin" NOT NULL, "error" TEXT DEFAULT "", "linkorder" INTEGER DEFAULT 0 NOT NULL, "package" INTEGER DEFAULT 0 NOT NULL, FOREIGN KEY(package) REFERENCES packages(id))') self.c.execute('CREATE INDEX IF NOT EXISTS "pIdIndex" ON links(package)') - 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 "")') - 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)') + 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 "")') + 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)') self.c.execute('CREATE VIEW IF NOT EXISTS "pstats" AS \ 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 + # try to lower ids self.c.execute('SELECT max(id) FROM LINKS') fid = self.c.fetchone()[0] - if fid: - fid = int(fid) - else: - fid = 0 + fid = int(fid) if fid else 0 self.c.execute('UPDATE SQLITE_SEQUENCE SET seq=? WHERE name=?', (fid, "links")) - self.c.execute('SELECT max(id) FROM packages') pid = self.c.fetchone()[0] - if pid: - pid = int(pid) - else: - pid = 0 + pid = int(pid) if pid else 0 self.c.execute('UPDATE SQLITE_SEQUENCE SET seq=? WHERE name=?', (pid, "packages")) self.c.execute('VACUUM') @@ -263,7 +264,7 @@ class DatabaseBackend(Thread): print "Converting old Django DB" conn = sqlite3.connect('pyload.db') c = conn.cursor() - c.execute("SELECT username, password, email from auth_user WHERE is_superuser") + c.execute("SELECT username, password, email FROM auth_user WHERE is_superuser") users = [] for r in c: pw = r[1].split("$") diff --git a/pyload/database/File.py b/pyload/database/File.py index 7cbe1890a..d97dc7c5e 100644 --- a/pyload/database/File.py +++ b/pyload/database/File.py @@ -2,7 +2,6 @@ # @author: RaNaN, mkaay from threading import RLock -from time import time from pyload.utils import formatSize, lock from pyload.manager.Event import InsertEvent, ReloadAllEvent, RemoveEvent, UpdateEvent @@ -25,7 +24,9 @@ class FileHandler(object): self.core = core # translations - self.statusMsg = [_("finished"), _("offline"), _("online"), _("queued"), _("skipped"), _("waiting"), _("temp. offline"), _("starting"), _("failed"), _("aborted"), _("decrypting"), _("custom"), _("downloading"), _("processing"), _("unknown")] + self.statusMsg = [_("finished"), _("offline"), _("online"), _("queued"), _("skipped"), _("waiting"), + _("temp. offline"), _("starting"), _("failed"), _("aborted"), _("decrypting"), _("custom"), + _("downloading"), _("processing"), _("unknown")] self.cache = {} # holds instances for files self.packageCache = {} # same for packages @@ -34,7 +35,7 @@ class FileHandler(object): self.jobCache = {} self.lock = RLock() #@TODO: should be a Lock w/o R - #self.lock._Verbose__verbose = True + # self.lock._Verbose__verbose = True self.filecount = -1 # if an invalid value is set get current value from db self.queuecount = -1 # number of package to be loaded @@ -52,6 +53,7 @@ class FileHandler(object): args[0].queuecount = -1 args[0].jobCache = {} return func(*args) + return new @@ -149,7 +151,8 @@ class FileHandler(object): p = self.getPackage(id) if not p: - if id in self.packageCache: del self.packageCache[id] + if id in self.packageCache: + del self.packageCache[id] return oldorder = p.order @@ -349,7 +352,7 @@ class FileHandler(object): #@TODO: maybe the new job has to be approved... - #pyfile = self.getFile(self.jobCache[occ].pop()) + # pyfile = self.getFile(self.jobCache[occ].pop()) return pyfile @@ -498,11 +501,11 @@ class FileHandler(object): if pack.queue != p.queue or pack.order < 0 or pack == p: continue if p.order > position: - if pack.order >= position and pack.order < p.order: + if position <= pack.order < p.order: pack.order += 1 pack.notifyChange() elif p.order < position: - if pack.order <= position and pack.order > p.order: + if position >= pack.order > p.order: pack.order -= 1 pack.notifyChange() @@ -529,11 +532,11 @@ class FileHandler(object): if pyfile.packageid != f['package'] or pyfile.order < 0: continue if f['order'] > position: - if pyfile.order >= position and pyfile.order < f['order']: + if position <= pyfile.order < f['order']: pyfile.order += 1 pyfile.notifyChange() elif f['order'] < position: - if pyfile.order <= position and pyfile.order > f['order']: + if position >= pyfile.order > f['order']: pyfile.order -= 1 pyfile.notifyChange() @@ -592,11 +595,9 @@ class FileHandler(object): new_packs.update(self.db.getAllPackages(1)) # get new packages only from db - deleted = [] - for id in old_packs.iterkeys(): - if id not in new_packs: - deleted.append(id) - self.deletePackage(int(id)) + deleted = [id for id in old_packs.iterkeys() if id not in new_packs] + for id_deleted in deleted: + self.deletePackage(int(id_deleted)) return deleted @@ -614,21 +615,26 @@ 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] @@ -655,7 +661,8 @@ class FileMethods(object): @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, ".".join(plugintype, pluginname), package, order)) + self.c.execute('INSERT INTO links(url, name, plugin, package, linkorder) VALUES(?,?,?,?,?)', + (url, name, ".".join(plugintype, pluginname), package, order)) return self.c.lastrowid @@ -671,7 +678,8 @@ class FileMethods(object): @style.queue def addPackage(self, name, folder, queue): order = self._nextPackageOrder(queue) - self.c.execute('INSERT INTO packages(name, folder, queue, packageorder) VALUES(?,?,?,?)', (name, folder, queue, order)) + self.c.execute('INSERT INTO packages(name, folder, queue, packageorder) VALUES(?,?,?,?)', + (name, folder, queue, order)) return self.c.lastrowid @@ -679,13 +687,15 @@ class FileMethods(object): def deletePackage(self, p): self.c.execute('DELETE FROM links WHERE package=?', (str(p.id),)) self.c.execute('DELETE FROM packages WHERE id=?', (str(p.id),)) - self.c.execute('UPDATE packages SET packageorder=packageorder-1 WHERE packageorder > ? AND queue=?', (p.order, p.queue)) + self.c.execute('UPDATE packages SET packageorder=packageorder-1 WHERE packageorder > ? AND queue=?', + (p.order, p.queue)) @style.queue def deleteLink(self, f): self.c.execute('DELETE FROM links WHERE id=?', (str(f.id),)) - self.c.execute('UPDATE links SET linkorder=linkorder-1 WHERE linkorder > ? AND package=?', (f.order, str(f.packageid))) + self.c.execute('UPDATE links SET linkorder=linkorder-1 WHERE linkorder > ? AND package=?', + (f.order, str(f.packageid))) @style.queue @@ -702,7 +712,9 @@ class FileMethods(object): } """ - 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]] = { @@ -763,7 +775,8 @@ class FileMethods(object): @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: @@ -788,7 +801,9 @@ class FileMethods(object): @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: @@ -811,13 +826,14 @@ class FileMethods(object): @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, str(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, str(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))) - + 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): @@ -836,9 +852,13 @@ class FileMethods(object): position = self._nextPackageOrder(p.queue) if not noMove: if p.order > position: - 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=packageorder+1 WHERE packageorder >= ? AND packageorder < ? AND queue=? AND packageorder >= 0', + (position, p.order, p.queue)) elif p.order < position: - 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=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))) @@ -847,9 +867,11 @@ class FileMethods(object): def reorderLink(self, f, position): """ reorder link with f as dict for pyfile """ if f['order'] > position: - 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=linkorder+1 WHERE linkorder >= ? AND linkorder < ? AND package=?', + (position, f['order'], f['package'])) elif f['order'] < position: - 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=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'])) @@ -857,7 +879,8 @@ class FileMethods(object): @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))) + self.c.execute('UPDATE packages SET packageorder=packageorder-1 WHERE packageorder > ? AND queue=? AND id != ?', + (p.order, p.queue, str(p.id))) @style.async @@ -877,7 +900,7 @@ class FileMethods(object): r = self.c.fetchone() if not r: return None - return PyPackage(self.manager, id, * r) + return PyPackage(self.manager, id, *r) #-------------------------------------------------------------------------- @@ -885,13 +908,14 @@ class FileMethods(object): @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 r = list(r) r[5] = tuple(r[5].split('.')) - return PyFile(self.manager, id, * r) + return PyFile(self.manager, id, *r) @style.queue @@ -947,7 +971,9 @@ class FileMethods(object): @style.queue def findDuplicates(self, id, folder, filename): """ checks if filename exists with different id and same package """ - self.c.execute("SELECT l.plugin FROM links as l INNER JOIN packages as p ON l.package=p.id AND p.folder=? WHERE l.id!=? AND l.status=0 AND l.name=?", (folder, id, filename)) + self.c.execute( + "SELECT l.plugin FROM links as l INNER JOIN packages as p ON l.package=p.id AND p.folder=? WHERE l.id!=? AND l.status=0 AND l.name=?", + (folder, id, filename)) return self.c.fetchone() @@ -956,4 +982,5 @@ class FileMethods(object): self.c.execute("DELETE FROM links;") self.c.execute("DELETE FROM packages;") + DatabaseBackend.registerSub(FileMethods) diff --git a/pyload/database/Storage.py b/pyload/database/Storage.py index 45ad18b2d..70932b55c 100644 --- a/pyload/database/Storage.py +++ b/pyload/database/Storage.py @@ -26,14 +26,12 @@ class StorageMethods(object): return row[0] else: db.c.execute("SELECT key, value FROM storage WHERE identifier=?", (identifier,)) - d = {} - for row in db.c: - d[row[0]] = row[1] - return d + return {row[0]: row[1] for row in db.c} @style.queue def delStorage(db, identifier, key): db.c.execute("DELETE FROM storage WHERE identifier=? AND key=?", (identifier, key)) + DatabaseBackend.registerSub(StorageMethods) diff --git a/pyload/database/User.py b/pyload/database/User.py index e11961e32..df510c54f 100644 --- a/pyload/database/User.py +++ b/pyload/database/User.py @@ -76,24 +76,18 @@ class UserMethods(object): @style.queue def listUsers(db): db.c.execute('SELECT name FROM users') - users = [] - for row in db.c: - users.append(row[0]) - return users + return [row[0] for row in db.c] @style.queue def getAllUserData(db): db.c.execute("SELECT name, permission, role, template, email FROM users") - user = {} - for r in db.c: - user[r[0]] = {"permission": r[1], "role": r[2], "template": r[3], "email": r[4]} - - return user + return {{"permission": r[1], "role": r[2], "template": r[3], "email": r[4]} for r in db.c} @style.queue def removeUser(db, user): db.c.execute('DELETE FROM users WHERE name=?', (user,)) + DatabaseBackend.registerSub(UserMethods) |