Plugins/CheckerPlugins/CodeStyleChecker/DocStyleChecker.py

branch
Py2 comp.
changeset 3142
55030c09e142
parent 3060
5883ce99ee12
parent 3083
6382a74d9599
child 3145
a9de05d4a22f
equal deleted inserted replaced
3141:72f3bde98c58 3142:55030c09e142
23 import tokenize 23 import tokenize
24 import ast 24 import ast
25 import sys 25 import sys
26 26
27 from PyQt4.QtCore import QT_TRANSLATE_NOOP, QCoreApplication 27 from PyQt4.QtCore import QT_TRANSLATE_NOOP, QCoreApplication
28
29 PyCF_ONLY_AST = 1024
28 30
29 31
30 class DocStyleContext(object): 32 class DocStyleContext(object):
31 """ 33 """
32 Class implementing the source context. 34 Class implementing the source context.
116 "D203", "D205", 118 "D203", "D205",
117 "D221", "D222", 119 "D221", "D222",
118 "D231", "D234", "D235", "D236", "D237", "D238", "D239", 120 "D231", "D234", "D235", "D236", "D237", "D238", "D239",
119 "D242", "D243", "D244", "D245", "D246", "D247", 121 "D242", "D243", "D244", "D245", "D246", "D247",
120 "D250", "D251", 122 "D250", "D251",
123
124 "D901",
121 ] 125 ]
122 126
123 Messages = { 127 Messages = {
124 "D101": QT_TRANSLATE_NOOP( 128 "D101": QT_TRANSLATE_NOOP(
125 "DocStyleChecker", "module is missing a docstring"), 129 "DocStyleChecker", "module is missing a docstring"),
228 " raises an exception"), 232 " raises an exception"),
229 "D251": QT_TRANSLATE_NOOP( 233 "D251": QT_TRANSLATE_NOOP(
230 "DocStyleChecker", 234 "DocStyleChecker",
231 "docstring contains a @exception line but function/method doesn't" 235 "docstring contains a @exception line but function/method doesn't"
232 " raise an exception"), 236 " raise an exception"),
237
238 "D901": QT_TRANSLATE_NOOP(
239 "DocStyleChecker", "{0}: {1}"),
240 }
241
242 MessagesSampleArgs = {
243 "D901": ["SyntaxError", "Invalid Syntax"],
233 } 244 }
234 245
235 def __init__(self, source, filename, select, ignore, expected, repeat, 246 def __init__(self, source, filename, select, ignore, expected, repeat,
236 maxLineLength=79, docType="pep257"): 247 maxLineLength=79, docType="pep257"):
237 """ 248 """
392 text = code + " " + QCoreApplication.translate( 403 text = code + " " + QCoreApplication.translate(
393 "DocStyleChecker", "no message for this code defined") 404 "DocStyleChecker", "no message for this code defined")
394 # record the issue with one based line number 405 # record the issue with one based line number
395 self.errors.append((self.__filename, lineNumber + 1, offset, text)) 406 self.errors.append((self.__filename, lineNumber + 1, offset, text))
396 407
408 def __reportInvalidSyntax(self):
409 """
410 Private method to report a syntax error.
411 """
412 exc_type, exc = sys.exc_info()[:2]
413 if len(exc.args) > 1:
414 offset = exc.args[1]
415 if len(offset) > 2:
416 offset = offset[1:3]
417 else:
418 offset = (1, 0)
419 self.__error(offset[0] - 1, offset[1] or 0,
420 'D901', exc_type.__name__, exc.args[0])
421
397 @classmethod 422 @classmethod
398 def getMessage(cls, code, *args): 423 def getMessage(cls, code, *args):
399 """ 424 """
400 Class method to get a translated and formatted message for a 425 Class method to get a translated and formatted message for a
401 given code. 426 given code.
438 # don't do anything, if essential data is missing 463 # don't do anything, if essential data is missing
439 return 464 return
440 465
441 if not self.__checkers: 466 if not self.__checkers:
442 # don't do anything, if no codes were selected 467 # don't do anything, if no codes were selected
468 return
469
470 try:
471 compile(''.join(self.__source), '', 'exec', PyCF_ONLY_AST)
472 except (SyntaxError, TypeError):
473 self.__reportInvalidSyntax()
443 return 474 return
444 475
445 for keyword in self.__keywords: 476 for keyword in self.__keywords:
446 if keyword in self.__checkers: 477 if keyword in self.__checkers:
447 for check in self.__checkers[keyword]: 478 for check in self.__checkers[keyword]:

eric ide

mercurial