diff -r 1ae73306422a -r 725eaca7bc4b RadonMetrics/CyclomaticComplexityDialog.py --- a/RadonMetrics/CyclomaticComplexityDialog.py Mon Sep 19 17:43:37 2022 +0200 +++ b/RadonMetrics/CyclomaticComplexityDialog.py Mon Sep 19 17:54:33 2022 +0200 @@ -14,8 +14,13 @@ from PyQt6.QtCore import pyqtSlot, Qt, QTimer, QLocale from PyQt6.QtGui import QColor from PyQt6.QtWidgets import ( - QDialog, QDialogButtonBox, QAbstractButton, QHeaderView, QTreeWidgetItem, - QApplication, QMenu + QDialog, + QDialogButtonBox, + QAbstractButton, + QHeaderView, + QTreeWidgetItem, + QApplication, + QMenu, ) from .Ui_CyclomaticComplexityDialog import Ui_CyclomaticComplexityDialog @@ -30,13 +35,14 @@ Class implementing a dialog to show the cyclomatic complexity (McCabe complexity). """ + FilePathRole = Qt.ItemDataRole.UserRole + 1 LineNumberRole = Qt.ItemDataRole.UserRole + 2 - + def __init__(self, radonService, isSingle=False, parent=None): """ Constructor - + @param radonService reference to the service @type RadonMetricsPlugin @param isSingle flag indicating a single file dialog @@ -47,70 +53,73 @@ super().__init__(parent) self.setupUi(self) self.setWindowFlags(Qt.WindowType.Window) - - self.buttonBox.button( - QDialogButtonBox.StandardButton.Close).setEnabled(False) - self.buttonBox.button( - QDialogButtonBox.StandardButton.Cancel).setDefault(True) - + + self.buttonBox.button(QDialogButtonBox.StandardButton.Close).setEnabled(False) + self.buttonBox.button(QDialogButtonBox.StandardButton.Cancel).setDefault(True) + self.resultList.headerItem().setText(self.resultList.columnCount(), "") - + self.rankComboBox.addItems(["A", "B", "C", "D", "E", "F"]) self.rankComboBox.setCurrentIndex(self.rankComboBox.findText("D")) self.__minimumRank = "D" - + self.radonService = radonService self.radonService.complexityDone.connect(self.__processResult) self.radonService.error.connect(self.__processError) self.radonService.batchFinished.connect(self.__batchFinished) - + self.__isSingle = isSingle self.cancelled = False - + self.__project = ericApp().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 colspan=3><b>Ranking:</b></td></tr>" - "<tr><td><b>A</b></td><td>1 - 5</td>" - "<td>(low risk - simple block)</td></tr>" - "<tr><td><b>B</b></td><td>6 - 10</td>" - "<td>(low risk - well structured and stable block)</td></tr>" - "<tr><td><b>C</b></td><td>11 - 20</td>" - "<td>(moderate risk - slightly complex block)</td></tr>" - "<tr><td><b>D</b></td><td>21 - 30</td>" - "<td>(more than moderate risk - more complex block)</td></tr>" - "<tr><td><b>E</b></td><td>31 - 40</td>" - "<td>(high risk - complex block, alarming)</td></tr>" - "<tr><td><b>F</b></td><td>> 40</td>" - "<td>(very high risk - error-prone, unstable block)</td></tr>" - "</table>" - )) - self.typeLabel.setText(self.tr( - "<table>" - "<tr><td colspan=2><b>Type:</b></td></tr>" - "<tr><td><b>C</b></td><td>Class</td></tr>" - "<tr><td><b>F</b></td><td>Function</td></tr>" - "<tr><td><b>M</b></td><td>Method</td></tr>" - "</table>" - )) - + + self.explanationLabel.setText( + self.tr( + "<table>" + "<tr><td colspan=3><b>Ranking:</b></td></tr>" + "<tr><td><b>A</b></td><td>1 - 5</td>" + "<td>(low risk - simple block)</td></tr>" + "<tr><td><b>B</b></td><td>6 - 10</td>" + "<td>(low risk - well structured and stable block)</td></tr>" + "<tr><td><b>C</b></td><td>11 - 20</td>" + "<td>(moderate risk - slightly complex block)</td></tr>" + "<tr><td><b>D</b></td><td>21 - 30</td>" + "<td>(more than moderate risk - more complex block)</td></tr>" + "<tr><td><b>E</b></td><td>31 - 40</td>" + "<td>(high risk - complex block, alarming)</td></tr>" + "<tr><td><b>F</b></td><td>> 40</td>" + "<td>(very high risk - error-prone, unstable block)</td></tr>" + "</table>" + ) + ) + self.typeLabel.setText( + self.tr( + "<table>" + "<tr><td colspan=2><b>Type:</b></td></tr>" + "<tr><td><b>C</b></td><td>Class</td></tr>" + "<tr><td><b>F</b></td><td>Function</td></tr>" + "<tr><td><b>M</b></td><td>Method</td></tr>" + "</table>" + ) + ) + self.__mappedType = { "class": "C", "function": "F", "method": "M", } - + try: usesDarkPalette = ericApp().usesDarkPalette() except AttributeError: from PyQt6.QtGui import QPalette + palette = ericApp().palette() lightness = palette.color(QPalette.Window).lightness() usesDarkPalette = lightness <= 128 @@ -142,56 +151,54 @@ "E": QColor("#ff0000"), "F": QColor("#ff0000"), } - + self.__menu = QMenu(self) - self.__menu.addAction(self.tr("Collapse all"), - self.__resultCollapse) + self.__menu.addAction(self.tr("Collapse all"), self.__resultCollapse) self.__menu.addAction(self.tr("Expand all"), self.__resultExpand) - self.resultList.setContextMenuPolicy( - Qt.ContextMenuPolicy.CustomContextMenu) - self.resultList.customContextMenuRequested.connect( - self.__showContextMenu) - + self.resultList.setContextMenuPolicy(Qt.ContextMenuPolicy.CustomContextMenu) + self.resultList.customContextMenuRequested.connect(self.__showContextMenu) + def __resizeResultColumns(self): """ Private method to resize the list columns. """ - self.resultList.header().resizeSections( - QHeaderView.ResizeMode.ResizeToContents) + self.resultList.header().resizeSections(QHeaderView.ResizeMode.ResizeToContents) self.resultList.header().setStretchLastSection(True) - + def __createFileItem(self, filename): """ Private method to create a new file item in the result list. - + @param filename name of the file @type str @return reference to the created item @rtype QTreeWidgetItem """ - itm = QTreeWidgetItem( - [self.__project.getRelativePath(filename)]) + itm = QTreeWidgetItem([self.__project.getRelativePath(filename)]) itm.setData(0, self.FilePathRole, filename) itm.setData(0, self.LineNumberRole, 1) return itm - + def __createResultItem(self, parentItem, values): """ Private slot to create a new item in the result list. - + @param parentItem reference to the parent item @type QTreeWidgetItem @param values values to be displayed @type dict """ if values["rank"] >= self.__minimumRank: - itm = QTreeWidgetItem(parentItem, [ - self.__mappedType[values["type"]], - values["fullname"], - "{0:3}".format(values["complexity"]), - values["rank"], - "{0:6}".format(values["lineno"]), - ]) + itm = QTreeWidgetItem( + parentItem, + [ + self.__mappedType[values["type"]], + values["fullname"], + "{0:3}".format(values["complexity"]), + values["rank"], + "{0:6}".format(values["lineno"]), + ], + ) itm.setTextAlignment(2, Qt.AlignmentFlag.AlignRight) itm.setTextAlignment(3, Qt.AlignmentFlag.AlignHCenter) itm.setTextAlignment(4, Qt.AlignmentFlag.AlignRight) @@ -199,44 +206,41 @@ itm.setBackground(3, self.__rankColors[values["rank"]]) if values["type"] in self.__typeColors: itm.setForeground(0, self.__typeColors[values["type"]]) - itm.setData(0, self.FilePathRole, - parentItem.data(0, self.FilePathRole)) + itm.setData(0, self.FilePathRole, parentItem.data(0, self.FilePathRole)) itm.setData(0, self.LineNumberRole, values["lineno"]) - + if "methods" in values: for method in values["methods"]: self.__createResultItem(parentItem, method) - + if "closures" in values and values["closures"]: for closure in values["closures"]: self.__createResultItem(parentItem, closure) - + 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 = QTreeWidgetItem(self.resultList, [self.tr("Errors")]) self.__errorItem.setExpanded(True) self.__errorItem.setForeground(0, Qt.GlobalColor.red) - - msg = "{0} ({1})".format(self.__project.getRelativePath(filename), - message) + + msg = "{0} ({1})".format(self.__project.getRelativePath(filename), message) if not self.resultList.findItems(msg, Qt.MatchFlag.MatchExactly): itm = QTreeWidgetItem(self.__errorItem, [msg]) itm.setForeground(0, Qt.GlobalColor.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 @@ -244,31 +248,28 @@ """ self.__fileList = fileList[:] self.__project = project - - self.buttonBox.button( - QDialogButtonBox.StandardButton.Close).setEnabled(True) - self.buttonBox.button( - QDialogButtonBox.StandardButton.Cancel).setEnabled(False) - self.buttonBox.button( - QDialogButtonBox.StandardButton.Close).setDefault(True) - + + self.buttonBox.button(QDialogButtonBox.StandardButton.Close).setEnabled(True) + self.buttonBox.button(QDialogButtonBox.StandardButton.Cancel).setEnabled(False) + self.buttonBox.button(QDialogButtonBox.StandardButton.Close).setDefault(True) + self.filterFrame.setVisible(True) - - self.__data = self.__project.getData( - "OTHERTOOLSPARMS", "RadonCodeMetrics") + + self.__data = self.__project.getData("OTHERTOOLSPARMS", "RadonCodeMetrics") if self.__data is None or "ExcludeFiles" not in self.__data: self.__data = {"ExcludeFiles": ""} if "MinimumRank" not in self.__data: self.__data["MinimumRank"] = "D" self.excludeFilesEdit.setText(self.__data["ExcludeFiles"]) self.__minimumRank = self.__data["MinimumRank"] - self.rankComboBox.setCurrentIndex(self.rankComboBox.findText( - self.__minimumRank)) - + self.rankComboBox.setCurrentIndex( + self.rankComboBox.findText(self.__minimumRank) + ) + def start(self, fn, minRank="D"): """ Public slot to start the cyclomatic complexity determination. - + @param fn file or list of files or directory to show the cyclomatic complexity for @type str or list of str @@ -280,24 +281,20 @@ self.resultList.clear() self.summaryLabel.clear() QApplication.processEvents() - - self.buttonBox.button( - QDialogButtonBox.StandardButton.Close).setEnabled(False) - self.buttonBox.button( - QDialogButtonBox.StandardButton.Cancel).setEnabled(True) - self.buttonBox.button( - QDialogButtonBox.StandardButton.Cancel).setDefault(True) + + self.buttonBox.button(QDialogButtonBox.StandardButton.Close).setEnabled(False) + self.buttonBox.button(QDialogButtonBox.StandardButton.Cancel).setEnabled(True) + self.buttonBox.button(QDialogButtonBox.StandardButton.Cancel).setDefault(True) self.rankComboBox.setEnabled(False) QApplication.processEvents() - + if isinstance(fn, list): self.files = fn elif os.path.isdir(fn): self.files = [] extensions = set(Preferences.getPython("Python3Extensions")) for ext in extensions: - self.files.extend( - Utilities.direntries(fn, True, '*{0}'.format(ext), 0)) + self.files.extend(Utilities.direntries(fn, True, "*{0}".format(ext), 0)) else: self.files = [fn] self.files.sort() @@ -307,7 +304,7 @@ self.files.remove(f) if self.__isSingle: self.__fileList = self.files[:] - + self.__summary = { "A": 0, "B": 0, @@ -318,18 +315,18 @@ } self.__ccSum = 0 self.__ccCount = 0 - + self.__minimumRank = self.rankComboBox.currentText() - + 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) QApplication.processEvents() - + # now go through all the files self.progress = 0 if len(self.files) == 1: @@ -338,14 +335,14 @@ else: self.__batch = True self.cyclomaticComplexityBatch() - - def cyclomaticComplexity(self, codestring=''): + + def cyclomaticComplexity(self, codestring=""): """ Public method to start a cyclomatic complexity calculation for one Python file. - + The results are reported to the __processResult slot. - + @param codestring optional sourcestring @type str """ @@ -354,14 +351,14 @@ self.checkProgress.setValue(1) self.__finish() return - + self.filename = self.files.pop(0) self.checkProgress.setValue(self.progress) QApplication.processEvents() - + if self.cancelled: return - + try: self.source = Utilities.readEncodedFile(self.filename)[0] self.source = Utilities.normalizeCode(self.source) @@ -373,42 +370,41 @@ return self.__finished = False - self.radonService.cyclomaticComplexity( - None, self.filename, self.source) + self.radonService.cyclomaticComplexity(None, self.filename, self.source) def cyclomaticComplexityBatch(self): """ Public method to start a cyclomatic complexity calculation batch job. - + The results are reported to the __processResult slot. """ self.__lastFileItem = None - + argumentsList = [] for progress, filename in enumerate(self.files, start=1): self.checkProgress.setValue(progress) QApplication.processEvents() - + try: source = Utilities.readEncodedFile(filename)[0] source = Utilities.normalizeCode(source) except (UnicodeError, OSError) 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.cyclomaticComplexityBatch(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"] """ @@ -416,11 +412,11 @@ 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 @@ -430,12 +426,12 @@ """ if type_ == "cc": self.__createErrorItem(fn, msg) - + def __processResult(self, fn, result): """ Private slot called after perfoming a cyclomatic complexity calculation on one file. - + @param fn filename of the file @type str @param result result dict @@ -443,14 +439,14 @@ """ 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 - + QApplication.processEvents() - + if "error" in result: self.__createErrorItem(fn, result["error"]) else: @@ -462,105 +458,108 @@ self.resultList.addTopLevelItem(fitm) fitm.setExpanded(True) fitm.setFirstColumnSpanned(True) - + self.__ccCount += result["count"] self.__ccSum += result["total_cc"] for rank in result["summary"]: self.__summary[rank] += result["summary"][rank] - + self.progress += 1 - + self.checkProgress.setValue(self.progress) QApplication.processEvents() - + if not self.__batch: self.cyclomaticComplexity() - + def __finish(self): """ Private slot called when the action or the user pressed the button. """ from radon.complexity import cc_rank - + if not self.__finished: self.__finished = True - + # re-enable updates of the list self.resultList.setSortingEnabled(True) self.resultList.sortItems(0, Qt.SortOrder.AscendingOrder) self.resultList.sortItems(1, Qt.SortOrder.AscendingOrder) self.resultList.setUpdatesEnabled(True) - + self.cancelled = True - self.buttonBox.button( - QDialogButtonBox.StandardButton.Close).setEnabled(True) - self.buttonBox.button( - QDialogButtonBox.StandardButton.Cancel).setEnabled(False) - self.buttonBox.button( - QDialogButtonBox.StandardButton.Close).setDefault(True) + self.buttonBox.button(QDialogButtonBox.StandardButton.Close).setEnabled( + True + ) + self.buttonBox.button(QDialogButtonBox.StandardButton.Cancel).setEnabled( + False + ) + self.buttonBox.button(QDialogButtonBox.StandardButton.Close).setDefault( + True + ) self.rankComboBox.setEnabled(True) - + self.resultList.header().resizeSections( - QHeaderView.ResizeMode.ResizeToContents) + QHeaderView.ResizeMode.ResizeToContents + ) self.resultList.header().setStretchLastSection(True) self.resultList.header().setSectionResizeMode( - QHeaderView.ResizeMode.Interactive) - + QHeaderView.ResizeMode.Interactive + ) + averageCC = float(self.__ccSum) / (self.__ccCount or 1) - - self.summaryLabel.setText(self.tr( - "<b>Summary:</b><br/>" - "{0} blocks (classes, functions, methods) analyzed.<br/>" - "Average complexity: {7} ({8})" - "<table>" - "<tr><td width=30><b>A</b></td>" - "<td align='right'>{1} blocks</td></tr>" - "<tr><td width=30><b>B</b></td>" - "<td align='right'>{2} blocks</td></tr>" - "<tr><td width=30><b>C</b></td>" - "<td align='right'>{3} blocks</td></tr>" - "<tr><td width=30><b>D</b></td>" - "<td align='right'>{4} blocks</td></tr>" - "<tr><td width=30><b>E</b></td>" - "<td align='right'>{5} blocks</td></tr>" - "<tr><td width=30><b>F</b></td>" - "<td align='right'>{6} blocks</td></tr>" - "</table>" - ).format( - self.__locale.toString(self.__ccCount), - self.__locale.toString(self.__summary["A"]), - self.__locale.toString(self.__summary["B"]), - self.__locale.toString(self.__summary["C"]), - self.__locale.toString(self.__summary["D"]), - self.__locale.toString(self.__summary["E"]), - self.__locale.toString(self.__summary["F"]), - cc_rank(averageCC), - self.__locale.toString(averageCC, "f", 1) - )) - + + self.summaryLabel.setText( + self.tr( + "<b>Summary:</b><br/>" + "{0} blocks (classes, functions, methods) analyzed.<br/>" + "Average complexity: {7} ({8})" + "<table>" + "<tr><td width=30><b>A</b></td>" + "<td align='right'>{1} blocks</td></tr>" + "<tr><td width=30><b>B</b></td>" + "<td align='right'>{2} blocks</td></tr>" + "<tr><td width=30><b>C</b></td>" + "<td align='right'>{3} blocks</td></tr>" + "<tr><td width=30><b>D</b></td>" + "<td align='right'>{4} blocks</td></tr>" + "<tr><td width=30><b>E</b></td>" + "<td align='right'>{5} blocks</td></tr>" + "<tr><td width=30><b>F</b></td>" + "<td align='right'>{6} blocks</td></tr>" + "</table>" + ).format( + self.__locale.toString(self.__ccCount), + self.__locale.toString(self.__summary["A"]), + self.__locale.toString(self.__summary["B"]), + self.__locale.toString(self.__summary["C"]), + self.__locale.toString(self.__summary["D"]), + self.__locale.toString(self.__summary["E"]), + self.__locale.toString(self.__summary["F"]), + cc_rank(averageCC), + self.__locale.toString(averageCC, "f", 1), + ) + ) + self.checkProgress.setVisible(False) - + @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.StandardButton.Close - ): + if button == self.buttonBox.button(QDialogButtonBox.StandardButton.Close): self.close() - elif button == self.buttonBox.button( - QDialogButtonBox.StandardButton.Cancel - ): + elif button == self.buttonBox.button(QDialogButtonBox.StandardButton.Cancel): if self.__batch: self.radonService.cancelComplexityBatch() QTimer.singleShot(1000, self.__finish) else: self.__finish() - + @pyqtSlot() def on_startButton_clicked(self): """ @@ -568,70 +567,67 @@ """ fileList = self.__fileList[:] dataChanged = False - + filterString = self.excludeFilesEdit.text() if ( - "ExcludeFiles" not in self.__data or - filterString != self.__data["ExcludeFiles"] + "ExcludeFiles" not in self.__data + or filterString != self.__data["ExcludeFiles"] ): self.__data["ExcludeFiles"] = filterString dataChanged = True - filterList = [f.strip() for f in filterString.split(",") - if f.strip()] + filterList = [f.strip() for f in filterString.split(",") if f.strip()] if filterList: for fileFilter in filterList: - fileList = [f for f in fileList - if not fnmatch.fnmatch(f, fileFilter)] - + fileList = [f for f in fileList if not fnmatch.fnmatch(f, fileFilter)] + minimumRank = self.rankComboBox.currentText() if ( - "MinimumRank" not in self.__data or - minimumRank != self.__data["MinimumRank"] + "MinimumRank" not in self.__data + or minimumRank != self.__data["MinimumRank"] ): self.__data["MinimumRank"] = minimumRank dataChanged = True - + if dataChanged: - self.__project.setData( - "OTHERTOOLSPARMS", "RadonCodeMetrics", self.__data) - + self.__project.setData("OTHERTOOLSPARMS", "RadonCodeMetrics", self.__data) + self.start(fileList) - + def __showContextMenu(self, coord): """ Private slot to show the context menu of the resultlist. - + @param coord the position of the mouse pointer (QPoint) """ if self.resultList.topLevelItemCount() > 0: self.__menu.popup(self.mapToGlobal(coord)) - + def __resultCollapse(self): """ Private slot to collapse all entries of the resultlist. """ for index in range(self.resultList.topLevelItemCount()): self.resultList.topLevelItem(index).setExpanded(False) - + def __resultExpand(self): """ Private slot to expand all entries of the resultlist. """ for index in range(self.resultList.topLevelItemCount()): self.resultList.topLevelItem(index).setExpanded(True) - + def clear(self): """ Public method to clear all results. """ self.resultList.clear() self.summaryLabel.clear() - + @pyqtSlot(QTreeWidgetItem, int) def on_resultList_itemActivated(self, item, column): """ Private slot to handle the activation of a result item. - + @param item reference to the activated item @type QTreeWidgetItem @param column activated column @@ -642,12 +638,12 @@ if filename: vm = ericApp().getObject("ViewManager") vm.openSourceFile(filename, lineno) - + @pyqtSlot(str) def on_rankComboBox_activated(self, rank): """ Private slot to handle the selection of a minimum rank. - + @param rank selected minimum rank @type str """