PluginMetricsRadon.py

changeset 10
8b1920a22df3
parent 9
7f6e04213998
child 11
de8cadbd6a41
equal deleted inserted replaced
9:7f6e04213998 10:8b1920a22df3
51 determined for a file 51 determined for a file
52 @signal maintainabilityIndexDone(str, dict) emitted when the 52 @signal maintainabilityIndexDone(str, dict) emitted when the
53 maintainability index was determined for a file 53 maintainability index was determined for a file
54 @signal complexityDone(str, dict) emitted when the 54 @signal complexityDone(str, dict) emitted when the
55 cyclomatic complexity was determined for a file 55 cyclomatic complexity was determined for a file
56 @signal error(str, str) emitted in case of an error 56 @signal error(str, str, str) emitted in case of an error
57 @signal batchFinished() emitted when a code metrics batch is done 57 @signal batchFinished(str) emitted when a code metrics batch is done
58 """ 58 """
59 metricsDone = pyqtSignal(str, dict) 59 metricsDone = pyqtSignal(str, dict)
60 maintainabilityIndexDone = pyqtSignal(str, dict) 60 maintainabilityIndexDone = pyqtSignal(str, dict)
61 complexityDone = pyqtSignal(str, dict) 61 complexityDone = pyqtSignal(str, dict)
62 error = pyqtSignal(str, str) 62 error = pyqtSignal(str, str, str)
63 batchFinished = pyqtSignal() 63 batchFinished = pyqtSignal(str)
64 64
65 def __init__(self, ui): 65 def __init__(self, ui):
66 """ 66 """
67 Constructor 67 Constructor
68 68
75 75
76 self.backgroundService = e5App().getObject("BackgroundService") 76 self.backgroundService = e5App().getObject("BackgroundService")
77 77
78 path = os.path.join(os.path.dirname(__file__), packageName) 78 path = os.path.join(os.path.dirname(__file__), packageName)
79 try: 79 try:
80 # raw code metrics calculation
80 self.backgroundService.serviceConnect( 81 self.backgroundService.serviceConnect(
81 'radon', 'Python2', path, 'CodeMetricsCalculator', 82 'radon_raw', 'Python2', path, 'CodeMetricsCalculator',
82 self.metricsCalculationDone, 83 lambda fn, res: self.metricsCalculationDone("raw", fn, res),
83 onErrorCallback=self.serviceErrorPy2, 84 onErrorCallback=lambda fx, lang, fn, msg: self.serviceErrorPy2(
84 onBatchDone=self.batchJobDone) 85 "raw", fx, lang, fn, msg),
86 onBatchDone=lambda fx, lang: self.batchJobDone(
87 "raw", fx, lang))
85 self.backgroundService.serviceConnect( 88 self.backgroundService.serviceConnect(
86 'radon', 'Python3', path, 'CodeMetricsCalculator', 89 'radon_raw', 'Python3', path, 'CodeMetricsCalculator',
87 self.metricsCalculationDone, 90 lambda fn, res: self.metricsCalculationDone("raw", fn, res),
88 onErrorCallback=self.serviceErrorPy3, 91 onErrorCallback=lambda fx, lang, fn, msg: self.serviceErrorPy3(
89 onBatchDone=self.batchJobDone) 92 "raw", fx, lang, fn, msg),
93 onBatchDone=lambda fx, lang: self.batchJobDone(
94 "raw", fx, lang))
95
96 # maintainability index calculation
97 self.backgroundService.serviceConnect(
98 'radon_mi', 'Python2', path, 'MaintainabilityIndexCalculator',
99 lambda fn, res: self.metricsCalculationDone("mi", fn, res),
100 onErrorCallback=lambda fx, lang, fn, msg: self.serviceErrorPy2(
101 "mi", fx, lang, fn, msg),
102 onBatchDone=lambda fx, lang: self.batchJobDone(
103 "mi", fx, lang))
104 self.backgroundService.serviceConnect(
105 'radon_mi', 'Python3', path, 'MaintainabilityIndexCalculator',
106 lambda fn, res: self.metricsCalculationDone("mi", fn, res),
107 onErrorCallback=lambda fx, lang, fn, msg: self.serviceErrorPy3(
108 "mi", fx, lang, fn, msg),
109 onBatchDone=lambda fx, lang: self.batchJobDone(
110 "mi", fx, lang))
111
90 self.hasBatch = True 112 self.hasBatch = True
91 except TypeError: 113 except TypeError:
114 # backward compatibility for eric 6.0
115 # raw code metrics calculation
92 self.backgroundService.serviceConnect( 116 self.backgroundService.serviceConnect(
93 'radon', 'Python2', path, 'CodeMetricsCalculator', 117 'radon_raw', 'Python2', path, 'CodeMetricsCalculator',
94 self.metricsCalculationDone, 118 lambda fn, res: self.metricsCalculationDone("raw", fn, res),
95 onErrorCallback=self.serviceErrorPy2) 119 onErrorCallback=lambda fx, lang, fn, msg: self.serviceErrorPy2(
120 "raw", fx, lang, fn, msg))
96 self.backgroundService.serviceConnect( 121 self.backgroundService.serviceConnect(
97 'radon', 'Python3', path, 'CodeMetricsCalculator', 122 'radon_raw', 'Python3', path, 'CodeMetricsCalculator',
98 self.metricsCalculationDone, 123 lambda fn, res: self.metricsCalculationDone("raw", fn, res),
99 onErrorCallback=self.serviceErrorPy3) 124 onErrorCallback=lambda fx, lang, fn, msg: self.serviceErrorPy3(
125 "raw", fx, lang, fn, msg))
126
127 # maintainability index calculation
128 self.backgroundService.serviceConnect(
129 'radon_mi', 'Python2', path, 'MaintainabilityIndexCalculator',
130 lambda fn, res: self.metricsCalculationDone("mi", fn, res),
131 onErrorCallback=lambda fx, lang, fn, msg: self.serviceErrorPy2(
132 "mi", fx, lang, fn, msg))
133 self.backgroundService.serviceConnect(
134 'radon_mi', 'Python3', path, 'MaintainabilityIndexCalculator',
135 lambda fn, res: self.metricsCalculationDone("mi", fn, res),
136 onErrorCallback=lambda fx, lang, fn, msg: self.serviceErrorPy3(
137 "mi", fx, lang, fn, msg))
138
100 self.hasBatch = False 139 self.hasBatch = False
101 140
102 self.queuedBatches = [] 141 self.queuedBatches = {
103 self.batchesFinished = True 142 "raw": [],
143 "mi": [],
144 "cc": [],
145 }
146 self.batchesFinished = {
147 "raw": True,
148 "mi": True,
149 "cc": True,
150 }
104 151
105 self.__translator = None 152 self.__translator = None
106 self.__loadTranslator() 153 self.__loadTranslator()
107 154
108 def __serviceError(self, fn, msg): 155 def __serviceError(self, type_, fn, msg):
109 """ 156 """
110 Private slot handling service errors. 157 Private slot handling service errors.
111 158
159 @param type_ type of the calculated metrics
160 @type str, one of ["raw", "mi", "cc"]
112 @param fn file name 161 @param fn file name
113 @type str 162 @type str
114 @param msg message text 163 @param msg message text
115 @type str 164 @type str
116 """ 165 """
117 self.error.emit(fn, msg) 166 self.error.emit(type_, fn, msg)
118 167
119 def serviceErrorPy2(self, fx, lang, fn, msg): 168 def serviceErrorPy2(self, type_, fx, lang, fn, msg):
120 """ 169 """
121 Public slot handling service errors for Python 2. 170 Public slot handling service errors for Python 2.
122 171
172 @param type_ type of the calculated metrics
173 @type str, one of ["raw", "mi", "cc"]
123 @param fx service name 174 @param fx service name
124 @type str 175 @type str
125 @param lang language 176 @param lang language
126 @type str 177 @type str
127 @param fn file name 178 @param fn file name
128 @type str 179 @type str
129 @param msg message text 180 @param msg message text
130 @type str 181 @type str
131 """ 182 """
132 if fx in ['radon', 'batch_radon'] and lang == 'Python2': 183 if fx in ['radon_' + type_, 'batch_radon_' + type_] and \
133 if fx == 'radon': 184 lang == 'Python2':
134 self.__serviceError(fn, msg) 185 if fx == 'radon_' + type_:
186 self.__serviceError(type_, fn, msg)
135 else: 187 else:
136 self.__serviceError(self.tr("Python 2 batch job"), msg) 188 self.__serviceError(type_, self.tr("Python 2 batch job"), msg)
137 self.batchJobDone(fx, lang) 189 self.batchJobDone(type_, fx, lang)
138 190
139 def serviceErrorPy3(self, fx, lang, fn, msg): 191 def serviceErrorPy3(self, type_, fx, lang, fn, msg):
140 """ 192 """
141 Public slot handling service errors for Python 3. 193 Public slot handling service errors for Python 3.
142 194
195 @param type_ type of the calculated metrics
196 @type str, one of ["raw", "mi", "cc"]
143 @param fx service name 197 @param fx service name
144 @type str 198 @type str
145 @param lang language 199 @param lang language
146 @type str 200 @type str
147 @param fn file name 201 @param fn file name
148 @type str 202 @type str
149 @param msg message text 203 @param msg message text
150 @type str 204 @type str
151 """ 205 """
152 if fx in ['radon', 'batch_radon'] and lang == 'Python3': 206 if fx in ['radon_' + type_, 'batch_radon_' + type_] and \
153 if fx == 'radon': 207 lang == 'Python3':
154 self.__serviceError(fn, msg) 208 if fx == 'radon_' + type_:
209 self.__serviceError(type_, fn, msg)
155 else: 210 else:
156 self.__serviceError(self.tr("Python 3 batch job"), msg) 211 self.__serviceError(type_, self.tr("Python 3 batch job"), msg)
157 self.batchJobDone(fx, lang) 212 self.batchJobDone(type_, fx, lang)
158 213
159 def batchJobDone(self, fx, lang): 214 def batchJobDone(self, type_, fx, lang):
160 """ 215 """
161 Public slot handling the completion of a batch job. 216 Public slot handling the completion of a batch job.
162 217
218 @param type_ type of the calculated metrics
219 @type str, one of ["raw", "mi", "cc"]
163 @param fx service name 220 @param fx service name
164 @type str 221 @type str
165 @param lang language 222 @param lang language
166 @type str 223 @type str
167 """ 224 """
168 if fx in ['radon', 'batch_radon']: 225 if fx in ['radon_' + type_, 'batch_radon_' + type_]:
169 if lang in self.queuedBatches: 226 if lang in self.queuedBatches[type_]:
170 self.queuedBatches.remove(lang) 227 self.queuedBatches[type_].remove(lang)
171 # prevent sending the signal multiple times 228 # prevent sending the signal multiple times
172 if len(self.queuedBatches) == 0 and not self.batchesFinished: 229 if len(self.queuedBatches[type_]) == 0 and \
173 self.batchFinished.emit() 230 not self.batchesFinished[type_]:
174 self.batchesFinished = True 231 self.batchFinished.emit(type_)
175 232 self.batchesFinished[type_] = True
176 def metricsCalculationDone(self, filename, metricsType, result): 233
234 def metricsCalculationDone(self, type_, filename, result):
177 """ 235 """
178 Public slot to dispatch the result. 236 Public slot to dispatch the result.
179 237
238 @param type_ type of the calculated metrics
239 @type str, one of ["raw", "mi", "cc"]
180 @param filename name of the file the results belong to 240 @param filename name of the file the results belong to
181 @type str 241 @type str
182 @param metricsType type of the calculated metrics
183 @type str, one of ["raw", "mi", "cc"]
184 @param result result dictionary 242 @param result result dictionary
185 @type dict 243 @type dict
186 """ 244 """
187 if metricsType == "raw": 245 if type_ == "raw":
188 self.metricsDone.emit(filename, result) 246 self.metricsDone.emit(filename, result)
189 elif metricsType == "mi": 247 elif type_ == "mi":
190 self.maintainabilityIndexDone.emit(filename, result) 248 self.maintainabilityIndexDone.emit(filename, result)
191 elif metricsType == "cc": 249 elif type_ == "cc":
192 self.complexityDone.emit(filename, result) 250 self.complexityDone.emit(filename, result)
193 else: 251 else:
194 self.error.emit( 252 self.error.emit(
253 type_,
195 filename, 254 filename,
196 self.tr("Unknown metrics result received ({0}).").format( 255 self.tr("Unknown metrics result received ({0}).").format(
197 metricsType) 256 type_)
198 ) 257 )
199 258
200 def __initialize(self): 259 def __initialize(self):
201 """ 260 """
202 Private slot to (re)initialize the plugin. 261 Private slot to (re)initialize the plugin.
238 lang = 'Python{0}'.format(determinePythonVersion(filename, source)) 297 lang = 'Python{0}'.format(determinePythonVersion(filename, source))
239 if lang not in ['Python2', 'Python3']: 298 if lang not in ['Python2', 'Python3']:
240 return 299 return
241 300
242 self.backgroundService.enqueueRequest( 301 self.backgroundService.enqueueRequest(
243 'radon', lang, filename, [source, 'raw']) 302 'radon_raw', lang, filename, [source])
244 303
245 def rawMetricsBatch(self, argumentsList): 304 def rawMetricsBatch(self, argumentsList):
246 """ 305 """
247 Public method to prepare raw code metrics calculation on multiple 306 Public method to prepare raw code metrics calculation on multiple
248 Python source files. 307 Python source files.
258 for filename, source in argumentsList: 317 for filename, source in argumentsList:
259 lang = 'Python{0}'.format(determinePythonVersion(filename, source)) 318 lang = 'Python{0}'.format(determinePythonVersion(filename, source))
260 if lang not in ['Python2', 'Python3']: 319 if lang not in ['Python2', 'Python3']:
261 continue 320 continue
262 else: 321 else:
263 data[lang].append((filename, source, 'raw')) 322 data[lang].append((filename, source))
264 323
265 self.queuedBatches = [] 324 self.queuedBatches["raw"] = []
266 for lang in ['Python2', 'Python3']: 325 for lang in ['Python2', 'Python3']:
267 if data[lang]: 326 if data[lang]:
268 self.queuedBatches.append(lang) 327 self.queuedBatches["raw"].append(lang)
269 self.backgroundService.enqueueRequest('batch_radon', lang, "", 328 self.backgroundService.enqueueRequest('batch_radon_raw', lang,
270 data[lang]) 329 "", data[lang])
271 self.batchesFinished = False 330 self.batchesFinished["raw"] = False
272 331
273 def cancelRawMetricsBatch(self): 332 def cancelRawMetricsBatch(self):
274 """ 333 """
275 Public method to cancel all batch jobs. 334 Public method to cancel all batch jobs.
276 """ 335 """
277 for lang in ['Python2', 'Python3']: 336 for lang in ['Python2', 'Python3']:
278 self.backgroundService.requestCancel('batch_radon', lang) 337 self.backgroundService.requestCancel('batch_radon_raw', lang)
279 338
280 def maintainabilityIndex(self, lang, filename, source): 339 def maintainabilityIndex(self, lang, filename, source):
281 """ 340 """
282 Public method to prepare maintainability index calculation on one 341 Public method to prepare maintainability index calculation on one
283 Python source file. 342 Python source file.
294 lang = 'Python{0}'.format(determinePythonVersion(filename, source)) 353 lang = 'Python{0}'.format(determinePythonVersion(filename, source))
295 if lang not in ['Python2', 'Python3']: 354 if lang not in ['Python2', 'Python3']:
296 return 355 return
297 356
298 self.backgroundService.enqueueRequest( 357 self.backgroundService.enqueueRequest(
299 'radon', lang, filename, [source, 'mi']) 358 'radon_mi', lang, filename, [source])
300 359
301 def maintainabilityIndexBatch(self, argumentsList): 360 def maintainabilityIndexBatch(self, argumentsList):
302 """ 361 """
303 Public method to prepare maintainability index calculation on multiple 362 Public method to prepare maintainability index calculation on multiple
304 Python source files. 363 Python source files.
314 for filename, source in argumentsList: 373 for filename, source in argumentsList:
315 lang = 'Python{0}'.format(determinePythonVersion(filename, source)) 374 lang = 'Python{0}'.format(determinePythonVersion(filename, source))
316 if lang not in ['Python2', 'Python3']: 375 if lang not in ['Python2', 'Python3']:
317 continue 376 continue
318 else: 377 else:
319 data[lang].append((filename, source, 'mi')) 378 data[lang].append((filename, source))
320 379
321 self.queuedBatches = [] 380 self.queuedBatches["mi"] = []
322 for lang in ['Python2', 'Python3']: 381 for lang in ['Python2', 'Python3']:
323 if data[lang]: 382 if data[lang]:
324 self.queuedBatches.append(lang) 383 self.queuedBatches["mi"].append(lang)
325 self.backgroundService.enqueueRequest('batch_radon', lang, "", 384 self.backgroundService.enqueueRequest('batch_radon_mi', lang,
326 data[lang]) 385 "", data[lang])
327 self.batchesFinished = False 386 self.batchesFinished["mi"] = False
328 387
329 def cancelMaintainabilityIndexBatch(self): 388 def cancelMaintainabilityIndexBatch(self):
330 """ 389 """
331 Public method to cancel all batch jobs. 390 Public method to cancel all batch jobs.
332 """ 391 """
333 for lang in ['Python2', 'Python3']: 392 for lang in ['Python2', 'Python3']:
334 self.backgroundService.requestCancel('batch_radon', lang) 393 self.backgroundService.requestCancel('batch_radon_mi', lang)
335 394
336 def activate(self): 395 def activate(self):
337 """ 396 """
338 Public method to activate this plug-in. 397 Public method to activate this plug-in.
339 398

eric ide

mercurial