FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
python-language-server/pyls/plugins/flake8_lint.py at develop · deepnote/python-language-server · GitHub
Uh oh!
There was an error while loading.
Please reload this page
.
deepnote
/
python-language-server
Public
forked from
palantir/python-language-server
Notifications
You must be signed in to change notification settings
Fork
6
Star
3
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
python-language-server
/
pyls
/
plugins
/
flake8_lint.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
167 lines (144 loc) · 5.25 KB
Breadcrumbs
python-language-server
/
pyls
/
plugins
/
flake8_lint.py
Copy path
File metadata and controls
167 lines (144 loc) · 5.25 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
# Copyright 2019 Palantir Technologies, Inc.
"""Linter pluging for flake8"""
import
logging
import
os
.
path
import
re
from
subprocess
import
Popen
,
PIPE
from
pyls
import
hookimpl
,
lsp
log
=
logging
.
getLogger
(
__name__
)
FIX_IGNORES_RE
=
re
.
compile
(
r'([^a-zA-Z0-9_,]*;.*(\W+||$))'
)
@
hookimpl
def
pyls_settings
():
# Default flake8 to disabled
return
{
'plugins'
: {
'flake8'
: {
'enabled'
:
False
}}}
@
hookimpl
def
pyls_lint
(
workspace
,
document
):
config
=
workspace
.
_config
settings
=
config
.
plugin_settings
(
'flake8'
,
document_path
=
document
.
path
)
log
.
debug
(
"Got flake8 settings: %s"
,
settings
)
opts
=
{
'config'
:
settings
.
get
(
'config'
),
'exclude'
:
settings
.
get
(
'exclude'
),
'filename'
:
settings
.
get
(
'filename'
),
'hang-closing'
:
settings
.
get
(
'hangClosing'
),
'ignore'
:
settings
.
get
(
'ignore'
),
'max-line-length'
:
settings
.
get
(
'maxLineLength'
),
'select'
:
settings
.
get
(
'select'
),
}
# flake takes only absolute path to the config. So we should check and
# convert if necessary
if
opts
.
get
(
'config'
)
and
not
os
.
path
.
isabs
(
opts
.
get
(
'config'
)):
opts
[
'config'
]
=
os
.
path
.
abspath
(
os
.
path
.
expanduser
(
os
.
path
.
expandvars
(
opts
.
get
(
'config'
)
)))
log
.
debug
(
"using flake8 with config: %s"
,
opts
[
'config'
])
# Call the flake8 utility then parse diagnostics from stdout
flake8_executable
=
settings
.
get
(
'executable'
,
'flake8'
)
args
=
build_args
(
opts
)
output
=
run_flake8
(
flake8_executable
,
args
,
document
)
return
parse_stdout
(
document
,
output
)
def
run_flake8
(
flake8_executable
,
args
,
document
):
"""Run flake8 with the provided arguments, logs errors
from stderr if any.
"""
# a quick temporary fix to deal with Atom
args
=
[(
i
if
not
i
.
startswith
(
'--ignore='
)
else
FIX_IGNORES_RE
.
sub
(
''
,
i
))
for
i
in
args
if
i
is
not
None
]
# if executable looks like a path resolve it
if
not
os
.
path
.
isfile
(
flake8_executable
)
and
os
.
sep
in
flake8_executable
:
flake8_executable
=
os
.
path
.
abspath
(
os
.
path
.
expanduser
(
os
.
path
.
expandvars
(
flake8_executable
))
)
log
.
debug
(
"Calling %s with args: '%s'"
,
flake8_executable
,
args
)
try
:
cmd
=
[
flake8_executable
]
cmd
.
extend
(
args
)
p
=
Popen
(
cmd
,
stdin
=
PIPE
,
stdout
=
PIPE
,
stderr
=
PIPE
)
except
IOError
:
log
.
debug
(
"Can't execute %s. Trying with 'python -m flake8'"
,
flake8_executable
)
cmd
=
[
'python'
,
'-m'
,
'flake8'
]
cmd
.
extend
(
args
)
p
=
Popen
(
cmd
,
stdin
=
PIPE
,
stdout
=
PIPE
,
stderr
=
PIPE
)
(
stdout
,
stderr
)
=
p
.
communicate
(
document
.
source
.
encode
())
if
stderr
:
log
.
error
(
"Error while running flake8 '%s'"
,
stderr
.
decode
())
return
stdout
.
decode
()
def
build_args
(
options
):
"""Build arguments for calling flake8.
Args:
options: dictionary of argument names and their values.
"""
args
=
[
'-'
]
# use stdin
for
arg_name
,
arg_val
in
options
.
items
():
if
arg_val
is
None
:
continue
arg
=
None
if
isinstance
(
arg_val
,
list
):
arg
=
'--{}={}'
.
format
(
arg_name
,
','
.
join
(
arg_val
))
elif
isinstance
(
arg_val
,
bool
):
if
arg_val
:
arg
=
'--{}'
.
format
(
arg_name
)
else
:
arg
=
'--{}={}'
.
format
(
arg_name
,
arg_val
)
args
.
append
(
arg
)
return
args
def
parse_stdout
(
document
,
stdout
):
"""
Build a diagnostics from flake8's output, it should extract every result and format
it into a dict that looks like this:
{
'source': 'flake8',
'code': code, # 'E501'
'range': {
'start': {
'line': start_line,
'character': start_column,
},
'end': {
'line': end_line,
'character': end_column,
},
},
'message': msg,
'severity': lsp.DiagnosticSeverity.*,
}
Args:
document: The document to be linted.
stdout: output from flake8
Returns:
A list of dictionaries.
"""
diagnostics
=
[]
lines
=
stdout
.
splitlines
()
for
raw_line
in
lines
:
parsed_line
=
re
.
match
(
r'(.*):(\d*):(\d*): (\w*) (.*)'
,
raw_line
)
if
not
parsed_line
:
log
.
debug
(
"Flake8 output parser can't parse line '%s'"
,
raw_line
)
continue
parsed_line
=
parsed_line
.
groups
()
if
len
(
parsed_line
)
!=
5
:
log
.
debug
(
"Flake8 output parser can't parse line '%s'"
,
raw_line
)
continue
_
,
line
,
character
,
code
,
msg
=
parsed_line
line
=
int
(
line
)
-
1
character
=
int
(
character
)
-
1
diagnostics
.
append
(
{
'source'
:
'flake8'
,
'code'
:
code
,
'range'
: {
'start'
: {
'line'
:
line
,
'character'
:
character
},
'end'
: {
'line'
:
line
,
# no way to determine the column
'character'
:
len
(
document
.
lines
[
line
])
}
},
'message'
:
msg
,
'severity'
:
lsp
.
DiagnosticSeverity
.
Warning
,
}
)
return
diagnostics
Back
|
FazBrowse Home
|
New Git URL