pdfjs: Replace inline-removal with own function

Easier to unit-test and easier to understand for other programmers.
This commit is contained in:
Daniel Schadt 2015-12-08 16:46:18 +01:00
parent c0b3160676
commit 1279e418ee

View File

@ -107,7 +107,7 @@ def get_pdfjs_res(path):
# 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, path[path.index('/') + 1:]]
names_to_try = [path, _remove_prefix(path)]
for system_path in SYSTEM_PDFJS_PATHS:
content = _read_from_system(system_path, names_to_try)
if content is not None:
@ -130,6 +130,19 @@ def get_pdfjs_res(path):
return text_content.encode('utf-8')
def _remove_prefix(path):
"""Remove the web/ or build/ prefix of a pdfjs-file-path.
Args:
path: Path as string where the prefix should be stripped off.
"""
prefixes = {'web/', 'build/'}
if any(path.startswith(prefix) for prefix in prefixes):
return path.split('/', maxsplit=1)[1]
# Return the unchanged path if no prefix is found
return path
def _read_from_system(system_path, names):
"""Try to read a file with one of the given names in system_path.