From 539c9be3db45190dce6880d371bf5baa2ee33bee Mon Sep 17 00:00:00 2001 From: Timon Engelke Date: Mon, 4 Sep 2023 17:17:11 +0200 Subject: [PATCH] Use single call to importlib.metadata.entry_points for all groups --- catkin_tools/commands/catkin.py | 10 ++-------- catkin_tools/utils.py | 18 +++++++++++------- 2 files changed, 13 insertions(+), 15 deletions(-) diff --git a/catkin_tools/commands/catkin.py b/catkin_tools/commands/catkin.py index d7dc4df5..02676d98 100644 --- a/catkin_tools/commands/catkin.py +++ b/catkin_tools/commands/catkin.py @@ -13,7 +13,6 @@ # limitations under the License. import argparse -import functools import importlib.metadata import os import sys @@ -31,20 +30,15 @@ CATKIN_COMMAND_VERB_GROUP = 'catkin_tools.commands.catkin.verbs' -@functools.lru_cache(maxsize=None) -def _get_verb_entrypoints(): - return list(entry_points(group=CATKIN_COMMAND_VERB_GROUP)) - - def list_verbs(): verbs = [] - for entry_point in _get_verb_entrypoints(): + for entry_point in entry_points(group=CATKIN_COMMAND_VERB_GROUP): verbs.append(entry_point.name) return verbs def load_verb_description(verb_name): - for entry_point in _get_verb_entrypoints(): + for entry_point in entry_points(group=CATKIN_COMMAND_VERB_GROUP): if entry_point.name == verb_name: return entry_point.load() diff --git a/catkin_tools/utils.py b/catkin_tools/utils.py index e94c3956..b8b6ac13 100644 --- a/catkin_tools/utils.py +++ b/catkin_tools/utils.py @@ -12,17 +12,21 @@ # See the License for the specific language governing permissions and # limitations under the License. +import functools import os import sys -if sys.version_info >= (3, 10): - from importlib.metadata import entry_points -else: - import importlib.metadata - def entry_points(*, group): - for ep in importlib.metadata.entry_points().get(group, []): - yield ep +def entry_points(*, group): + @functools.lru_cache(maxsize=None) + def _entry_points(): + from importlib.metadata import entry_points + return entry_points() + + if sys.version_info >= (3, 10): + return _entry_points().select(group=group) + else: + return _entry_points().get(group, []) def which(program):