RadonMetrics/MaintainabilityIndexDialog.py

Sat, 23 Dec 2023 15:48:48 +0100

author
Detlev Offenbach <detlev@die-offenbachs.de>
date
Sat, 23 Dec 2023 15:48:48 +0100
branch
eric7
changeset 110
261babeb7cc6
parent 109
47df4b69f699
child 126
9ecce748f0ac
permissions
-rw-r--r--

Updated copyright for 2024.

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

# Copyright (c) 2015 - 2024 Detlev Offenbach <detlev@die-offenbachs.de>
#

"""
Module implementing a dialog to show maintainability indexes.
"""

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,
    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_MaintainabilityIndexDialog import Ui_MaintainabilityIndexDialog


class MaintainabilityIndexDialog(QDialog, Ui_MaintainabilityIndexDialog):
    """
    Class implementing a dialog to show maintainability indexes.
    """

    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.resultList.headerItem().setText(self.resultList.columnCount(), "")

        self.radonService = radonService
        self.radonService.maintainabilityIndexDone.connect(self.__processResult)
        self.radonService.error.connect(self.__processError)
        self.radonService.batchFinished.connect(self.__batchFinished)

        self.__batch = False
        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>Rank</b></td><td><b>MI Score</b></td>"
                "<td><b>Maintainability</b></td></tr>"
                "<tr><td><b>A</b></td><td>100 - 20</td><td>very high</td></tr>"
                "<tr><td><b>B</b></td><td>19 - 10</td><td>medium</td></tr>"
                "<tr><td><b>C</b></td><td>9 - 0</td><td>extremely low</td></tr>"
                "</table>"
            )
        )

        try:
            usesDarkPalette = ericApp().usesDarkPalette()
        except AttributeError:
            from PyQt6.QtGui import QPalette  # noqa: I101, I102

            palette = ericApp().palette()
            lightness = palette.color(QPalette.Window).lightness()
            usesDarkPalette = lightness <= 128
        if usesDarkPalette:
            self.__rankColors = {
                "A": QColor("#308030"),
                "B": QColor("#808030"),
                "C": QColor("#803030"),
            }
        else:
            self.__rankColors = {
                "A": QColor("#00ff00"),
                "B": QColor("#ffff00"),
                "C": QColor("#ff0000"),
            }

    def __resizeResultColumns(self):
        """
        Private method to resize the list columns.
        """
        self.resultList.header().resizeSections(
            QHeaderView.ResizeMode.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)]
        try:
            data.append(
                "{0:>6}".format(self.__locale.toString(float(values["mi"]), "f", 2))
            )
        except ValueError:
            data.append(values["mi"])
        data.append(values["rank"])
        itm = QTreeWidgetItem(self.resultList, data)
        itm.setTextAlignment(1, Qt.AlignmentFlag.AlignRight)
        itm.setTextAlignment(2, Qt.AlignmentFlag.AlignHCenter)
        if values["rank"] in self.__rankColors:
            itm.setBackground(2, self.__rankColors[values["rank"]])
        itm.setData(0, self.FilePathRole, filename)

        if values["rank"] in self.__summary:
            self.__summary[values["rank"]] += 1

    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 maintainability index determination.

        @param fn file or list of files or directory to show
            the maintainability index for
        @type str or list of str
        """
        self.__errorItem = None
        self.resultList.clear()
        self.summaryLabel.clear()
        self.cancelled = False
        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 = {
            "A": 0,
            "B": 0,
            "C": 0,
        }

        if len(self.files) > 0:
            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.maintainabilityIndex()
            else:
                self.__batch = True
                self.maintainabilityIndexBatch()

    def maintainabilityIndex(self):
        """
        Public method to start a maintainability index calculation for one
        Python file.

        The results are reported to the __processResult slot.
        """
        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)

        if self.cancelled:
            return

        try:
            self.source = Utilities.readEncodedFile(self.filename)[0]
            self.source = Utilities.normalizeCode(self.source)
        except (OSError, UnicodeError) as msg:
            self.__createErrorItem(self.filename, str(msg).rstrip())
            self.progress += 1
            # Continue with next file
            self.maintainabilityIndex()
            return

        self.__finished = False
        self.radonService.maintainabilityIndex(None, self.filename, self.source)

    def maintainabilityIndexBatch(self):
        """
        Public method to start a maintainability index 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)

            try:
                source = Utilities.readEncodedFile(filename)[0]
                source = Utilities.normalizeCode(source)
            except (OSError, UnicodeError) 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)

        self.__finished = False
        self.radonService.maintainabilityIndexBatch(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_ == "mi":
            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_ == "mi":
            self.__createErrorItem(fn, msg)

    def __processResult(self, fn, result):
        """
        Private slot called after perfoming a maintainability index 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)

        if not self.__batch:
            self.maintainabilityIndex()

    def __finish(self):
        """
        Private slot called when the action or the user pressed the button.
        """
        if not self.__finished:
            self.__finished = True

            self.resultList.setSortingEnabled(True)
            self.resultList.sortItems(0, Qt.SortOrder.AscendingOrder)

            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.summaryLabel.setText(
                self.tr(
                    "<table>"
                    "<tr><td colspan=2><b>Summary:</b></td></tr>"
                    "<tr><td><b>A</b></td><td align='right'>{0} files</td></tr>"
                    "<tr><td><b>B</b></td><td align='right'>{1} files</td></tr>"
                    "<tr><td><b>C</b></td><td align='right'>{2} files</td></tr>"
                    "</table>"
                ).format(
                    self.__locale.toString(self.__summary["A"]),
                    self.__locale.toString(self.__summary["B"]),
                    self.__locale.toString(self.__summary["C"]),
                )
            )

            self.checkProgress.setVisible(False)

    def __cancel(self):
        """
        Private method to cancel the current check run.
        """
        if self.__batch:
            self.radonService.cancelMaintainabilityIndexBatch()
            QTimer.singleShot(1000, self.__finish)
        else:
            self.__finish()

    def closeEvent(self, evt):
        """
        Protected method to handle a close event.

        @param evt reference to the close event
        @type QCloseEvent
        """
        self.__cancel()

    @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):
            self.__cancel()

    @pyqtSlot()
    def on_startButton_clicked(self):
        """
        Private slot to start a maintainability index 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.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)
        if filename:
            vm = ericApp().getObject("ViewManager")
            vm.openSourceFile(filename)

eric ide

mercurial