--- a/PluginPrintRemover.py Thu Dec 30 12:51:48 2021 +0100 +++ b/PluginPrintRemover.py Wed Sep 21 10:58:59 2022 +0200 @@ -41,41 +41,43 @@ error = "" printRemoverPluginObject = None - + def createPrintRemoverPage(configDlg): """ Module function to create the Print Remover configuration page. - + @param configDlg reference to the configuration dialog @type ConfigurationWidget @return reference to the configuration page @rtype PrintRemoverPage """ global printRemoverPluginObject - from PrintRemover.ConfigurationPage.PrintRemoverPage import ( - PrintRemoverPage - ) + from PrintRemover.ConfigurationPage.PrintRemoverPage import PrintRemoverPage + return PrintRemoverPage(printRemoverPluginObject) def getConfigData(): """ Module function returning data as required by the configuration dialog. - + @return dictionary containing the relevant data @rtype dict """ usesDarkPalette = ericApp().usesDarkPalette() iconSuffix = "dark" if usesDarkPalette else "light" - + return { "printRemoverPage": [ - QCoreApplication.translate("PrintRemoverPlugin", - "Print Remover"), - os.path.join("PrintRemover", "icons", - "printRemover-{0}".format(iconSuffix)), - createPrintRemoverPage, None, None], + QCoreApplication.translate("PrintRemoverPlugin", "Print Remover"), + os.path.join( + "PrintRemover", "icons", "printRemover-{0}".format(iconSuffix) + ), + createPrintRemoverPage, + None, + None, + ], } @@ -90,46 +92,46 @@ """ Class implementing the Print Remover plugin. """ + PreferencesKey = "PrintRemover" - + def __init__(self, ui): """ Constructor - + @param ui reference to the user interface object @type UserInterface """ super().__init__(ui) self.__ui = ui - + self.__defaults = { - "StartswithStrings": [ - "print(", "print ", "console.log"], + "StartswithStrings": ["print(", "print ", "console.log"], } - + self.__translator = None self.__loadTranslator() - + self.__initMenu() - + self.__editors = {} self.__mainActions = [] - + def activate(self): """ Public method to activate this plugin. - + @return tuple of None and activation statu @rtype tuple of (None, bool) """ global error - error = "" # clear previous error - + error = "" # clear previous error + global printRemoverPluginObject printRemoverPluginObject = self - + self.__ui.showMenu.connect(self.__populateMenu) - + menu = self.__ui.getMenu("plugin_tools") if menu is not None: if not menu.isEmpty(): @@ -137,23 +139,21 @@ self.__mainActions.append(act) act = menu.addMenu(self.__menu) self.__mainActions.append(act) - - ericApp().getObject("ViewManager").editorOpenedEd.connect( - self.__editorOpened) - ericApp().getObject("ViewManager").editorClosedEd.connect( - self.__editorClosed) - + + ericApp().getObject("ViewManager").editorOpenedEd.connect(self.__editorOpened) + ericApp().getObject("ViewManager").editorClosedEd.connect(self.__editorClosed) + for editor in ericApp().getObject("ViewManager").getOpenEditors(): self.__editorOpened(editor) - + return None, True - + def deactivate(self): """ Public method to deactivate this plugin. """ self.__ui.showMenu.disconnect(self.__populateMenu) - + menu = self.__ui.getMenu("plugin_tools") if menu is not None: for act in self.__mainActions: @@ -161,10 +161,12 @@ self.__mainActions = [] ericApp().getObject("ViewManager").editorOpenedEd.disconnect( - self.__editorOpened) + self.__editorOpened + ) ericApp().getObject("ViewManager").editorClosedEd.disconnect( - self.__editorClosed) - + self.__editorClosed + ) + for editor, acts in self.__editors.items(): editor.showMenu.disconnect(self.__editorShowMenu) menu = editor.getMenu("Tools") @@ -172,7 +174,7 @@ for act in acts: menu.removeAction(act) self.__editors = {} - + def __loadTranslator(self): """ Private method to load the translation file. @@ -181,7 +183,8 @@ loc = self.__ui.getLocale() if loc and loc != "C": locale_dir = os.path.join( - os.path.dirname(__file__), "PrintRemover", "i18n") + os.path.dirname(__file__), "PrintRemover", "i18n" + ) translation = "printremover_{0}".format(loc) translator = QTranslator(None) loaded = translator.load(translation, locale_dir) @@ -189,14 +192,16 @@ self.__translator = translator ericApp().installTranslator(self.__translator) else: - print("Warning: translation file '{0}' could not be" - " loaded.".format(translation)) + print( + "Warning: translation file '{0}' could not be" + " loaded.".format(translation) + ) print("Using default.") - + def getPreferences(self, key): """ Public method to retrieve the various settings. - + @param key the key of the value to get @type str @return value of the requested setting @@ -205,23 +210,25 @@ if key in ["StartswithStrings"]: return Preferences.toList( Preferences.Prefs.settings.value( - self.PreferencesKey + "/" + key, self.__defaults[key])) + self.PreferencesKey + "/" + key, self.__defaults[key] + ) + ) else: return Preferences.Prefs.settings.value( - self.PreferencesKey + "/" + key, self.__defaults[key]) - + self.PreferencesKey + "/" + key, self.__defaults[key] + ) + def setPreferences(self, key, value): """ Public method to store the various settings. - + @param key key of the setting to be set @type str @param value value to be set @type Any """ - Preferences.Prefs.settings.setValue( - self.PreferencesKey + "/" + key, value) - + Preferences.Prefs.settings.setValue(self.PreferencesKey + "/" + key, value) + def __initMenu(self): """ Private method to initialize the menu. @@ -230,11 +237,11 @@ self.__menu.setEnabled(False) self.__menu.aboutToShow.connect(self.__showMenu) self.__menu.triggered.connect(self.__removeLine) - + def __populateMenu(self, name, menu): """ Private slot to populate the tools menu with our entries. - + @param name name of the menu @type str @param menu reference to the menu to be populated @@ -242,22 +249,22 @@ """ if name not in ["Tools", "PluginTools"]: return - + editor = ericApp().getObject("ViewManager").activeWindow() - + if name == "Tools": if not menu.isEmpty(): menu.addSeparator() - + act = menu.addMenu(self.__menu) act.setEnabled(editor is not None) elif name == "PluginTools" and self.__mainActions: self.__mainActions[-1].setEnabled(editor is not None) - + def __editorOpened(self, editor): """ Private slot called, when a new editor was opened. - + @param editor reference to the new editor @type Editor """ @@ -271,11 +278,11 @@ self.__menu.setEnabled(True) self.__editors[editor].append(act) editor.showMenu.connect(self.__editorShowMenu) - + def __editorClosed(self, editor): """ Private slot called, when an editor was closed. - + @param editor reference to the editor @type Editor """ @@ -283,12 +290,12 @@ del self.__editors[editor] if not self.__editors: self.__menu.setEnabled(False) - + def __editorShowMenu(self, menuName, menu, editor): """ Private slot called, when the the editor context menu or a submenu is about to be shown. - + @param menuName name of the menu to be shown @type str @param menu reference to the menu @@ -296,10 +303,7 @@ @param editor reference to the editor @type Editor """ - if ( - menuName == "Tools" and - self.__menu.menuAction() not in menu.actions() - ): + if menuName == "Tools" and self.__menu.menuAction() not in menu.actions(): # Re-add our menu self.__editors[editor] = [] if not menu.isEmpty(): @@ -307,50 +311,52 @@ self.__editors[editor].append(act) act = menu.addMenu(self.__menu) self.__editors[editor].append(act) - + def __showMenu(self): """ Private slot to build the menu hierarchy. """ self.__menu.clear() for startString in self.getPreferences("StartswithStrings"): - if startString == '--Separator--': + if startString == "--Separator--": self.__menu.addSeparator() else: - act = self.__menu.addAction( - self.tr("Remove '{0}'").format(startString)) + act = self.__menu.addAction(self.tr("Remove '{0}'").format(startString)) act.setData(startString) - + @pyqtSlot(QAction) def __removeLine(self, act): """ Private slot to remove lines starting with the selected pattern. - + @param act reference to the action that was triggered @type QAction """ if act is None: return - + editor = ericApp().getObject("ViewManager").activeWindow() if editor is None: return - + pattern = act.data() if not pattern: return - + text = editor.text() - newText = "".join([ - line for line in text.splitlines(True) - if not line.lstrip().startswith(pattern) or - "__NO_REMOVE__" in line - ]) + newText = "".join( + [ + line + for line in text.splitlines(True) + if not line.lstrip().startswith(pattern) or "__NO_REMOVE__" in line + ] + ) if newText != text: editor.beginUndoAction() editor.selectAll() editor.replaceSelectedText(newText) editor.endUndoAction() + # # eflag: noqa = M801