RadonMetrics/CyclomaticComplexityDialog.py

Fri, 01 Jan 2016 12:18:55 +0100

author
Detlev Offenbach <detlev@die-offenbachs.de>
date
Fri, 01 Jan 2016 12:18:55 +0100
changeset 37
7fd806094f0f
parent 32
f8d1858fdb52
child 42
04457b4ceda5
permissions
-rw-r--r--

Updated copyright for 2016.

# -*- coding: utf-8 -*-

# Copyright (c) 2015 - 2016 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, QMenu
)

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).
    """
    FilePathRole = Qt.UserRole + 1
    LineNumberRole = Qt.UserRole + 2
    
    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.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.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.__mappedType = {
            "class": "C",
            "function": "F",
            "method": "M",
        }
        self.__typeColors = {
            "class": Qt.blue,
            "function": Qt.darkCyan,
            "method": Qt.magenta,
        }
        self.__rankColors = {
            "A": Qt.green,
            "B": Qt.green,
            "C": Qt.yellow,
            "D": Qt.yellow,
            "E": Qt.red,
            "F": Qt.red,
        }
        
        self.__menu = QMenu(self)
        self.__menu.addAction(self.tr("Collapse all"),
                              self.__resultCollapse)
        self.__menu.addAction(self.tr("Expand all"), self.__resultExpand)
        self.resultList.setContextMenuPolicy(Qt.CustomContextMenu)
        self.resultList.customContextMenuRequested.connect(
            self.__showContextMenu)
    
    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.__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.setTextAlignment(2, Qt.Alignment(Qt.AlignRight))
            itm.setTextAlignment(3, Qt.Alignment(Qt.AlignHCenter))
            itm.setTextAlignment(4, Qt.Alignment(Qt.AlignRight))
            if values["rank"] in self.__rankColors:
                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.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.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": ""}
        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))
    
    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
        @param minRank minimum rank of entries to be shown
        @type str (one character out of A - F)
        """
        self.__errorItem = None
        self.resultList.clear()
        self.summaryLabel.clear()
        self.cancelled = False
        QApplication.processEvents()
        
        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.__minimumRank = minRank
        
        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
        
        self.checkProgressLabel.setPath(self.__project.getRelativePath(fn))
        QApplication.processEvents()
        
        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)
                if fitm.childCount() > 0:
                    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.AscendingOrder)
            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 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>{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[:]
        dataChanged = False
        
        filterString = self.excludeFilesEdit.text()
        if "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()]
        if filterList:
            for filter in filterList:
                fileList = \
                    [f for f in fileList if not fnmatch.fnmatch(f, filter)]
        
        minimumRank = self.rankComboBox.currentText()
        if "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.start(fileList, minRank=minimumRank)
    
    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
        @type int
        """
        filename = item.data(0, self.FilePathRole)
        lineno = item.data(0, self.LineNumberRole)
        if filename:
            vm = e5App().getObject("ViewManager")
            vm.openSourceFile(filename, lineno)

eric ide

mercurial