RadonMetrics/CyclomaticComplexityDialog.py

changeset 13
22bc345844e7
child 15
62ffe3d426e5
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/RadonMetrics/CyclomaticComplexityDialog.py	Sat Sep 19 18:24:07 2015 +0200
@@ -0,0 +1,513 @@
+# -*- coding: utf-8 -*-
+
+# Copyright (c) 2015 Detlev Offenbach <detlev@die-offenbachs.de>
+#
+
+"""
+Module implementing a dialog to show the cyclomatic complexity (McCabe
+complexity).
+"""
+
+from __future__ import unicode_literals
+
+try:
+    str = unicode       # __IGNORE_EXCEPTION__ __IGNORE_WARNING__
+except NameError:
+    pass
+
+import os
+import fnmatch
+import sys
+
+sys.path.insert(0, os.path.dirname(__file__))
+
+from PyQt5.QtCore import pyqtSlot, qVersion, Qt, QTimer, QLocale
+from PyQt5.QtWidgets import (
+    QDialog, QDialogButtonBox, QAbstractButton, QHeaderView, QTreeWidgetItem,
+    QApplication
+)
+
+from .Ui_CyclomaticComplexityDialog import Ui_CyclomaticComplexityDialog
+from E5Gui.E5Application import e5App
+
+import Preferences
+import Utilities
+
+
+class CyclomaticComplexityDialog(QDialog, Ui_CyclomaticComplexityDialog):
+    """
+    Class implementing a dialog to show the cyclomatic complexity (McCabe
+    complexity).
+    """
+    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(CyclomaticComplexityDialog, 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.resultList.headerItem().setText(self.resultList.columnCount(), "")
+        
+        self.radonService = radonService
+        self.radonService.complexityDone.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 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>&gt; 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.__rankColors = {
+            "A": Qt.green,
+            "B": Qt.green,
+            "C": Qt.yellow,
+            "D": Qt.yellow,
+            "E": Qt.red,
+            "F": Qt.red,
+        }
+    
+    def __resizeResultColumns(self):
+        """
+        Private method to resize the list columns.
+        """
+        self.resultList.header().resizeSections(QHeaderView.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.resultList,
+            [self.__project.getRelativePath(filename)])
+        itm.setExpanded(True)
+        itm.setFirstColumnSpanned(True)
+        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
+        """
+        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.Alignment(Qt.AlignRight))
+        itm.setTextAlignment(3, Qt.Alignment(Qt.AlignHCenter))
+        itm.setTextAlignment(4, Qt.Alignment(Qt.AlignRight))
+        if values["rank"] in ["A", "B", "C", "D", "E", "F"]:
+            itm.setBackground(3, self.__rankColors[values["rank"]])
+        
+        if "methods" in values:
+            itm.setExpanded(True)
+            for method in values["methods"]:
+                self.__createResultItem(itm, method)
+        
+        if "closures" in values and values["closures"]:
+            itm.setExpanded(True)
+            for closure in values["closures"]:
+                self.__createResultItem(itm, 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.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 cyclomatic complexity determination.
+        
+        @param fn file or list of files or directory to show
+            the cyclomatic complexity 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 = {
+            "A": 0,
+            "B": 0,
+            "C": 0,
+            "D": 0,
+            "E": 0,
+            "F": 0,
+        }
+        self.__ccSum = 0
+        self.__ccCount = 0
+        
+        self.__mappedType = {
+            "class": "C",
+            "function": "F",
+            "method": "M",
+        }
+        
+        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.cyclomaticComplexity()
+            else:
+                self.__batch = True
+                self.cyclomaticComplexityBatch()
+    
+    def cyclomaticComplexity(self, codestring=''):
+        """
+        Public method to start a cyclomatic complexity 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.cyclomaticComplexity()
+            return
+
+        self.__finished = False
+        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
+        
+        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.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"]
+        """
+        if type_ == "cc":
+            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_ == "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
+        @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:
+            if result["result"]:
+                fitm = self.__createFileItem(fn)
+                for resultDict in result["result"]:
+                    self.__createResultItem(fitm, resultDict)
+        
+            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)
+        self.checkProgressLabel.setPath(self.__project.getRelativePath(fn))
+        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
+            
+            # reenable updates of the list
+            self.resultList.setSortingEnabled(True)
+            self.resultList.sortItems(1, Qt.AscendingOrder)
+            self.resultList.setUpdatesEnabled(True)
+            
+            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)
+            
+            averageCC = float(self.__ccSum) / self.__ccCount
+            
+            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>{1} blocks</td></tr>"
+                "<tr><td width=30><b>B</b></td><td>{2} blocks</td></tr>"
+                "<tr><td width=30><b>C</b></td><td>{3} blocks</td></tr>"
+                "<tr><td width=30><b>D</b></td><td>{4} blocks</td></tr>"
+                "<tr><td width=30><b>E</b></td><td>{5} blocks</td></tr>"
+                "<tr><td width=30><b>F</b></td><td>{6} blocks</td></tr>"
+                "</table>"
+            ).format(
+                self.__ccCount,
+                self.__summary["A"],
+                self.__summary["B"],
+                self.__summary["C"],
+                self.__summary["D"],
+                self.__summary["E"],
+                self.__summary["F"],
+                cc_rank(averageCC),
+                self.__locale.toString(averageCC, "f", 1)
+            ))
+            
+            self.checkProgress.setVisible(False)
+            self.checkProgressLabel.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.Close):
+            self.close()
+        elif button == self.buttonBox.button(QDialogButtonBox.Cancel):
+            if self.__batch:
+                self.radonService.cancelComplexityBatch()
+                QTimer.singleShot(1000, self.__finish)
+            else:
+                self.__finish()
+    
+    @pyqtSlot()
+    def on_startButton_clicked(self):
+        """
+        Private slot to start a cyclomatic complexity 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)

eric ide

mercurial