Skip to content

Commit 4394a83

Browse files
committed
Add importlib_metadata to compat.py and discover_entry_points to import_tools.py
1 parent 57a87c6 commit 4394a83

File tree

4 files changed

+56
-6
lines changed

4 files changed

+56
-6
lines changed

.isort.cfg

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ known_third_party =
2020
enum_tools
2121
faker
2222
github
23+
importlib_metadata
2324
importlib_resources
2425
iniconfig
2526
pandas

domdf_python_tools/compat.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,13 @@
1212
1313
`importlib_resources <https://importlib-resources.readthedocs.io/en/latest/>`_ on Python 3.6;
1414
:mod:`importlib.resources` on Python 3.7 and later.
15+
16+
.. py:data:: importlib_metadata
17+
18+
`importlib_metadata <https://importlib-metadata.readthedocs.io/en/latest/>`_ on Python 3.7 and earlier;
19+
:mod:`importlib.metadata` on Python 3.8 and later.
20+
21+
.. versionadded:: 1.1.0
1522
"""
1623
#
1724
# Copyright © 2020 Dominic Davis-Foster <[email protected]>
@@ -35,11 +42,19 @@
3542
# stdlib
3643
import sys
3744

38-
__all__ = ["importlib_resources"]
45+
__all__ = ["importlib_resources", "importlib_metadata"]
3946

4047
if sys.version_info < (3, 7): # pragma: no cover (>=py37)
4148
# 3rd party
4249
import importlib_resources
4350
else: # pragma: no cover (<py37)
4451
# stdlib
4552
import importlib.resources as importlib_resources
53+
54+
55+
if sys.version_info[:2] < (3, 8): # pragma: no cover (>=py38)
56+
# 3rd party
57+
import importlib_metadata # type: ignore
58+
else: # pragma: no cover (<py38)
59+
# stdlib
60+
import importlib.metadata as importlib_metadata

domdf_python_tools/import_tools.py

Lines changed: 38 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@
5353
# 3rd party
5454
from 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

requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
colorama>=0.4.3
22
deprecation>=2.1.0
33
importlib_resources>=3.0.0; python_version < "3.7"
4+
importlib_metadata>=1.5.0; python_version < "3.8"
45
pydash>=4.7.4
56
typing_extensions>=3.7.4.3

0 commit comments

Comments
 (0)