text.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. from prospector.formatters.base import Formatter
  2. __all__ = ("TextFormatter",)
  3. # pylint: disable=unnecessary-lambda
  4. class TextFormatter(Formatter):
  5. summary_labels = (
  6. ("started", "Started"),
  7. ("completed", "Finished"),
  8. ("time_taken", "Time Taken", lambda x: "%s seconds" % x),
  9. ("formatter", "Formatter"),
  10. ("profiles", "Profiles"),
  11. ("strictness", "Strictness"),
  12. ("libraries", "Libraries Used", lambda x: ", ".join(x)),
  13. ("tools", "Tools Run", lambda x: ", ".join(x)),
  14. ("adaptors", "Adaptors", lambda x: ", ".join(x)),
  15. ("message_count", "Messages Found"),
  16. ("external_config", "External Config"),
  17. )
  18. def render_summary(self):
  19. output = [
  20. "Check Information",
  21. "=================",
  22. ]
  23. label_width = max(len(label[1]) for label in self.summary_labels)
  24. for summary_label in self.summary_labels:
  25. key = summary_label[0]
  26. if key in self.summary:
  27. label = summary_label[1]
  28. if len(summary_label) > 2:
  29. value = summary_label[2](self.summary[key])
  30. else:
  31. value = self.summary[key]
  32. output.append(
  33. " %s: %s"
  34. % (
  35. label.rjust(label_width),
  36. value,
  37. )
  38. )
  39. return "\n".join(output)
  40. def render_message(self, message):
  41. output = []
  42. if message.location.module:
  43. output.append(f"{message.location.module} ({self._make_path(message.location.path)}):")
  44. else:
  45. output.append("%s:" % self._make_path(message.location.path))
  46. output.append(
  47. " L%s:%s %s: %s - %s"
  48. % (
  49. message.location.line or "-",
  50. message.location.character if message.location.character else "-",
  51. message.location.function,
  52. message.source,
  53. message.code,
  54. )
  55. )
  56. output.append(" %s" % message.message)
  57. return "\n".join(output)
  58. def render_messages(self):
  59. output = [
  60. "Messages",
  61. "========",
  62. "",
  63. ]
  64. for message in self.messages:
  65. output.append(self.render_message(message))
  66. output.append("")
  67. return "\n".join(output)
  68. def render_profile(self):
  69. output = ["Profile", "=======", "", self.profile.as_yaml().strip()]
  70. return "\n".join(output)
  71. def render(self, summary=True, messages=True, profile=False):
  72. output = []
  73. if messages and self.messages: # if there are no messages, don't render an empty header
  74. output.append(self.render_messages())
  75. if profile:
  76. output.append(self.render_profile())
  77. if summary:
  78. output.append(self.render_summary())
  79. return "\n\n\n".join(output) + "\n"