Plugins/CheckerPlugins/CodeStyleChecker/pep8.py

branch
BgService
changeset 3413
5e63f809732a
parent 3209
c5432abceb25
child 3456
96232974dcdb
equal deleted inserted replaced
3412:9364dab2d472 3413:5e63f809732a
74 from configparser import RawConfigParser 74 from configparser import RawConfigParser
75 from io import TextIOWrapper 75 from io import TextIOWrapper
76 except ImportError: 76 except ImportError:
77 from ConfigParser import RawConfigParser # __IGNORE_WARNING__ 77 from ConfigParser import RawConfigParser # __IGNORE_WARNING__
78 78
79 # Tell 'lupdate' which strings to keep for translation.
80 QT_TRANSLATE_NOOP = lambda mod, txt: txt
81
82 DEFAULT_EXCLUDE = '.svn,CVS,.bzr,.hg,.git,__pycache__' 79 DEFAULT_EXCLUDE = '.svn,CVS,.bzr,.hg,.git,__pycache__'
83 DEFAULT_IGNORE = 'E123,E226,E24' 80 DEFAULT_IGNORE = 'E123,E226,E24'
84 if sys.platform == 'win32': 81 if sys.platform == 'win32':
85 DEFAULT_CONFIG = os.path.expanduser(r'~\.pep8') 82 DEFAULT_CONFIG = os.path.expanduser(r'~\.pep8')
86 else: 83 else:
125 122
126 # Work around Python < 2.6 behaviour, which does not generate NL after 123 # Work around Python < 2.6 behaviour, which does not generate NL after
127 # a comment which is on a line by itself. 124 # a comment which is on a line by itself.
128 COMMENT_WITH_NL = tokenize.generate_tokens(['#\n'].pop).send(None)[1] == '#\n' 125 COMMENT_WITH_NL = tokenize.generate_tokens(['#\n'].pop).send(None)[1] == '#\n'
129 126
130
131 ##############################################################################
132 # Helper functions for translated and formatted messages
133 ##############################################################################
134
135
136 pep8_messages = {
137 "E101": QT_TRANSLATE_NOOP("pep8",
138 "indentation contains mixed spaces and tabs"),
139 "E111": QT_TRANSLATE_NOOP("pep8",
140 "indentation is not a multiple of four"),
141 "E112": QT_TRANSLATE_NOOP("pep8",
142 "expected an indented block"),
143 "E113": QT_TRANSLATE_NOOP("pep8",
144 "unexpected indentation"),
145 "E121": QT_TRANSLATE_NOOP("pep8",
146 "continuation line indentation is not a multiple of four"),
147 "E122": QT_TRANSLATE_NOOP("pep8",
148 "continuation line missing indentation or outdented"),
149 "E123": QT_TRANSLATE_NOOP("pep8",
150 "closing bracket does not match indentation of opening"
151 " bracket's line"),
152 "E124": QT_TRANSLATE_NOOP("pep8",
153 "closing bracket does not match visual indentation"),
154 "E125": QT_TRANSLATE_NOOP("pep8",
155 "continuation line does not distinguish itself from next"
156 " logical line"),
157 "E126": QT_TRANSLATE_NOOP("pep8",
158 "continuation line over-indented for hanging indent"),
159 "E127": QT_TRANSLATE_NOOP("pep8",
160 "continuation line over-indented for visual indent"),
161 "E128": QT_TRANSLATE_NOOP("pep8",
162 "continuation line under-indented for visual indent"),
163 "E133": QT_TRANSLATE_NOOP("pep8",
164 "closing bracket is missing indentation"),
165 "W191": QT_TRANSLATE_NOOP("pep8",
166 "indentation contains tabs"),
167 "E201": QT_TRANSLATE_NOOP("pep8",
168 "whitespace after '{0}'"),
169 "E202": QT_TRANSLATE_NOOP("pep8",
170 "whitespace before '{0}'"),
171 "E203": QT_TRANSLATE_NOOP("pep8",
172 "whitespace before '{0}'"),
173 "E211": QT_TRANSLATE_NOOP("pep8",
174 "whitespace before '{0}'"),
175 "E221": QT_TRANSLATE_NOOP("pep8",
176 "multiple spaces before operator"),
177 "E222": QT_TRANSLATE_NOOP("pep8",
178 "multiple spaces after operator"),
179 "E223": QT_TRANSLATE_NOOP("pep8",
180 "tab before operator"),
181 "E224": QT_TRANSLATE_NOOP("pep8",
182 "tab after operator"),
183 "E225": QT_TRANSLATE_NOOP("pep8",
184 "missing whitespace around operator"),
185 "E226": QT_TRANSLATE_NOOP("pep8",
186 "missing whitespace around arithmetic operator"),
187 "E227": QT_TRANSLATE_NOOP("pep8",
188 "missing whitespace around bitwise or shift operator"),
189 "E228": QT_TRANSLATE_NOOP("pep8",
190 "missing whitespace around modulo operator"),
191 "E231": QT_TRANSLATE_NOOP("pep8",
192 "missing whitespace after '{0}'"),
193 "E241": QT_TRANSLATE_NOOP("pep8",
194 "multiple spaces after '{0}'"),
195 "E242": QT_TRANSLATE_NOOP("pep8",
196 "tab after '{0}'"),
197 "E251": QT_TRANSLATE_NOOP("pep8",
198 "unexpected spaces around keyword / parameter equals"),
199 "E261": QT_TRANSLATE_NOOP("pep8",
200 "at least two spaces before inline comment"),
201 "E262": QT_TRANSLATE_NOOP("pep8",
202 "inline comment should start with '# '"),
203 "E271": QT_TRANSLATE_NOOP("pep8",
204 "multiple spaces after keyword"),
205 "E272": QT_TRANSLATE_NOOP("pep8",
206 "multiple spaces before keyword"),
207 "E273": QT_TRANSLATE_NOOP("pep8",
208 "tab after keyword"),
209 "E274": QT_TRANSLATE_NOOP("pep8",
210 "tab before keyword"),
211 "W291": QT_TRANSLATE_NOOP("pep8",
212 "trailing whitespace"),
213 "W292": QT_TRANSLATE_NOOP("pep8",
214 "no newline at end of file"),
215 "W293": QT_TRANSLATE_NOOP("pep8",
216 "blank line contains whitespace"),
217 "E301": QT_TRANSLATE_NOOP("pep8",
218 "expected 1 blank line, found 0"),
219 "E302": QT_TRANSLATE_NOOP("pep8",
220 "expected 2 blank lines, found {0}"),
221 "E303": QT_TRANSLATE_NOOP("pep8",
222 "too many blank lines ({0})"),
223 "E304": QT_TRANSLATE_NOOP("pep8",
224 "blank lines found after function decorator"),
225 "W391": QT_TRANSLATE_NOOP("pep8",
226 "blank line at end of file"),
227 "E401": QT_TRANSLATE_NOOP("pep8",
228 "multiple imports on one line"),
229 "E501": QT_TRANSLATE_NOOP("pep8",
230 "line too long ({0} > {1} characters)"),
231 "E502": QT_TRANSLATE_NOOP("pep8",
232 "the backslash is redundant between brackets"),
233 "W601": QT_TRANSLATE_NOOP("pep8",
234 ".has_key() is deprecated, use 'in'"),
235 "W602": QT_TRANSLATE_NOOP("pep8",
236 "deprecated form of raising exception"),
237 "W603": QT_TRANSLATE_NOOP("pep8",
238 "'<>' is deprecated, use '!='"),
239 "W604": QT_TRANSLATE_NOOP("pep8",
240 "backticks are deprecated, use 'repr()'"),
241 "E701": QT_TRANSLATE_NOOP("pep8",
242 "multiple statements on one line (colon)"),
243 "E702": QT_TRANSLATE_NOOP("pep8",
244 "multiple statements on one line (semicolon)"),
245 "E703": QT_TRANSLATE_NOOP("pep8",
246 "statement ends with a semicolon"),
247 "E711": QT_TRANSLATE_NOOP("pep8",
248 "comparison to {0} should be {1}"),
249 "E712": QT_TRANSLATE_NOOP("pep8",
250 "comparison to {0} should be {1}"),
251 "E721": QT_TRANSLATE_NOOP("pep8",
252 "do not compare types, use 'isinstance()'"),
253 "E901": QT_TRANSLATE_NOOP("pep8",
254 "{0}: {1}"),
255 }
256
257 pep8_messages_sample_args = {
258 "E201": ["([{"],
259 "E202": ["}])"],
260 "E203": [",;:"],
261 "E211": ["(["],
262 "E231": [",;:"],
263 "E241": [",;:"],
264 "E242": [",;:"],
265 "E302": [1],
266 "E303": [3],
267 "E501": [85, 79],
268 "E711": ["None", "'if cond is None:'"],
269 "E712": ["True", "'if cond is True:' or 'if cond:'"],
270 "E901": ["SyntaxError", "Invalid Syntax"],
271 }
272
273
274 def getMessage(code, *args):
275 """
276 Function to get a translated and formatted message for a given code.
277
278 @param code message code (string)
279 @param args arguments for a formatted message (list)
280 @return translated and formatted message (string)
281 """
282 if code in pep8_messages:
283 args = [str(arg) for arg in args]
284 return '@@'.join([code + ' ' + pep8_messages[code]] + args)
285 else:
286 return code + ' ' + QT_TRANSLATE_NOOP(
287 "pep8", "no message for this code defined")
288 127
289 ############################################################################## 128 ##############################################################################
290 # Plugins (check functions) for physical lines 129 # Plugins (check functions) for physical lines
291 ############################################################################## 130 ##############################################################################
292 131
1627 return 1466 return
1628 if code in self.counters: 1467 if code in self.counters:
1629 self.counters[code] += 1 1468 self.counters[code] += 1
1630 else: 1469 else:
1631 self.counters[code] = 1 1470 self.counters[code] = 1
1632 self.messages[code] = text[5:] 1471 self.messages[code] = []
1633 # Don't care about expected errors or warnings 1472 # Don't care about expected errors or warnings
1634 if code in self.expected: 1473 if code in self.expected:
1635 return 1474 return
1636 if self.print_filename and not self.file_errors: 1475 if self.print_filename and not self.file_errors:
1637 print(self.filename) 1476 print(self.filename)
1641 1480
1642 def error_args(self, line_number, offset, code, check, *args): 1481 def error_args(self, line_number, offset, code, check, *args):
1643 """Report an error, according to options.""" 1482 """Report an error, according to options."""
1644 if self._ignore_code(code): 1483 if self._ignore_code(code):
1645 return 1484 return
1646 text = getMessage(code, *args)
1647 if code in self.counters: 1485 if code in self.counters:
1648 self.counters[code] += 1 1486 self.counters[code] += 1
1649 else: 1487 else:
1650 self.counters[code] = 1 1488 self.counters[code] = 1
1651 self.messages[code] = text[5:] 1489 self.messages[code] = args
1652 # Don't care about expected errors or warnings 1490 # Don't care about expected errors or warnings
1653 if code in self.expected: 1491 if code in self.expected:
1654 return 1492 return
1655 if self.print_filename and not self.file_errors: 1493 if self.print_filename and not self.file_errors:
1656 print(self.filename) 1494 print(self.filename)
1728 def error_args(self, line_number, offset, code, check, *args): 1566 def error_args(self, line_number, offset, code, check, *args):
1729 """Report an error, according to options.""" 1567 """Report an error, according to options."""
1730 code = super(StandardReport, self).error_args(line_number, offset, 1568 code = super(StandardReport, self).error_args(line_number, offset,
1731 code, check, *args) 1569 code, check, *args)
1732 if code and (self.counters[code] == 1 or self._repeat): 1570 if code and (self.counters[code] == 1 or self._repeat):
1733 text = getMessage(code, *args)
1734 self._deferred_print.append( 1571 self._deferred_print.append(
1735 (line_number, offset, code, text[5:], check.__doc__)) 1572 (line_number, offset, code, args, check.__doc__))
1736 return code 1573 return code
1737 1574
1738 def get_file_results(self): 1575 def get_file_results(self):
1739 """Print the result and return the overall count for this file.""" 1576 """Print the result and return the overall count for this file."""
1740 self._deferred_print.sort() 1577 self._deferred_print.sort()

eric ide

mercurial