This repository was archived by the owner on Sep 1, 2023. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathpymode.py
More file actions
185 lines (146 loc) · 4.87 KB
/
Copy pathpymode.py
File metadata and controls
185 lines (146 loc) · 4.87 KB
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
import locale
import vim
locale.setlocale(locale.LC_CTYPE, "C")
def check_file():
filename = vim.current.buffer.name
checkers = vim.eval('g:pymode_lint_checker').split(',')
ignore = vim.eval("g:pymode_lint_ignore")
ignore = ignore and ignore.split(',') or []
select = vim.eval("g:pymode_lint_select")
select = select and select.split(',') or []
errors = []
for c in checkers:
checker = globals().get(c)
if checker:
try:
errors += checker(filename)
except SyntaxError as e:
errors.append(dict(
lnum=e.lineno,
col=e.offset,
text=e.args[0]
))
break
except Exception as e:
print(e)
for e in errors:
e.update(
col=e.get('col') or '',
text=e.get('text', '').replace("'", "\"").split('\n')[0],
filename=filename,
bufnr=vim.current.buffer.number,
)
errors = [e for e in errors if _ignore_error(e, select, ignore)]
errors = sorted(errors, key=lambda x: x['lnum'])
vim.command(('let b:qf_list = %s' % repr(errors)).replace('\': u', '\': '))
def mccabe(filename):
import mccabe as mc
return mc.get_module_complexity(filename)
def pep8(filename):
PEP8 or _init_pep8()
checker = PEP8['module'].Checker(filename)
checker.check_all()
return checker.errors
def pylint(filename):
import io
from logilab.astng.builder import MANAGER
PYLINT or _init_pylint()
linter = PYLINT['lint']
MANAGER.astng_cache.clear()
linter.reporter.out = io.StringIO()
linter.check(filename)
errors, linter.reporter.errors = linter.reporter.errors, []
return errors
def pyflakes(filename):
from pyflakes import checker
import _ast
codeString = file(filename, 'U').read() + '\n'
errors = []
try:
# XXX Py3k: Validate this error condition
tree = compile(codeString, filename, "exec", _ast.PyCF_ONLY_AST)
except SyntaxError as value:
msg = value.args[0]
if codeString is None:
vim.command('echoerr "%s: problem decoding source"' % filename)
else:
lineno, _, text = value.lineno, value.offset, value.text
errors.append(dict(
lnum = str(lineno),
col=value.col,
text = msg,
type = 'E'
))
else:
w = checker.Checker(tree, filename)
w.messages.sort(lambda a, b: cmp(a.lineno, b.lineno))
for w in w.messages:
errors.append(dict(
lnum=w.lineno,
col=w.col,
text=w.message % w.message_args,
type='E'
))
return errors
PYLINT = dict()
def _init_pylint():
from pylint import lint, checkers
import re
class VimReporter(object):
def __init__(self):
self.errors = []
def add_message(self, msg_id, location, msg):
_, _, line, col = location[1:]
self.errors.append(dict(
lnum=line,
col=col,
text="%s %s" % (msg_id, msg),
type=msg_id[0]
))
PYLINT['lint'] = lint.PyLinter()
PYLINT['re'] = re.compile('^(?:.:)?[^:]+:(\d+): \[([EWRCI]+)[^\]]*\] (.*)$')
checkers.initialize(PYLINT['lint'])
PYLINT['lint'].load_file_configuration(vim.eval("g:pymode_lint_config"))
PYLINT['lint'].set_option("output-format", "parseable")
PYLINT['lint'].set_option("include-ids", 1)
PYLINT['lint'].set_option("reports", 0)
PYLINT['lint'].reporter = VimReporter()
PEP8 = dict()
def _init_pep8():
import pep8 as p8
class _PEP8Options(object):
# Default options taken from pep8.process_options()
max_complexity = -1
verbose = False
quiet = False
no_repeat = False
exclude = [exc.rstrip('/') for exc in p8.DEFAULT_EXCLUDE.split(',')]
filename = ['*.py']
select = []
ignore = p8.DEFAULT_IGNORE.split(',') # or []?
show_source = False
show_pep8 = False
statistics = False
count = False
benchmark = False
testsuite = ''
doctest = False
logical_checks = physical_checks = None
messages = counters = None
# default p8 setup
p8.options = _PEP8Options()
p8.options.physical_checks = p8.find_checks('physical_line')
p8.options.logical_checks = p8.find_checks('logical_line')
p8.options.counters = dict.fromkeys(p8.BENCHMARK_KEYS, 0)
p8.options.messages = {}
p8.args = []
PEP8['init'] = True
PEP8['module'] = p8
def _ignore_error(e, select, ignore):
for s in select:
if e['text'].startswith(s):
return True
for i in ignore:
if e['text'].startswith(i):
return False
return True