summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFlorian Bruhin <me@the-compiler.org>2018-10-04 16:41:16 +0200
committerFlorian Bruhin <me@the-compiler.org>2018-10-04 16:57:43 +0200
commit8913b896ae20b70c7181587270bc1efbe5e30257 (patch)
tree377e34ce0fc1afb2d0569e4f2a046afd86be1c40
parentda69bd9b8fc45f47956b576593e7a021a1cf1aa5 (diff)
downloadqutebrowser-8913b896ae20b70c7181587270bc1efbe5e30257.tar.gz
qutebrowser-8913b896ae20b70c7181587270bc1efbe5e30257.zip
Use a proper data dir for PDF.js
(cherry picked from commit 29142f763c4f7acb5fc75ee1788960c34234fa85)
-rw-r--r--qutebrowser/browser/pdfjs.py35
-rw-r--r--qutebrowser/html/no_pdfjs.html2
-rw-r--r--tests/unit/browser/test_pdfjs.py26
3 files changed, 40 insertions, 23 deletions
diff --git a/qutebrowser/browser/pdfjs.py b/qutebrowser/browser/pdfjs.py
index 4f719f11a..eb9f503e1 100644
--- a/qutebrowser/browser/pdfjs.py
+++ b/qutebrowser/browser/pdfjs.py
@@ -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
diff --git a/qutebrowser/html/no_pdfjs.html b/qutebrowser/html/no_pdfjs.html
index c38273f33..1fe90260a 100644
--- a/qutebrowser/html/no_pdfjs.html
+++ b/qutebrowser/html/no_pdfjs.html
@@ -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
diff --git a/tests/unit/browser/test_pdfjs.py b/tests/unit/browser/test_pdfjs.py
index 3ccd255ce..1f0133d02 100644
--- a/tests/unit/browser/test_pdfjs.py
+++ b/tests/unit/browser/test_pdfjs.py
@@ -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):