lib.py 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. # See utils/checkpackagelib/readme.txt before editing this file.
  2. from checkpackagelib.base import _CheckFunction
  3. class ConsecutiveEmptyLines(_CheckFunction):
  4. def before(self):
  5. self.lastline = "non empty"
  6. def check_line(self, lineno, text):
  7. if text.strip() == "" == self.lastline.strip():
  8. return ["{}:{}: consecutive empty lines"
  9. .format(self.filename, lineno)]
  10. self.lastline = text
  11. class EmptyLastLine(_CheckFunction):
  12. def before(self):
  13. self.lastlineno = 0
  14. self.lastline = "non empty"
  15. def check_line(self, lineno, text):
  16. self.lastlineno = lineno
  17. self.lastline = text
  18. def after(self):
  19. if self.lastline.strip() == "":
  20. return ["{}:{}: empty line at end of file"
  21. .format(self.filename, self.lastlineno)]
  22. class NewlineAtEof(_CheckFunction):
  23. def before(self):
  24. self.lastlineno = 0
  25. self.lastline = "\n"
  26. def check_line(self, lineno, text):
  27. self.lastlineno = lineno
  28. self.lastline = text
  29. def after(self):
  30. if self.lastline == self.lastline.rstrip("\r\n"):
  31. return ["{}:{}: missing newline at end of file"
  32. .format(self.filename, self.lastlineno),
  33. self.lastline]
  34. class TrailingSpace(_CheckFunction):
  35. def check_line(self, lineno, text):
  36. line = text.rstrip("\r\n")
  37. if line != line.rstrip():
  38. return ["{}:{}: line contains trailing whitespace"
  39. .format(self.filename, lineno),
  40. text]
  41. class Utf8Characters(_CheckFunction):
  42. def is_ascii(self, s):
  43. try:
  44. return all(ord(c) < 128 for c in s)
  45. except TypeError:
  46. return False
  47. def check_line(self, lineno, text):
  48. if not self.is_ascii(text):
  49. return ["{}:{}: line contains UTF-8 characters"
  50. .format(self.filename, lineno),
  51. text]