summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTorsten Marek <shlomme@gmail.com>2014-08-16 17:40:44 -0700
committerTorsten Marek <shlomme@gmail.com>2014-08-16 17:40:44 -0700
commitdf737168a764b1b418127ed8eadf8c148f47e118 (patch)
treef3a333de7748fedae8e3772a93d7a4c60fda21a7
parent920712fce6a808482d521170fc75b325b4001788 (diff)
downloadpylint-git-df737168a764b1b418127ed8eadf8c148f47e118.tar.gz
Move definition of Message class out of reporters module, and make it a namedtuple.
-rw-r--r--reporters/__init__.py22
-rw-r--r--reporters/guireporter.py5
-rw-r--r--reporters/html.py6
-rw-r--r--reporters/text.py14
-rw-r--r--test/test_functional.py6
-rw-r--r--utils.py32
6 files changed, 47 insertions, 38 deletions
diff --git a/reporters/__init__.py b/reporters/__init__.py
index 12d193f5d..28d7071bf 100644
--- a/reporters/__init__.py
+++ b/reporters/__init__.py
@@ -17,7 +17,6 @@ import sys
import locale
import os
-from pylint.utils import MSG_TYPES
from pylint import utils
@@ -41,27 +40,6 @@ def diff_string(old, new):
return diff_str
-class Message(object):
- """This class represent a message to be issued by the reporters"""
-
- def __init__(self, reporter, msg_id, location, msg):
- self.msg_id = msg_id
- self.abspath, self.module, self.obj, self.line, self.column = location
- self.path = self.abspath.replace(reporter.path_strip_prefix, '')
- self.msg = msg
- self.C = msg_id[0]
- self.category = MSG_TYPES[msg_id[0]]
- self.symbol = reporter.linter.msgs_store.check_message_id(msg_id).symbol
-
- def format(self, template):
- """Format the message according to the given template.
-
- The template format is the one of the format method :
- cf. http://docs.python.org/2/library/string.html#formatstrings
- """
- return template.format(**(self.__dict__))
-
-
class BaseReporter(object):
"""base class for reporters
diff --git a/reporters/guireporter.py b/reporters/guireporter.py
index 331eb179e..8ebc2703e 100644
--- a/reporters/guireporter.py
+++ b/reporters/guireporter.py
@@ -3,7 +3,8 @@
import sys
from pylint.interfaces import IReporter
-from pylint.reporters import BaseReporter, Message
+from pylint.reporters import BaseReporter
+from pylint import utils
from logilab.common.ureports import TextWriter
@@ -20,7 +21,7 @@ class GUIReporter(BaseReporter):
def add_message(self, msg_id, location, msg):
"""manage message of different type and in the context of path"""
- message = Message(self, msg_id, location, msg)
+ message = utils.Message(msg_id, self.linter.msgs_store.check_message_id(msg_id).symbol, location, msg)
self.gui.msg_queue.put(message)
def _display(self, layout):
diff --git a/reporters/html.py b/reporters/html.py
index 71d46ebab..441f68a4d 100644
--- a/reporters/html.py
+++ b/reporters/html.py
@@ -18,8 +18,9 @@ from cgi import escape
from logilab.common.ureports import HTMLWriter, Section, Table
+from pylint import utils
from pylint.interfaces import IReporter
-from pylint.reporters import BaseReporter, Message
+from pylint.reporters import BaseReporter
class HTMLReporter(BaseReporter):
@@ -35,7 +36,8 @@ class HTMLReporter(BaseReporter):
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)
+ msg = utils.Message(msg_id, self.linter.msgs_store.check_message_id(msg_id).symbol,
+ location, msg)
self.msgs += (msg.category, msg.module, msg.obj,
str(msg.line), str(msg.column), escape(msg.msg))
diff --git a/reporters/text.py b/reporters/text.py
index 04245f70e..83bfe605e 100644
--- a/reporters/text.py
+++ b/reporters/text.py
@@ -23,7 +23,8 @@ from logilab.common.ureports import TextWriter
from logilab.common.textutils import colorize_ansi
from pylint.interfaces import IReporter
-from pylint.reporters import BaseReporter, Message
+from pylint.reporters import BaseReporter
+from pylint import utils
TITLE_UNDERLINES = ['', '=', '-', '.']
@@ -50,7 +51,8 @@ class TextReporter(BaseReporter):
def add_message(self, msg_id, location, msg):
"""manage message of different type and in the context of path"""
- m = Message(self, msg_id, location, msg)
+ m = utils.Message(msg_id, self.linter.msgs_store.check_message_id(msg_id).symbol,
+ location, msg)
if m.module not in self._modules:
if m.module:
self.writeln('************* Module %s' % m.module)
@@ -118,7 +120,7 @@ class ColorizedTextReporter(TextReporter):
"""manage message of different types, and colorize output
using ansi escape codes
"""
- msg = Message(self, msg_id, location, msg)
+ msg = utils.Message(msg_id, self.linter.msgs_store.check_message_id(msg_id).symbol, location, msg)
if msg.module not in self._modules:
color, style = self._get_decoration('S')
if msg.module:
@@ -130,8 +132,10 @@ class ColorizedTextReporter(TextReporter):
self.writeln(modsep)
self._modules.add(msg.module)
color, style = self._get_decoration(msg.C)
- for attr in ('msg', 'symbol', 'category', 'C'):
- setattr(msg, attr, colorize_ansi(getattr(msg, attr), color, style))
+
+ msg = msg._replace(
+ **{attr: colorize_ansi(getattr(msg, attr), color, style)
+ for attr in ('msg', 'symbol', 'category', 'C')})
self.write_message(msg)
diff --git a/test/test_functional.py b/test/test_functional.py
index fb495e0c7..a6c67805f 100644
--- a/test/test_functional.py
+++ b/test/test_functional.py
@@ -9,10 +9,10 @@ import re
import sys
import unittest
+from pylint import checkers
from pylint import lint
from pylint import reporters
-from pylint import checkers
-
+from pylint import utils
class NoFileError(Exception):
pass
@@ -49,7 +49,7 @@ def parse_python_version(str):
class TestReporter(reporters.BaseReporter):
def add_message(self, msg_id, location, msg):
- self.messages.append(reporters.Message(self, msg_id, location, msg))
+ self.messages.append(utils.Message(msg_id, self.linter.msgs_store.check_message_id(msg_id).symbol, location, msg))
def on_set_current_module(self, module, filepath):
self.messages = []
diff --git a/utils.py b/utils.py
index 9a7189e0c..59a957a1a 100644
--- a/utils.py
+++ b/utils.py
@@ -17,11 +17,13 @@
main pylint class
"""
+import collections
+import os
import re
import sys
import tokenize
-import os
-from warnings import warn
+import warnings
+
from os.path import dirname, basename, splitext, exists, isdir, join, normpath
from logilab.common.interface import implements
@@ -75,6 +77,28 @@ class WarningScope(object):
LINE = 'line-based-msg'
NODE = 'node-based-msg'
+_MsgBase = collections.namedtuple(
+ '_MsgBase',
+ ['msg_id', 'symbol', 'msg', 'C', 'category', 'abspath', 'module', 'obj',
+ 'line', 'column'])
+
+
+class Message(_MsgBase):
+ """This class represent a message to be issued by the reporters"""
+ def __new__(cls, msg_id, symbol, location, msg):
+ return _MsgBase.__new__(
+ cls, msg_id, symbol, msg, msg_id[0], MSG_TYPES[msg_id[0]], *location)
+
+ def format(self, template):
+ """Format the message according to the given template.
+
+ The template format is the one of the format method :
+ cf. http://docs.python.org/2/library/string.html#formatstrings
+ """
+ # For some reason, _asdict on derived namedtuples does not work with
+ # Python 3.4. Needs some investigation.
+ return template.format(**dict(zip(self._fields, self)))
+
def get_module_and_frameid(node):
"""return the module name and the frame id in the module"""
@@ -124,8 +148,8 @@ def build_message_def(checker, msgid, msg_tuple):
# messages should have a symbol, but for backward compatibility
# they may not.
(msg, descr) = msg_tuple
- warn("[pylint 0.26] description of message %s doesn't include "
- "a symbolic name" % msgid, DeprecationWarning)
+ warnings.warn("[pylint 0.26] description of message %s doesn't include "
+ "a symbolic name" % msgid, DeprecationWarning)
symbol = None
options.setdefault('scope', default_scope)
return MessageDefinition(checker, msgid, msg, descr, symbol, **options)