reporter.py 2.9 KB

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