| 1 |
"""Module for dealing with entry points.""" |
| 2 |
from typing import Optional, Set |
| 3 |
|
| 4 |
# TODO importlib.metadata was introduced into the standard library in python 3.8 |
| 5 |
# so we can change this when we drop support for 3.7 |
| 6 |
# also, from importlib_metadata changed its API in v4.0, to use the python 3.10 API |
| 7 |
# however, because of https://github.com/python/importlib_metadata/issues/308 |
| 8 |
# we do not assume that we have this API, and instead use try/except for the new/old APIs |
| •9 |
from importlib.metadata import EntryPoint |
| 10 |
from importlib.metadata import entry_points as eps |
| 11 |
|
| 12 |
ENTRY_POINT_GROUP_READER = "jcache.readers" |
| 13 |
ENTRY_POINT_GROUP_EXEC = "jcache.executors" |
| 14 |
|
| 15 |
|
| 16 |
def list_group_names(group: str) -> Set[str]: |
| 17 |
"""Return the entry points within a group.""" |
| 18 |
all_eps = eps() |
| 19 |
try: |
| 20 |
# importlib_metadata v4 / python 3.10 |
| 21 |
return all_eps.select(group=group).names |
| 22 |
except (AttributeError, TypeError): |
| 23 |
return {ep.name for ep in all_eps.get(group, [])} |
| 24 |
|
| 25 |
|
| 26 |
def get_entry_point(group: str, name: str) -> Optional[EntryPoint]: |
| 27 |
"""Return the entry point with the given name in the given group.""" |
| 28 |
all_eps = eps() |
| 29 |
try: |
| 30 |
# importlib_metadata v4 / python 3.10 |
| 31 |
found = all_eps.select(group=group, name=name) |
| 32 |
ep = found[name] if name in found.names else None |
| 33 |
except (AttributeError, TypeError): |
| 34 |
found = {ep.name: ep for ep in all_eps.get(group, [])} |
| 35 |
ep = found[name] if name in found else None |
| 36 |
return ep |