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