Skip to content

Commit 7bf932d

Browse files
committed
Add "discover_entry_points_by_name" to "import_tools"
1 parent 3891aeb commit 7bf932d

File tree

4 files changed

+87
-12
lines changed

4 files changed

+87
-12
lines changed

domdf_python_tools/import_tools.py

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -48,15 +48,15 @@
4848
import inspect
4949
import pkgutil
5050
from types import ModuleType
51-
from typing import Any, Callable, List, Optional, Type, overload
51+
from typing import Any, Callable, Dict, List, Optional, Type, overload
5252

5353
# 3rd party
5454
from typing_extensions import Literal
5555

5656
# this package
5757
from domdf_python_tools.compat import importlib_metadata
5858

59-
__all__ = ["discover", "discover_entry_points"]
59+
__all__ = ["discover", "discover_entry_points", "discover_entry_points_by_name"]
6060

6161

6262
@overload
@@ -158,14 +158,41 @@ def discover_entry_points(
158158
:return: List of matching objects.
159159
"""
160160

161-
matching_objects = []
161+
return list(discover_entry_points_by_name(group_name, object_match_func=match_func).values())
162+
163+
164+
def discover_entry_points_by_name(
165+
group_name: str,
166+
name_match_func: Optional[Callable[[Any], bool]] = None,
167+
object_match_func: Optional[Callable[[Any], bool]] = None,
168+
) -> Dict[str, Any]:
169+
"""
170+
Returns a mapping of entry point names to the entry points in the given category,
171+
optionally filtered by ``match_func``.
172+
173+
.. versionadded:: 2.5.0
174+
175+
:param group_name: The entry point group name, e.g. ``'entry_points'``.
176+
:param name_match_func: Function taking the entry point name and returning :py:obj:`True`
177+
if the entry point is to be included in the output.
178+
:default name_match_func: :py:obj:`None`, which includes all entry points.
179+
:param object_match_func: Function taking an object and returning :py:obj:`True`
180+
if the object is to be included in the output.
181+
:default object_match_func: :py:obj:`None`, which includes all objects.
182+
""" # noqa: D400
183+
184+
matching_objects = {}
162185

163186
for entry_point in importlib_metadata.entry_points().get(group_name, ()):
164-
entry_point = entry_point.load()
165187

166-
if match_func is not None and not match_func(entry_point):
188+
if name_match_func is not None and not name_match_func(entry_point.name):
189+
continue
190+
191+
entry_point_obj = entry_point.load()
192+
193+
if object_match_func is not None and not object_match_func(entry_point_obj):
167194
continue
168195

169-
matching_objects.append(entry_point)
196+
matching_objects[entry_point.name] = entry_point_obj
170197

171198
return matching_objects

tests/test_import_tools.py

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
from pytest_regressions.data_regression import DataRegressionFixture
99

1010
# this package
11-
from domdf_python_tools.import_tools import discover, discover_entry_points
11+
from domdf_python_tools.import_tools import discover, discover_entry_points, discover_entry_points_by_name
1212

1313
sys.path.append('.')
1414
sys.path.append("tests")
@@ -98,9 +98,19 @@ def test_discover_errors(obj, expects):
9898

9999

100100
def test_discover_entry_points(data_regression: DataRegressionFixture):
101+
entry_points = discover_entry_points("flake8.extension", lambda f: f.__name__.startswith("break"))
102+
data_regression.check([f.__name__ for f in entry_points])
101103

102-
entry_points = [
103-
f.__name__
104-
for f in discover_entry_points("flake8.extension", lambda f: f.__name__.startswith("break"))
105-
]
106-
data_regression.check(entry_points)
104+
105+
def test_discover_entry_points_by_name_object_match_func(data_regression: DataRegressionFixture):
106+
entry_points = discover_entry_points_by_name(
107+
"flake8.extension", object_match_func=lambda f: f.__name__.startswith("break")
108+
)
109+
data_regression.check({k: v.__name__ for k, v in entry_points.items()})
110+
111+
112+
def test_discover_entry_points_by_name_name_match_func(data_regression: DataRegressionFixture):
113+
entry_points = discover_entry_points_by_name(
114+
"flake8.extension", name_match_func=lambda n: n.startswith("pycodestyle.")
115+
)
116+
data_regression.check({k: v.__name__ for k, v in entry_points.items()})
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
pycodestyle.ambiguous_identifier: ambiguous_identifier
2+
pycodestyle.bare_except: bare_except
3+
pycodestyle.blank_lines: blank_lines
4+
pycodestyle.break_after_binary_operator: break_after_binary_operator
5+
pycodestyle.break_before_binary_operator: break_before_binary_operator
6+
pycodestyle.comparison_negative: comparison_negative
7+
pycodestyle.comparison_to_singleton: comparison_to_singleton
8+
pycodestyle.comparison_type: comparison_type
9+
pycodestyle.compound_statements: compound_statements
10+
pycodestyle.continued_indentation: continued_indentation
11+
pycodestyle.explicit_line_join: explicit_line_join
12+
pycodestyle.extraneous_whitespace: extraneous_whitespace
13+
pycodestyle.imports_on_separate_lines: imports_on_separate_lines
14+
pycodestyle.indentation: indentation
15+
pycodestyle.maximum_doc_length: maximum_doc_length
16+
pycodestyle.maximum_line_length: maximum_line_length
17+
pycodestyle.missing_whitespace: missing_whitespace
18+
pycodestyle.missing_whitespace_after_import_keyword: missing_whitespace_after_import_keyword
19+
pycodestyle.missing_whitespace_around_operator: missing_whitespace_around_operator
20+
pycodestyle.module_imports_on_top_of_file: module_imports_on_top_of_file
21+
pycodestyle.python_3000_async_await_keywords: python_3000_async_await_keywords
22+
pycodestyle.python_3000_backticks: python_3000_backticks
23+
pycodestyle.python_3000_has_key: python_3000_has_key
24+
pycodestyle.python_3000_invalid_escape_sequence: python_3000_invalid_escape_sequence
25+
pycodestyle.python_3000_not_equal: python_3000_not_equal
26+
pycodestyle.python_3000_raise_comma: python_3000_raise_comma
27+
pycodestyle.tabs_obsolete: tabs_obsolete
28+
pycodestyle.tabs_or_spaces: tabs_or_spaces
29+
pycodestyle.trailing_blank_lines: trailing_blank_lines
30+
pycodestyle.trailing_whitespace: trailing_whitespace
31+
pycodestyle.whitespace_around_comma: whitespace_around_comma
32+
pycodestyle.whitespace_around_keywords: whitespace_around_keywords
33+
pycodestyle.whitespace_around_named_parameter_equals: whitespace_around_named_parameter_equals
34+
pycodestyle.whitespace_around_operator: whitespace_around_operator
35+
pycodestyle.whitespace_before_comment: whitespace_before_comment
36+
pycodestyle.whitespace_before_parameters: whitespace_before_parameters
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
pycodestyle.break_after_binary_operator: break_after_binary_operator
2+
pycodestyle.break_before_binary_operator: break_before_binary_operator

0 commit comments

Comments
 (0)