html.py 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. # Copyright (c) 2003-2006 Sylvain Thenault (thenault@gmail.com).
  2. # Copyright (c) 2003-2011 LOGILAB S.A. (Paris, FRANCE).
  3. # This program is free software; you can redistribute it and/or modify it under
  4. # the terms of the GNU General Public License as published by the Free Software
  5. # Foundation; either version 2 of the License, or (at your option) any later
  6. # version.
  7. #
  8. # This program is distributed in the hope that it will be useful, but WITHOUT
  9. # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  10. # FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  11. #
  12. # You should have received a copy of the GNU General Public License along with
  13. # this program; if not, write to the Free Software Foundation, Inc.,
  14. # 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  15. """HTML reporter"""
  16. import sys
  17. from cgi import escape
  18. from logilab.common.ureports import HTMLWriter, Section, Table
  19. from pylint.interfaces import IReporter
  20. from pylint.reporters import BaseReporter
  21. class HTMLReporter(BaseReporter):
  22. """report messages and layouts in HTML"""
  23. __implements__ = IReporter
  24. extension = 'html'
  25. def __init__(self, output=sys.stdout):
  26. BaseReporter.__init__(self, output)
  27. self.msgs = []
  28. def add_message(self, msg_id, location, msg):
  29. """manage message of different type and in the context of path"""
  30. module, obj, line, col_offset = location[1:]
  31. if self.include_ids:
  32. sigle = msg_id
  33. else:
  34. sigle = msg_id[0]
  35. self.msgs += [sigle, module, obj, str(line), str(col_offset), escape(msg)]
  36. def set_output(self, output=None):
  37. """set output stream
  38. messages buffered for old output is processed first"""
  39. if self.out and self.msgs:
  40. self._display(Section())
  41. BaseReporter.set_output(self, output)
  42. def _display(self, layout):
  43. """launch layouts display
  44. overridden from BaseReporter to add insert the messages section
  45. (in add_message, message is not displayed, just collected so it
  46. can be displayed in an html table)
  47. """
  48. if self.msgs:
  49. # add stored messages to the layout
  50. msgs = ['type', 'module', 'object', 'line', 'col_offset', 'message']
  51. msgs += self.msgs
  52. sect = Section('Messages')
  53. layout.append(sect)
  54. sect.append(Table(cols=6, children=msgs, rheaders=1))
  55. self.msgs = []
  56. HTMLWriter().format(layout, self.out)