File: _compat.py

package info (click to toggle)
python-babel 2.17.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,888 kB
  • sloc: python: 14,521; makefile: 177; javascript: 77; sh: 8
file content (34 lines) | stat: -rw-r--r-- 1,163 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
import sys
from functools import partial


def find_entrypoints(group_name: str):
    """
    Find entrypoints of a given group using either `importlib.metadata` or the
    older `pkg_resources` mechanism.

    Yields tuples of the entrypoint name and a callable function that will
    load the actual entrypoint.
    """
    if sys.version_info >= (3, 10):
        # "Changed in version 3.10: importlib.metadata is no longer provisional."
        try:
            from importlib.metadata import entry_points
        except ImportError:
            pass
        else:
            eps = entry_points(group=group_name)
            # Only do this if this implementation of `importlib.metadata` is
            # modern enough to not return a dict.
            if not isinstance(eps, dict):
                for entry_point in eps:
                    yield (entry_point.name, entry_point.load)
                return

    try:
        from pkg_resources import working_set
    except ImportError:
        pass
    else:
        for entry_point in working_set.iter_entry_points(group_name):
            yield (entry_point.name, partial(entry_point.load, require=True))