helpers.py 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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. from __future__ import annotations
  19. from typing import Any
  20. from airflow.configuration import conf
  21. from airflow.settings import json
  22. from airflow.utils.log.secrets_masker import redact
  23. def serialize_template_field(template_field: Any, name: str) -> str | dict | list | int | float:
  24. """
  25. Return a serializable representation of the templated field.
  26. If ``templated_field`` contains a class or instance that requires recursive
  27. templating, store them as strings. Otherwise simply return the field as-is.
  28. """
  29. def is_jsonable(x):
  30. try:
  31. json.dumps(x)
  32. except (TypeError, OverflowError):
  33. return False
  34. else:
  35. return True
  36. max_length = conf.getint("core", "max_templated_field_length")
  37. if not is_jsonable(template_field):
  38. serialized = str(template_field)
  39. if len(serialized) > max_length:
  40. rendered = redact(serialized, name)
  41. return (
  42. "Truncated. You can change this behaviour in [core]max_templated_field_length. "
  43. f"{rendered[:max_length - 79]!r}... "
  44. )
  45. return str(template_field)
  46. else:
  47. if not template_field:
  48. return template_field
  49. serialized = str(template_field)
  50. if len(serialized) > max_length:
  51. rendered = redact(serialized, name)
  52. return (
  53. "Truncated. You can change this behaviour in [core]max_templated_field_length. "
  54. f"{rendered[:max_length - 79]!r}... "
  55. )
  56. return template_field