Skip to content

Commit

Permalink
Add fallback on pkg_resources for python<3.9
Browse files Browse the repository at this point in the history
  • Loading branch information
t20100 committed Jul 19, 2023
1 parent c46dbcf commit f3aab23
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 6 deletions.
2 changes: 2 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,8 @@ def get_project_configuration():
"h5py",
"fabio>=0.9",
]
if sys.version_info < (3, 9):
install_requires.append("setuptools") # For pkg_resources

# extras requirements: target 'full' to install all dependencies at once
full_requires = [
Expand Down
15 changes: 9 additions & 6 deletions src/silx/resources/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@
import sys
from typing import NamedTuple, Optional

if sys.version_info < (3, 9):
import pkg_resources


logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -152,9 +155,10 @@ def list_dir(resource: str) -> list[str]:
path = resource_filename(resource)
return os.listdir(path)

package_name = '.'.join([resource_directory.package_name] + resource_name.split('/'))
if sys.version_info < (3, 9):
return [entry.name for entry in importlib.resources.contents(package_name)]
return pkg_resources.resource_listdir(resource_directory.package_name, resource_name)

package_name = '.'.join([resource_directory.package_name] + resource_name.split('/'))
return [entry.name for entry in importlib.resources.files(package_name).iterdir()]


Expand Down Expand Up @@ -269,11 +273,10 @@ def _resource_filename(
return cached_path

if sys.version_info < (3, 9):
file_context = importlib.resources.path(package_name, resource_name)
else:
file_context = importlib.resources.as_file(
importlib.resources.files(package_name) / resource_name)
return pkg_resources.resource_filename(package_name, resource_name)

file_context = importlib.resources.as_file(
importlib.resources.files(package_name) / resource_name)
path = _file_manager.enter_context(file_context)
path_string = str(path.absolute())
_file_cache[cache_key] = path_string
Expand Down

0 comments on commit f3aab23

Please sign in to comment.