Coverage for /pythoncovmergedfiles/medio/medio/src/airflow/build/lib/airflow/utils/entry_points.py: 52%

23 statements  

« prev     ^ index     » next       coverage.py v7.2.7, created at 2023-06-07 06:35 +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 

18 

19import collections 

20import functools 

21import logging 

22from typing import Iterator, Tuple 

23 

24try: 

25 import importlib_metadata as metadata 

26except ImportError: 

27 from importlib import metadata # type: ignore[no-redef] 

28 

29log = logging.getLogger(__name__) 

30 

31EPnD = Tuple[metadata.EntryPoint, metadata.Distribution] 

32 

33 

34@functools.lru_cache(maxsize=None) 

35def _get_grouped_entry_points() -> dict[str, list[EPnD]]: 

36 mapping: dict[str, list[EPnD]] = collections.defaultdict(list) 

37 for dist in metadata.distributions(): 

38 try: 

39 for e in dist.entry_points: 

40 mapping[e.group].append((e, dist)) 

41 except Exception as e: 

42 log.warning("Error when retrieving package metadata (skipping it): %s, %s", dist, e) 

43 return mapping 

44 

45 

46def entry_points_with_dist(group: str) -> Iterator[EPnD]: 

47 """Retrieve entry points of the given group. 

48 

49 This is like the ``entry_points()`` function from ``importlib.metadata``, 

50 except it also returns the distribution the entry point was loaded from. 

51 

52 Note that this may return multiple distributions to the same package if they 

53 are loaded from different ``sys.path`` entries. The caller site should 

54 implement appropriate deduplication logic if needed. 

55 

56 :param group: Filter results to only this entrypoint group 

57 :return: Generator of (EntryPoint, Distribution) objects for the specified groups 

58 """ 

59 return iter(_get_grouped_entry_points()[group])