Skip to content

Commit

Permalink
fix: Use importlib-resources over deprecated pkg_resources (#139)
Browse files Browse the repository at this point in the history
* Add importlib-resources as an install dependency for Python 3.8 as Python 3.12 deprecates
  and removes pkg_resources.
* Replace use of pkg_resources.resource_filename with importlib.resources.files
  for Python 3.9+ and importlib_resources.files for Python 3.8.
* Apply isort.
  • Loading branch information
matthewfeickert authored Jun 14, 2024
1 parent 0eedf18 commit 5db010b
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 23 deletions.
1 change: 1 addition & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ install_requires =
jsonschema>=3.0.0
pyyaml>=5.1 # for parsing CLI options
yadage-schemas>=0.10.7 # c.f. https://github.com/yadage/yadage-schemas/issues/35
importlib-resources>=5.10;python_version<'3.9' # for accessing package filepaths
python_requires = >=3.8
include_package_data = True
package_dir =
Expand Down
19 changes: 11 additions & 8 deletions src/recastatlas/config.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
import os
import yaml
import pkg_resources
import glob
import logging
import os

try:
from importlib.resources import files
except ImportError:
# Support Python 3.8 as importlib.resources added in Python 3.9
# https://docs.python.org/3/library/importlib.resources.html#importlib.resources.files
from importlib_resources import files

import jsonschema
import yaml

log = logging.getLogger(__name__)

Expand Down Expand Up @@ -73,11 +80,7 @@ def backends(self):
}

def catalogue_paths(self, include_default=True):
paths = (
[pkg_resources.resource_filename("recastatlas", "data/catalogue")]
if include_default
else []
)
paths = [files("recastatlas") / "data/catalogue"] if include_default else []
configpath = os.environ.get("RECAST_ATLAS_CATALOGUE")

if configpath:
Expand Down
18 changes: 13 additions & 5 deletions src/recastatlas/subcommands/auth.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
import click
import sys
import os
import re
import shutil
import pkg_resources
import sys

try:
from importlib.resources import files
except ImportError:
# Support Python 3.8 as importlib.resources added in Python 3.9
# https://docs.python.org/3/library/importlib.resources.html#importlib.resources.files
from importlib_resources import files

import click

from ..backends import run_sync_packtivity
from ..config import config

Expand Down Expand Up @@ -132,11 +140,11 @@ def write(basedir):
)

shutil.copy(
pkg_resources.resource_filename("recastatlas", "data/getkrb_reana.sh"),
files("recastatlas") / "data/getkrb_reana.sh",
os.path.join(basedir, "getkrb_reana.sh"),
)
shutil.copy(
pkg_resources.resource_filename("recastatlas", "data/expect_script.sh"),
files("recastatlas") / "data/expect_script.sh",
os.path.join(basedir, "expect_script.sh"),
)
click.echo(
Expand Down
24 changes: 14 additions & 10 deletions src/recastatlas/subcommands/catalogue.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,22 @@
import click
import yaml
import getpass
import logging
import os
from distutils.dir_util import copy_tree
import string
import logging
from distutils.dir_util import copy_tree

import pkg_resources
import getpass
try:
from importlib.resources import files
except ImportError:
# Support Python 3.8 as importlib.resources added in Python 3.9
# https://docs.python.org/3/library/importlib.resources.html#importlib.resources.files
from importlib_resources import files

import click
import yaml

from ..config import config
from ..testing import validate_entry


log = logging.getLogger(__name__)
default_meta = {"author": "unknown", "short_description": "no description", "tags": []}

Expand All @@ -37,9 +42,8 @@ def check(name):
@click.argument("name")
@click.argument("path")
def create(name, path):
template_path = pkg_resources.resource_filename(
"recastatlas", "data/templates/helloworld"
)

template_path = files("recastatlas") / "data/templates/helloworld"
copy_tree(template_path, path)
recast_file = os.path.join(path, "recast.yml")
data = string.Template(open(recast_file).read()).safe_substitute(
Expand Down

0 comments on commit 5db010b

Please sign in to comment.