1# Copyright The OpenTelemetry Authors
2# SPDX-License-Identifier: Apache-2.0
3
4"""
5Caching and compatibility wrapper for standard library ``importlib.metadata``.
6
7This module caches ``entry_points()`` to avoid reloading entry points from disk on every call.
8It also normalizes minor differences across python versions 3.10+. References to issues:
9
10- https://github.com/open-telemetry/opentelemetry-python/pull/4735
11- https://github.com/open-telemetry/opentelemetry-python/pull/5203
12"""
13
14from functools import cache
15from importlib.metadata import (
16 Distribution,
17 EntryPoint,
18 EntryPoints,
19 PackageNotFoundError,
20 distributions,
21 requires,
22 version,
23)
24from importlib.metadata import entry_points as original_entry_points
25from typing import Any
26
27
28def _as_entry_points(eps: Any) -> EntryPoints:
29 if isinstance(eps, EntryPoints):
30 return eps
31 # Handle Python 3.10 SelectableGroups (dict-like)
32 return EntryPoints(ep for group_eps in eps.values() for ep in group_eps)
33
34
35@cache
36def _original_entry_points_cached() -> EntryPoints:
37 return _as_entry_points(original_entry_points())
38
39
40def entry_points(**params) -> EntryPoints:
41 return _original_entry_points_cached().select(**params)
42
43
44__all__ = [
45 "entry_points",
46 "version",
47 "EntryPoint",
48 "EntryPoints",
49 "requires",
50 "Distribution",
51 "distributions",
52 "PackageNotFoundError",
53]