reporter.py 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. """Functions for constructing the requested report plugin."""
  2. from __future__ import annotations
  3. import argparse
  4. import logging
  5. from flake8.formatting.base import BaseFormatter
  6. from flake8.plugins.finder import LoadedPlugin
  7. LOG = logging.getLogger(__name__)
  8. def make(
  9. reporters: dict[str, LoadedPlugin],
  10. options: argparse.Namespace,
  11. ) -> BaseFormatter:
  12. """Make the formatter from the requested user options.
  13. - if :option:`flake8 --quiet` is specified, return the ``quiet-filename``
  14. formatter.
  15. - if :option:`flake8 --quiet` is specified at least twice, return the
  16. ``quiet-nothing`` formatter.
  17. - otherwise attempt to return the formatter by name.
  18. - failing that, assume it is a format string and return the ``default``
  19. formatter.
  20. """
  21. format_name = options.format
  22. if options.quiet == 1:
  23. format_name = "quiet-filename"
  24. elif options.quiet >= 2:
  25. format_name = "quiet-nothing"
  26. try:
  27. format_plugin = reporters[format_name]
  28. except KeyError:
  29. LOG.warning(
  30. "%r is an unknown formatter. Falling back to default.",
  31. format_name,
  32. )
  33. format_plugin = reporters["default"]
  34. return format_plugin.obj(options)