From 8bc62a068152e9f7b15cce8c98152dd315448404 Mon Sep 17 00:00:00 2001 From: Dries Van De Putte Date: Thu, 18 Apr 2024 19:39:01 -0400 Subject: [PATCH] Use importlib.resources instead of pkg_resources (from setuptools) --- pahfit/features/features.py | 5 ++--- pahfit/helpers.py | 12 +++++------- pahfit/instrument.py | 5 ++--- 3 files changed, 9 insertions(+), 13 deletions(-) diff --git a/pahfit/features/features.py b/pahfit/features/features.py index da5550e5..67302ef1 100644 --- a/pahfit/features/features.py +++ b/pahfit/features/features.py @@ -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 @@ -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) diff --git a/pahfit/helpers.py b/pahfit/helpers.py index cc42836e..65c46065 100644 --- a/pahfit/helpers.py +++ b/pahfit/helpers.py @@ -1,5 +1,5 @@ import os -import pkg_resources +from importlib import resources from specutils import Spectrum1D @@ -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): @@ -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: @@ -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": diff --git a/pahfit/instrument.py b/pahfit/instrument.py index 3828d9c7..c55bdb8a 100644 --- a/pahfit/instrument.py +++ b/pahfit/instrument.py @@ -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 @@ -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)