Skip to content

Commit

Permalink
Use importlib_resources instead of pkg_resources for resource files (#…
Browse files Browse the repository at this point in the history
…791)

* DEV : Use importlib_resources instead of pkg_resources

this time, to replace the use of pkg_resources to access files from
packages

Note : I dont expect this to work on master because the
importlib_resources version available on edm is older than the latest
version.

	modified:   etstool.py
	modified:   pyface/__init__.py
	modified:   pyface/resource/resource_manager.py
	modified:   pyface/tests/test_image_resource.py

* CLN : Specify minimum package version

and minor cleanup based on review comments

	modified:   etstool.py
	modified:   pyface/__init__.py
	modified:   pyface/resource/resource_manager.py
	modified:   pyface/tests/test_image_resource.py

* CLN : Minor cleanup

remove an unused import and update an outdated comment

	modified:   pyface/resource/resource_manager.py
	modified:   pyface/tests/test_image_resource.py

* CLN : Remove unnecessary temporary variable

	modified:   pyface/tests/test_image_resource.py
  • Loading branch information
Poruri Sai Rahul authored Nov 17, 2020
1 parent 7e32dbf commit 8f6e9cd
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 11 deletions.
1 change: 1 addition & 0 deletions etstool.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@

dependencies = {
"importlib_metadata",
"importlib_resources>=1.1.0",
"traits" + TRAITS_VERSION_REQUIRES,
"traitsui",
"numpy",
Expand Down
2 changes: 1 addition & 1 deletion pyface/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
__version__ = "not-built"


__requires__ = ["importlib-metadata", "traits>=6"]
__requires__ = ["importlib-metadata", "importlib-resources>=1.1.0", "traits>=6"]
__extras_require__ = {
"wx": ["wxpython>=4", "numpy"],
"pyqt": ["pyqt>=4.10", "pygments"],
Expand Down
14 changes: 8 additions & 6 deletions pyface/resource/resource_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
from os.path import join
from zipfile import is_zipfile, ZipFile

from importlib_resources import files

from traits.api import HasTraits, Instance, List
from traits.util.resource import get_path

Expand Down Expand Up @@ -110,17 +112,17 @@ def _locate_image(self, image_name, resource_path, size):

for dirname in resource_path:

# If we come across a reference to a module, use pkg_resources
# to try and find the image inside of an .egg, .zip, etc.
# If we come across a reference to a module, try and find the
# image inside of an .egg, .zip, etc.
if isinstance(dirname, types.ModuleType):
from pkg_resources import resource_string

for path in subdirs:
for extension in extensions:
searchpath = "%s/%s%s" % (path, basename, extension)
try:
data = resource_string(
dirname.__name__, searchpath
data = (
files(dirname.__name__)
.joinpath(searchpath)
.read_bytes()
)
return ImageReference(
self.resource_factory, data=data
Expand Down
8 changes: 4 additions & 4 deletions pyface/tests/test_image_resource.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,10 @@

import os
import platform
import pkg_resources
import unittest

from importlib_resources import files

import pyface
import pyface.tests
from ..image_resource import ImageResource
Expand All @@ -28,9 +29,8 @@
)


SEARCH_PATH = pkg_resources.resource_filename("pyface", "images")
IMAGE_DIR = pkg_resources.resource_filename(__name__, "images")
IMAGE_PATH = os.path.join(IMAGE_DIR, "core.png")
SEARCH_PATH = os.fspath(files("pyface") / "images")
IMAGE_PATH = os.fspath(files("pyface.tests") / "images" / "core.png")


class TestImageResource(unittest.TestCase):
Expand Down

0 comments on commit 8f6e9cd

Please sign in to comment.