Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

migrate importlib to resolve warning about pkg_resources #3061

Merged
merged 7 commits into from
Aug 30, 2024
25 changes: 23 additions & 2 deletions src_py/pkgdata.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,28 @@ def getResource(identifier, pkgname=__name__):
import os

try:
from pkg_resources import resource_stream, resource_exists
if sys.version_info[:2] > (3, 8):
from importlib.resources import files

def resource_exists(_package_or_requirement, _resource_name):
_package_or_requirement = _package_or_requirement.split(".")[0]
return files(_package_or_requirement).joinpath(_resource_name).is_file()

def resource_stream(_package_or_requirement, _resource_name):
_package_or_requirement = _package_or_requirement.split(".")[0]
ref = files(_package_or_requirement).joinpath(_resource_name)
return ref.open('rb')
else:
from importlib import resources

def resource_exists(_package_or_requirement, _resource_name):
_package_or_requirement = _package_or_requirement.split(".")[0]
return resources.is_resource(_package_or_requirement, _resource_name) # pylint: disable=deprecated-method

def resource_stream(_package_or_requirement, _resource_name):
_package_or_requirement = _package_or_requirement.split(".")[0]
return resources.open_binary(_package_or_requirement, _resource_name) # pylint: disable=deprecated-method

except ImportError:

def resource_exists(_package_or_requirement, _resource_name):
Expand All @@ -33,7 +54,7 @@ def resource_exists(_package_or_requirement, _resource_name):
"""
return False

def resource_stream(_package_of_requirement, _resource_name):
def resource_stream(_package_or_requirement, _resource_name):
"""
A stub for when we fail to import this function.

Expand Down
Loading