reporter.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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 0 if the error came from stdin
  50. lineno = max(lineno, 1)
  51. if offset is not None:
  52. if sys.version_info < (3, 8) and text is not None:
  53. offset = offset - (len(text) - len(line)) + 1
  54. # some versions of python emit an offset of -1 for certain encoding errors
  55. offset = max(offset, 1)
  56. self._stderr.write('%s:%d:%d: %s\n' %
  57. (filename, lineno, offset, msg))
  58. else:
  59. self._stderr.write('%s:%d: %s\n' % (filename, lineno, msg))
  60. if line is not None:
  61. self._stderr.write(line)
  62. self._stderr.write('\n')
  63. if offset is not None:
  64. self._stderr.write(re.sub(r'\S', ' ', line[:offset - 1]) +
  65. "^\n")
  66. def flake(self, message):
  67. """
  68. pyflakes found something wrong with the code.
  69. @param: A L{pyflakes.messages.Message}.
  70. """
  71. self._stdout.write(str(message))
  72. self._stdout.write('\n')
  73. def _makeDefaultReporter():
  74. """
  75. Make a reporter that can be used when no reporter is specified.
  76. """
  77. return Reporter(sys.stdout, sys.stderr)