Plugins/CheckerPlugins/Tabnanny/Tabnanny.py

changeset 4237
ff8a3e769fca
parent 4021
195a471c327b
child 4541
e8ddd9d76414
equal deleted inserted replaced
4236:8d4e498a7af8 4237:ff8a3e769fca
46 import tokenize 46 import tokenize
47 try: 47 try:
48 import StringIO as io 48 import StringIO as io
49 except (ImportError): 49 except (ImportError):
50 import io # __IGNORE_WARNING__ 50 import io # __IGNORE_WARNING__
51 import multiprocessing
51 52
52 if not hasattr(tokenize, 'NL'): 53 if not hasattr(tokenize, 'NL'):
53 raise ValueError("tokenize.NL doesn't exist -- tokenize module too old") 54 raise ValueError("tokenize.NL doesn't exist -- tokenize module too old")
54 55
55 __all__ = ["check", "NannyNag", "process_tokens"] 56 __all__ = ["check", "NannyNag", "process_tokens"]
60 Initialize the service and return the entry point. 61 Initialize the service and return the entry point.
61 62
62 @return the entry point for the background client (function) 63 @return the entry point for the background client (function)
63 """ 64 """
64 return check 65 return check
66
67
68 def initBatchService():
69 """
70 Initialize the batch service and return the entry point.
71
72 @return the entry point for the background client (function)
73 """
74 return batchCheck
65 75
66 76
67 class NannyNag(Exception): 77 class NannyNag(Exception):
68 """ 78 """
69 Class implementing an exception for indentation issues. 79 Class implementing an exception for indentation issues.
116 @return A tuple indicating status (True = an error was found), the 126 @return A tuple indicating status (True = an error was found), the
117 filename, the linenumber and the error message 127 filename, the linenumber and the error message
118 (boolean, string, string, string). The values are only 128 (boolean, string, string, string). The values are only
119 valid, if the status is True. 129 valid, if the status is True.
120 """ 130 """
131 return __check(file, text)
132
133
134 def batchCheck(argumentsList, send, fx, cancelled):
135 """
136 Module function to check a batch of files for whitespace related problems.
137
138 @param argumentsList list of arguments tuples as given for check
139 @param send reference to send function (function)
140 @param fx registered service name (string)
141 @param cancelled reference to function checking for a cancellation
142 (function)
143 """
144 try:
145 NumberOfProcesses = multiprocessing.cpu_count()
146 if NumberOfProcesses >= 1:
147 NumberOfProcesses -= 1
148 except NotImplementedError:
149 NumberOfProcesses = 1
150
151 # Create queues
152 taskQueue = multiprocessing.Queue()
153 doneQueue = multiprocessing.Queue()
154
155 # Submit tasks (initially two time number of processes
156 initialTasks = 2 * NumberOfProcesses
157 for task in argumentsList[:initialTasks]:
158 taskQueue.put(task)
159
160 # Start worker processes
161 for i in range(NumberOfProcesses):
162 multiprocessing.Process(target=worker, args=(taskQueue, doneQueue))\
163 .start()
164
165 # Get and send results
166 endIndex = len(argumentsList) - initialTasks
167 for i in range(len(argumentsList)):
168 filename, result = doneQueue.get()
169 send(fx, filename, result)
170 if cancelled():
171 # just exit the loop ignoring the results of queued tasks
172 break
173 if i < endIndex:
174 taskQueue.put(argumentsList[i + initialTasks])
175
176 # Tell child processes to stop
177 for i in range(NumberOfProcesses):
178 taskQueue.put('STOP')
179
180
181 def worker(input, output):
182 """
183 Module function acting as the parallel worker for the style check.
184
185 @param input input queue (multiprocessing.Queue)
186 @param output output queue (multiprocessing.Queue)
187 """
188 for filename, source in iter(input.get, 'STOP'):
189 result = __check(filename, source)
190 output.put((filename, result))
191
192
193 def __check(file, text=""):
194 """
195 Private function to check one Python source file for whitespace related
196 problems.
197
198 @param file source filename (string)
199 @param text source text (string)
200 @return A tuple indicating status (True = an error was found), the
201 filename, the linenumber and the error message
202 (boolean, string, string). The values are only
203 valid, if the status is True.
204 """
121 global indents, check_equal 205 global indents, check_equal
122 indents = [Whitespace("")] 206 indents = [Whitespace("")]
123 check_equal = 0 207 check_equal = 0
124 if not text: 208 if not text:
125 return (True, "1", "Error: source code missing.") 209 return (True, "1", "Error: source code missing.")
141 return (True, str(badline), line) 225 return (True, str(badline), line)
142 226
143 except Exception as err: 227 except Exception as err:
144 return (True, "1", "Unspecific Error: {0}".format(str(err))) 228 return (True, "1", "Unspecific Error: {0}".format(str(err)))
145 229
146 return (False, None, None) 230 return (False, "", "")
147 231
148 232
149 class Whitespace(object): 233 class Whitespace(object):
150 """ 234 """
151 Class implementing the whitespace checker. 235 Class implementing the whitespace checker.

eric ide

mercurial