de7e40537d
- a few issues to work out still, and editor changes not done yet - for communication between webengine and python code, we set window .location to 'http://anki/<something>' - the leading http is necessary for qt to call the link handler, which was introduced in qt5.5 - the designer files now use a promoted qobject to create instances of AnkiWebView - we use the css zoom property to alter webengine font size based on system dpi - prefs and addons folder stored in new location (at least for now)
88 lines
2.8 KiB
Python
88 lines
2.8 KiB
Python
# Copyright: Damien Elmes <anki@ichi2.net>
|
|
# -*- coding: utf-8 -*-
|
|
# License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
|
|
|
|
import time, re, traceback
|
|
from aqt.qt import *
|
|
from anki.sync import httpCon
|
|
from aqt.utils import showWarning
|
|
from anki.hooks import addHook, remHook
|
|
import aqt.sync # monkey-patches httplib2
|
|
|
|
def download(mw, code):
|
|
"Download addon/deck from AnkiWeb. On success caller must stop progress diag."
|
|
# check code is valid
|
|
try:
|
|
code = int(code)
|
|
except ValueError:
|
|
showWarning(_("Invalid code."))
|
|
return
|
|
# create downloading thread
|
|
thread = Downloader(code)
|
|
def onRecv():
|
|
try:
|
|
mw.progress.update(label="%dKB downloaded" % (thread.recvTotal/1024))
|
|
except NameError:
|
|
# some users report the following error on long downloads
|
|
# NameError: free variable 'mw' referenced before assignment in enclosing scope
|
|
# unsure why this is happening, but guard against throwing the
|
|
# error
|
|
pass
|
|
thread.recv.connect(onRecv)
|
|
thread.start()
|
|
mw.progress.start(immediate=True)
|
|
while not thread.isFinished():
|
|
mw.app.processEvents()
|
|
thread.wait(100)
|
|
if not thread.error:
|
|
# success
|
|
return thread.data, thread.fname
|
|
else:
|
|
mw.progress.finish()
|
|
showWarning(_("Download failed: %s") % thread.error)
|
|
|
|
class Downloader(QThread):
|
|
|
|
recv = pyqtSignal()
|
|
|
|
def __init__(self, code):
|
|
QThread.__init__(self)
|
|
self.code = code
|
|
self.error = None
|
|
|
|
def run(self):
|
|
# setup progress handler
|
|
self.byteUpdate = time.time()
|
|
self.recvTotal = 0
|
|
def canPost():
|
|
if (time.time() - self.byteUpdate) > 0.1:
|
|
self.byteUpdate = time.time()
|
|
return True
|
|
def recvEvent(bytes):
|
|
self.recvTotal += bytes
|
|
if canPost():
|
|
self.recv.emit()
|
|
addHook("httpRecv", recvEvent)
|
|
con = httpCon()
|
|
try:
|
|
resp, cont = con.request(
|
|
aqt.appShared + "download/%d" % self.code)
|
|
except Exception as e:
|
|
exc = traceback.format_exc()
|
|
try:
|
|
self.error = str(e[0])
|
|
except:
|
|
self.error = str(exc)
|
|
return
|
|
finally:
|
|
remHook("httpRecv", recvEvent)
|
|
if resp['status'] == '200':
|
|
self.error = None
|
|
self.fname = re.match("attachment; filename=(.+)",
|
|
resp['content-disposition']).group(1)
|
|
self.data = cont
|
|
elif resp['status'] == '403':
|
|
self.error = _("Invalid code.")
|
|
else:
|
|
self.error = _("Error downloading: %s") % resp['status']
|