Skip to content

Commit

Permalink
Use importlib.resources instead of pkg_resources (from setuptools)
Browse files Browse the repository at this point in the history
  • Loading branch information
Dries Van De Putte committed Apr 18, 2024
1 parent 171c645 commit 8bc62a0
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 13 deletions.
5 changes: 2 additions & 3 deletions pahfit/features/features.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
from astropy.table import vstack, Table, TableAttribute
from astropy.io.misc.yaml import yaml
import astropy.units as u
from pkg_resources import resource_filename
from importlib import resources
from pahfit.errors import PAHFITFeatureError
from pahfit.features.features_format import BoundedMaskedColumn, BoundedParTableFormatter

Expand Down Expand Up @@ -158,8 +158,7 @@ def _read_scipack(cls, file):
feat_tables = dict()

if not os.path.isfile(file):
pack_path = resource_filename("pahfit", "packs/science")
file = os.path.join(pack_path, file)
file = resources.files("pahfit") / "packs/science" / file
try:
with open(file) as fd:
scipack = yaml.load(fd, Loader=UniqueKeyLoader)
Expand Down
12 changes: 5 additions & 7 deletions pahfit/helpers.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import os
import pkg_resources
from importlib import resources

from specutils import Spectrum1D

Expand Down Expand Up @@ -32,14 +32,13 @@ def find_packfile(packfile):
if os.path.isfile(packfile):
packfile_found = packfile
else:
pack_path = pkg_resources.resource_filename("pahfit", "packs/science/")
test_packfile = "{}/{}".format(pack_path, packfile)
test_packfile = resources.files("pahfit") / "packs/science" / packfile
if os.path.isfile(test_packfile):
packfile_found = test_packfile
else:
raise ValueError("Input packfile {} not found".format(packfile))

return packfile_found
return str(packfile_found)


def read_spectrum(specfile, format=None):
Expand All @@ -63,8 +62,7 @@ def read_spectrum(specfile, format=None):
"""
# resolve filename
if not os.path.isfile(specfile):
pack_path = pkg_resources.resource_filename("pahfit", "data/")
test_specfile = "{}/{}".format(pack_path, specfile)
test_specfile = resources.files("pahfit") / "data" / specfile
if os.path.isfile(test_specfile):
specfile = test_specfile
else:
Expand All @@ -75,7 +73,7 @@ def read_spectrum(specfile, format=None):
tformat = None
# process user-specified or filename extension based format
if format is None:
suffix = specfile.split(".")[-1].lower()
suffix = specfile.name.split(".")[-1].lower()
if suffix == "ecsv":
tformat = "ECSV"
elif suffix == "ipac":
Expand Down
5 changes: 2 additions & 3 deletions pahfit/instrument.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,10 @@

import os
from pathlib import Path
import glob
import numpy as np
from numpy.polynomial import Polynomial
from astropy.io.misc import yaml
from pkg_resources import resource_filename
from importlib import resources
from pahfit.errors import PAHFITPackError, PAHFITWarning
from warnings import warn

Expand All @@ -25,7 +24,7 @@
def read_instrument_packs():
"""Read all instrument packs into the 'packs' variable."""
global packs
for pack in glob.glob(resource_filename("pahfit", "packs/instrument/*.yaml")):
for pack in (resources.files("pahfit") / "packs/instrument").glob("*.yaml"):
try:
with open(pack) as fd:
p = yaml.load(fd)
Expand Down

0 comments on commit 8bc62a0

Please sign in to comment.