Sat, 19 Sep 2015 18:24:07 +0200
Implemented the cyclomatic complexity stuff.
# -*- coding: utf-8 -*- # Copyright (c) 2015 Detlev Offenbach <detlev@die-offenbachs.de> # """ Module implementing a dialog to show raw code metrics. """ from __future__ import unicode_literals try: str = unicode # __IGNORE_EXCEPTION__ __IGNORE_WARNING__ except NameError: pass import os import fnmatch from PyQt5.QtCore import pyqtSlot, qVersion, Qt, QTimer, QLocale from PyQt5.QtWidgets import ( QDialog, QDialogButtonBox, QAbstractButton, QHeaderView, QTreeWidgetItem, QApplication ) from .Ui_RawMetricsDialog import Ui_RawMetricsDialog from E5Gui.E5Application import e5App import Preferences import Utilities class RawMetricsDialog(QDialog, Ui_RawMetricsDialog): """ Class implementing a dialog to show raw code metrics. """ def __init__(self, radonService, parent=None): """ Constructor @param radonService reference to the service @type RadonMetricsPlugin @param parent reference to the parent widget @type QWidget """ super(RawMetricsDialog, self).__init__(parent) self.setupUi(self) self.setWindowFlags(Qt.Window) self.buttonBox.button(QDialogButtonBox.Close).setEnabled(False) self.buttonBox.button(QDialogButtonBox.Cancel).setDefault(True) self.summaryList.headerItem().setText( self.summaryList.columnCount(), "") self.summaryList.header().resizeSection(0, 200) self.summaryList.header().resizeSection(1, 100) self.resultList.headerItem().setText(self.resultList.columnCount(), "") self.radonService = radonService self.radonService.metricsDone.connect(self.__processResult) self.radonService.error.connect(self.__processError) self.radonService.batchFinished.connect(self.__batchFinished) self.cancelled = False self.__project = e5App().getObject("Project") self.__locale = QLocale() self.__finished = True self.__errorItem = None self.__fileList = [] self.filterFrame.setVisible(False) self.explanationLabel.setText(self.tr( "<table>" "<tr><td><b>LOC</b></td>" "<td>Lines of code (LOC = SLOC + Empty)</td></tr>" "<tr><td><b>SLOC</b></td><td>Source lines of code</td></tr>" "<tr><td><b>LLOC</b></td><td>Logical lines of code</td></tr>" "<tr><td><b>Comments</b></td><td>Comment lines</td></tr>" "<tr><td><b>Multi</b></td>" "<td>Lines in multi line strings</td></tr>" "<tr><td><b>Empty</b></td><td>Blank lines</td></tr>" "<tr><td colspan=2><b>Comment Statistics:</b></td</tr>" "<tr><td><b>C % L</b></td><td>Comments to lines ratio</td></tr>" "<tr><td><b>C % S</b></td>" "<td>Comments to source lines ratio</td></tr>" "<tr><td><b>C + M % L</b></td>" "<td>Comments plus multi line strings to lines ratio</td></tr>" "</table>" )) def __resizeResultColumns(self): """ Private method to resize the list columns. """ self.resultList.header().resizeSections(QHeaderView.ResizeToContents) self.resultList.header().setStretchLastSection(True) def __createResultItem(self, filename, values): """ Private slot to create a new item in the result list. @param filename name of the file @type str @param values values to be displayed @type dict """ data = [self.__project.getRelativePath(filename)] for value in self.__getValues(values): try: data.append("{0:5}".format(int(value))) except ValueError: data.append(value) data.append("{0:3.0%}".format( values["comments"] / (float(values["loc"]) or 1))) data.append("{0:3.0%}".format( values["comments"] / (float(values["sloc"]) or 1))) data.append("{0:3.0%}".format( (values["comments"] + values["multi"]) / (float(values["loc"]) or 1))) itm = QTreeWidgetItem(self.resultList, data) for col in range(1, 10): itm.setTextAlignment(col, Qt.Alignment(Qt.AlignRight)) def __createErrorItem(self, filename, message): """ Private slot to create a new error item in the result list. @param filename name of the file @type str @param message error message @type str """ if self.__errorItem is None: self.__errorItem = QTreeWidgetItem(self.resultList, [ self.tr("Errors")]) self.__errorItem.setExpanded(True) self.__errorItem.setForeground(0, Qt.red) msg = "{0} ({1})".format(self.__project.getRelativePath(filename), message) if not self.resultList.findItems(msg, Qt.MatchExactly): itm = QTreeWidgetItem(self.__errorItem, [msg]) itm.setForeground(0, Qt.red) itm.setFirstColumnSpanned(True) def prepare(self, fileList, project): """ Public method to prepare the dialog with a list of filenames. @param fileList list of filenames @type list of str @param project reference to the project object @type Project """ self.__fileList = fileList[:] self.__project = project self.buttonBox.button(QDialogButtonBox.Close).setEnabled(True) self.buttonBox.button(QDialogButtonBox.Cancel).setEnabled(False) self.buttonBox.button(QDialogButtonBox.Close).setDefault(True) self.filterFrame.setVisible(True) self.__data = self.__project.getData( "OTHERTOOLSPARMS", "RadonCodeMetrics") if self.__data is None or "ExcludeFiles" not in self.__data: self.__data = {"ExcludeFiles": ""} self.excludeFilesEdit.setText(self.__data["ExcludeFiles"]) def start(self, fn): """ Public slot to start the code metrics determination. @param fn file or list of files or directory to show the code metrics for @type str or list of str """ self.__errorItem = None self.resultList.clear() self.cancelled = False self.buttonBox.button(QDialogButtonBox.Close).setEnabled(False) self.buttonBox.button(QDialogButtonBox.Cancel).setEnabled(True) self.buttonBox.button(QDialogButtonBox.Cancel).setDefault(True) QApplication.processEvents() if isinstance(fn, list): self.files = fn elif os.path.isdir(fn): self.files = [] extensions = set(Preferences.getPython("PythonExtensions") + Preferences.getPython("Python3Extensions")) for ext in extensions: self.files.extend( Utilities.direntries(fn, True, '*{0}'.format(ext), 0)) else: self.files = [fn] self.files.sort() # check for missing files for f in self.files[:]: if not os.path.exists(f): self.files.remove(f) self.__summary = {"files": 0} for key in ['loc', 'sloc', 'lloc', 'comments', 'multi', 'blank']: self.__summary[key] = 0 if len(self.files) > 0: # disable updates of the list for speed self.resultList.setUpdatesEnabled(False) self.resultList.setSortingEnabled(False) self.checkProgress.setMaximum(len(self.files)) self.checkProgress.setVisible(len(self.files) > 1) self.checkProgressLabel.setVisible(len(self.files) > 1) QApplication.processEvents() # now go through all the files self.progress = 0 if len(self.files) == 1 or not self.radonService.hasBatch: self.__batch = False self.rawMetrics() else: self.__batch = True self.rawMetricsBatch() def rawMetrics(self, codestring=''): """ Public method to start a code metrics calculation for one Python file. The results are reported to the __processResult slot. @keyparam codestring optional sourcestring @type str """ if not self.files: self.checkProgressLabel.setPath("") self.checkProgress.setMaximum(1) self.checkProgress.setValue(1) self.__finish() return self.filename = self.files.pop(0) self.checkProgress.setValue(self.progress) self.checkProgressLabel.setPath(self.filename) QApplication.processEvents() if self.cancelled: return try: self.source = Utilities.readEncodedFile(self.filename)[0] self.source = Utilities.normalizeCode(self.source) except (UnicodeError, IOError) as msg: self.__createErrorItem(self.filename, str(msg).rstrip()) self.progress += 1 # Continue with next file self.rawMetrics() return self.__finished = False self.radonService.rawMetrics( None, self.filename, self.source) def rawMetricsBatch(self): """ Public method to start a code metrics calculation batch job. The results are reported to the __processResult slot. """ self.__lastFileItem = None self.checkProgressLabel.setPath(self.tr("Preparing files...")) progress = 0 argumentsList = [] for filename in self.files: progress += 1 self.checkProgress.setValue(progress) QApplication.processEvents() try: source = Utilities.readEncodedFile(filename)[0] source = Utilities.normalizeCode(source) except (UnicodeError, IOError) as msg: self.__createErrorItem(filename, str(msg).rstrip()) continue argumentsList.append((filename, source)) # reset the progress bar to the checked files self.checkProgress.setValue(self.progress) QApplication.processEvents() self.__finished = False self.radonService.rawMetricsBatch(argumentsList) def __batchFinished(self, type_): """ Private slot handling the completion of a batch job. @param type_ type of the calculated metrics @type str, one of ["raw", "mi", "cc"] """ if type_ == "raw": self.checkProgressLabel.setPath("") self.checkProgress.setMaximum(1) self.checkProgress.setValue(1) self.__finish() def __processError(self, type_, fn, msg): """ Private slot to process an error indication from the service. @param type_ type of the calculated metrics @type str, one of ["raw", "mi", "cc"] @param fn filename of the file @type str @param msg error message @type str """ if type_ == "raw": self.__createErrorItem(fn, msg) def __processResult(self, fn, result): """ Private slot called after perfoming a code metrics calculation on one file. @param fn filename of the file @type str @param result result dict @type dict """ if self.__finished: return # Check if it's the requested file, otherwise ignore signal if not # in batch mode if not self.__batch and fn != self.filename: return if "error" in result: self.__createErrorItem(fn, result["error"]) else: self.__createResultItem(fn, result) self.progress += 1 self.checkProgress.setValue(self.progress) self.checkProgressLabel.setPath(self.__project.getRelativePath(fn)) QApplication.processEvents() if not self.__batch: self.rawMetrics() def __getValues(self, result): """ Private method to extract the code metric values. @param result result dict @type dict @return list of values suitable for display @rtype list of str """ v = [] for key in ['loc', 'sloc', 'lloc', 'comments', 'multi', 'blank']: val = result.get(key, 0) if val: v.append(self.__locale.toString(val)) else: v.append('') self.__summary[key] += int(val) self.__summary["files"] += 1 return v def __finish(self): """ Private slot called when the action or the user pressed the button. """ if not self.__finished: self.__finished = True # reenable updates of the list self.resultList.setSortingEnabled(True) self.resultList.setUpdatesEnabled(True) self.__createSummary() self.cancelled = True self.buttonBox.button(QDialogButtonBox.Close).setEnabled(True) self.buttonBox.button(QDialogButtonBox.Cancel).setEnabled(False) self.buttonBox.button(QDialogButtonBox.Close).setDefault(True) self.resultList.header().resizeSections( QHeaderView.ResizeToContents) self.resultList.header().setStretchLastSection(True) if qVersion() >= "5.0.0": self.resultList.header().setSectionResizeMode( QHeaderView.Interactive) else: self.resultList.header().setResizeMode(QHeaderView.Interactive) self.checkProgress.setVisible(False) self.checkProgressLabel.setVisible(False) def __createSummary(self): """ Private method to create the code metrics summary. """ self.__createSummaryItem( self.tr("Files"), self.__locale.toString(self.__summary["files"])) self.__createSummaryItem( self.tr("LOC"), self.__locale.toString(self.__summary["loc"])) self.__createSummaryItem( self.tr("SLOC"), self.__locale.toString(self.__summary["sloc"])) self.__createSummaryItem( self.tr("LLOC"), self.__locale.toString(self.__summary["lloc"])) self.__createSummaryItem( self.tr("Comments"), self.__locale.toString(self.__summary["comments"])) self.__createSummaryItem( self.tr("Multi"), self.__locale.toString(self.__summary["multi"])) self.__createSummaryItem( self.tr("Empty"), self.__locale.toString(self.__summary["blank"])) self.summaryList.header().resizeSections(QHeaderView.ResizeToContents) self.summaryList.header().setStretchLastSection(True) def __createSummaryItem(self, col0, col1): """ Private slot to create a new item in the summary list. @param col0 string for column 0 (string) @param col1 string for column 1 (string) """ itm = QTreeWidgetItem(self.summaryList, [col0, col1]) itm.setTextAlignment(1, Qt.Alignment(Qt.AlignRight)) @pyqtSlot(QAbstractButton) def on_buttonBox_clicked(self, button): """ Private slot called by a button of the button box clicked. @param button button that was clicked @type QAbstractButton """ if button == self.buttonBox.button(QDialogButtonBox.Close): self.close() elif button == self.buttonBox.button(QDialogButtonBox.Cancel): if self.__batch: self.radonService.cancelRawMetricsBatch() QTimer.singleShot(1000, self.__finish) else: self.__finish() @pyqtSlot() def on_startButton_clicked(self): """ Private slot to start a code metrics run. """ fileList = self.__fileList[:] filterString = self.excludeFilesEdit.text() if "ExcludeFiles" not in self.__data or \ filterString != self.__data["ExcludeFiles"]: self.__data["ExcludeFiles"] = filterString self.__project.setData( "OTHERTOOLSPARMS", "RadonCodeMetrics", self.__data) filterList = [f.strip() for f in filterString.split(",") if f.strip()] if filterList: for filter in filterList: fileList = \ [f for f in fileList if not fnmatch.fnmatch(f, filter)] self.start(fileList)