From 2672e858e511c7d63d77ddbfce49becd5824aa07 Mon Sep 17 00:00:00 2001 From: "Jens W. Klein" Date: Tue, 27 Feb 2024 18:01:56 +0100 Subject: [PATCH] you can not have nice things with unstable APIs --- src/mxmake/topics.py | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/src/mxmake/topics.py b/src/mxmake/topics.py index 4997b54..94a91ea 100644 --- a/src/mxmake/topics.py +++ b/src/mxmake/topics.py @@ -10,6 +10,15 @@ import typing +try: + # do we have Python 3.12+ + from importlib.metadata import EntryPoints # type: ignore # noqa: F401 + + HAS_IMPORTLIB_ENTRYPOINTS = True +except ImportError: + HAS_IMPORTLIB_ENTRYPOINTS = False + + @dataclass class Setting: name: str @@ -165,9 +174,14 @@ def domain(self, name: str) -> typing.Optional[Domain]: @functools.lru_cache(maxsize=4096) def load_topics() -> typing.List[Topic]: - eps = entry_points() - ep_topics = [ep for ep in eps if ep.group == "mxmake.topics"] - return [ep.load() for ep in ep_topics] + if HAS_IMPORTLIB_ENTRYPOINTS: + topics_eps = entry_points(group="mxmake.topics") # type: ignore + else: + eps = entry_points() + if "mxmake.topics" not in eps: + return [] + topics_eps = eps["mxmake.topics"] + return [ep.load() for ep in topics_eps] def get_topic(name: str) -> Topic: