def fetchData(self): self.parent.setProgressParent(None) self.parent.startProgress() self.parent.updateProgress() try: socket.setdefaulttimeout(30) try: sock = urllib2.urlopen( URL + "search?t=%d&c=1" % self.type) data = sock.read() try: data = gzip.GzipFile(fileobj=cStringIO.StringIO(data)).read() except: # the server is sending gzipped data, but a transparent # proxy or antivirus software may be decompressing it # before we get it pass self.allList = simplejson.loads(unicode(data)) except: showInfo(self.conErrMsg % cgi.escape(unicode( traceback.format_exc(), "utf-8", "replace"))) self.close() self.ok = False return finally: self.parent.finishProgress() socket.setdefaulttimeout(None) self.form.search.setFocus() self.typeChanged() self.limit()
def accept(self): if self.type == 0: if not self.parent.saveAndClose(hideWelcome=True, parent=self): return QDialog.accept(self) # fixme: use namedtmp (fd, tmpname) = tempfile.mkstemp(prefix="anki") tmpfile = os.fdopen(fd, "w+b") cnt = 0 try: socket.setdefaulttimeout(30) self.parent.setProgressParent(self) self.parent.startProgress() self.parent.updateProgress() try: sock = urllib2.urlopen(URL + "get?id=%d" % self.curRow[R_ID]) while 1: data = sock.read(32768) if not data: break cnt += len(data) tmpfile.write(data) self.parent.updateProgress(label=_("Downloaded %dKB") % (cnt / 1024.0)) except: showInfo(self.conErrMsg % cgi.escape(unicode(traceback.format_exc(), "utf-8", "replace"))) self.close() return finally: socket.setdefaulttimeout(None) self.parent.setProgressParent(None) self.parent.finishProgress() QDialog.accept(self) # file is fetched tmpfile.seek(0) self.handleFile(tmpfile) QDialog.accept(self)
def handleFile(self, file): ext = os.path.splitext(self.curRow[R_FNAME])[1] if ext == ".zip": z = zipfile.ZipFile(file) else: z = None tit = self.curRow[R_TITLE] tit = re.sub("[^][A-Za-z0-9 ()\-]", "", tit) tit = tit[0:40] if self.type == 0: # col dd = self.parent.pm.profile['documentDir'] p = os.path.join(dd, tit + ".anki") if os.path.exists(p): tit += "%d" % time.time() for l in z.namelist(): if l == "shared.anki": dpath = os.path.join(dd, tit + ".anki") open(dpath, "wb").write(z.read(l)) elif l.startswith("shared.media/"): try: os.mkdir(os.path.join(dd, tit + ".media")) except OSError: pass open(os.path.join(dd, tit + ".media", os.path.basename(l)),"wb").write(z.read(l)) self.parent.loadCol(dpath) else: pd = self.parent.pluginsFolder() if z: for l in z.infolist(): try: os.makedirs(os.path.join( pd, os.path.dirname(l.filename))) except OSError: pass if l.filename.endswith("/"): # directory continue path = os.path.join(pd, l.filename) open(path, "wb").write(z.read(l.filename)) else: open(os.path.join(pd, tit + ext), "wb").write(file.read()) showInfo(_("Plugin downloaded. Please restart Anki."), parent=self)
def handleFile(self, file): ext = os.path.splitext(self.curRow[R_FNAME])[1] if ext == ".zip": z = zipfile.ZipFile(file) else: z = None tit = self.curRow[R_TITLE] tit = re.sub("[^][A-Za-z0-9 ()\-]", "", tit) tit = tit[0:40] if self.type == 0: # deck dd = self.parent.config['documentDir'] p = os.path.join(dd, tit + ".anki") if os.path.exists(p): tit += "%d" % time.time() for l in z.namelist(): if l == "shared.anki": dpath = os.path.join(dd, tit + ".anki") open(dpath, "wb").write(z.read(l)) elif l.startswith("shared.media/"): try: os.mkdir(os.path.join(dd, tit + ".media")) except OSError: pass open(os.path.join(dd, tit + ".media", os.path.basename(l)),"wb").write(z.read(l)) self.parent.loadDeck(dpath) else: pd = self.parent.pluginsFolder() if z: for l in z.infolist(): try: os.makedirs(os.path.join( pd, os.path.dirname(l.filename))) except OSError: pass if l.filename.endswith("/"): # directory continue path = os.path.join(pd, l.filename) open(path, "wb").write(z.read(l.filename)) else: open(os.path.join(pd, tit + ext), "wb").write(file.read()) showInfo(_("Plugin downloaded. Please restart Anki."), parent=self)
def accept(self): if self.type == 0: if not self.parent.saveAndClose(hideWelcome=True, parent=self): return QDialog.accept(self) # fixme: use namedtmp (fd, tmpname) = tempfile.mkstemp(prefix="anki") tmpfile = os.fdopen(fd, "w+b") cnt = 0 try: socket.setdefaulttimeout(30) self.parent.setProgressParent(self) self.parent.startProgress() self.parent.updateProgress() try: sock = urllib2.urlopen( URL + "get?id=%d" % self.curRow[R_ID]) while 1: data = sock.read(32768) if not data: break cnt += len(data) tmpfile.write(data) self.parent.updateProgress( label=_("Downloaded %dKB") % (cnt/1024.0)) except: showInfo(self.conErrMsg % cgi.escape(unicode( traceback.format_exc(), "utf-8", "replace"))) self.close() return finally: socket.setdefaulttimeout(None) self.parent.setProgressParent(None) self.parent.finishProgress() QDialog.accept(self) # file is fetched tmpfile.seek(0) self.handleFile(tmpfile) QDialog.accept(self)