Plugins/CheckerPlugins/SyntaxChecker/SyntaxCheck.py

branch
maintenance
changeset 5680
b93cb6353cc0
parent 5672
495b53f37f6c
child 5683
66b11f5171e8
equal deleted inserted replaced
5655:884cd9c9ce05 5680:b93cb6353cc0
11 import ast 11 import ast
12 import re 12 import re
13 import sys 13 import sys
14 import traceback 14 import traceback
15 import multiprocessing 15 import multiprocessing
16 import queue
16 17
17 try: 18 try:
18 from pyflakes.checker import Checker 19 from pyflakes.checker import Checker
19 from pyflakes.messages import ImportStarUsed, ImportStarUsage 20 from pyflakes.messages import ImportStarUsed, ImportStarUsage
20 except ImportError: 21 except ImportError:
63 pass 64 pass
64 65
65 return codestring 66 return codestring
66 67
67 68
68 def extractLineFlags(line, startComment="#", endComment=""): 69 def extractLineFlags(line, startComment="#", endComment="", flagsLine=False):
69 """ 70 """
70 Function to extract flags starting and ending with '__' from a line 71 Function to extract flags starting and ending with '__' from a line
71 comment. 72 comment.
72 73
73 @param line line to extract flags from (string) 74 @param line line to extract flags from (string)
74 @keyparam startComment string identifying the start of the comment (string) 75 @keyparam startComment string identifying the start of the comment (string)
75 @keyparam endComment string identifying the end of a comment (string) 76 @keyparam endComment string identifying the end of a comment (string)
77 @keyparam flagsLine flag indicating to check for a flags only line (bool)
76 @return list containing the extracted flags (list of strings) 78 @return list containing the extracted flags (list of strings)
77 """ 79 """
78 flags = [] 80 flags = []
79 81
80 pos = line.rfind(startComment) 82 if not flagsLine or (
81 if pos >= 0: 83 flagsLine and line.strip().startswith(startComment)):
82 comment = line[pos + len(startComment):].strip() 84 pos = line.rfind(startComment)
83 if endComment: 85 if pos >= 0:
84 comment = comment.replace("endComment", "") 86 comment = line[pos + len(startComment):].strip()
85 flags = [f.strip() for f in comment.split() 87 if endComment:
86 if (f.startswith("__") and f.endswith("__"))] 88 endPos = line.rfind(endComment)
89 if endPos >= 0:
90 comment = comment[:endPos]
91 flags = [f.strip() for f in comment.split()
92 if (f.startswith("__") and f.endswith("__"))]
87 return flags 93 return flags
88 94
89 95
90 def syntaxAndPyflakesCheck(filename, codestring, checkFlakes=True, 96 def syntaxAndPyflakesCheck(filename, codestring, checkFlakes=True,
91 ignoreStarImportWarnings=False): 97 ignoreStarImportWarnings=False):
140 .start() 146 .start()
141 147
142 # Get and send results 148 # Get and send results
143 endIndex = len(argumentsList) - initialTasks 149 endIndex = len(argumentsList) - initialTasks
144 for i in range(len(argumentsList)): 150 for i in range(len(argumentsList)):
145 filename, result = doneQueue.get() 151 resultSent = False
146 send(fx, filename, result) 152 wasCancelled = False
147 if cancelled(): 153
154 while not resultSent:
155 try:
156 # get result (waiting max. 3 seconds and send it to frontend
157 filename, result = doneQueue.get()
158 send(fx, filename, result)
159 resultSent = True
160 except queue.Empty:
161 # ignore empty queue, just carry on
162 if cancelled():
163 wasCancelled = True
164 break
165
166 if wasCancelled or cancelled():
148 # just exit the loop ignoring the results of queued tasks 167 # just exit the loop ignoring the results of queued tasks
149 break 168 break
169
150 if i < endIndex: 170 if i < endIndex:
151 taskQueue.put(argumentsList[i + initialTasks]) 171 taskQueue.put(argumentsList[i + initialTasks])
152 172
153 # Tell child processes to stop 173 # Tell child processes to stop
154 for i in range(NumberOfProcesses): 174 for i in range(NumberOfProcesses):
155 taskQueue.put('STOP') 175 taskQueue.put('STOP')
156 176
157 177
158 def worker(input, output): 178 def worker(inputQueue, outputQueue):
159 """ 179 """
160 Module function acting as the parallel worker for the style check. 180 Module function acting as the parallel worker for the style check.
161 181
162 @param input input queue (multiprocessing.Queue) 182 @param inputQueue input queue (multiprocessing.Queue)
163 @param output output queue (multiprocessing.Queue) 183 @param outputQueue output queue (multiprocessing.Queue)
164 """ 184 """
165 for filename, args in iter(input.get, 'STOP'): 185 for filename, args in iter(inputQueue.get, 'STOP'):
166 source, checkFlakes, ignoreStarImportWarnings = args 186 source, checkFlakes, ignoreStarImportWarnings = args
167 result = __syntaxAndPyflakesCheck(filename, source, checkFlakes, 187 result = __syntaxAndPyflakesCheck(filename, source, checkFlakes,
168 ignoreStarImportWarnings) 188 ignoreStarImportWarnings)
169 output.put((filename, result)) 189 outputQueue.put((filename, result))
170 190
171 191
172 def __syntaxAndPyflakesCheck(filename, codestring, checkFlakes=True, 192 def __syntaxAndPyflakesCheck(filename, codestring, checkFlakes=True,
173 ignoreStarImportWarnings=False): 193 ignoreStarImportWarnings=False):
174 """ 194 """
260 try: 280 try:
261 fn = detail.filename 281 fn = detail.filename
262 line = detail.lineno 282 line = detail.lineno
263 error = detail.msg 283 error = detail.msg
264 return [{'error': (fn, line, 0, "", error)}] 284 return [{'error': (fn, line, 0, "", error)}]
265 except: # __IGNORE_WARNING__ 285 except Exception:
266 pass 286 pass
267 287
268 # pyflakes 288 # pyflakes
269 if not checkFlakes: 289 if not checkFlakes:
270 return [{}] 290 return [{}]
280 isinstance(warning, ImportStarUsage) 300 isinstance(warning, ImportStarUsage)
281 ): 301 ):
282 continue 302 continue
283 303
284 _fn, lineno, col, message, msg_args = warning.getMessageData() 304 _fn, lineno, col, message, msg_args = warning.getMessageData()
285 if "__IGNORE_WARNING__" not in extractLineFlags( 305 lineFlags = extractLineFlags(lines[lineno - 1].strip())
286 lines[lineno - 1].strip()): 306 try:
307 lineFlags += extractLineFlags(lines[lineno].strip(),
308 flagsLine=True)
309 except IndexError:
310 pass
311 if "__IGNORE_WARNING__" not in lineFlags:
287 results.append((_fn, lineno, col, "", message, msg_args)) 312 results.append((_fn, lineno, col, "", message, msg_args))
288 except SyntaxError as err: 313 except SyntaxError as err:
289 if err.text.strip(): 314 if err.text.strip():
290 msg = err.text.strip() 315 msg = err.text.strip()
291 else: 316 else:

eric ide

mercurial