File: _pylint.py

package info (click to toggle)
python-rjsmin 1.0.12%2Bdfsg1-4
  • links: PTS, VCS
  • area: main
  • in suites: buster, stretch
  • size: 2,032 kB
  • ctags: 1,029
  • sloc: python: 5,787; ansic: 634; makefile: 28; sh: 24
file content (271 lines) | stat: -rw-r--r-- 8,678 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
# -*- coding: ascii -*-
#
# Copyright 2006 - 2014
# Andr\xe9 Malo or his licensors, as applicable
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""
=================================
 Support for code analysis tools
=================================

Support for code analysis tools.
"""
__author__ = "Andr\xe9 Malo"
__docformat__ = "restructuredtext en"

import re as _re
import sys as _sys

from _setup import term as _term
from _setup import shell as _shell


class NotFinished(Exception):
    """ Exception used for message passing in the stream filter """


class NotParseable(Exception):
    """ Exception used for message passing in the stream filter """


class SpecialMessage(Exception):
    """ Exception used for message passing in the stream filter """


class FilterStream(object):
    """ Stream filter """
    _LINERE = _re.compile(r'''
        (?P<name>[^:]+)
        :
        (?P<lineno>\d+)
        :\s+
        \[(?P<mid>[^\],]+)(?:,\s+(?P<func>[^\]]+))?\]
        \s+
        (?P<desc>.*)
    ''', _re.X)
    _SIMRE = _re.compile(r'in (?P<number>\d+) files')
    _CYCRE = _re.compile(r'\((?P<cycle>[^)]+)\)')

    def __init__(self, term, stream=_sys.stdout):
        self.written = False
        self._stream = stream
        self._lastname = None
        self._cycled = False
        self._term = dict(term)
        self._buffer = ''

    def write(self, towrite):
        """ Stream write function """
        self._buffer += towrite
        term = self._term

        while True:
            try:
                name, lineno, mid, func, desc = self._parse()
            except NotFinished:
                break
            except SpecialMessage as e:
                self._dospecial(e)
                continue
            except NotParseable as e:
                self._print_literal(str(e.args[0]))
                continue

            if name != self._lastname:
                if self._lastname is not None:
                    self._stream.write("\n")
                term['path'] = name
                self._stream.write(
                    "%(BOLD)s>>> %(path)s%(NORMAL)s\n" % term
                )
                self._lastname = name
                self.written = True

            term['mid'] = mid
            if mid.startswith('E') or mid.startswith('F'):
                self._stream.write("%(BOLD)s%(RED)s%(mid)s%(NORMAL)s" % term)
            elif mid == 'W0511':
                self._stream.write(
                    "%(BOLD)s%(GREEN)s%(mid)s%(NORMAL)s" % term
                )
            else:
                self._stream.write(
                    "%(BOLD)s%(YELLOW)s%(mid)s%(NORMAL)s" % term
                )

            if int(lineno) != 0:
                term['lineno'] = lineno
                self._stream.write(" (%(lineno)s" % term)
                if func:
                    term['func'] = func
                    self._stream.write(
                        ", %(BOLD)s%(YELLOW)s%(func)s%(NORMAL)s" % term
                    )
                self._stream.write(')')

            self._stream.write(": %s\n" % desc)
            self._stream.flush()

        return

    def _print_literal(self, line):
        """ Print literal """
        suppress = (
            line.startswith('Unable to get imported names for ') or
            line.startswith(
                "Exception exceptions.RuntimeError: 'generator "
                "ignored GeneratorExit' in <generator object at") or
            line.startswith(
                "Exception RuntimeError: 'generator "
                "ignored GeneratorExit' in <generator object") or
            not line.strip()
        )
        if not suppress:
            self._stream.write("%s\n" % line)
            self._stream.flush()
            self.written = True

    def _dospecial(self, e):
        """ Deal with special messages """
        if e.args[0] == 'R0401':
            pos = self._buffer.find('\n')
            _, self._buffer = (
                self._buffer[:pos + 1], self._buffer[pos + 1:]
            )
            term = self._term
            term['mid'] = e.args[0]
            if not self._cycled:
                self._cycled = True
                self._stream.write('\n')
                self._stream.write(
                    "%(BOLD)s%(YELLOW)s%(mid)s%(NORMAL)s" % term
                )
                self._stream.write(": Cyclic imports\n")
            match = self._CYCRE.search(e.args[1])
            term['cycle'] = match.group('cycle')
            self._stream.write("%(BOLD)s@@@ %(NORMAL)s%(cycle)s\n" % term)
            self._stream.flush()
            self.written = True

        elif e.args[0] == 'R0801':
            match = self._SIMRE.search(e.args[1])
            if not match:
                raise AssertionError(
                    'Could not determine number of similar files'
                )

            numfiles = int(match.group('number'))
            pos = -1
            for _ in range(numfiles + 1):
                pos = self._buffer.find('\n', pos + 1)
            if pos >= 0:
                lines = self._buffer[:pos + 1]
                self._buffer = self._buffer[pos + 1:]
                term = self._term

                self._stream.write("\n")
                for name in lines.splitlines()[1:]:
                    name = name.rstrip()[2:]
                    term['path'] = name
                    self._stream.write(
                        "%(BOLD)s=== %(path)s%(NORMAL)s\n" % term
                    )
                    self._lastname = name

                term['mid'] = e.args[0]
                self._stream.write(
                    "%(BOLD)s%(YELLOW)s%(mid)s%(NORMAL)s" % term
                )
                self._stream.write(": %s\n" % e.args[1])
                self._stream.flush()
                self.written = True

    def _parse(self):
        """ Parse output """
        if '\n' not in self._buffer:
            raise NotFinished()

        line = self._buffer[:self._buffer.find('\n') + 1]
        self._buffer = self._buffer[len(line):]
        line = line.rstrip()
        match = self._LINERE.match(line)
        if not match:
            raise NotParseable(line)

        mid = match.group('mid')
        if mid in ('R0801', 'R0401'):
            self._buffer = "%s\n%s" % (line, self._buffer)
            raise SpecialMessage(mid, match.group('desc'))

        return match.group('name', 'lineno', 'mid', 'func', 'desc')


def run(config, *args):
    """ Run pylint """
    try:
        from pylint import lint
        from pylint.reporters import text
    except ImportError:
        return 2

    if config is None:
        config = _shell.native('pylintrc')
    argv = [
        '--rcfile', config,
        '--reports', 'no',
    ]

    stream = FilterStream(_term.terminfo())

    old_stderr = _sys.stderr
    try:
        # pylint: disable = E1101
        _sys.stderr = stream
        from pylint import __pkginfo__
        if __pkginfo__.numversion >= (1, 0, 0):
            reporter = text.TextReporter(stream)
            argv.extend([
                '--msg-template',
                '{path}:{line}: [{msg_id}({symbol}), {obj}] {msg}'
            ])
        else:
            argv.extend([
                '--output-format', 'parseable',
                '--include-ids', 'yes'
            ])
            if __pkginfo__.numversion < (0, 13):
                lint.REPORTER_OPT_MAP['parseable'] = \
                    lambda: text.TextReporter2(stream)
                reporter = text.TextReporter2(stream)
            else:
                reporter = text.ParseableTextReporter(stream)
                lint.REPORTER_OPT_MAP['parseable'] = lambda: reporter

        for path in args:
            try:
                try:
                    lint.Run(argv + [path], reporter=reporter)
                except SystemExit:
                    pass  # don't accept the exit. strange errors happen...

                if stream.written:
                    print()
                    stream.written = False
            except KeyboardInterrupt:
                print()
                raise
    finally:
        _sys.stderr = old_stderr

    return 0