Statistics
| Revision:

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 / __init__.py @ 745

History | View | Annotate | Download (4.91 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
"""utilities methods and classes for reporters"""
15
from __future__ import print_function
16

    
17
import sys
18
import locale
19
import os
20
import warnings
21

    
22
import six
23

    
24
CMPS = ['=', '-', '+']
25

    
26
# py3k has no more cmp builtin
27
if sys.version_info >= (3, 0):
28
    def cmp(a, b): # pylint: disable=redefined-builtin
29
        return (a > b) - (a < b)
30

    
31
def diff_string(old, new):
32
    """given a old and new int value, return a string representing the
33
    difference
34
    """
35
    diff = abs(old - new)
36
    diff_str = "%s%s" % (CMPS[cmp(old, new)], diff and ('%.2f' % diff) or '')
37
    return diff_str
38

    
39

    
40
class BaseReporter(object):
41
    """base class for reporters
42

43
    symbols: show short symbolic names for messages.
44
    """
45

    
46
    extension = ''
47

    
48
    def __init__(self, output=None):
49
        self.linter = None
50
        self.section = 0
51
        self.out = None
52
        self.out_encoding = None
53
        self.set_output(output)
54
        # Build the path prefix to strip to get relative paths
55
        self.path_strip_prefix = os.getcwd() + os.sep
56

    
57
    def handle_message(self, msg):
58
        """Handle a new message triggered on the current file.
59

60
        Invokes the legacy add_message API by default."""
61
        self.add_message(
62
            msg.msg_id, (msg.abspath, msg.module, msg.obj, msg.line, msg.column),
63
            msg.msg)
64

    
65
    def add_message(self, msg_id, location, msg):
66
        """Deprecated, do not use."""
67
        # pylint: disable=no-self-use,unused-argument
68
        msg = ("This method is deprecated, use handle_message instead. "
69
               "It will be removed in Pylint 1.6.")
70
        warnings.warn(msg, DeprecationWarning, stacklevel=2)
71

    
72
    def set_output(self, output=None):
73
        """set output stream"""
74
        self.out = output or sys.stdout
75

    
76
    if six.PY3:
77
        encode = lambda self, string: string
78
    else:
79
        def encode(self, string):
80
            if not isinstance(string, six.text_type):
81
                return string
82
            encoding = (getattr(self.out, 'encoding', None) or
83
                        locale.getdefaultlocale()[1] or
84
                        sys.getdefaultencoding())
85
            # errors=replace, we don't want to crash when attempting to show
86
            # source code line that can't be encoded with the current locale
87
            # settings
88
            return string.encode(encoding, 'replace')
89

    
90
    def writeln(self, string=''):
91
        """write a line in the output buffer"""
92
        print(self.encode(string), file=self.out)
93

    
94
    def display_reports(self, layout):
95
        """display results encapsulated in the layout tree"""
96
        self.section = 0
97
        if hasattr(layout, 'report_id'):
98
            layout.children[0].children[0].data += ' (%s)' % layout.report_id
99
        self._display(layout)
100

    
101
    def display_results(self, layout):
102
        warnings.warn("display_results is deprecated, use display_reports instead. "
103
                      "The former will be removed in Pylint 2.0.",
104
                      DeprecationWarning)
105
        self.display_reports(layout)
106

    
107
    def _display(self, layout):
108
        """display the layout"""
109
        raise NotImplementedError()
110

    
111
    def display_messages(self, layout):
112
        """Hook for displaying the messages of the reporter
113

114
        This will be called whenever the underlying messages
115
        needs to be displayed. For some reporters, it probably
116
        doesn't make sense to display messages as soon as they
117
        are available, so some mechanism of storing them could be used.
118
        This method can be implemented to display them after they've
119
        been aggregated.
120
        """
121

    
122
    # Event callbacks
123

    
124
    def on_set_current_module(self, module, filepath):
125
        """Hook called when a module starts to be analysed."""
126

    
127
    def on_close(self, stats, previous_stats):
128
        """Hook called when a module finished analyzing."""
129

    
130

    
131
class CollectingReporter(BaseReporter):
132
    """collects messages"""
133

    
134
    name = 'collector'
135

    
136
    def __init__(self):
137
        BaseReporter.__init__(self)
138
        self.messages = []
139

    
140
    def handle_message(self, msg):
141
        self.messages.append(msg)
142

    
143
    _display = None
144

    
145

    
146
def initialize(linter):
147
    """initialize linter with reporters in this package """
148
    from pylint import utils
149
    utils.register_plugins(linter, __path__[0])