reporter.py 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. """Functions for construcing the requested report plugin."""
  2. import argparse
  3. import logging
  4. from typing import Dict
  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)