Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/airflow/utils/yaml.py: 29%

24 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. 

17"""Use libyaml for YAML dump/load operations where possible. 

18 

19If libyaml is available we will use it -- it is significantly faster. 

20 

21This module delegates all other properties to the yaml module, so it can be used as: 

22 

23.. code-block:: python 

24 import airflow.utils.yaml as yaml 

25 

26And then be used directly in place of the normal python module. 

27""" 

28from __future__ import annotations 

29 

30from typing import TYPE_CHECKING, Any, BinaryIO, TextIO, cast 

31 

32if TYPE_CHECKING: 

33 from yaml.error import MarkedYAMLError, YAMLError # noqa 

34 

35 

36def safe_load(stream: bytes | str | BinaryIO | TextIO) -> Any: 

37 """Like yaml.safe_load, but use the C libyaml for speed where we can.""" 

38 # delay import until use. 

39 from yaml import load as orig 

40 

41 try: 

42 from yaml import CSafeLoader as SafeLoader 

43 except ImportError: 

44 from yaml import SafeLoader # type: ignore[assignment, no-redef] 

45 

46 return orig(stream, SafeLoader) 

47 

48 

49def dump(data: Any, **kwargs) -> str: 

50 """Like yaml.safe_dump, but use the C libyaml for speed where we can.""" 

51 # delay import until use. 

52 from yaml import dump as orig 

53 

54 try: 

55 from yaml import CSafeDumper as SafeDumper 

56 except ImportError: 

57 from yaml import SafeDumper # type: ignore[assignment, no-redef] 

58 

59 return cast(str, orig(data, Dumper=SafeDumper, **kwargs)) 

60 

61 

62def __getattr__(name): 

63 # Delegate anything else to the yaml module 

64 import yaml 

65 

66 if name == "FullLoader": 

67 # Try to use CFullLoader by default 

68 getattr(yaml, "CFullLoader", yaml.FullLoader) 

69 

70 return getattr(yaml, name)