Coverage for /pythoncovmergedfiles/medio/medio/src/airflow/airflow/utils/entry_points.py: 35%
23 statements
« prev ^ index » next coverage.py v7.0.1, created at 2022-12-25 06:11 +0000
« prev ^ index » next coverage.py v7.0.1, created at 2022-12-25 06:11 +0000
1# Licensed to the Apache Software Foundation (ASF) under one
2# or more contributor license agreements. See the NOTICE file
3# distributed with this work for additional information
4# regarding copyright ownership. The ASF licenses this file
5# to you under the Apache License, Version 2.0 (the
6# "License"); you may not use this file except in compliance
7# with the License. You may obtain a copy of the License at
8#
9# http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing,
12# software distributed under the License is distributed on an
13# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14# KIND, either express or implied. See the License for the
15# specific language governing permissions and limitations
16# under the License.
17from __future__ import annotations
19import logging
20from typing import Iterator
22from packaging.utils import canonicalize_name
24try:
25 import importlib_metadata as metadata
26except ImportError:
27 from importlib import metadata # type: ignore[no-redef]
29log = logging.getLogger(__name__)
32def entry_points_with_dist(group: str) -> Iterator[tuple[metadata.EntryPoint, metadata.Distribution]]:
33 """Retrieve entry points of the given group.
35 This is like the ``entry_points()`` function from importlib.metadata,
36 except it also returns the distribution the entry_point was loaded from.
38 :param group: Filter results to only this entrypoint group
39 :return: Generator of (EntryPoint, Distribution) objects for the specified groups
40 """
41 loaded: set[str] = set()
42 for dist in metadata.distributions():
43 try:
44 key = canonicalize_name(dist.metadata["Name"])
45 if key in loaded:
46 continue
47 loaded.add(key)
48 for e in dist.entry_points:
49 if e.group != group:
50 continue
51 yield e, dist
52 except Exception as e:
53 log.warning("Error when retrieving package metadata (skipping it): %s, %s", dist, e)