config_command.py 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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. """Config sub-commands."""
  18. from __future__ import annotations
  19. from io import StringIO
  20. import pygments
  21. from pygments.lexers.configs import IniLexer
  22. from airflow.configuration import conf
  23. from airflow.exceptions import AirflowConfigException
  24. from airflow.utils.cli import should_use_colors
  25. from airflow.utils.code_utils import get_terminal_formatter
  26. from airflow.utils.providers_configuration_loader import providers_configuration_loaded
  27. @providers_configuration_loaded
  28. def show_config(args):
  29. """Show current application configuration."""
  30. with StringIO() as output:
  31. conf.write(
  32. output,
  33. section=args.section,
  34. include_examples=args.include_examples or args.defaults,
  35. include_descriptions=args.include_descriptions or args.defaults,
  36. include_sources=args.include_sources and not args.defaults,
  37. include_env_vars=args.include_env_vars or args.defaults,
  38. include_providers=not args.exclude_providers,
  39. comment_out_everything=args.comment_out_everything or args.defaults,
  40. only_defaults=args.defaults,
  41. )
  42. code = output.getvalue()
  43. if should_use_colors(args):
  44. code = pygments.highlight(code=code, formatter=get_terminal_formatter(), lexer=IniLexer())
  45. print(code)
  46. @providers_configuration_loaded
  47. def get_value(args):
  48. """Get one value from configuration."""
  49. # while this will make get_value quite a bit slower we must initialize configuration
  50. # for providers because we do not know what sections and options will be available after
  51. # providers are initialized. Theoretically Providers might add new sections and options
  52. # but also override defaults for existing options, so without loading all providers we
  53. # cannot be sure what is the final value of the option.
  54. try:
  55. value = conf.get(args.section, args.option)
  56. print(value)
  57. except AirflowConfigException:
  58. pass