From 5d3217a42473cf2f49f3546afea64f271ce79b20 Mon Sep 17 00:00:00 2001 From: Dan Goldsmith Date: Wed, 29 Jan 2014 14:13:07 +0000 Subject: [PATCH] Basic HTML parsing of message format implemented --- reporters/html.py | 54 ++++++++++++++++++++++++++++++++++++----------- 1 file changed, 42 insertions(+), 12 deletions(-) diff --git a/reporters/html.py b/reporters/html.py index 18ecf51d4..a84316466 100644 --- a/reporters/html.py +++ b/reporters/html.py @@ -16,11 +16,20 @@ import sys from cgi import escape +import string #To parse formatter + from logilab.common.ureports import HTMLWriter, Section, Table from pylint.interfaces import IReporter from pylint.reporters import BaseReporter, Message +#self.linter.config.msg_template or self.line_format +#line_format = '{C}:{line:3d},{column:2d}: {msg} ({symbol})' +#Default +# self.msgs += (msg.category, msg.module, msg.obj, +# str(msg.line), str(msg.column), escape(msg.msg)) +#Header +# msgs = ['type', 'module', 'object', 'line', 'col_offset', 'message'] class HTMLReporter(BaseReporter): """report messages and layouts in HTML""" @@ -32,18 +41,42 @@ class HTMLReporter(BaseReporter): def __init__(self, output=sys.stdout): BaseReporter.__init__(self, output) self.msgs = [] + #Add placeholders for title and parsed messages + self.header = None + self.msgargs = [] + + def _parse_template(self): + """Helper function to parse the message template""" + self.header = [] + if self.linter.config.msg_template: + msg_template = self.linter.config.msg_template + else: + msg_template = '{category}{module}{obj}{line}{column}{msg}' + + #Parse the message template + str_formatter = string.Formatter() + parsed_str = str_formatter.parse(msg_template) + + for item in parsed_str: + if item[1]: + self.header.append(item[1]) + self.msgargs.append(item[1]) def add_message(self, msg_id, location, msg): """manage message of different type and in the context of path""" msg = Message(self, msg_id, location, msg) #self.msgs += (msg.category, msg.module, msg.obj, # str(msg.line), str(msg.column), escape(msg.msg)) - if self.linter.config.html_ids: - self.msgs += (msg.category, msg.msg_id, msg.module, msg.obj, - str(msg.line), str(msg.column), escape(msg.msg)) - else: - self.msgs += (msg.category, msg.module, msg.obj, - str(msg.line), str(msg.column), escape(msg.msg)) + + #It would be better to do this in init, but currently we do not + #have access to the linter (as it is setup in lint.set_reporter() + #Therfore we try to parse just the once. + if self.header is None: + self._parse_template() + + #We want to add the lines given by the template + thismsg = [str(msg.__dict__.get(x, "None")) for x in self.msgargs] + self.msgs += thismsg def set_output(self, output=None): """set output stream @@ -60,14 +93,11 @@ class HTMLReporter(BaseReporter): (in add_message, message is not displayed, just collected so it can be displayed in an html table) """ + if self.msgs: # add stored messages to the layout - if self.linter.config.html_ids: - msgs = ['type', 'id', 'module', 'object', 'line', 'col_offset', 'message'] - cols = 7 - else: - msgs = ['type', 'module', 'object', 'line', 'col_offset', 'message'] - cols = 6 + msgs = self.header + cols = len(self.header) msgs += self.msgs sect = Section('Messages') layout.append(sect)