-class UserRequestedCancel(Exception):
- pass
-
-class HTTPProgressDownloader(client.HTTPDownloader):
- def __init__(self, url, outfile, headers=None):
- client.HTTPDownloader.__init__(self, url, outfile, headers=headers, agent="Dreambox .NFI Download Plugin")
- self.status = None
- self.progress_callback = None
- self.deferred = defer.Deferred()
-
- def noPage(self, reason):
- if self.status == "304":
- print reason.getErrorMessage()
- client.HTTPDownloader.page(self, "")
- else:
- client.HTTPDownloader.noPage(self, reason)
-
- def gotHeaders(self, headers):
- if self.status == "200":
- if headers.has_key("content-length"):
- self.totalbytes = int(headers["content-length"][0])
- else:
- self.totalbytes = 0
- self.currentbytes = 0.0
- return client.HTTPDownloader.gotHeaders(self, headers)
-
- def pagePart(self, packet):
- if self.status == "200":
- self.currentbytes += len(packet)
- if self.totalbytes and self.progress_callback:
- self.progress_callback(self.currentbytes, self.totalbytes)
- return client.HTTPDownloader.pagePart(self, packet)
-
- def pageEnd(self):
- return client.HTTPDownloader.pageEnd(self)
-
-class downloadWithProgress:
- def __init__(self, url, outputfile, contextFactory=None, *args, **kwargs):
- scheme, host, port, path = client._parse(url)
- self.factory = HTTPProgressDownloader(url, outputfile, *args, **kwargs)
- self.connection = reactor.connectTCP(host, port, self.factory)
-
- def start(self):
- return self.factory.deferred
-
- def stop(self):
- print "[stop]"
- self.connection.disconnect()
- #self.factory.deferred.errback(failure.Failure(UserRequestedCancel))
-
- def addProgress(self, progress_callback):
- print "[addProgress]"
- self.factory.progress_callback = progress_callback
-
-class Feedlist(MenuList):
- def __init__(self, list=[], enableWrapAround = False):
- MenuList.__init__(self, list, enableWrapAround, eListboxPythonMultiContent)
- self.l.setFont(0, gFont("Regular", 16))
- self.l.setItemHeight(22)
-
- def clear(self):
- del self.list[:]
- self.l.setList(self.list)
-
- def getNFIname(self):
- l = self.l.getCurrentSelection()
- return l and l[0][0]
-
- def getNFIurl(self):
- l = self.l.getCurrentSelection()
- return l and l[0][1]
-
- def getNFOname(self):
- l = self.l.getCurrentSelection()
- return l and l[0][0][:-3]+"nfo"
-
- def getNFOurl(self):
- l = self.l.getCurrentSelection()
- return l and l[0][1][:-3]+"nfo"
-
- def isValid(self):
- l = self.l.getCurrentSelection()
- if l[0] == 0:
- return False
- else:
- return True
-
- def moveSelection(self,idx=0):
- if self.instance is not None:
- self.instance.moveSelectionTo(idx)
-
-class NFIDownload(Screen):
- LIST_SOURCE = 1
- LIST_DEST = 2
- skin = """
- <screen name="NFIDownload" position="90,95" size="560,420" title="Image download utility">
- <ePixmap pixmap="skin_default/buttons/red.png" position="0,0" zPosition="0" size="140,40" transparent="1" alphatest="on" />
- <ePixmap pixmap="skin_default/buttons/green.png" position="140,0" zPosition="0" size="140,40" transparent="1" alphatest="on" />
- <ePixmap pixmap="skin_default/buttons/yellow.png" position="280,0" zPosition="0" size="140,40" transparent="1" alphatest="on" />
- <ePixmap pixmap="skin_default/buttons/blue.png" position="420,0" zPosition="0" size="140,40" transparent="1" alphatest="on" />
- <widget source="key_red" render="Label" position="0,0" zPosition="1" size="140,40" font="Regular;20" valign="center" halign="center" backgroundColor="#9f1313" transparent="1" />
- <widget source="key_green" render="Label" position="140,0" zPosition="1" size="140,40" font="Regular;20" valign="center" halign="center" backgroundColor="#1f771f" transparent="1" />
- <widget source="key_yellow" render="Label" position="280,0" zPosition="1" size="140,40" font="Regular;20" valign="center" halign="center" backgroundColor="#a08500" transparent="1" />
- <widget source="key_blue" render="Label" position="420,0" zPosition="1" size="140,40" font="Regular;20" valign="center" halign="center" backgroundColor="#18188b" transparent="1" />
-
- <widget source="label_top" render="Label" position="10,44" size="240,20" font="Regular;16" />
- <widget name="feedlist" position="10,66" size="250,222" scrollbarMode="showOnDemand" />
- <widget name="destlist" position="0,66" size="260,222" scrollbarMode="showOnDemand" />
-
- <widget source="label_bottom" render="Label" position="10,312" size="240,18" font="Regular;16"/>
- <widget source="path_bottom" render="Label" position="10,330" size="250,42" font="Regular;18" />
-
- <widget source="infolabel" render="Label" position="270,44" size="280,284" font="Regular;16" />
- <widget source="job_progressbar" render="Progress" position="10,374" size="540,26" borderWidth="1" backgroundColor="#254f7497" />
- <widget source="job_progresslabel" render="Label" position="130,378" zPosition="2" font="Regular;18" halign="center" transparent="1" size="300,22" foregroundColor="#000000" />
- <widget source="statusbar" render="Label" position="10,404" size="540,16" font="Regular;16" foregroundColor="#cccccc" />
- </screen>"""
-
- def __init__(self, session, destdir="/tmp/"):
- self.skin = NFIDownload.skin
- Screen.__init__(self, session)
+class ImageDownloadJob(Job):
+ def __init__(self, url, filename, device=None, mountpoint="/"):
+ Job.__init__(self, _("Download .NFI-Files for USB-Flasher"))
+ if device:
+ MountTask(self, device, mountpoint)
+ ImageDownloadTask(self, url, mountpoint+filename)
+ ImageDownloadTask(self, url[:-4]+".nfo", mountpoint+filename[:-4]+".nfo")
+ if device:
+ UmountTask(self, mountpoint)
+
+ def retry(self):
+ self.tasks[0].args += self.tasks[0].retryargs
+ Job.retry(self)
+
+class MountTask(Task):
+ def __init__(self, job, device, mountpoint):
+ Task.__init__(self, job, ("mount"))
+ self.setTool("mount")
+ self.args += [device, mountpoint, "-orw,sync"]
+ self.weighting = 1
+
+ def processOutput(self, data):
+ print "[MountTask] output:", data
+
+class UmountTask(Task):
+ def __init__(self, job, mountpoint):
+ Task.__init__(self, job, ("mount"))
+ self.setTool("umount")
+ self.args += [mountpoint]
+ self.weighting = 1
+
+class DownloaderPostcondition(Condition):
+ def check(self, task):
+ return task.returncode == 0
+
+ def getErrorMessage(self, task):
+ return self.error_message