diff options
author | Eli Schwartz <eschwartz@archlinux.org> | 2020-12-07 12:37:42 -0500 |
---|---|---|
committer | Eli Schwartz <eschwartz@archlinux.org> | 2020-12-07 14:25:05 -0500 |
commit | 1649c6fe1990367c73b600c445fb266ac0a9c102 (patch) | |
tree | 33de48ecab756c76034301a85bb51fbd5b775039 /qutebrowser/utils/utils.py | |
parent | 0a9cfaa35a368bde2b4d15bf1babaad52e1cd2a7 (diff) | |
download | qutebrowser-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.
Diffstat (limited to 'qutebrowser/utils/utils.py')
-rw-r--r-- | qutebrowser/utils/utils.py | 12 |
1 files changed, 8 insertions, 4 deletions
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: |