gvsig-scripting / org.gvsig.scripting / trunk / org.gvsig.scripting / org.gvsig.scripting.app / org.gvsig.scripting.app.mainplugin / src / main / resources-plugin / scripting / lib / pylint / reporters / html.py @ 745
History | View | Annotate | Download (3.73 KB)
1 |
# Copyright (c) 2003-2013 LOGILAB S.A. (Paris, FRANCE).
|
---|---|
2 |
# This program is free software; you can redistribute it and/or modify it under
|
3 |
# the terms of the GNU General Public License as published by the Free Software
|
4 |
# Foundation; either version 2 of the License, or (at your option) any later
|
5 |
# version.
|
6 |
#
|
7 |
# This program is distributed in the hope that it will be useful, but WITHOUT
|
8 |
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
9 |
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
|
10 |
#
|
11 |
# You should have received a copy of the GNU General Public License along with
|
12 |
# this program; if not, write to the Free Software Foundation, Inc.,
|
13 |
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
14 |
"""HTML reporter"""
|
15 |
|
16 |
import itertools |
17 |
import string |
18 |
import sys |
19 |
|
20 |
import six |
21 |
|
22 |
from pylint.interfaces import IReporter |
23 |
from pylint.reporters import BaseReporter |
24 |
from pylint.reporters.ureports.html_writer import HTMLWriter |
25 |
from pylint.reporters.ureports.nodes import Section, Table |
26 |
|
27 |
|
28 |
class HTMLReporter(BaseReporter): |
29 |
"""report messages and layouts in HTML"""
|
30 |
|
31 |
__implements__ = IReporter |
32 |
name = 'html'
|
33 |
extension = 'html'
|
34 |
|
35 |
def __init__(self, output=sys.stdout): |
36 |
BaseReporter.__init__(self, output)
|
37 |
self.msgs = []
|
38 |
# Add placeholders for title and parsed messages
|
39 |
self.header = None |
40 |
self.msgargs = []
|
41 |
|
42 |
@staticmethod
|
43 |
def _parse_msg_template(msg_template): |
44 |
formatter = string.Formatter() |
45 |
parsed = formatter.parse(msg_template) |
46 |
for item in parsed: |
47 |
if item[1]: |
48 |
yield item[1] |
49 |
|
50 |
def _parse_template(self): |
51 |
"""Helper function to parse the message template"""
|
52 |
self.header = []
|
53 |
if self.linter.config.msg_template: |
54 |
msg_template = self.linter.config.msg_template
|
55 |
else:
|
56 |
msg_template = '{category}{module}{obj}{line}{column}{msg}'
|
57 |
|
58 |
_header, _msgs = itertools.tee(self._parse_msg_template(msg_template))
|
59 |
self.header = list(_header) |
60 |
self.msgargs = list(_msgs) |
61 |
|
62 |
def handle_message(self, msg): |
63 |
"""manage message of different type and in the context of path"""
|
64 |
|
65 |
# It would be better to do this in init, but currently we do not
|
66 |
# have access to the linter (as it is setup in lint.set_reporter()
|
67 |
# Therefore we try to parse just the once.
|
68 |
if self.header is None: |
69 |
self._parse_template()
|
70 |
|
71 |
# We want to add the lines given by the template
|
72 |
values = [getattr(msg, field) for field in self.msgargs] |
73 |
self.msgs += [value if isinstance(value, six.text_type) else str(value) |
74 |
for value in values] |
75 |
|
76 |
def set_output(self, output=None): |
77 |
"""set output stream
|
78 |
|
79 |
messages buffered for old output is processed first"""
|
80 |
if self.out and self.msgs: |
81 |
self._display(Section())
|
82 |
BaseReporter.set_output(self, output)
|
83 |
|
84 |
def _display(self, layout): |
85 |
"""launch layouts display
|
86 |
|
87 |
overridden from BaseReporter to add insert the messages section
|
88 |
(in add_message, message is not displayed, just collected so it
|
89 |
can be displayed in an html table)
|
90 |
"""
|
91 |
HTMLWriter().format(layout, self.out)
|
92 |
|
93 |
def display_messages(self, layout): |
94 |
if self.msgs: |
95 |
# add stored messages to the layout
|
96 |
msgs = self.header
|
97 |
cols = len(self.header) |
98 |
msgs += self.msgs
|
99 |
sect = Section('Messages')
|
100 |
layout.append(sect) |
101 |
sect.append(Table(cols=cols, children=msgs, rheaders=1))
|
102 |
self.msgs = []
|
103 |
self._display(layout)
|
104 |
|
105 |
|
106 |
def register(linter): |
107 |
"""Register the reporter classes with the linter."""
|
108 |
linter.register_reporter(HTMLReporter) |