--- a/PluginMetricsRadon.py Sun Apr 25 16:35:17 2021 +0200 +++ b/PluginMetricsRadon.py Sun Apr 25 16:41:47 2021 +0200 @@ -7,6 +7,7 @@ Module implementing the radon code metrics plug-in. """ +import contextlib import os from PyQt5.QtCore import pyqtSignal, QObject, QTranslator @@ -26,7 +27,7 @@ author = "Detlev Offenbach <detlev@die-offenbachs.de>" autoactivate = True deactivateable = True -version = "3.0.2" +version = "3.1.0" className = "RadonMetricsPlugin" packageName = "RadonMetrics" shortDescription = "Code metrics plugin using radon package" @@ -70,7 +71,7 @@ @param ui reference to the user interface object @type UI.UserInterface """ - super(RadonMetricsPlugin, self).__init__(ui) + super().__init__(ui) self.__ui = ui self.__initialize() @@ -613,76 +614,76 @@ """ if ( menuName == "Show" and - e5App().getObject("Project").getProjectLanguage() == "Python3" + e5App().getObject("Project").getProjectLanguage() == "Python3" and + self.__projectBrowserMenu is None ): - if self.__projectBrowserMenu is None: - self.__projectBrowserMenu = menu - - act = menu.addSeparator() - self.__projectBrowserSeparatorActs.append(act) - - # header action - act = QAction(self.tr("Radon"), self) - font = act.font() - font.setBold(True) - act.setFont(font) - act.triggered.connect(self.__showRadonVersion) - menu.addAction(act) - self.__projectBrowserMetricsActs.append(act) - - act = E5Action( - self.tr('Code Metrics'), - self.tr('Code &Metrics...'), 0, 0, - self, '') - act.setStatusTip(self.tr( - 'Show raw code metrics.')) - act.setWhatsThis(self.tr( - """<b>Code Metrics...</b>""" - """<p>This calculates raw code metrics of Python files""" - """ and shows the amount of lines of code, logical lines""" - """ of code, source lines of code, comment lines,""" - """ multi-line strings and blank lines.</p>""" - )) - act.triggered.connect(self.__projectBrowserRawMetrics) - menu.addAction(act) - self.__projectBrowserMetricsActs.append(act) - - act = E5Action( - self.tr('Maintainability Index'), - self.tr('Maintainability &Index...'), 0, 0, - self, '') - act.setStatusTip(self.tr( - 'Show the maintainability index for Python files.')) - act.setWhatsThis(self.tr( - """<b>Maintainability Index...</b>""" - """<p>This calculates the maintainability index of""" - """ Python files and shows it together with a ranking.""" - """</p>""" - )) - act.triggered.connect( - self.__projectBrowserMaintainabilityIndex) - menu.addAction(act) - self.__projectBrowserMetricsActs.append(act) - - act = E5Action( - self.tr('Cyclomatic Complexity'), - self.tr('Cyclomatic &Complexity...'), 0, 0, - self, '') - act.setStatusTip(self.tr( - 'Show the cyclomatic complexity for Python files.')) - act.setWhatsThis(self.tr( - """<b>Cyclomatic Complexity...</b>""" - """<p>This calculates the cyclomatic complexity of""" - """ Python files and shows it together with a ranking.""" - """</p>""" - )) - act.triggered.connect( - self.__projectBrowserCyclomaticComplexity) - menu.addAction(act) - self.__projectBrowserMetricsActs.append(act) - - act = menu.addSeparator() - self.__projectBrowserSeparatorActs.append(act) + self.__projectBrowserMenu = menu + + act = menu.addSeparator() + self.__projectBrowserSeparatorActs.append(act) + + # header action + act = QAction(self.tr("Radon"), self) + font = act.font() + font.setBold(True) + act.setFont(font) + act.triggered.connect(self.__showRadonVersion) + menu.addAction(act) + self.__projectBrowserMetricsActs.append(act) + + act = E5Action( + self.tr('Code Metrics'), + self.tr('Code &Metrics...'), 0, 0, + self, '') + act.setStatusTip(self.tr( + 'Show raw code metrics.')) + act.setWhatsThis(self.tr( + """<b>Code Metrics...</b>""" + """<p>This calculates raw code metrics of Python files""" + """ and shows the amount of lines of code, logical lines""" + """ of code, source lines of code, comment lines,""" + """ multi-line strings and blank lines.</p>""" + )) + act.triggered.connect(self.__projectBrowserRawMetrics) + menu.addAction(act) + self.__projectBrowserMetricsActs.append(act) + + act = E5Action( + self.tr('Maintainability Index'), + self.tr('Maintainability &Index...'), 0, 0, + self, '') + act.setStatusTip(self.tr( + 'Show the maintainability index for Python files.')) + act.setWhatsThis(self.tr( + """<b>Maintainability Index...</b>""" + """<p>This calculates the maintainability index of""" + """ Python files and shows it together with a ranking.""" + """</p>""" + )) + act.triggered.connect( + self.__projectBrowserMaintainabilityIndex) + menu.addAction(act) + self.__projectBrowserMetricsActs.append(act) + + act = E5Action( + self.tr('Cyclomatic Complexity'), + self.tr('Cyclomatic &Complexity...'), 0, 0, + self, '') + act.setStatusTip(self.tr( + 'Show the cyclomatic complexity for Python files.')) + act.setWhatsThis(self.tr( + """<b>Cyclomatic Complexity...</b>""" + """<p>This calculates the cyclomatic complexity of""" + """ Python files and shows it together with a ranking.""" + """</p>""" + )) + act.triggered.connect( + self.__projectBrowserCyclomaticComplexity) + menu.addAction(act) + self.__projectBrowserMetricsActs.append(act) + + act = menu.addSeparator() + self.__projectBrowserSeparatorActs.append(act) def __editorOpened(self, editor): """ @@ -706,10 +707,8 @@ @param editor reference to the editor (QScintilla.Editor) """ - try: + with contextlib.suppress(ValueError): self.__editors.remove(editor) - except ValueError: - pass def __editorRenamed(self, editor): """ @@ -791,13 +790,16 @@ menu. """ editor = e5App().getObject("ViewManager").activeWindow() - if editor is not None: - if editor.checkDirty() and editor.getFileName() is not None: - if self.__editorRawMetricsDialog is None: - from RadonMetrics.RawMetricsDialog import RawMetricsDialog - self.__editorRawMetricsDialog = RawMetricsDialog(self) - self.__editorRawMetricsDialog.show() - self.__editorRawMetricsDialog.start(editor.getFileName()) + if ( + editor is not None and + editor.checkDirty() and + editor.getFileName() is not None + ): + if self.__editorRawMetricsDialog is None: + from RadonMetrics.RawMetricsDialog import RawMetricsDialog + self.__editorRawMetricsDialog = RawMetricsDialog(self) + self.__editorRawMetricsDialog.show() + self.__editorRawMetricsDialog.start(editor.getFileName()) ################################################################## ## Maintainability index calculations @@ -857,15 +859,18 @@ show menu. """ editor = e5App().getObject("ViewManager").activeWindow() - if editor is not None: - if editor.checkDirty() and editor.getFileName() is not None: - if self.__editorMIDialog is None: - from RadonMetrics.MaintainabilityIndexDialog import ( - MaintainabilityIndexDialog - ) - self.__editorMIDialog = MaintainabilityIndexDialog(self) - self.__editorMIDialog.show() - self.__editorMIDialog.start(editor.getFileName()) + if ( + editor is not None and + editor.checkDirty() and + editor.getFileName() is not None + ): + if self.__editorMIDialog is None: + from RadonMetrics.MaintainabilityIndexDialog import ( + MaintainabilityIndexDialog + ) + self.__editorMIDialog = MaintainabilityIndexDialog(self) + self.__editorMIDialog.show() + self.__editorMIDialog.start(editor.getFileName()) ################################################################## ## Cyclomatic complexity calculations @@ -926,16 +931,19 @@ show menu. """ editor = e5App().getObject("ViewManager").activeWindow() - if editor is not None: - if editor.checkDirty() and editor.getFileName() is not None: - if self.__editorCCDialog is None: - from RadonMetrics.CyclomaticComplexityDialog import ( - CyclomaticComplexityDialog - ) - self.__editorCCDialog = CyclomaticComplexityDialog( - self, isSingle=True) - self.__editorCCDialog.show() - self.__editorCCDialog.start(editor.getFileName()) + if ( + editor is not None and + editor.checkDirty() and + editor.getFileName() is not None + ): + if self.__editorCCDialog is None: + from RadonMetrics.CyclomaticComplexityDialog import ( + CyclomaticComplexityDialog + ) + self.__editorCCDialog = CyclomaticComplexityDialog( + self, isSingle=True) + self.__editorCCDialog.show() + self.__editorCCDialog.start(editor.getFileName()) ################################################################## ## Radon info display