From 2e56a6b4a42763cafb1e8ffc59e3911c51935006 Mon Sep 17 00:00:00 2001 From: Thomas Vuillaume Date: Fri, 11 Jul 2025 14:28:44 +0200 Subject: [PATCH 1/4] 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 - Add fallback to importlib_resources for Python < 3.9 compatibility - Fix bare except clause for better exception handling - Resolves deprecation warning and future-proofs against pkg_resources removal Fixes #207 --- ctaplot/io/dataset.py | 36 +++++++++++++++++++++++++++++------- 1 file changed, 29 insertions(+), 7 deletions(-) diff --git a/ctaplot/io/dataset.py b/ctaplot/io/dataset.py index b0eea41..1a88ce4 100644 --- a/ctaplot/io/dataset.py +++ b/ctaplot/io/dataset.py @@ -1,7 +1,11 @@ -import pkg_resources import os import sys import numpy as np +try: + from importlib.resources import files as importlib_files, as_file +except ImportError: + # Fallback for Python < 3.9 + from importlib_resources import files as importlib_files, as_file __all__ = ['get'] @@ -98,11 +102,18 @@ def get(resource_name): try: resource_path = find_resource(resource_name) except FileNotFoundError: - if not pkg_resources.resource_exists(__name__, resource_name): + # Check if resource exists using importlib.resources + try: + resource_ref = importlib_files(__name__) / resource_name + if resource_ref.is_file(): + with as_file(resource_ref) as resource_path: + return str(resource_path) + else: + raise FileNotFoundError("Couldn't find resource: '{}'" + .format(resource_name)) + except (FileNotFoundError, AttributeError): raise FileNotFoundError("Couldn't find resource: '{}'" .format(resource_name)) - else: - resource_path = pkg_resources.resource_filename(__name__, resource_name) return resource_path @@ -120,8 +131,19 @@ def find_resource(resource_name): str - absolute path to the resource """ # If ctaplot is installed via python setup.py develop, data files stay in share - share_dir = os.path.join(pkg_resources.resource_filename('ctaplot', ''), '../share/') - gammaboard_dir = os.path.join(pkg_resources.resource_filename('ctaplot', ''), 'gammaboard/') + try: + # Get the ctaplot package directory using importlib.resources + ctaplot_ref = importlib_files('ctaplot') + with as_file(ctaplot_ref) as ctaplot_path: + share_dir = os.path.join(str(ctaplot_path), '../share/') + gammaboard_dir = os.path.join(str(ctaplot_path), 'gammaboard/') + except (ImportError, FileNotFoundError): + # Fallback if importlib.resources doesn't work + import ctaplot + ctaplot_path = os.path.dirname(ctaplot.__file__) + share_dir = os.path.join(ctaplot_path, '../share/') + gammaboard_dir = os.path.join(ctaplot_path, 'gammaboard/') + resources_dirs = [share_dir, gammaboard_dir] for res_dir in resources_dirs: for root, dirs, files in os.walk(res_dir): @@ -156,7 +178,7 @@ def load_any_resource(filename): try: data = np.loadtxt(get(filename), skiprows=sr, unpack=True) break - except: + except Exception: sr += 1 return data From e9c50be7529f8900e5f5131c7a5712f44991f7ad Mon Sep 17 00:00:00 2001 From: Thomas Vuillaume Date: Fri, 11 Jul 2025 14:30:56 +0200 Subject: [PATCH 2/4] rm importlib_resources --- ctaplot/io/dataset.py | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/ctaplot/io/dataset.py b/ctaplot/io/dataset.py index 1a88ce4..0511edf 100644 --- a/ctaplot/io/dataset.py +++ b/ctaplot/io/dataset.py @@ -1,11 +1,8 @@ import os import sys import numpy as np -try: - from importlib.resources import files as importlib_files, as_file -except ImportError: - # Fallback for Python < 3.9 - from importlib_resources import files as importlib_files, as_file +from importlib.resources import files as importlib_files, as_file + __all__ = ['get'] From 66f3e6d0f692a990a7bc78f4a7ecb3d3f2e448c6 Mon Sep 17 00:00:00 2001 From: Thomas Vuillaume Date: Fri, 11 Jul 2025 14:37:24 +0200 Subject: [PATCH 3/4] Drop support for Python < 3.9 - Update python_requires from >=3.6 to >=3.9 in setup.cfg - Remove Python 3.8 from CI test matrix in GitHub Actions - Update environment.yml to require Python >= 3.9 - Add specific Python version classifiers (3.9, 3.10, 3.11) to setup.py This aligns with the migration to importlib.resources which is available in Python 3.9+ and removes the need for importlib_resources fallback. --- .github/workflows/continuousintegration.yml | 2 +- environment.yml | 2 +- setup.cfg | 2 +- setup.py | 3 +++ 4 files changed, 6 insertions(+), 3 deletions(-) diff --git a/.github/workflows/continuousintegration.yml b/.github/workflows/continuousintegration.yml index 596e6eb..612af08 100644 --- a/.github/workflows/continuousintegration.yml +++ b/.github/workflows/continuousintegration.yml @@ -7,7 +7,7 @@ jobs: runs-on: ubuntu-latest strategy: matrix: - python-version: [3.8, 3.9, "3.10", "3.11"] + python-version: [3.9, "3.10", "3.11"] steps: - uses: actions/checkout@v2 diff --git a/environment.yml b/environment.yml index e728b31..b2652b9 100644 --- a/environment.yml +++ b/environment.yml @@ -9,7 +9,7 @@ dependencies: - matplotlib - nbsphinx - numpy - - python >= 3.6 + - python >= 3.9 - pandas - pyyaml - pytables diff --git a/setup.cfg b/setup.cfg index 655f2b1..deec55a 100644 --- a/setup.cfg +++ b/setup.cfg @@ -9,4 +9,4 @@ github_project = cta-observatory/cta-plot license = MIT [options] -python_requires = >=3.6 +python_requires = >=3.9 diff --git a/setup.py b/setup.py index e789330..0d919e6 100644 --- a/setup.py +++ b/setup.py @@ -45,6 +45,9 @@ def get_property(prop, project): classifiers=[ 'Intended Audience :: Science/Research', 'Programming Language :: Python :: 3', + 'Programming Language :: Python :: 3.9', + 'Programming Language :: Python :: 3.10', + 'Programming Language :: Python :: 3.11', 'Topic :: Scientific/Engineering :: Astronomy', ], data_files=[('ctaplot', dataset), ], From 27824577722cb4c54bd55a7dbd6371e55052ba2d Mon Sep 17 00:00:00 2001 From: Thomas Vuillaume Date: Fri, 11 Jul 2025 14:39:26 +0200 Subject: [PATCH 4/4] Fix ReadTheDocs configuration: add required sphinx.configuration key - Add explicit sphinx.configuration pointing to docs/conf.py - Resolves ReadTheDocs deprecation warning about missing Sphinx configuration --- .readthedocs.yml | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.readthedocs.yml b/.readthedocs.yml index d8d5c35..2668ce0 100644 --- a/.readthedocs.yml +++ b/.readthedocs.yml @@ -5,6 +5,9 @@ build: tools: python: "3.11" +sphinx: + configuration: docs/conf.py + python: install: - requirements: docs/requirements.txt