plugins_command.py 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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. from __future__ import annotations
  18. import inspect
  19. from typing import Any
  20. from airflow import plugins_manager
  21. from airflow.cli.simple_table import AirflowConsole
  22. from airflow.plugins_manager import PluginsDirectorySource, get_plugin_info
  23. from airflow.utils.cli import suppress_logs_and_warning
  24. from airflow.utils.providers_configuration_loader import providers_configuration_loaded
  25. def _get_name(class_like_object) -> str:
  26. if isinstance(class_like_object, (str, PluginsDirectorySource)):
  27. return str(class_like_object)
  28. if inspect.isclass(class_like_object):
  29. return class_like_object.__name__
  30. return class_like_object.__class__.__name__
  31. def _join_plugins_names(value: list[Any] | Any) -> str:
  32. value = value if isinstance(value, list) else [value]
  33. return ",".join(_get_name(v) for v in value)
  34. @suppress_logs_and_warning
  35. @providers_configuration_loaded
  36. def dump_plugins(args):
  37. """Dump plugins information."""
  38. plugins_info: list[dict[str, str]] = get_plugin_info()
  39. if not plugins_manager.plugins:
  40. print("No plugins loaded")
  41. return
  42. # Remove empty info
  43. if args.output == "table":
  44. # We can do plugins_info[0] as the element it will exist as there's
  45. # at least one plugin at this point
  46. for col in list(plugins_info[0]):
  47. if all(not bool(p[col]) for p in plugins_info):
  48. for plugin in plugins_info:
  49. del plugin[col]
  50. AirflowConsole().print_as(plugins_info, output=args.output)