mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-09 03:04:10 -04:00
DRYer
This commit is contained in:
parent
1ccc979081
commit
2c15434bab
@ -14,8 +14,8 @@ from functools import partial
|
|||||||
from threading import Thread
|
from threading import Thread
|
||||||
|
|
||||||
from PyQt5.Qt import (
|
from PyQt5.Qt import (
|
||||||
QApplication, QBuffer, QByteArray, QHBoxLayout, QIcon, QMenu, QSize, QTimer,
|
QApplication, QByteArray, QHBoxLayout, QIcon, QMenu, QSize, QTimer, QToolBar,
|
||||||
QToolBar, QUrl, QVBoxLayout, QWidget, pyqtSignal
|
QUrl, QVBoxLayout, QWidget, pyqtSignal
|
||||||
)
|
)
|
||||||
from PyQt5.QtWebEngineCore import QWebEngineUrlSchemeHandler
|
from PyQt5.QtWebEngineCore import QWebEngineUrlSchemeHandler
|
||||||
from PyQt5.QtWebEngineWidgets import (
|
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.ebooks.oeb.polish.parsing import parse
|
||||||
from calibre.gui2 import NO_URL_FORMATTING, error_dialog, open_url
|
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.tweak_book import TOP, actions, current_container, editors, tprefs
|
||||||
|
from calibre.gui2.viewer2.web_view import send_reply
|
||||||
from calibre.gui2.webengine import (
|
from calibre.gui2.webengine import (
|
||||||
Bridge, RestartingWebEngineView, create_script, from_js, insert_scripts,
|
Bridge, RestartingWebEngineView, create_script, from_js, insert_scripts,
|
||||||
secure_webengine, to_js
|
secure_webengine, to_js
|
||||||
@ -40,12 +41,6 @@ from polyglot.builtins import unicode_type
|
|||||||
from polyglot.queue import Empty, Queue
|
from polyglot.queue import Empty, Queue
|
||||||
from polyglot.urllib import urlparse
|
from polyglot.urllib import urlparse
|
||||||
|
|
||||||
try:
|
|
||||||
from PyQt5 import sip
|
|
||||||
except ImportError:
|
|
||||||
import sip
|
|
||||||
|
|
||||||
|
|
||||||
shutdown = object()
|
shutdown = object()
|
||||||
|
|
||||||
|
|
||||||
@ -203,25 +198,12 @@ class UrlSchemeHandler(QWebEngineUrlSchemeHandler):
|
|||||||
'application/x-font-truetype':'application/x-font-ttf',
|
'application/x-font-truetype':'application/x-font-ttf',
|
||||||
'application/font-sfnt': 'application/x-font-ttf',
|
'application/font-sfnt': 'application/x-font-ttf',
|
||||||
}.get(mime_type, mime_type)
|
}.get(mime_type, mime_type)
|
||||||
self.send_reply(rq, mime_type, data)
|
send_reply(rq, mime_type, data)
|
||||||
except Exception:
|
except Exception:
|
||||||
import traceback
|
import traceback
|
||||||
traceback.print_exc()
|
traceback.print_exc()
|
||||||
rq.fail(rq.RequestFailed)
|
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):
|
def check_for_parse(self):
|
||||||
remove = []
|
remove = []
|
||||||
for name, requests in self.requests.iteritems():
|
for name, requests in self.requests.iteritems():
|
||||||
@ -230,7 +212,7 @@ class UrlSchemeHandler(QWebEngineUrlSchemeHandler):
|
|||||||
if not isinstance(data, bytes):
|
if not isinstance(data, bytes):
|
||||||
data = data.encode('utf-8')
|
data = data.encode('utf-8')
|
||||||
for mime_type, rq in requests:
|
for mime_type, rq in requests:
|
||||||
self.send_reply(rq, mime_type, data)
|
send_reply(rq, mime_type, data)
|
||||||
remove.append(name)
|
remove.append(name)
|
||||||
for name in remove:
|
for name in remove:
|
||||||
del self.requests[name]
|
del self.requests[name]
|
||||||
|
@ -57,6 +57,21 @@ def get_data(name):
|
|||||||
return None, None
|
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):
|
class UrlSchemeHandler(QWebEngineUrlSchemeHandler):
|
||||||
|
|
||||||
def __init__(self, parent=None):
|
def __init__(self, parent=None):
|
||||||
@ -88,7 +103,7 @@ class UrlSchemeHandler(QWebEngineUrlSchemeHandler):
|
|||||||
'application/x-font-truetype':'application/x-font-ttf',
|
'application/x-font-truetype':'application/x-font-ttf',
|
||||||
'application/font-sfnt': 'application/x-font-ttf',
|
'application/font-sfnt': 'application/x-font-ttf',
|
||||||
}.get(mime_type, mime_type)
|
}.get(mime_type, mime_type)
|
||||||
self.send_reply(rq, mime_type, data)
|
send_reply(rq, mime_type, data)
|
||||||
except Exception:
|
except Exception:
|
||||||
import traceback
|
import traceback
|
||||||
traceback.print_exc()
|
traceback.print_exc()
|
||||||
@ -97,7 +112,7 @@ class UrlSchemeHandler(QWebEngineUrlSchemeHandler):
|
|||||||
manifest, mime_type = get_data('calibre-book-manifest.json')
|
manifest, mime_type = get_data('calibre-book-manifest.json')
|
||||||
metadata = get_data('calibre-book-metadata.json')[0]
|
metadata = get_data('calibre-book-metadata.json')[0]
|
||||||
data = b'[' + manifest + b',' + metadata + b']'
|
data = b'[' + manifest + b',' + metadata + b']'
|
||||||
self.send_reply(rq, mime_type, data)
|
send_reply(rq, mime_type, data)
|
||||||
elif name.startswith('mathjax/'):
|
elif name.startswith('mathjax/'):
|
||||||
from calibre.gui2.viewer2.mathjax import monkeypatch_mathjax
|
from calibre.gui2.viewer2.mathjax import monkeypatch_mathjax
|
||||||
if name == 'mathjax/manifest.json':
|
if name == 'mathjax/manifest.json':
|
||||||
@ -105,7 +120,7 @@ class UrlSchemeHandler(QWebEngineUrlSchemeHandler):
|
|||||||
import json
|
import json
|
||||||
from calibre.srv.books import get_mathjax_manifest
|
from calibre.srv.books import get_mathjax_manifest
|
||||||
self.mathjax_manifest = json.dumps(get_mathjax_manifest()['files'])
|
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
|
return
|
||||||
path = os.path.abspath(os.path.join(self.mathjax_dir, '..', name))
|
path = os.path.abspath(os.path.join(self.mathjax_dir, '..', name))
|
||||||
if path.startswith(self.mathjax_dir):
|
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 = open(os.path.expanduser('~/work/mathjax/unpacked/MathJax.js')).read()
|
||||||
raw = monkeypatch_mathjax(raw.decode('utf-8')).encode('utf-8')
|
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)
|
|
||||||
# }}}
|
# }}}
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user