forked from dreadatour/Flake8Lint
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lint.py
150 lines (118 loc) · 4.32 KB
/
lint.py
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
# -*- coding: utf-8 -*-
"""
Flake8 lint worker.
"""
import os
# We will use Stéphane Klein fork of flake8 until it not merged into flake8.
# This version includes last version of pep8.
# See: https://bitbucket.org/tarek/flake8/issue/23/use-pep8-configuration-file
from flake8_harobed import pyflakes, pep8, mccabe, util
# Monkey-patching is a big evil (don't do this),
# but hardcode is a much more bigger evil. Hate hardcore!
from monkey_patching import pyflakes_check, mccabe_get_code_complexity
pyflakes.check = pyflakes_check
mccabe.get_code_complexity = mccabe_get_code_complexity
class Pep8Report(pep8.BaseReport):
"""
Collect all results of the checks.
"""
def __init__(self, options):
"""
Initialize reporter.
"""
super(Pep8Report, self).__init__(options)
# errors "collection"
self.errors = []
def error(self, line_number, offset, text, check):
"""
Get error and save it into errors collection.
"""
code = super(Pep8Report, self).error(line_number, offset, text, check)
if code:
self.errors.append((self.line_offset + line_number, offset, text))
return code
def lint(filename, settings):
"""
Run flake8 lint with internal interpreter.
"""
# check if active view contains file
if not filename or not os.path.exists(filename):
return
# skip file check if 'noqa' for whole file is set
if util.skip_file(filename):
return
# place for warnings =)
warnings = []
# lint with pyflakes
if settings.get('pyflakes', True):
warnings.extend(pyflakes.checkPath(filename))
# lint with pep8
if settings.get('pep8', True):
pep8style = pep8.StyleGuide(
reporter=Pep8Report,
ignore=settings.get('ignore', []),
max_line_length=settings.get('pep8_max_line_length')
)
pep8style.input_file(filename)
warnings.extend(pep8style.options.report.errors)
# check complexity
complexity = settings.get('complexity', -1)
if complexity > -1:
warnings.extend(mccabe.get_module_complexity(filename, complexity))
return warnings
def lint_external(filename, settings, interpreter, linter):
"""
Run flake8 lint with external interpreter.
"""
import subprocess
# check if active view contains file
if not filename or not os.path.exists(filename):
return
# skip file check if 'noqa' for whole file is set
if util.skip_file(filename):
return
# first argument is interpreter
arguments = [interpreter, linter]
# do we need to run pyflake lint
if settings.get('pyflakes', True):
arguments.append('--pyflakes')
# do we need to run pep8 lint
if settings.get('pep8', True):
arguments.append('--pep8')
max_line_length = settings.get('pep8_max_line_length')
arguments.append('--pep8-max-line-length')
arguments.append(str(max_line_length))
# do we need to run complexity check
complexity = settings.get('complexity', -1)
if complexity > 0:
arguments.extend(('--complexity', str(complexity)))
# last argument is script to check filename
arguments.append(filename)
# place for warnings =)
warnings = []
# run subprocess
proc = subprocess.Popen(arguments, stdout=subprocess.PIPE)
# parse STDOUT for warnings and errors
for line in proc.stdout:
warning = line.strip().split(':', 2)
if len(warning) == 3:
warnings.append((int(warning[0]), int(warning[1]), warning[2]))
# and return them =)
return warnings
if __name__ == "__main__":
import argparse
# parse arguments
parser = argparse.ArgumentParser()
parser.add_argument("filename")
parser.add_argument('--pyflakes', action='store_true',
help="run pyflakes lint")
parser.add_argument('--pep8', action='store_true',
help="run pep8 lint")
parser.add_argument('--complexity', type=int, help="check complexity")
parser.add_argument('--pep8-max-line-length', type=int,
help="pep8 max line length")
settings = parser.parse_args().__dict__
filename = settings.pop('filename')
# run lint and print errors
for warning in lint(filename, settings):
print "%d:%d:%s" % warning