]>
Commit | Line | Data |
---|---|---|
53e6db90 DC |
1 | """ |
2 | Provide the Reporter class. | |
3 | """ | |
4 | ||
5 | import re | |
6 | import sys | |
7 | ||
8 | ||
9 | class Reporter: | |
10 | """ | |
11 | Formats the results of pyflakes checks to users. | |
12 | """ | |
13 | ||
14 | def __init__(self, warningStream, errorStream): | |
15 | """ | |
16 | Construct a L{Reporter}. | |
17 | ||
18 | @param warningStream: A file-like object where warnings will be | |
19 | written to. The stream's C{write} method must accept unicode. | |
20 | C{sys.stdout} is a good value. | |
21 | @param errorStream: A file-like object where error output will be | |
22 | written to. The stream's C{write} method must accept unicode. | |
23 | C{sys.stderr} is a good value. | |
24 | """ | |
25 | self._stdout = warningStream | |
26 | self._stderr = errorStream | |
27 | ||
28 | def unexpectedError(self, filename, msg): | |
29 | """ | |
30 | An unexpected error occurred trying to process C{filename}. | |
31 | ||
32 | @param filename: The path to a file that we could not process. | |
33 | @ptype filename: C{unicode} | |
34 | @param msg: A message explaining the problem. | |
35 | @ptype msg: C{unicode} | |
36 | """ | |
37 | self._stderr.write(f"{filename}: {msg}\n") | |
38 | ||
39 | def syntaxError(self, filename, msg, lineno, offset, text): | |
40 | """ | |
41 | There was a syntax error in C{filename}. | |
42 | ||
43 | @param filename: The path to the file with the syntax error. | |
44 | @ptype filename: C{unicode} | |
45 | @param msg: An explanation of the syntax error. | |
46 | @ptype msg: C{unicode} | |
47 | @param lineno: The line number where the syntax error occurred. | |
48 | @ptype lineno: C{int} | |
49 | @param offset: The column on which the syntax error occurred, or None. | |
50 | @ptype offset: C{int} | |
51 | @param text: The source code containing the syntax error. | |
52 | @ptype text: C{unicode} | |
53 | """ | |
54 | if text is None: | |
55 | line = None | |
56 | else: | |
57 | line = text.splitlines()[-1] | |
58 | ||
59 | # lineno might be None if the error was during tokenization | |
60 | # lineno might be 0 if the error came from stdin | |
61 | lineno = max(lineno or 0, 1) | |
62 | ||
63 | if offset is not None: | |
64 | # some versions of python emit an offset of -1 for certain encoding errors | |
65 | offset = max(offset, 1) | |
66 | self._stderr.write('%s:%d:%d: %s\n' % | |
67 | (filename, lineno, offset, msg)) | |
68 | else: | |
69 | self._stderr.write('%s:%d: %s\n' % (filename, lineno, msg)) | |
70 | ||
71 | if line is not None: | |
72 | self._stderr.write(line) | |
73 | self._stderr.write('\n') | |
74 | if offset is not None: | |
75 | self._stderr.write(re.sub(r'\S', ' ', line[:offset - 1]) + | |
76 | "^\n") | |
77 | ||
78 | def flake(self, message): | |
79 | """ | |
80 | pyflakes found something wrong with the code. | |
81 | ||
82 | @param: A L{pyflakes.messages.Message}. | |
83 | """ | |
84 | self._stdout.write(str(message)) | |
85 | self._stdout.write('\n') | |
86 | ||
87 | ||
88 | def _makeDefaultReporter(): | |
89 | """ | |
90 | Make a reporter that can be used when no reporter is specified. | |
91 | """ | |
92 | return Reporter(sys.stdout, sys.stderr) |