Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/airflow/serialization/helpers.py: 26%

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

27 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"""Serialized DAG and BaseOperator.""" 

18 

19from __future__ import annotations 

20 

21from typing import Any 

22 

23from airflow.configuration import conf 

24from airflow.settings import json 

25from airflow.utils.log.secrets_masker import redact 

26 

27 

28def serialize_template_field(template_field: Any, name: str) -> str | dict | list | int | float: 

29 """Return a serializable representation of the templated field. 

30 

31 If ``templated_field`` contains a class or instance that requires recursive 

32 templating, store them as strings. Otherwise simply return the field as-is. 

33 """ 

34 

35 def is_jsonable(x): 

36 try: 

37 json.dumps(x) 

38 except (TypeError, OverflowError): 

39 return False 

40 else: 

41 return True 

42 

43 max_length = conf.getint("core", "max_templated_field_length") 

44 

45 if not is_jsonable(template_field): 

46 serialized = str(template_field) 

47 if len(serialized) > max_length: 

48 rendered = redact(serialized, name) 

49 return ( 

50 "Truncated. You can change this behaviour in [core]max_templated_field_length. " 

51 f"{rendered[:max_length - 79]!r}... " 

52 ) 

53 return str(template_field) 

54 else: 

55 if not template_field: 

56 return template_field 

57 serialized = str(template_field) 

58 if len(serialized) > max_length: 

59 rendered = redact(serialized, name) 

60 return ( 

61 "Truncated. You can change this behaviour in [core]max_templated_field_length. " 

62 f"{rendered[:max_length - 79]!r}... " 

63 ) 

64 return template_field