Use a proper data dir for PDF.js

This commit is contained in:
Florian Bruhin 2018-10-04 16:41:16 +02:00
parent c472adfa82
commit 29142f763c
3 changed files with 40 additions and 23 deletions
qutebrowser
tests/unit/browser

View File

@ -24,7 +24,8 @@ import os
from PyQt5.QtCore import QUrl, QUrlQuery
from qutebrowser.utils import utils, javascript, jinja, qtutils, usertypes
from qutebrowser.utils import (utils, javascript, jinja, qtutils, usertypes,
standarddir)
from qutebrowser.misc import objects
from qutebrowser.config import config
@ -55,7 +56,8 @@ def generate_pdfjs_page(filename, url):
if not is_available():
return jinja.render('no_pdfjs.html',
url=url.toDisplayString(),
title="PDF.js not found")
title="PDF.js not found",
pdfjs_dir=os.path.join(standarddir.data(), 'pdfjs'))
html = get_pdfjs_res('web/viewer.html').decode('utf-8')
script = _generate_pdfjs_script(filename)
@ -110,19 +112,6 @@ def _generate_pdfjs_script(filename):
objects.backend == usertypes.Backend.QtWebEngine))
SYSTEM_PDFJS_PATHS = [
# Debian pdf.js-common
# Arch Linux pdfjs (AUR)
'/usr/share/pdf.js/',
# Arch Linux pdf.js (AUR)
'/usr/share/javascript/pdf.js/',
# Debian libjs-pdf
'/usr/share/javascript/pdf/',
# fallback
os.path.expanduser('~/.local/share/qutebrowser/pdfjs/'),
]
def get_pdfjs_res_and_path(path):
"""Get a pdf.js resource in binary format.
@ -137,11 +126,25 @@ def get_pdfjs_res_and_path(path):
content = None
file_path = None
system_paths = [
# Debian pdf.js-common
# Arch Linux pdfjs (AUR)
'/usr/share/pdf.js/',
# Arch Linux pdf.js (AUR)
'/usr/share/javascript/pdf.js/',
# Debian libjs-pdf
'/usr/share/javascript/pdf/',
# fallback
os.path.join(standarddir.data(), 'pdfjs'),
# hardcoded fallback for --temp-basedir
os.path.expanduser('~/.local/share/qutebrowser/pdfjs/'),
]
# First try a system wide installation
# System installations might strip off the 'build/' or 'web/' prefixes.
# qute expects them, so we need to adjust for it.
names_to_try = [path, _remove_prefix(path)]
for system_path in SYSTEM_PDFJS_PATHS:
for system_path in system_paths:
content, file_path = _read_from_system(system_path, names_to_try)
if content is not None:
break

View File

@ -111,7 +111,7 @@ li {
<li>
You can manually download the pdf.js archive
<a href="https://mozilla.github.io/pdf.js/getting_started/#download">here</a>
and extract it to <code>~/.local/share/qutebrowser/pdfjs</code>
and extract it to <code>{{ pdfjs_dir }}</code>
<br>
<span class="warning">Warning:</span> Using this method you are
responsible for yourself to keep the installation updated! If a

View File

@ -17,6 +17,8 @@
# You should have received a copy of the GNU General Public License
# along with qutebrowser. If not, see <http://www.gnu.org/licenses/>.
import os.path
import pytest
from PyQt5.QtCore import QUrl
@ -24,10 +26,14 @@ from qutebrowser.browser import pdfjs
from qutebrowser.utils import usertypes, utils
@pytest.fixture(autouse=True)
def patch_data_dir(monkeypatch, tmpdir):
monkeypatch.setattr(pdfjs.standarddir, 'data',
lambda: str(tmpdir / 'data'))
@pytest.mark.parametrize('available, snippet', [
pytest.param(True, '<title>PDF.js viewer</title>',
marks=pytest.mark.skipif(not pdfjs.is_available(),
reason='PDF.js unavailable')),
(True, '<title>PDF.js viewer</title>'),
(False, '<h1>No pdf.js installation found</h1>'),
('force', 'fake PDF.js'),
])
@ -36,8 +42,12 @@ def test_generate_pdfjs_page(available, snippet, monkeypatch):
monkeypatch.setattr(pdfjs, 'is_available', lambda: True)
monkeypatch.setattr(pdfjs, 'get_pdfjs_res',
lambda filename: b'fake PDF.js')
elif available:
if not pdfjs.is_available():
pytest.skip("PDF.js unavailable")
monkeypatch.setattr(pdfjs, 'is_available', lambda: True)
else:
monkeypatch.setattr(pdfjs, 'is_available', lambda: available)
monkeypatch.setattr(pdfjs, 'is_available', lambda: False)
content = pdfjs.generate_pdfjs_page('example.pdf', QUrl())
print(content)
@ -110,7 +120,8 @@ class TestResources:
read_system_mock.assert_called_with('/usr/share/pdf.js/',
['web/test', 'test'])
def test_get_pdfjs_res_bundled(self, read_system_mock, read_file_mock):
def test_get_pdfjs_res_bundled(self, read_system_mock, read_file_mock,
tmpdir):
read_system_mock.return_value = (None, None)
read_file_mock.return_value = b'content'
@ -118,7 +129,10 @@ class TestResources:
assert pdfjs.get_pdfjs_res_and_path('web/test') == (b'content', None)
assert pdfjs.get_pdfjs_res('web/test') == b'content'
for path in pdfjs.SYSTEM_PDFJS_PATHS:
for path in ['/usr/share/pdf.js/',
str(tmpdir / 'data' / 'pdfjs'),
# hardcoded for --temp-basedir
os.path.expanduser('~/.local/share/qutebrowser/pdfjs/')]:
read_system_mock.assert_any_call(path, ['web/test', 'test'])
def test_get_pdfjs_res_not_found(self, read_system_mock, read_file_mock):