File: entry_points.py

package info (click to toggle)
jupyter-cache 1.0.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 840 kB
  • sloc: python: 2,601; makefile: 40; sh: 9
file content (36 lines) | stat: -rw-r--r-- 1,466 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
"""Module for dealing with entry points."""
from typing import Optional, Set

# TODO importlib.metadata was introduced into the standard library in python 3.8
# so we can change this when we drop support for 3.7
# also, from importlib_metadata changed its API in v4.0, to use the python 3.10 API
# however, because of https://github.com/python/importlib_metadata/issues/308
# we do not assume that we have this API, and instead use try/except for the new/old APIs
from importlib.metadata import EntryPoint
from importlib.metadata import entry_points as eps

ENTRY_POINT_GROUP_READER = "jcache.readers"
ENTRY_POINT_GROUP_EXEC = "jcache.executors"


def list_group_names(group: str) -> Set[str]:
    """Return the entry points within a group."""
    all_eps = eps()
    try:
        # importlib_metadata v4 / python 3.10
        return all_eps.select(group=group).names
    except (AttributeError, TypeError):
        return {ep.name for ep in all_eps.get(group, [])}


def get_entry_point(group: str, name: str) -> Optional[EntryPoint]:
    """Return the entry point with the given name in the given group."""
    all_eps = eps()
    try:
        # importlib_metadata v4 / python 3.10
        found = all_eps.select(group=group, name=name)
        ep = found[name] if name in found.names else None
    except (AttributeError, TypeError):
        found = {ep.name: ep for ep in all_eps.get(group, [])}
        ep = found[name] if name in found else None
    return ep