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

Shortcuts on this page

r m x   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

24 statements  

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""" 

28 

29from __future__ import annotations 

30 

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

32 

33if TYPE_CHECKING: 

34 from yaml.error import MarkedYAMLError, YAMLError # noqa: F401 

35 

36 

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

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

39 # delay import until use. 

40 from yaml import load as orig 

41 

42 try: 

43 from yaml import CSafeLoader as SafeLoader 

44 except ImportError: 

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

46 

47 return orig(stream, SafeLoader) 

48 

49 

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

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

52 # delay import until use. 

53 from yaml import dump as orig 

54 

55 try: 

56 from yaml import CSafeDumper as SafeDumper 

57 except ImportError: 

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

59 

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

61 

62 

63def __getattr__(name): 

64 # Delegate anything else to the yaml module 

65 import yaml 

66 

67 if name == "FullLoader": 

68 # Try to use CFullLoader by default 

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

70 

71 return getattr(yaml, name)