RadonMetrics/CodeMetricsCalculator.py

branch
eric7
changeset 94
725eaca7bc4b
parent 90
1405e41edc0b
child 104
6eac83394939
equal deleted inserted replaced
93:1ae73306422a 94:725eaca7bc4b
12 12
13 13
14 def initService(): 14 def initService():
15 """ 15 """
16 Initialize the service and return the entry point. 16 Initialize the service and return the entry point.
17 17
18 @return the entry point for the background client (function) 18 @return the entry point for the background client (function)
19 """ 19 """
20 return rawCodeMetrics 20 return rawCodeMetrics
21 21
22 22
23 def initBatchService(): 23 def initBatchService():
24 """ 24 """
25 Initialize the batch service and return the entry point. 25 Initialize the batch service and return the entry point.
26 26
27 @return the entry point for the background client (function) 27 @return the entry point for the background client (function)
28 """ 28 """
29 return batchRawCodeMetrics 29 return batchRawCodeMetrics
30 30
31 31
32 def rawCodeMetrics(file, text=""): 32 def rawCodeMetrics(file, text=""):
33 """ 33 """
34 Private function to calculate the raw code metrics of one file. 34 Private function to calculate the raw code metrics of one file.
35 35
36 @param file source filename 36 @param file source filename
37 @type str 37 @type str
38 @param text source text 38 @param text source text
39 @type str 39 @type str
40 @return tuple containing the result dictionary 40 @return tuple containing the result dictionary
44 44
45 45
46 def batchRawCodeMetrics(argumentsList, send, fx, cancelled, maxProcesses=0): 46 def batchRawCodeMetrics(argumentsList, send, fx, cancelled, maxProcesses=0):
47 """ 47 """
48 Module function to calculate the raw code metrics for a batch of files. 48 Module function to calculate the raw code metrics for a batch of files.
49 49
50 @param argumentsList list of arguments tuples as given for rawCodeMetrics 50 @param argumentsList list of arguments tuples as given for rawCodeMetrics
51 @type list 51 @type list
52 @param send reference to send function 52 @param send reference to send function
53 @type function 53 @type function
54 @param fx registered service name 54 @param fx registered service name
78 for task in argumentsList[:initialTasks]: 78 for task in argumentsList[:initialTasks]:
79 taskQueue.put(task) 79 taskQueue.put(task)
80 80
81 # Start worker processes 81 # Start worker processes
82 workers = [ 82 workers = [
83 multiprocessing.Process( 83 multiprocessing.Process(target=workerTask, args=(taskQueue, doneQueue))
84 target=workerTask, args=(taskQueue, doneQueue) 84 for _ in range(NumberOfProcesses)
85 ) for _ in range(NumberOfProcesses)
86 ] 85 ]
87 for worker in workers: 86 for worker in workers:
88 worker.start() 87 worker.start()
89 88
90 # Get and send results 89 # Get and send results
91 endIndex = len(argumentsList) - initialTasks 90 endIndex = len(argumentsList) - initialTasks
92 for i in range(len(argumentsList)): 91 for i in range(len(argumentsList)):
93 resultSent = False 92 resultSent = False
94 wasCancelled = False 93 wasCancelled = False
95 94
96 while not resultSent: 95 while not resultSent:
97 try: 96 try:
98 # get result (waiting max. 3 seconds and send it to frontend 97 # get result (waiting max. 3 seconds and send it to frontend
99 filename, result = doneQueue.get() 98 filename, result = doneQueue.get()
100 send(fx, filename, result) 99 send(fx, filename, result)
102 except queue.Empty: 101 except queue.Empty:
103 # ignore empty queue, just carry on 102 # ignore empty queue, just carry on
104 if cancelled(): 103 if cancelled():
105 wasCancelled = True 104 wasCancelled = True
106 break 105 break
107 106
108 if wasCancelled or cancelled(): 107 if wasCancelled or cancelled():
109 # just exit the loop ignoring the results of queued tasks 108 # just exit the loop ignoring the results of queued tasks
110 break 109 break
111 110
112 if i < endIndex: 111 if i < endIndex:
113 taskQueue.put(argumentsList[i + initialTasks]) 112 taskQueue.put(argumentsList[i + initialTasks])
114 113
115 # Tell child processes to stop 114 # Tell child processes to stop
116 for _ in range(NumberOfProcesses): 115 for _ in range(NumberOfProcesses):
117 taskQueue.put('STOP') 116 taskQueue.put("STOP")
118 117
119 for worker in workers: 118 for worker in workers:
120 worker.join() 119 worker.join()
121 worker.close() 120 worker.close()
122 121
123 122
124 def workerTask(inputQueue, outputQueue): 123 def workerTask(inputQueue, outputQueue):
125 """ 124 """
126 Module function acting as the parallel worker for the raw code metrics 125 Module function acting as the parallel worker for the raw code metrics
127 calculation. 126 calculation.
128 127
129 @param inputQueue input queue 128 @param inputQueue input queue
130 @type multiprocessing.Queue 129 @type multiprocessing.Queue
131 @param outputQueue output queue 130 @param outputQueue output queue
132 @type multiprocessing.Queue 131 @type multiprocessing.Queue
133 """ 132 """
134 for filename, source in iter(inputQueue.get, 'STOP'): 133 for filename, source in iter(inputQueue.get, "STOP"):
135 result = __rawCodeMetrics(filename, source) 134 result = __rawCodeMetrics(filename, source)
136 outputQueue.put((filename, result)) 135 outputQueue.put((filename, result))
137 136
138 137
139 def __rawCodeMetrics(file, text=""): 138 def __rawCodeMetrics(file, text=""):
140 """ 139 """
141 Private function to calculate the raw code metrics for one Python file. 140 Private function to calculate the raw code metrics for one Python file.
142 141
143 @param file source filename 142 @param file source filename
144 @type str 143 @type str
145 @param text source text 144 @param text source text
146 @type str 145 @type str
147 @return tuple containing the result dictionary 146 @return tuple containing the result dictionary
148 @rtype (tuple of dict) 147 @rtype (tuple of dict)
149 """ 148 """
150 from radon.raw import analyze 149 from radon.raw import analyze
150
151 try: 151 try:
152 res = __raw2Dict(analyze(text)) 152 res = __raw2Dict(analyze(text))
153 except Exception as err: 153 except Exception as err:
154 res = {"error": str(err)} 154 res = {"error": str(err)}
155 return (res, ) 155 return (res,)
156 156
157 157
158 def __raw2Dict(obj): 158 def __raw2Dict(obj):
159 """ 159 """
160 Private function to convert an object holding raw analysis results into a 160 Private function to convert an object holding raw analysis results into a
161 dictionary. 161 dictionary.
162 162
163 @param obj object as returned from analyze() 163 @param obj object as returned from analyze()
164 @type radon.raw.Module 164 @type radon.raw.Module
165 @return conversion result 165 @return conversion result
166 @rtype dict 166 @rtype dict
167 """ 167 """

eric ide

mercurial