Wed, 21 Dec 2022 08:52:36 +0100
Adapted some import statements to eric 23.1 and newer.
# -*- coding: utf-8 -*- # Copyright (c) 2015 - 2022 Detlev Offenbach <detlev@die-offenbachs.de> # """ Module implementing a dialog to show raw code metrics. """ import fnmatch import os from PyQt6.QtCore import QLocale, Qt, QTimer, pyqtSlot from PyQt6.QtWidgets import ( QAbstractButton, QApplication, QDialog, QDialogButtonBox, QHeaderView, QTreeWidgetItem, ) from eric7 import Preferences, Utilities from eric7.EricWidgets.EricApplication import ericApp try: from eric7.SystemUtilities.FileSystemUtilities import direntries except ImportError: # imports for eric < 23.1 from eric7.Utilities import direntries from .Ui_RawMetricsDialog import Ui_RawMetricsDialog class RawMetricsDialog(QDialog, Ui_RawMetricsDialog): """ Class implementing a dialog to show raw code metrics. """ FilePathRole = Qt.ItemDataRole.UserRole + 1 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().__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.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 = 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><b>LOC</b></td>" "<td>Lines of code</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>Empty Comments</b></td><td>Comment lines not" " containing code</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.ResizeMode.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( min(values["comments"] / (float(values["loc"]) or 1), 1.0) ) ) data.append( "{0:3.0%}".format( min(values["comments"] / (float(values["sloc"]) or 1), 1.0) ) ) data.append( "{0:3.0%}".format( min( (values["comments"] + values["multi"]) / (float(values["loc"]) or 1), 1.0, ) ) ) itm = QTreeWidgetItem(self.resultList, data) for col in range(1, 10): itm.setTextAlignment(col, Qt.AlignmentFlag.AlignRight) itm.setData(0, self.FilePathRole, filename) 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.GlobalColor.red) 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 @type Project """ 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.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.cancelled = False self.__errorItem = None self.resultList.clear() self.summaryList.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) 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(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", "lloc", "sloc", "comments", "multi", "single_comments", "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) QApplication.processEvents() # now go through all the files self.progress = 0 if len(self.files) == 1: 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. @param codestring optional sourcestring @type str """ if not self.files: self.checkProgress.setMaximum(1) 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) except (UnicodeError, OSError) 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 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.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.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 QApplication.processEvents() if "error" in result: self.__createErrorItem(fn, result["error"]) else: self.__createResultItem(fn, result) self.progress += 1 self.checkProgress.setValue(self.progress) 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", "single_comments", "blank", ]: val = result.get(key, -1) if val >= 0: 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.sortItems(0, Qt.SortOrder.AscendingOrder) self.resultList.setUpdatesEnabled(True) self.__createSummary() 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.resultList.header().resizeSections( QHeaderView.ResizeMode.ResizeToContents ) self.resultList.header().setStretchLastSection(True) self.resultList.header().setSectionResizeMode( QHeaderView.ResizeMode.Interactive ) self.checkProgress.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("Comment Lines"), self.__locale.toString(self.__summary["comments"]) ) self.__createSummaryItem( self.tr("Empty Comments"), self.__locale.toString(self.__summary["single_comments"]), ) self.__createSummaryItem( self.tr("Multiline Strings"), self.__locale.toString(self.__summary["multi"]), ) self.__createSummaryItem( self.tr("Empty Lines"), self.__locale.toString(self.__summary["blank"]) ) self.__createSummaryItem( self.tr("C % L"), "{0:3.0%}".format( min( self.__summary["comments"] / (float(self.__summary["loc"]) or 1), 1.0, ) ), ) self.__createSummaryItem( self.tr("C % S"), "{0:3.0%}".format( min( self.__summary["comments"] / (float(self.__summary["sloc"]) or 1), 1.0, ) ), ) self.__createSummaryItem( self.tr("C + M % L"), "{0:3.0%}".format( min( (self.__summary["comments"] + self.__summary["multi"]) / (float(self.__summary["loc"]) or 1), 1.0, ) ), ) self.summaryList.header().resizeSections( QHeaderView.ResizeMode.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.AlignmentFlag.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.StandardButton.Close): self.close() elif button == self.buttonBox.button(QDialogButtonBox.StandardButton.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 fileFilter in filterList: fileList = [f for f in fileList if not fnmatch.fnmatch(f, fileFilter)] self.start(fileList) def clear(self): """ Public method to clear all results. """ self.resultList.clear() self.summaryList.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 @type int """ filename = item.data(0, self.FilePathRole) if filename: vm = ericApp().getObject("ViewManager") vm.openSourceFile(filename)