Skip to content

Commit 701c681

Browse files
committed
deprecate pkg_resources
1 parent a612e04 commit 701c681

File tree

2 files changed

+33
-12
lines changed

2 files changed

+33
-12
lines changed

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616
setup(
1717
name="stempeg",
18-
version="0.2.4",
18+
version="0.2.5",
1919
description="Read and write stem/multistream audio files",
2020
long_description=long_description,
2121
long_description_content_type="text/markdown",

stempeg/__init__.py

Lines changed: 32 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,15 @@
2727
import subprocess as sp
2828
from os import path as op
2929
import argparse
30-
import pkg_resources
30+
31+
# Prefer importlib.resources to avoid deprecated pkg_resources
32+
try:
33+
from importlib.resources import files, as_file # Python 3.9+
34+
35+
_USE_IMPORTLIB_RESOURCES = True
36+
except Exception: # pragma: no cover - fallback on very old Python
37+
_USE_IMPORTLIB_RESOURCES = False
38+
import pkg_resources # type: ignore
3139

3240
__version__ = "0.2.2"
3341

@@ -38,12 +46,19 @@ def example_stem_path():
3846
Returns
3947
-------
4048
filename : str
41-
Path to the stem file
49+
Filesystem path to the stem file (temp path if package is zipped)
4250
"""
43-
return pkg_resources.resource_filename(
44-
__name__,
45-
'data/The Easton Ellises - Falcon 69.stem.mp4'
46-
)
51+
rel_path = "data/The Easton Ellises - Falcon 69.stem.mp4"
52+
if _USE_IMPORTLIB_RESOURCES:
53+
ref = files("stempeg").joinpath(rel_path)
54+
try:
55+
# as_file handles zipped/zip-safe installs by creating a tmp file
56+
with as_file(ref) as concrete_path:
57+
return str(concrete_path)
58+
except Exception:
59+
return str(ref)
60+
else: # fallback
61+
return pkg_resources.resource_filename(__name__, rel_path)
4762

4863

4964
def default_metadata():
@@ -52,12 +67,18 @@ def default_metadata():
5267
Returns
5368
-------
5469
filename : str
55-
Path to the json file
70+
Filesystem path to the json file (temp path if package is zipped)
5671
"""
57-
return pkg_resources.resource_filename(
58-
__name__,
59-
'data/default_metadata.json'
60-
)
72+
rel_path = "data/default_metadata.json"
73+
if _USE_IMPORTLIB_RESOURCES:
74+
ref = files("stempeg").joinpath(rel_path)
75+
try:
76+
with as_file(ref) as concrete_path:
77+
return str(concrete_path)
78+
except Exception:
79+
return str(ref)
80+
else: # fallback
81+
return pkg_resources.resource_filename(__name__, rel_path)
6182

6283

6384
def ffmpeg_version():

0 commit comments

Comments
 (0)