5353# 3rd party
5454from typing_extensions import Literal
5555
56+ from domdf_python_tools .compat import importlib_metadata
57+
5658__all__ = ["discover" ]
5759
5860
@@ -80,10 +82,11 @@ def discover(
8082 exclude_side_effects : bool = True ,
8183 ) -> List [Any ]:
8284 """
83- Returns a list of objects in the directory matched by ``match_func``.
85+ Returns a list of objects in the given module,
86+ optionally filtered by ``match_func``.
8487
8588 :param package: A Python package
86- :param match_func: Function taking an object and returning true if the object is to be included in the output.
89+ :param match_func: Function taking an object and returning :py:obj:`True` if the object is to be included in the output.
8790 :default match_func: :py:obj:`None`, which includes all objects.
8891 :param exclude_side_effects: Don't include objects that are only there because of an import side effect.
8992
@@ -94,7 +97,7 @@ def discover(
9497 Added the ``exclude_side_effects`` parameter.
9598 """
9699
97- matched_classes = list ()
100+ matching_objects = []
98101
99102 for _ , module_name , _ in pkgutil .walk_packages (
100103 # https://github.com/python/mypy/issues/1422
@@ -114,9 +117,9 @@ def discover(
114117 if imported_objects .__module__ != module .__name__ :
115118 continue
116119
117- matched_classes .append (imported_objects )
120+ matching_objects .append (imported_objects )
118121
119- return matched_classes
122+ return matching_objects
120123
121124
122125#
@@ -132,3 +135,33 @@ def discover(
132135# spec.loader.exec_module(mod)
133136# sys.modules[mod.__name__] = mod
134137# return mod
138+
139+
140+ def discover_entry_points (
141+ group_name : str ,
142+ match_func : Optional [Callable [[Any ], bool ]] = None ,
143+ ) -> List [Any ]:
144+ """
145+ Returns a list of entry points in the given category,
146+ optionally filtered by ``match_func``.
147+
148+ :param group_name: The entry point group name, e.g. ``'entry_points'``.
149+ :param match_func: Function taking an object and returning :py:obj:`True` if the object is to be included in the output.
150+ :default match_func: :py:obj:`None`, which includes all objects.
151+
152+ :return: List of matching objects.
153+
154+ .. versionadded:: 1.1.0
155+ """
156+
157+ matching_objects = []
158+
159+ for entry_point in importlib_metadata .entry_points ().get (group_name , ()):
160+ entry_point = entry_point .load ()
161+
162+ if match_func is not None and not match_func (entry_point ):
163+ continue
164+
165+ matching_objects .append (entry_point )
166+
167+ return matching_objects
0 commit comments