diff options
Diffstat (limited to 'module/FileDatabase.py')
-rw-r--r-- | module/FileDatabase.py | 276 |
1 files changed, 18 insertions, 258 deletions
diff --git a/module/FileDatabase.py b/module/FileDatabase.py index 9225873fe..9e5417c75 100644 --- a/module/FileDatabase.py +++ b/module/FileDatabase.py @@ -16,6 +16,7 @@ @author: RaNaN @author: mkaay """ + from Queue import Queue from os import remove from os.path import exists @@ -32,6 +33,9 @@ from module.PullEvents import ReloadAllEvent from module.PullEvents import RemoveEvent from module.PullEvents import UpdateEvent +from module.PyPackage import PyPackage +from module.PyFile import PyFile + try: from pysqlite2 import dbapi2 as sqlite3 except: @@ -468,7 +472,6 @@ class FileHandler: p.order = position self.db.commit() - e = ReloadAllEvent("collector" if not p.queue else "queue") self.core.pullManager.addEvent(e) @@ -500,15 +503,16 @@ class FileHandler: e = ReloadAllEvent("collector" if not self.getPackage(f["package"]).queue else "queue") - self.core.pullManager.addEvent(e) @change def updateFileInfo(self, data, pid): """ updates file info (name, size, status, url)""" - self.db.updateLinkInfo(data) + ids = self.db.updateLinkInfo(data) - #@TODO package update event + for fid in ids: + e = UpdateEvent("file", fid, "collector" if not self.getFile(fid).package().queue else "queue") + self.core.pullManager.addEvent(e) def checkPackageFinished(self, pyfile): """ checks if package is finished and calls hookmanager """ @@ -729,7 +733,8 @@ class FileDatabaseBackend(Thread): 'error': r[5], 'plugin': r[6], 'package': r[7], - 'order': r[8] + 'order': r[8], + 'progress': 100 if r[4] in (0, 4) else 0 } return data @@ -785,7 +790,8 @@ class FileDatabaseBackend(Thread): 'error': r[5], 'plugin': r[6], 'package': r[7], - 'order': r[8] + 'order': r[8], + 'progress': 100 if r[4] in (0, 4) else 0 } return data @@ -822,10 +828,15 @@ class FileDatabaseBackend(Thread): def updatePackage(self, p): self.c.execute('UPDATE packages SET name=?,folder=?,site=?,password=?,queue=?,priority=? WHERE id=?', (p.name, p.folder, p.site, p.password, p.queue, p.priority, str(p.id))) - @async + @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 NOT IN (0,8,12,13)', 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 @queue def reorderPackage(self, p, position, noMove=False): @@ -925,257 +936,6 @@ class FileDatabaseBackend(Thread): self.c.execute("SELECT id FROM links WHERE package=? AND status NOT IN (0, 13) LIMIT 3", (str(pid),)) return [r[0] for r in self.c] - -class PyFile(): - def __init__(self, manager, id, url, name, size, status, error, pluginname, package, order): - self.m = manager - - self.id = int(id) - self.url = url - self.name = name - self.size = size - self.status = status - self.pluginname = pluginname - self.packageid = package #should not be used, use package() instead - self.error = error - self.order = order - # database information ends here - - self.plugin = None - - self.waitUntil = 0 # time() + time to wait - - # status attributes - self.active = False #obsolete? - self.abort = False - self.reconnected = False - - #hook progress - self.alternativePercent = None - - self.m.cache[int(id)] = self - - - def __repr__(self): - return "PyFile %s: %s@%s" % (self.id, self.name, self.pluginname) - - 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.pluginname) - self.plugin = self.pluginclass(self) - - - def package(self): - """ return package instance""" - return self.m.getPackage(self.packageid) - - def setStatus(self, status): - self.status = statusMap[status] - self.sync() #@TODO needed aslong no better job approving exists - - def hasStatus(self, status): - return statusMap[status] == self.status - - def sync(self): - """sync PyFile instance with database""" - self.m.updateLink(self) - - def release(self): - """sync and remove from cache""" - self.sync() - if hasattr(self, "plugin"): - del self.plugin - self.m.releaseLink(self.id) - - def delete(self): - """delete pyfile from database""" - self.m.deleteLink(self.id) - - def toDict(self): - """return dict with all information for interface""" - return self.toDbDict() - - def toDbDict(self): - """return data as dict for databse - - format: - - { - id: {'url': url, 'name': name ... } - } - - """ - return { - self.id: { - 'id': self.id, - 'url': self.url, - 'name': self.name, - 'plugin': self.pluginname, - 'size': self.getSize(), - 'format_size': self.formatSize(), - 'status': self.status, - 'statusmsg': self.m.statusMsg[self.status], - 'package': self.packageid, - 'error': self.error, - 'order': self.order - } - } - - def abortDownload(self): - """abort pyfile if possible""" - while self.id in self.m.core.threadManager.processingIds(): - self.abort = True - if self.plugin and self.plugin.req: self.plugin.req.abort = True - sleep(0.1) - - self.abort = False - if hasattr(self, "plugin") and self.plugin and self.plugin.req: self.plugin.req.abort = False - self.release() - - def finishIfDone(self): - """set status to finish and release file if every thread is finished with it""" - - if self.id in self.m.core.threadManager.processingIds(): - return False - - self.setStatus("finished") - self.release() - return True - - def formatWait(self): - """ formats and return wait time in humanreadable format """ - seconds = self.waitUntil - time() - - if seconds < 0: return "00:00:00" - - hours, seconds = divmod(seconds, 3600) - minutes, seconds = divmod(seconds, 60) - return "%.2i:%.2i:%.2i" % (hours, minutes, seconds) - - def formatSize(self): - """ formats size to readable format """ - return formatSize(self.getSize()) - - def formatETA(self): - """ formats eta to readable format """ - seconds = self.getETA() - - if seconds < 0: return "00:00:00" - - hours, seconds = divmod(seconds, 3600) - minutes, seconds = divmod(seconds, 60) - return "%.2i:%.2i:%.2i" % (hours, minutes, seconds) - - def getSpeed(self): - """ calculates speed """ - try: - return self.plugin.req.get_speed() - except: - return 0 - - def getETA(self): - """ gets established time of arrival""" - try: - return self.plugin.req.get_ETA() - except: - return 0 - - def getBytesLeft(self): - """ gets bytes left """ - try: - return self.plugin.req.bytes_left() - except: - return 0 - - def getPercent(self): - """ get % of download """ - if self.alternativePercent: return self.alternativePercent - try: - return int((float(self.plugin.req.dl_arrived) / self.plugin.req.dl_size) * 100) - except: - return 0 - - def getSize(self): - """ get size of download """ - if self.size: return self.size - else: - try: - return self.plugin.req.dl_size - except: - return 0 - - def notifyChange(self): - e = UpdateEvent("file", self.id, "collector" if not self.package().queue else "queue") - self.m.core.pullManager.addEvent(e) - -class PyPackage(): - def __init__(self, manager, id, name, folder, site, password, queue, order, priority): - self.m = manager - self.m.packageCache[int(id)] = self - - self.id = int(id) - self.name = name - self.folder = folder - self.site = site - self.password = password - self.queue = queue - self.order = order - self.priority = priority - - - self.setFinished = False - - def toDict(self): - """return data as dict - - format: - - { - id: {'name': name ... 'links': {} } } - } - - """ - return { - self.id: { - 'id': self.id, - 'name': self.name, - 'folder': self.folder, - 'site': self.site, - 'password': self.password, - 'queue': self.queue, - 'order': self.order, - 'priority': self.priority, - 'links': {} - } - } - - def getChildren(self): - """get information about contained links""" - return self.m.getPackageData(self.id)["links"] - - def setPriority(self, priority): - self.priority = priority - self.sync() - - def sync(self): - """sync with db""" - self.m.updatePackage(self) - - def release(self): - """sync and delete from cache""" - self.sync() - self.m.releasePackage(self.id) - - def delete(self): - self.m.deletePackage(self.id) - - def notifyChange(self): - e = UpdateEvent("file", self.id, "collector" if not self.queue else "queue") - self.m.core.pullManager.addEvent(e) - - if __name__ == "__main__": pypath = "." |