This commit is contained in:
Kovid Goyal 2019-02-05 08:37:25 +05:30
parent 1ccc979081
commit 2c15434bab
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
2 changed files with 24 additions and 40 deletions

View File

@ -14,8 +14,8 @@ from functools import partial
from threading import Thread
from PyQt5.Qt import (
QApplication, QBuffer, QByteArray, QHBoxLayout, QIcon, QMenu, QSize, QTimer,
QToolBar, QUrl, QVBoxLayout, QWidget, pyqtSignal
QApplication, QByteArray, QHBoxLayout, QIcon, QMenu, QSize, QTimer, QToolBar,
QUrl, QVBoxLayout, QWidget, pyqtSignal
)
from PyQt5.QtWebEngineCore import QWebEngineUrlSchemeHandler
from PyQt5.QtWebEngineWidgets import (
@ -30,6 +30,7 @@ from calibre.ebooks.oeb.base import OEB_DOCS, XHTML_MIME, serialize
from calibre.ebooks.oeb.polish.parsing import parse
from calibre.gui2 import NO_URL_FORMATTING, error_dialog, open_url
from calibre.gui2.tweak_book import TOP, actions, current_container, editors, tprefs
from calibre.gui2.viewer2.web_view import send_reply
from calibre.gui2.webengine import (
Bridge, RestartingWebEngineView, create_script, from_js, insert_scripts,
secure_webengine, to_js
@ -40,12 +41,6 @@ from polyglot.builtins import unicode_type
from polyglot.queue import Empty, Queue
from polyglot.urllib import urlparse
try:
from PyQt5 import sip
except ImportError:
import sip
shutdown = object()
@ -203,25 +198,12 @@ class UrlSchemeHandler(QWebEngineUrlSchemeHandler):
'application/x-font-truetype':'application/x-font-ttf',
'application/font-sfnt': 'application/x-font-ttf',
}.get(mime_type, mime_type)
self.send_reply(rq, mime_type, data)
send_reply(rq, mime_type, data)
except Exception:
import traceback
traceback.print_exc()
rq.fail(rq.RequestFailed)
def send_reply(self, rq, mime_type, data):
if sip.isdeleted(rq):
return
buf = QBuffer(parent=rq)
buf.open(QBuffer.WriteOnly)
# we have to copy data into buf as it will be garbage
# collected by python
buf.write(data)
buf.seek(0)
buf.close()
buf.aboutToClose.connect(buf.deleteLater)
rq.reply(mime_type.encode('ascii'), buf)
def check_for_parse(self):
remove = []
for name, requests in self.requests.iteritems():
@ -230,7 +212,7 @@ class UrlSchemeHandler(QWebEngineUrlSchemeHandler):
if not isinstance(data, bytes):
data = data.encode('utf-8')
for mime_type, rq in requests:
self.send_reply(rq, mime_type, data)
send_reply(rq, mime_type, data)
remove.append(name)
for name in remove:
del self.requests[name]

View File

@ -57,6 +57,21 @@ def get_data(name):
return None, None
def send_reply(rq, mime_type, data):
if sip.isdeleted(rq):
return
# make the buf a child of rq so that it is automatically deleted when
# rq is deleted
buf = QBuffer(parent=rq)
buf.open(QBuffer.WriteOnly)
# we have to copy data into buf as it will be garbage
# collected by python
buf.write(data)
buf.seek(0)
buf.close()
rq.reply(mime_type.encode('ascii'), buf)
class UrlSchemeHandler(QWebEngineUrlSchemeHandler):
def __init__(self, parent=None):
@ -88,7 +103,7 @@ class UrlSchemeHandler(QWebEngineUrlSchemeHandler):
'application/x-font-truetype':'application/x-font-ttf',
'application/font-sfnt': 'application/x-font-ttf',
}.get(mime_type, mime_type)
self.send_reply(rq, mime_type, data)
send_reply(rq, mime_type, data)
except Exception:
import traceback
traceback.print_exc()
@ -97,7 +112,7 @@ class UrlSchemeHandler(QWebEngineUrlSchemeHandler):
manifest, mime_type = get_data('calibre-book-manifest.json')
metadata = get_data('calibre-book-metadata.json')[0]
data = b'[' + manifest + b',' + metadata + b']'
self.send_reply(rq, mime_type, data)
send_reply(rq, mime_type, data)
elif name.startswith('mathjax/'):
from calibre.gui2.viewer2.mathjax import monkeypatch_mathjax
if name == 'mathjax/manifest.json':
@ -105,7 +120,7 @@ class UrlSchemeHandler(QWebEngineUrlSchemeHandler):
import json
from calibre.srv.books import get_mathjax_manifest
self.mathjax_manifest = json.dumps(get_mathjax_manifest()['files'])
self.send_reply(rq, 'application/json', self.mathjax_manifest)
send_reply(rq, 'application/json', self.mathjax_manifest)
return
path = os.path.abspath(os.path.join(self.mathjax_dir, '..', name))
if path.startswith(self.mathjax_dir):
@ -121,21 +136,8 @@ class UrlSchemeHandler(QWebEngineUrlSchemeHandler):
# raw = open(os.path.expanduser('~/work/mathjax/unpacked/MathJax.js')).read()
raw = monkeypatch_mathjax(raw.decode('utf-8')).encode('utf-8')
self.send_reply(rq, mt, raw)
send_reply(rq, mt, raw)
def send_reply(self, rq, mime_type, data):
if sip.isdeleted(rq):
return
# make the buf a child of rq so that it is automatically deleted when
# rq is deleted
buf = QBuffer(parent=rq)
buf.open(QBuffer.WriteOnly)
# we have to copy data into buf as it will be garbage
# collected by python
buf.write(data)
buf.seek(0)
buf.close()
rq.reply(mime_type.encode('ascii'), buf)
# }}}