Skip to content

Commit

Permalink
Merge pull request #55 from josh-ashkinaze/dev/pkg_resources_change
Browse files Browse the repository at this point in the history
(docs) re-format get_resource_path docs for sphinx rst
  • Loading branch information
josh-ashkinaze authored Dec 22, 2024
2 parents 2c0255e + 2861317 commit 18ce6aa
Showing 1 changed file with 9 additions and 16 deletions.
25 changes: 9 additions & 16 deletions plurals/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -220,26 +220,19 @@ def format(self, avoid_double_period=True, **kwargs):


def get_resource_path(package: str, resource: str) -> str:
"""
Get the path to a resource file, supporting both pkg_resources and importlib.resources.
"""Get the path to a resource file, supporting both pkg_resources and importlib.resources.
Falls back to importlib.resources if pkg_resources is not available.
Problem this solves:
- In python 3.12, pkg_resources is being deprecated in favor of importlib.resources, and
it no longer comes with pkg_resources. This creates an import error unless user does `pip install setuptools` but it is
bad practice to add setuptools as a runtime dependency.
- If I just switch to importlib, this is limiting since importlib is only for 3.9+.
- So the solution is to use pkg_resources if available, and if not, use importlib.resources.
- In python 3.12, pkg_resources is being deprecated in favor of importlib.resources, and
it no longer comes with pkg_resources. This creates an import error unless user does
`pip install setuptools` but it is bad practice to add setuptools as a runtime dependency.
- If I just switch to importlib, this is limiting since importlib is only for 3.9+.
- So the solution is to use pkg_resources if available, and if not, use importlib.resources.
Args:
package: The package name (just __name__ usually)
resource: The resource path relative to the package
Returns:
str: The full path to the resource
package (str): The package name (just __name__ usually)
resource (str): The resource path relative to the package
"""
try:
import pkg_resources
Expand All @@ -248,4 +241,4 @@ def get_resource_path(package: str, resource: str) -> str:
from importlib import resources
root_package = package.split('.')[0]
with resources.path(root_package, resource) as path:
return str(path)
return str(path)

0 comments on commit 18ce6aa

Please sign in to comment.