Skip to content

Commit

Permalink
Allow running without entry_points metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
mhsmith committed May 2, 2024
1 parent 8eace47 commit e725bad
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 12 deletions.
1 change: 1 addition & 0 deletions changes/2546.misc.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Allow running without entry_points metadata
25 changes: 13 additions & 12 deletions core/src/toga/platform.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,14 @@ def get_current_platform():
current_platform = get_current_platform()


def find_backends():
# As of Setuptools 65.5, entry points are returned duplicated if the
# package is installed editable. Use a set to ensure that each entry point
# is only returned once.
# See https://github.com/pypa/setuptools/issues/3649
return sorted(set(entry_points(group="toga.backends")))


@lru_cache(maxsize=1)
def get_platform_factory():
"""Determine the current host platform and import the platform factory.
Expand All @@ -51,17 +59,13 @@ def get_platform_factory():
:returns: The factory for the host platform.
"""
toga_backends = entry_points(group="toga.backends")
if not toga_backends:
raise RuntimeError("No Toga backend could be loaded.")

backend_value = os.environ.get("TOGA_BACKEND")
if backend_value:
try:
factory = importlib.import_module(f"{backend_value}.factory")
except ModuleNotFoundError as e:
toga_backends_values = ", ".join(
[f"{backend.value!r}" for backend in toga_backends]
[f"{backend.value!r}" for backend in find_backends()]
)
# Android doesn't report Python exception chains in crashes
# (https://github.com/chaquo/chaquopy/issues/890), so include the original
Expand All @@ -72,13 +76,10 @@ def get_platform_factory():
f"not be loaded ({e}). It should be one of: {toga_backends_values}."
)
else:
# As of Setuptools 65.5, entry points are returned duplicated if the
# package is installed editable. Use a set to ensure that each entry point
# is only returned once.
# See https://github.com/pypa/setuptools/issues/3649
toga_backends = sorted(set(toga_backends))

if len(toga_backends) == 1:
toga_backends = find_backends()
if len(toga_backends) == 0:
raise RuntimeError("No Toga backend could be loaded.")
elif len(toga_backends) == 1:
backend = toga_backends[0]
else:
# multiple backends are installed: choose the one that matches the host platform
Expand Down

0 comments on commit e725bad

Please sign in to comment.