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

Dont rely on the existence of "select" on entry points #1000

Merged
merged 2 commits into from
Jul 18, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pyface/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@


__requires__ = [
'importlib-metadata>=3.6.0; python_version<"3.10"',
'importlib-metadata>=3.6.0; python_version<"3.8"',
'importlib-resources>=1.1.0; python_version<"3.9"',
"traits>=6.2"
]
Expand Down
31 changes: 25 additions & 6 deletions pyface/base_toolkit.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,8 @@

try:
# Starting Python 3.8, importlib.metadata is available in the Python
# standard library.
# standard library and starting Python 3.10, the "select" interface is
# available on EntryPoints.
import importlib.metadata as importlib_metadata
except ImportError:
import importlib_metadata
Expand Down Expand Up @@ -187,7 +188,15 @@ def import_toolkit(toolkit_name, entry_point="pyface.toolkits"):
If no toolkit is found, or if the toolkit cannot be loaded for some
reason.
"""
entry_point_group = importlib_metadata.entry_points().select(group=entry_point)

# This compatibility layer can be removed when we drop support for
# Python < 3.10. Ref https://github.com/enthought/pyface/issues/999.
all_entry_points = importlib_metadata.entry_points()
if hasattr(all_entry_points, "select"):
entry_point_group = all_entry_points.select(group=entry_point)
else:
entry_point_group = all_entry_points[entry_point]

plugins = [
plugin for plugin in entry_point_group if plugin.name == toolkit_name
]
Expand Down Expand Up @@ -255,10 +264,20 @@ def find_toolkit(entry_point, toolkits=None, priorities=default_priorities):
if ETSConfig.toolkit:
return import_toolkit(ETSConfig.toolkit, entry_point)

entry_points = [
plugin for plugin in importlib_metadata.entry_points().select(group=entry_point)
if toolkits is None or plugin.name in toolkits
]
# This compatibility layer can be removed when we drop support for
# Python < 3.10. Ref https://github.com/enthought/pyface/issues/999.
all_entry_points = importlib_metadata.entry_points()
if hasattr(all_entry_points, "select"):
entry_points = [
plugin for plugin in all_entry_points.select(group=entry_point)
if toolkits is None or plugin.name in toolkits
]
else:
entry_points = [
plugin for plugin in all_entry_points[entry_point]
if toolkits is None or plugin.name in toolkits
]

for plugin in sorted(entry_points, key=priorities):
try:
with ETSConfig.provisional_toolkit(plugin.name):
Expand Down
17 changes: 15 additions & 2 deletions pyface/tests/test_toolkit.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@

try:
# Starting Python 3.8, importlib.metadata is available in the Python
# standard library.
# standard library and starting Python 3.10, the "select" interface is
# available on EntryPoints.
from importlib.metadata import entry_points
except ImportError:
from importlib_metadata import entry_points
Expand All @@ -33,7 +34,19 @@ def test_bad_import(self):

def test_core_plugins(self):
# test that we can see appropriate core entrypoints
plugins = {ep.name for ep in entry_points().select(group='pyface.toolkits')}

# This compatibility layer can be removed when we drop support for
# Python < 3.10. Ref https://github.com/enthought/pyface/issues/999.
all_entry_points = entry_points()
if hasattr(all_entry_points, "select"):
plugins = {
ep.name
for ep in entry_points().select(group='pyface.toolkits')
}
else:
plugins = {
ep.name for ep in entry_points()['pyface.toolkits']
}
self.assertLessEqual({"qt4", "wx", "qt", "null"}, plugins)

def test_toolkit_object(self):
Expand Down