Plugins/CheckerPlugins/CodeStyleChecker/DocStyleChecker.py

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

eric ide

mercurial