Skip to content

Commit 065c527

Browse files
authored
Replace deprecated pkg_resources with importlib.resources (#208)
* Replace deprecated pkg_resources with importlib.resources - Replace pkg_resources.resource_exists() with importlib.resources.files() and .is_file() - Replace pkg_resources.resource_filename() with importlib.resources.as_file() context manager - Fix bare except clause for better exception handling - Resolves deprecation warning and future-proofs against pkg_resources removal drop support for python < 3.9 - fixes readthedocs error Fixes #207
1 parent e9efa0d commit 065c527

6 files changed

Lines changed: 35 additions & 10 deletions

File tree

.github/workflows/continuousintegration.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ jobs:
77
runs-on: ubuntu-latest
88
strategy:
99
matrix:
10-
python-version: [3.8, 3.9, "3.10", "3.11"]
10+
python-version: [3.9, "3.10", "3.11"]
1111

1212
steps:
1313
- uses: actions/checkout@v2

.readthedocs.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ build:
55
tools:
66
python: "3.11"
77

8+
sphinx:
9+
configuration: docs/conf.py
10+
811
python:
912
install:
1013
- requirements: docs/requirements.txt

ctaplot/io/dataset.py

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
import pkg_resources
21
import os
32
import sys
43
import numpy as np
4+
from importlib.resources import files as importlib_files, as_file
5+
56

67
__all__ = ['get']
78

@@ -98,11 +99,18 @@ def get(resource_name):
9899
try:
99100
resource_path = find_resource(resource_name)
100101
except FileNotFoundError:
101-
if not pkg_resources.resource_exists(__name__, resource_name):
102+
# Check if resource exists using importlib.resources
103+
try:
104+
resource_ref = importlib_files(__name__) / resource_name
105+
if resource_ref.is_file():
106+
with as_file(resource_ref) as resource_path:
107+
return str(resource_path)
108+
else:
109+
raise FileNotFoundError("Couldn't find resource: '{}'"
110+
.format(resource_name))
111+
except (FileNotFoundError, AttributeError):
102112
raise FileNotFoundError("Couldn't find resource: '{}'"
103113
.format(resource_name))
104-
else:
105-
resource_path = pkg_resources.resource_filename(__name__, resource_name)
106114
return resource_path
107115

108116

@@ -120,8 +128,19 @@ def find_resource(resource_name):
120128
str - absolute path to the resource
121129
"""
122130
# If ctaplot is installed via python setup.py develop, data files stay in share
123-
share_dir = os.path.join(pkg_resources.resource_filename('ctaplot', ''), '../share/')
124-
gammaboard_dir = os.path.join(pkg_resources.resource_filename('ctaplot', ''), 'gammaboard/')
131+
try:
132+
# Get the ctaplot package directory using importlib.resources
133+
ctaplot_ref = importlib_files('ctaplot')
134+
with as_file(ctaplot_ref) as ctaplot_path:
135+
share_dir = os.path.join(str(ctaplot_path), '../share/')
136+
gammaboard_dir = os.path.join(str(ctaplot_path), 'gammaboard/')
137+
except (ImportError, FileNotFoundError):
138+
# Fallback if importlib.resources doesn't work
139+
import ctaplot
140+
ctaplot_path = os.path.dirname(ctaplot.__file__)
141+
share_dir = os.path.join(ctaplot_path, '../share/')
142+
gammaboard_dir = os.path.join(ctaplot_path, 'gammaboard/')
143+
125144
resources_dirs = [share_dir, gammaboard_dir]
126145
for res_dir in resources_dirs:
127146
for root, dirs, files in os.walk(res_dir):
@@ -156,7 +175,7 @@ def load_any_resource(filename):
156175
try:
157176
data = np.loadtxt(get(filename), skiprows=sr, unpack=True)
158177
break
159-
except:
178+
except Exception:
160179
sr += 1
161180

162181
return data

environment.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ dependencies:
99
- matplotlib
1010
- nbsphinx
1111
- numpy
12-
- python >= 3.6
12+
- python >= 3.9
1313
- pandas
1414
- pyyaml
1515
- pytables

setup.cfg

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,4 @@ github_project = cta-observatory/cta-plot
99
license = MIT
1010

1111
[options]
12-
python_requires = >=3.6
12+
python_requires = >=3.9

setup.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,9 @@ def get_property(prop, project):
4545
classifiers=[
4646
'Intended Audience :: Science/Research',
4747
'Programming Language :: Python :: 3',
48+
'Programming Language :: Python :: 3.9',
49+
'Programming Language :: Python :: 3.10',
50+
'Programming Language :: Python :: 3.11',
4851
'Topic :: Scientific/Engineering :: Astronomy',
4952
],
5053
data_files=[('ctaplot', dataset), ],

0 commit comments

Comments
 (0)