summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEli Schwartz <eschwartz@archlinux.org>2020-12-07 12:37:42 -0500
committerEli Schwartz <eschwartz@archlinux.org>2020-12-07 14:25:05 -0500
commit1649c6fe1990367c73b600c445fb266ac0a9c102 (patch)
tree33de48ecab756c76034301a85bb51fbd5b775039
parent0a9cfaa35a368bde2b4d15bf1babaad52e1cd2a7 (diff)
downloadqutebrowser-1649c6fe1990367c73b600c445fb266ac0a9c102.tar.gz
qutebrowser-1649c6fe1990367c73b600c445fb266ac0a9c102.zip
Migrate read_file from pkg_resources to importlib.resources
In python 3.9, we can get any resource from a package subdirectory using files(), but on older versions of python, even where importlib.resources exists, we need the backport.
-rw-r--r--README.asciidoc2
-rw-r--r--qutebrowser/utils/utils.py12
-rw-r--r--qutebrowser/utils/version.py1
-rwxr-xr-xsetup.py3
-rw-r--r--tests/unit/utils/test_version.py2
5 files changed, 15 insertions, 5 deletions
diff --git a/README.asciidoc b/README.asciidoc
index 5437036be..6ab9784a8 100644
--- a/README.asciidoc
+++ b/README.asciidoc
@@ -131,6 +131,8 @@ The following software and libraries are required to run qutebrowser:
* http://pygments.org/[pygments]
* https://github.com/yaml/pyyaml[PyYAML]
* https://www.attrs.org/[attrs]
+* https://importlib-resources.readthedocs.io/[importlib_resources] (on python
+ 3.8 or older)
The following libraries are optional:
diff --git a/qutebrowser/utils/utils.py b/qutebrowser/utils/utils.py
index 31ff5bf50..f9d01fb62 100644
--- a/qutebrowser/utils/utils.py
+++ b/qutebrowser/utils/utils.py
@@ -42,6 +42,11 @@ from typing import (Any, Callable, IO, Iterator, Optional, Sequence, Tuple, Type
from PyQt5.QtCore import QUrl, QVersionNumber
from PyQt5.QtGui import QClipboard, QDesktopServices
from PyQt5.QtWidgets import QApplication
+# We cannot use the stdlib version on 3.7-3.8 because we need the files() API.
+if sys.version_info >= (3, 9):
+ import importlib.resources as importlib_resources
+else:
+ import importlib_resources
import pkg_resources
import yaml
try:
@@ -216,13 +221,12 @@ def read_file(filename: str, binary: bool = False) -> Any:
with open(fn, 'r', encoding='utf-8') as f:
return f.read()
else:
- data = pkg_resources.resource_string(
- qutebrowser.__name__, filename)
+ p = importlib_resources.files(qutebrowser) / filename
if binary:
- return data
+ return p.read_bytes()
- return data.decode('UTF-8')
+ return p.read_text()
def resource_filename(filename: str) -> str:
diff --git a/qutebrowser/utils/version.py b/qutebrowser/utils/version.py
index 64efe4c4f..09aeb5a13 100644
--- a/qutebrowser/utils/version.py
+++ b/qutebrowser/utils/version.py
@@ -263,6 +263,7 @@ def _module_versions() -> Sequence[str]:
('pygments', ['__version__']),
('yaml', ['__version__']),
('attr', ['__version__']),
+ ('importlib_resources', []),
('PyQt5.QtWebEngineWidgets', []),
('PyQt5.QtWebEngine', ['PYQT_WEBENGINE_VERSION_STR']),
('PyQt5.QtWebKitWidgets', []),
diff --git a/setup.py b/setup.py
index 1169eae81..137b514ca 100755
--- a/setup.py
+++ b/setup.py
@@ -71,7 +71,8 @@ try:
entry_points={'gui_scripts':
['qutebrowser = qutebrowser.qutebrowser:main']},
zip_safe=True,
- install_requires=['pypeg2', 'jinja2', 'pygments', 'PyYAML', 'attrs'],
+ install_requires=['pypeg2', 'jinja2', 'pygments', 'PyYAML', 'attrs',
+ 'importlib_resources>=1.1.0; python_version < "3.9"'],
python_requires='>=3.6',
name='qutebrowser',
version=_get_constant('version'),
diff --git a/tests/unit/utils/test_version.py b/tests/unit/utils/test_version.py
index 593557ae8..922692fdd 100644
--- a/tests/unit/utils/test_version.py
+++ b/tests/unit/utils/test_version.py
@@ -561,11 +561,13 @@ class ImportFake:
('pygments', True),
('yaml', True),
('attr', True),
+ ('importlib_resources', True),
('PyQt5.QtWebEngineWidgets', True),
('PyQt5.QtWebEngine', True),
('PyQt5.QtWebKitWidgets', True),
])
self.no_version_attribute = ['sip',
+ 'importlib_resources',
'PyQt5.QtWebEngineWidgets',
'PyQt5.QtWebKitWidgets',
'PyQt5.QtWebEngine']