22 from io import StringIO # __IGNORE_WARNING__ |
20 from io import StringIO # __IGNORE_WARNING__ |
23 import tokenize |
21 import tokenize |
24 import ast |
22 import ast |
25 import sys |
23 import sys |
26 |
24 |
27 from PyQt4.QtCore import QT_TRANSLATE_NOOP, QCoreApplication |
25 # Tell 'lupdate' which strings to keep for translation. |
28 |
26 QT_TRANSLATE_NOOP = lambda mod, txt: txt |
29 PyCF_ONLY_AST = 1024 |
|
30 |
27 |
31 |
28 |
32 class DocStyleContext(object): |
29 class DocStyleContext(object): |
33 """ |
30 """ |
34 Class implementing the source context. |
31 Class implementing the source context. |
395 # Don't care about expected codes |
392 # Don't care about expected codes |
396 if code in self.__expected: |
393 if code in self.__expected: |
397 return |
394 return |
398 |
395 |
399 if code and (self.counters[code] == 1 or self.__repeat): |
396 if code and (self.counters[code] == 1 or self.__repeat): |
400 if code in DocStyleChecker.Codes: |
397 text = self.getMessage(code, *args) |
401 text = self.getMessage(code, *args) |
|
402 else: |
|
403 text = code + " " + QCoreApplication.translate( |
|
404 "DocStyleChecker", "no message for this code defined") |
|
405 # record the issue with one based line number |
398 # record the issue with one based line number |
406 self.errors.append((self.__filename, lineNumber + 1, offset, text)) |
399 self.errors.append((self.__filename, lineNumber + 1, offset, text)) |
407 |
400 |
408 def __reportInvalidSyntax(self): |
401 def __reportInvalidSyntax(self): |
409 """ |
402 """ |
427 |
420 |
428 @param code message code (string) |
421 @param code message code (string) |
429 @param args arguments for a formatted message (list) |
422 @param args arguments for a formatted message (list) |
430 @return translated and formatted message (string) |
423 @return translated and formatted message (string) |
431 """ |
424 """ |
432 if code in DocStyleChecker.Messages: |
425 if code in cls.Messages: |
433 return code + " " + QCoreApplication.translate( |
426 return '@@'.join( |
434 "DocStyleChecker", |
427 [code + ' ' + cls.Messages[code]] + list(args)) |
435 DocStyleChecker.Messages[code]).format(*args) |
|
436 else: |
428 else: |
437 return code + " " + QCoreApplication.translate( |
429 return code + ' ' + QT_TRANSLATE_NOOP( |
438 "DocStyleChecker", "no message for this code defined") |
430 "DocStyleChecker", "no message for this code defined") |
439 |
431 |
440 def __resetReadline(self): |
432 def __resetReadline(self): |
441 """ |
433 """ |
442 Private method to reset the internal readline function. |
434 Private method to reset the internal readline function. |
466 if not self.__checkers: |
458 if not self.__checkers: |
467 # don't do anything, if no codes were selected |
459 # don't do anything, if no codes were selected |
468 return |
460 return |
469 |
461 |
470 try: |
462 try: |
471 compile(''.join(self.__source), '', 'exec', PyCF_ONLY_AST) |
463 compile(''.join(self.__source), '', 'exec', ast.PyCF_ONLY_AST) |
472 except (SyntaxError, TypeError): |
464 except (SyntaxError, TypeError): |
473 self.__reportInvalidSyntax() |
465 self.__reportInvalidSyntax() |
474 return |
466 return |
475 |
467 |
476 for keyword in self.__keywords: |
468 for keyword in self.__keywords: |