RadonMetrics/CyclomaticComplexityDialog.py

Wed, 21 Dec 2022 08:52:36 +0100

author
Detlev Offenbach <detlev@die-offenbachs.de>
date
Wed, 21 Dec 2022 08:52:36 +0100
branch
eric7
changeset 104
6eac83394939
parent 102
f7b964ea22a1
child 106
6422943b388f
permissions
-rw-r--r--

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 the cyclomatic complexity (McCabe
complexity).
"""

import fnmatch
import os

from PyQt6.QtCore import QLocale, Qt, QTimer, pyqtSlot
from PyQt6.QtGui import QColor
from PyQt6.QtWidgets import (
    QAbstractButton,
    QApplication,
    QDialog,
    QDialogButtonBox,
    QHeaderView,
    QMenu,
    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_CyclomaticComplexityDialog import Ui_CyclomaticComplexityDialog


class CyclomaticComplexityDialog(QDialog, Ui_CyclomaticComplexityDialog):
    """
    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
        @type bool
        @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.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>&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",
        }

        try:
            usesDarkPalette = ericApp().usesDarkPalette()
        except AttributeError:
            from PyQt6.QtGui import QPalette

            palette = ericApp().palette()
            lightness = palette.color(QPalette.Window).lightness()
            usesDarkPalette = lightness <= 128
        if usesDarkPalette:
            self.__typeColors = {
                "class": QColor("#ffe480"),
                "function": QColor("#99ffff"),
                "method": QColor("#ff99ff"),
            }
            self.__rankColors = {
                "A": QColor("#308030"),
                "B": QColor("#308030"),
                "C": QColor("#808030"),
                "D": QColor("#808030"),
                "E": QColor("#803030"),
                "F": QColor("#803030"),
            }
        else:
            self.__typeColors = {
                "class": QColor("#0000ff"),
                "function": QColor("#008080"),
                "method": QColor("#ff00ff"),
            }
            self.__rankColors = {
                "A": QColor("#00ff00"),
                "B": QColor("#00ff00"),
                "C": QColor("#ffff00"),
                "D": QColor("#ffff00"),
                "E": QColor("#ff0000"),
                "F": QColor("#ff0000"),
            }

        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.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().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.AlignmentFlag.AlignRight)
            itm.setTextAlignment(3, Qt.AlignmentFlag.AlignHCenter)
            itm.setTextAlignment(4, Qt.AlignmentFlag.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.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": ""}
        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.cancelled = False
        self.__errorItem = None
        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.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(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)
        if self.__isSingle:
            self.__fileList = self.files[:]

        self.__summary = {
            "A": 0,
            "B": 0,
            "C": 0,
            "D": 0,
            "E": 0,
            "F": 0,
        }
        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:
                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.

        @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.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

        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"]
        """
        if type_ == "cc":
            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

        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.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.rankComboBox.setEnabled(True)

            self.resultList.header().resizeSections(
                QHeaderView.ResizeMode.ResizeToContents
            )
            self.resultList.header().setStretchLastSection(True)
            self.resultList.header().setSectionResizeMode(
                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.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):
            self.close()
        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):
        """
        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 fileFilter in filterList:
                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"]
        ):
            self.__data["MinimumRank"] = minimumRank
            dataChanged = True

        if dataChanged:
            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
        @type int
        """
        filename = item.data(0, self.FilePathRole)
        lineno = item.data(0, self.LineNumberRole)
        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
        """
        if self.__isSingle:
            self.start(self.__fileList[:])

eric ide

mercurial