Skip to content

Commit a31979d

Browse files
authored
Port from pkg_resources to importlib.resources (#97)
Switch from `pkg_resources` which is deprecated (see https://setuptools.pypa.io/en/latest/pkg_resources.html) to `importlib.resources` (or `importlib_resources` for Python < 3.9).
1 parent 62a112f commit a31979d

6 files changed

Lines changed: 31 additions & 9 deletions

File tree

.github/workflows/tests.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,14 @@ jobs:
1010
strategy:
1111
matrix:
1212
python-version: [3.7, 3.8, 3.9, "3.10", "3.11", "3.12", "pypy3.9"]
13-
pytest-extra-options: ["--strict-config -W \"ignore:pkg_resources is deprecated\""]
13+
pytest-extra-options: ["--strict-config"]
1414
include:
1515
- python-version: "pypy2.7"
1616
pytest-extra-options: ""
1717
- python-version: "pypy3.11"
1818
pytest-extra-options: ""
1919
- python-version: "3.13.0-beta.2"
20-
pytest-extra-options: "--strict-config -W \"ignore:pkg_resources is deprecated\" -W ignore::DeprecationWarning"
20+
pytest-extra-options: "--strict-config -W ignore::DeprecationWarning"
2121
fail-fast: false
2222

2323
steps:

doc/loader.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -154,8 +154,8 @@ That is the same as:
154154
``package(name, path)``
155155
-----------------------
156156

157-
Uses the ``pkg_resources`` API to locate files in Python package data (which
158-
may be inside a ZIP archive).
157+
Uses the ``importlib.resources`` API to locate files in Python package data
158+
(which may be inside a ZIP archive).
159159

160160
.. code-block:: python
161161

genshi/template/loader.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@
1313

1414
"""Template loading and caching."""
1515

16+
try:
17+
from importlib.resources import open_binary as resources_open_binary
18+
except ImportError:
19+
from importlib_resources import open_binary as resources_open_binary
1620
import os
1721
try:
1822
import threading
@@ -300,10 +304,14 @@ def package(name, path):
300304
:return: the loader function to load templates from the given package
301305
:rtype: ``function``
302306
"""
303-
from pkg_resources import resource_stream
304307
def _load_from_package(filename):
305308
filepath = os.path.join(path, filename)
306-
return filepath, filename, resource_stream(name, filepath), None
309+
return (
310+
filepath,
311+
filename,
312+
resources_open_binary(name, filepath),
313+
None,
314+
)
307315
return _load_from_package
308316

309317
@staticmethod

genshi/template/plugin.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,17 @@
1616
CherryPy/Buffet.
1717
"""
1818

19+
try:
20+
from importlib.resources import (
21+
as_file as resources_as_file,
22+
files as resources_files,
23+
)
24+
except ImportError:
25+
from importlib_resources import (
26+
as_file as resources_as_file,
27+
files as resources_files,
28+
)
29+
1930
from genshi.compat import string_types
2031
from genshi.input import ET, HTML, XML
2132
from genshi.output import DocType
@@ -91,10 +102,11 @@ def load_template(self, templatename, template_string=None):
91102
if self.use_package_naming:
92103
divider = templatename.rfind('.')
93104
if divider >= 0:
94-
from pkg_resources import resource_filename
95105
package = templatename[:divider]
96106
basename = templatename[divider + 1:] + self.extension
97-
templatename = resource_filename(package, basename)
107+
resource = resources_files(package) / basename
108+
with resources_as_file(resource) as path:
109+
return self.loader.load(str(path))
98110

99111
return self.loader.load(templatename)
100112

setup.cfg

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ packages =
4343
genshi.template.tests.templates
4444
setup_requires =
4545
setuptools
46+
install_requires =
47+
importlib-resources; python_version<"3.9"
4648

4749
[options.entry_points]
4850
babel.extractors =

tox.ini

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
envlist = py27,py36,py37,py38,py39,py310,py311,py312,pypy,pypy3
33
[testenv]
44
deps=
5-
setuptools
5+
.
66
pytest
77
setenv=
88
PYTHONHASHSEED=0

0 commit comments

Comments
 (0)