json.py 930 B

12345678910111213141516171819202122232425262728293031
  1. import json
  2. from datetime import datetime
  3. from prospector.formatters.base import Formatter
  4. __all__ = ("JsonFormatter",)
  5. class JsonFormatter(Formatter):
  6. def render(self, summary=True, messages=True, profile=False):
  7. output = {}
  8. if summary:
  9. # we need to slightly change the types and format
  10. # of a few of the items in the summary to make
  11. # them play nice with JSON formatting
  12. munged = {}
  13. for key, value in self.summary.items():
  14. if isinstance(value, datetime):
  15. munged[key] = str(value)
  16. else:
  17. munged[key] = value
  18. output["summary"] = munged
  19. if profile:
  20. output["profile"] = self.profile.as_dict()
  21. if messages:
  22. output["messages"] = [self._message_to_dict(m) for m in self.messages]
  23. return json.dumps(output, indent=2)