53
53
# 3rd party
54
54
from typing_extensions import Literal
55
55
56
+ from domdf_python_tools .compat import importlib_metadata
57
+
56
58
__all__ = ["discover" ]
57
59
58
60
@@ -80,10 +82,11 @@ def discover(
80
82
exclude_side_effects : bool = True ,
81
83
) -> List [Any ]:
82
84
"""
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``.
84
87
85
88
: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.
87
90
:default match_func: :py:obj:`None`, which includes all objects.
88
91
:param exclude_side_effects: Don't include objects that are only there because of an import side effect.
89
92
@@ -94,7 +97,7 @@ def discover(
94
97
Added the ``exclude_side_effects`` parameter.
95
98
"""
96
99
97
- matched_classes = list ()
100
+ matching_objects = []
98
101
99
102
for _ , module_name , _ in pkgutil .walk_packages (
100
103
# https://github.com/python/mypy/issues/1422
@@ -114,9 +117,9 @@ def discover(
114
117
if imported_objects .__module__ != module .__name__ :
115
118
continue
116
119
117
- matched_classes .append (imported_objects )
120
+ matching_objects .append (imported_objects )
118
121
119
- return matched_classes
122
+ return matching_objects
120
123
121
124
122
125
#
@@ -132,3 +135,33 @@ def discover(
132
135
# spec.loader.exec_module(mod)
133
136
# sys.modules[mod.__name__] = mod
134
137
# 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