pdfjs: add file path to version information
Shows "bundled" if the bundled version is used.
This commit is contained in:
parent
6342febb44
commit
449a54c7d0
@ -100,21 +100,26 @@ SYSTEM_PDFJS_PATHS = [
|
|||||||
]
|
]
|
||||||
|
|
||||||
|
|
||||||
def get_pdfjs_res(path):
|
def get_pdfjs_res_and_path(path):
|
||||||
"""Get a pdf.js resource in binary format.
|
"""Get a pdf.js resource in binary format.
|
||||||
|
|
||||||
|
Returns a (content, path) tuple, where content is the file content and path
|
||||||
|
is the path where the file was found. If path is None, the bundled version
|
||||||
|
was used.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
path: The path inside the pdfjs directory.
|
path: The path inside the pdfjs directory.
|
||||||
"""
|
"""
|
||||||
path = path.lstrip('/')
|
path = path.lstrip('/')
|
||||||
content = None
|
content = None
|
||||||
|
file_path = None
|
||||||
|
|
||||||
# First try a system wide installation
|
# First try a system wide installation
|
||||||
# System installations might strip off the 'build/' or 'web/' prefixes.
|
# System installations might strip off the 'build/' or 'web/' prefixes.
|
||||||
# qute expects them, so we need to adjust for it.
|
# qute expects them, so we need to adjust for it.
|
||||||
names_to_try = [path, _remove_prefix(path)]
|
names_to_try = [path, _remove_prefix(path)]
|
||||||
for system_path in SYSTEM_PDFJS_PATHS:
|
for system_path in SYSTEM_PDFJS_PATHS:
|
||||||
content = _read_from_system(system_path, names_to_try)
|
content, file_path = _read_from_system(system_path, names_to_try)
|
||||||
if content is not None:
|
if content is not None:
|
||||||
break
|
break
|
||||||
|
|
||||||
@ -130,9 +135,19 @@ def get_pdfjs_res(path):
|
|||||||
# Might be script/html or might be binary
|
# Might be script/html or might be binary
|
||||||
text_content = content.decode('utf-8')
|
text_content = content.decode('utf-8')
|
||||||
except UnicodeDecodeError:
|
except UnicodeDecodeError:
|
||||||
return content
|
return (content, file_path)
|
||||||
text_content = fix_urls(text_content)
|
text_content = fix_urls(text_content)
|
||||||
return text_content.encode('utf-8')
|
return (text_content.encode('utf-8'), file_path)
|
||||||
|
|
||||||
|
|
||||||
|
def get_pdfjs_res(path):
|
||||||
|
"""Get a pdf.js resource in binary format.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
path: The path inside the pdfjs directory.
|
||||||
|
"""
|
||||||
|
content, _path = get_pdfjs_res_and_path(path)
|
||||||
|
return content
|
||||||
|
|
||||||
|
|
||||||
def _remove_prefix(path):
|
def _remove_prefix(path):
|
||||||
@ -151,10 +166,13 @@ def _remove_prefix(path):
|
|||||||
def _read_from_system(system_path, names):
|
def _read_from_system(system_path, names):
|
||||||
"""Try to read a file with one of the given names in system_path.
|
"""Try to read a file with one of the given names in system_path.
|
||||||
|
|
||||||
|
Returns a (content, path) tuple, where the path is the filepath that was
|
||||||
|
used.
|
||||||
|
|
||||||
Each file in names is considered equal, the first file that is found
|
Each file in names is considered equal, the first file that is found
|
||||||
is read and its binary content returned.
|
is read and its binary content returned.
|
||||||
|
|
||||||
Returns None if no file could be found
|
Returns (None, None) if no file could be found
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
system_path: The folder where the file should be searched.
|
system_path: The folder where the file should be searched.
|
||||||
@ -162,11 +180,12 @@ def _read_from_system(system_path, names):
|
|||||||
"""
|
"""
|
||||||
for name in names:
|
for name in names:
|
||||||
try:
|
try:
|
||||||
with open(os.path.join(system_path, name), 'rb') as f:
|
full_path = os.path.join(system_path, name)
|
||||||
return f.read()
|
with open(full_path, 'rb') as f:
|
||||||
|
return (f.read(), full_path)
|
||||||
except OSError:
|
except OSError:
|
||||||
continue
|
continue
|
||||||
return None
|
return (None, None)
|
||||||
|
|
||||||
|
|
||||||
def is_available():
|
def is_available():
|
||||||
|
@ -192,16 +192,20 @@ def _pdfjs_version():
|
|||||||
A string with the version number.
|
A string with the version number.
|
||||||
"""
|
"""
|
||||||
try:
|
try:
|
||||||
pdfjs_file = pdfjs.get_pdfjs_res('build/pdf.js').decode('utf-8')
|
pdfjs_file, file_path = pdfjs.get_pdfjs_res_and_path('build/pdf.js')
|
||||||
except pdfjs.PDFJSNotFound:
|
except pdfjs.PDFJSNotFound:
|
||||||
return 'no'
|
return 'no'
|
||||||
else:
|
else:
|
||||||
|
pdfjs_file = pdfjs_file.decode('utf-8')
|
||||||
version_re = re.compile(r"^PDFJS\.version = '([^']+)';$", re.MULTILINE)
|
version_re = re.compile(r"^PDFJS\.version = '([^']+)';$", re.MULTILINE)
|
||||||
match = version_re.search(pdfjs_file)
|
match = version_re.search(pdfjs_file)
|
||||||
if not match:
|
if not match:
|
||||||
return 'unknown'
|
pdfjs_version = 'unknown'
|
||||||
else:
|
else:
|
||||||
return match.group(1)
|
pdfjs_version = match.group(1)
|
||||||
|
if file_path is None:
|
||||||
|
file_path = 'bundled'
|
||||||
|
return '{} ({})'.format(pdfjs_version, file_path)
|
||||||
|
|
||||||
|
|
||||||
def version(short=False):
|
def version(short=False):
|
||||||
|
@ -535,14 +535,15 @@ class TestPDFJSVersion:
|
|||||||
"""Tests for _pdfjs_version."""
|
"""Tests for _pdfjs_version."""
|
||||||
|
|
||||||
def test_not_found(self, mocker):
|
def test_not_found(self, mocker):
|
||||||
mocker.patch('qutebrowser.utils.version.pdfjs.get_pdfjs_res',
|
mocker.patch('qutebrowser.utils.version.pdfjs.get_pdfjs_res_and_path',
|
||||||
side_effect=pdfjs.PDFJSNotFound)
|
side_effect=pdfjs.PDFJSNotFound)
|
||||||
assert version._pdfjs_version() == 'no'
|
assert version._pdfjs_version() == 'no'
|
||||||
|
|
||||||
def test_unknown(self, monkeypatch):
|
def test_unknown(self, monkeypatch):
|
||||||
monkeypatch.setattr('qutebrowser.utils.version.pdfjs.get_pdfjs_res',
|
monkeypatch.setattr(
|
||||||
lambda path: b'foobar')
|
'qutebrowser.utils.version.pdfjs.get_pdfjs_res_and_path',
|
||||||
assert version._pdfjs_version() == 'unknown'
|
lambda path: (b'foobar', None))
|
||||||
|
assert version._pdfjs_version() == 'unknown (bundled)'
|
||||||
|
|
||||||
def test_known(self, monkeypatch):
|
def test_known(self, monkeypatch):
|
||||||
pdfjs_code = textwrap.dedent("""
|
pdfjs_code = textwrap.dedent("""
|
||||||
@ -558,9 +559,10 @@ class TestPDFJSVersion:
|
|||||||
// Use strict in our context only - users might not want it
|
// Use strict in our context only - users might not want it
|
||||||
'use strict';
|
'use strict';
|
||||||
""").strip().encode('utf-8')
|
""").strip().encode('utf-8')
|
||||||
monkeypatch.setattr('qutebrowser.utils.version.pdfjs.get_pdfjs_res',
|
monkeypatch.setattr(
|
||||||
lambda path: pdfjs_code)
|
'qutebrowser.utils.version.pdfjs.get_pdfjs_res_and_path',
|
||||||
assert version._pdfjs_version() == '1.2.109'
|
lambda path: (pdfjs_code, '/foo/bar/pdf.js'))
|
||||||
|
assert version._pdfjs_version() == '1.2.109 (/foo/bar/pdf.js)'
|
||||||
|
|
||||||
|
|
||||||
class FakeQSslSocket:
|
class FakeQSslSocket:
|
||||||
|
Loading…
Reference in New Issue
Block a user