-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpylint_wrapper.py
More file actions
56 lines (47 loc) · 1.76 KB
/
pylint_wrapper.py
File metadata and controls
56 lines (47 loc) · 1.76 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
"""Helper function to make VSCode understand the output of pylint when run as Task"""
#!/usr/bin/env python
# pylint_wrapper.py
import sys
import subprocess
import re
# Mapping from pylint category letter → VSCode severity
CATEGORY_MAP = {
"E": "error",
"F": "error",
"W": "warning",
"C": "info",
"R": "hint",
}
# Collect command-line arguments for pylint
pylint_args = sys.argv[1:]
# Force msg-template to include category at the start
pylint_args = ["--msg-template={path}:{line}-{end_line}:{column}-{end_column}:" + \
" {msg_id}: {msg} ({symbol})"] + pylint_args
# Run pylint and capture output
proc = subprocess.Popen( # pylint: disable=consider-using-with
[sys.executable.replace("python.exe", "pylint.exe"), *pylint_args],
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
text=True,
bufsize=1 # line-buffered
)
# Regex to parse pylint output: path:line-end_line:col-end_col: category: message (symbol)
# Example: foo.py:10:5: E0602: Undefined variable 'bar' (undefined-variable)
pattern = re.compile(r"^(.*?):(\d+)-(\d*):(\d+)-(\d*): ([A-Z])(\d+): (.*) \((.*)\)$")
if proc.stdout is None:
sys.exit(1)
for line in proc.stdout:
line = line.rstrip()
m = pattern.match(line)
if m:
path, line_no, end_line, col_no, end_col, cat, code, msg, symbol = m.groups()
severity = CATEGORY_MAP.get(cat, "info")
# Output in a format the VSCode matcher can consume
print(f"{path}:{line_no}-{end_line}:{int(col_no)+1}-" +
f"{str(int(end_col)+1) if end_col != '' else ''}:" +
f" {severity}: {msg} (Pylint({cat}{code}:{symbol}))", flush=True)
else:
# print unmodified if it doesn’t match
print(line, flush=True)
proc.wait()
sys.exit(proc.returncode)