default.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. """Default formatting class for Flake8."""
  2. from __future__ import annotations
  3. from flake8.formatting import base
  4. from flake8.violation import Violation
  5. COLORS = {
  6. "bold": "\033[1m",
  7. "black": "\033[30m",
  8. "red": "\033[31m",
  9. "green": "\033[32m",
  10. "yellow": "\033[33m",
  11. "blue": "\033[34m",
  12. "magenta": "\033[35m",
  13. "cyan": "\033[36m",
  14. "white": "\033[37m",
  15. "reset": "\033[m",
  16. }
  17. COLORS_OFF = {k: "" for k in COLORS}
  18. class SimpleFormatter(base.BaseFormatter):
  19. """Simple abstraction for Default and Pylint formatter commonality.
  20. Sub-classes of this need to define an ``error_format`` attribute in order
  21. to succeed. The ``format`` method relies on that attribute and expects the
  22. ``error_format`` string to use the old-style formatting strings with named
  23. parameters:
  24. * code
  25. * text
  26. * path
  27. * row
  28. * col
  29. """
  30. error_format: str
  31. def format(self, error: Violation) -> str | None:
  32. """Format and write error out.
  33. If an output filename is specified, write formatted errors to that
  34. file. Otherwise, print the formatted error to standard out.
  35. """
  36. return self.error_format % {
  37. "code": error.code,
  38. "text": error.text,
  39. "path": error.filename,
  40. "row": error.line_number,
  41. "col": error.column_number,
  42. **(COLORS if self.color else COLORS_OFF),
  43. }
  44. class Default(SimpleFormatter):
  45. """Default formatter for Flake8.
  46. This also handles backwards compatibility for people specifying a custom
  47. format string.
  48. """
  49. error_format = (
  50. "%(bold)s%(path)s%(reset)s"
  51. "%(cyan)s:%(reset)s%(row)d%(cyan)s:%(reset)s%(col)d%(cyan)s:%(reset)s "
  52. "%(bold)s%(red)s%(code)s%(reset)s %(text)s"
  53. )
  54. def after_init(self) -> None:
  55. """Check for a custom format string."""
  56. if self.options.format.lower() != "default":
  57. self.error_format = self.options.format
  58. class Pylint(SimpleFormatter):
  59. """Pylint formatter for Flake8."""
  60. error_format = "%(path)s:%(row)d: [%(code)s] %(text)s"
  61. class FilenameOnly(SimpleFormatter):
  62. """Only print filenames, e.g., flake8 -q."""
  63. error_format = "%(path)s"
  64. def after_init(self) -> None:
  65. """Initialize our set of filenames."""
  66. self.filenames_already_printed: set[str] = set()
  67. def show_source(self, error: Violation) -> str | None:
  68. """Do not include the source code."""
  69. def format(self, error: Violation) -> str | None:
  70. """Ensure we only print each error once."""
  71. if error.filename not in self.filenames_already_printed:
  72. self.filenames_already_printed.add(error.filename)
  73. return super().format(error)
  74. else:
  75. return None
  76. class Nothing(base.BaseFormatter):
  77. """Print absolutely nothing."""
  78. def format(self, error: Violation) -> str | None:
  79. """Do nothing."""
  80. def show_source(self, error: Violation) -> str | None:
  81. """Do not print the source."""