77from pyls import hookimpl , lsp
88
99log = logging .getLogger (__name__ )
10+ FIX_IGNORES_RE = re .compile (r'([^a-zA-Z0-9_,]*;.*(\W+||$))' )
1011
1112
1213@hookimpl
@@ -16,7 +17,8 @@ def pyls_settings():
1617
1718
1819@hookimpl
19- def pyls_lint (config , document ):
20+ def pyls_lint (workspace , document ):
21+ config = workspace ._config
2022 settings = config .plugin_settings ('flake8' )
2123 log .debug ("Got flake8 settings: %s" , settings )
2224
@@ -39,39 +41,44 @@ def pyls_lint(config, document):
3941 log .debug ("using flake8 with config: %s" , opts ['config' ])
4042
4143 # Call the flake8 utility then parse diagnostics from stdout
42- args = build_args (opts , document .path )
43- output = run_flake8 (args )
44+ flake8_executable = settings .get ('executable' , 'flake8' )
45+
46+ args = build_args (opts )
47+ output = run_flake8 (flake8_executable , args , document )
4448 return parse_stdout (document , output )
4549
4650
47- def run_flake8 (args ):
51+ def run_flake8 (flake8_executable , args , document ):
4852 """Run flake8 with the provided arguments, logs errors
4953 from stderr if any.
5054 """
51- log .debug ("Calling flake8 with args: '%s'" , args )
55+ # a quick temporary fix to deal with Atom
56+ args = [(i if not i .startswith ('--ignore=' ) else FIX_IGNORES_RE .sub ('' , i ))
57+ for i in args if i is not None ]
58+
59+ log .debug ("Calling %s with args: '%s'" , flake8_executable , args )
5260 try :
53- cmd = ['flake8' ]
61+ cmd = [flake8_executable ]
5462 cmd .extend (args )
55- p = Popen (cmd , stdout = PIPE , stderr = PIPE )
63+ p = Popen (cmd , stdin = PIPE , stdout = PIPE , stderr = PIPE )
5664 except IOError :
57- log .debug ("Can't execute flake8 . Trying with 'python -m flake8'" )
65+ log .debug ("Can't execute %s . Trying with 'python -m flake8'" , flake8_executable )
5866 cmd = ['python' , '-m' , 'flake8' ]
5967 cmd .extend (args )
60- p = Popen (cmd , stdout = PIPE , stderr = PIPE )
61- (stdout , stderr ) = p .communicate ()
68+ p = Popen (cmd , stdin = PIPE , stdout = PIPE , stderr = PIPE )
69+ (stdout , stderr ) = p .communicate (document . source . encode () )
6270 if stderr :
6371 log .error ("Error while running flake8 '%s'" , stderr .decode ())
6472 return stdout .decode ()
6573
6674
67- def build_args (options , doc_path ):
75+ def build_args (options ):
6876 """Build arguments for calling flake8.
6977
7078 Args:
7179 options: dictionary of argument names and their values.
72- doc_path: path of the document to lint.
7380 """
74- args = [doc_path ]
81+ args = ['-' ] # use stdin
7582 for arg_name , arg_val in options .items ():
7683 if arg_val is None :
7784 continue
@@ -118,10 +125,16 @@ def parse_stdout(document, stdout):
118125 diagnostics = []
119126 lines = stdout .splitlines ()
120127 for raw_line in lines :
121- parsed_line = re .match (r'(.*):(\d*):(\d*): (\w*) (.*)' , raw_line ).groups ()
122- if not parsed_line or len (parsed_line ) != 5 :
128+ parsed_line = re .match (r'(.*):(\d*):(\d*): (\w*) (.*)' , raw_line )
129+ if not parsed_line :
130+ log .debug ("Flake8 output parser can't parse line '%s'" , raw_line )
131+ continue
132+
133+ parsed_line = parsed_line .groups ()
134+ if len (parsed_line ) != 5 :
123135 log .debug ("Flake8 output parser can't parse line '%s'" , raw_line )
124136 continue
137+
125138 _ , line , character , code , msg = parsed_line
126139 line = int (line ) - 1
127140 character = int (character ) - 1
0 commit comments