Wed, 21 Sep 2022 10:48:59 +0200
Reformatted source code with 'Black'.
--- a/.hgignore Thu Dec 30 12:58:58 2021 +0100 +++ b/.hgignore Wed Sep 21 10:48:59 2022 +0200 @@ -1,6 +1,7 @@ glob:.eric7project glob:.eric6project glob:.ropeproject +glob:.jedi glob:.directory glob:**.pyc glob:**.pyo
--- a/PluginSelectionEncloser.py Thu Dec 30 12:58:58 2021 +0100 +++ b/PluginSelectionEncloser.py Wed Sep 21 10:48:59 2022 +0200 @@ -40,12 +40,12 @@ error = "" selectionEncloserPluginObject = None - + def createSelectionEncloserPage(configDlg): """ Module function to create the Selection Encloser configuration page. - + @param configDlg reference to the configuration dialog @type ConfigurationWidget @return reference to the configuration page @@ -53,28 +53,32 @@ """ global selectionEncloserPluginObject from SelectionEncloser.ConfigurationPage.SelectionEncloserPage import ( - SelectionEncloserPage + SelectionEncloserPage, ) + return SelectionEncloserPage(selectionEncloserPluginObject) - + 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 { "selectionEncloserPage": [ - QCoreApplication.translate("SelectionEncloserPlugin", - "Selection Encloser"), - os.path.join("SelectionEncloser", "icons", - "selectionEncloser-{0}".format(iconSuffix)), - createSelectionEncloserPage, None, None], + QCoreApplication.translate("SelectionEncloserPlugin", "Selection Encloser"), + os.path.join( + "SelectionEncloser", "icons", "selectionEncloser-{0}".format(iconSuffix) + ), + createSelectionEncloserPage, + None, + None, + ], } @@ -89,66 +93,68 @@ """ Class implementing the Selection Encloser plugin. """ + PreferencesKey = "SelectionEncloser" - + def __init__(self, ui): """ Constructor - + @param ui reference to the user interface object @type UserInterface """ super().__init__(ui) self.__ui = ui - + # menu is a list of lists; each list consists of a string for the # submenu title and a list of submenu entries. Each submenu entry # consists of another list giving the title and the enclosing string # or formatting string. defaultMenu = [ - [self.tr("Quotes"), [ - ['"', '"'], - ["'", "'"], - ['"""', '"""'], - ["'''", "'''"] - ]], - [self.tr("HTML"), [ - ['<h1>', '<h1>{0}</h1>'], - ['<h2>', '<h2>{0}</h2>'], - ['<h3>', '<h3>{0}</h3>'], - ['--Separator--', ''], - ['<p>', '<p>{0}</p>'], - ['<div>', '<div>{0}</div>'], - ['<span>', '<span>{0}</span>'], - ]] + [ + self.tr("Quotes"), + [['"', '"'], ["'", "'"], ['"""', '"""'], ["'''", "'''"]], + ], + [ + self.tr("HTML"), + [ + ["<h1>", "<h1>{0}</h1>"], + ["<h2>", "<h2>{0}</h2>"], + ["<h3>", "<h3>{0}</h3>"], + ["--Separator--", ""], + ["<p>", "<p>{0}</p>"], + ["<div>", "<div>{0}</div>"], + ["<span>", "<span>{0}</span>"], + ], + ], ] self.__defaults = { "MenuHierarchy": json.dumps(defaultMenu), } - + 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 status @rtype (None, bool) """ global error - error = "" # clear previous error - + error = "" # clear previous error + global selectionEncloserPluginObject selectionEncloserPluginObject = self - + self.__ui.showMenu.connect(self.__populateMenu) - + menu = self.__ui.getMenu("plugin_tools") if menu is not None: if not menu.isEmpty(): @@ -156,23 +162,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: @@ -180,10 +184,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") @@ -191,7 +197,7 @@ for act in acts: menu.removeAction(act) self.__editors = {} - + def __loadTranslator(self): """ Private method to load the translation file. @@ -200,7 +206,8 @@ loc = self.__ui.getLocale() if loc and loc != "C": locale_dir = os.path.join( - os.path.dirname(__file__), "SelectionEncloser", "i18n") + os.path.dirname(__file__), "SelectionEncloser", "i18n" + ) translation = "selectionencloser_{0}".format(loc) translator = QTranslator(None) loaded = translator.load(translation, locale_dir) @@ -208,14 +215,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 key of the value to get @type str @return value of the requested setting @@ -224,15 +233,18 @@ if key in ["MenuHierarchy"]: return json.loads( 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 @@ -240,11 +252,11 @@ """ if key in ["MenuHierarchy"]: Preferences.Prefs.settings.setValue( - self.PreferencesKey + "/" + key, json.dumps(value)) + self.PreferencesKey + "/" + key, json.dumps(value) + ) else: - 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. @@ -252,11 +264,11 @@ self.__menu = QMenu("Enclose Selection") self.__menu.setEnabled(False) self.__menu.aboutToShow.connect(self.__showMenu) - + def __populateMenu(self, name, menu): """ Private slot to populate the tools menu with our entry. - + @param name name of the menu @type str @param menu reference to the menu to be populated @@ -264,22 +276,21 @@ """ 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 and editor.hasSelectedText()) elif name == "PluginTools" and self.__mainActions: - self.__menu.setEnabled(editor is not None and - editor.hasSelectedText()) - + self.__menu.setEnabled(editor is not None and editor.hasSelectedText()) + def __editorOpened(self, editor): """ Private slot called, when a new editor was opened. - + @param editor reference to the new editor @type Editor """ @@ -293,11 +304,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 """ @@ -305,12 +316,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 @@ -327,9 +338,9 @@ self.__editors[editor].append(act) act = menu.addMenu(self.__menu) self.__editors[editor].append(act) - - self.__menu.setEnabled(editor.selectedText() != '') - + + self.__menu.setEnabled(editor.selectedText() != "") + def __showMenu(self): """ Private slot to build the menu hierarchy. @@ -337,44 +348,44 @@ self.__menu.clear() hierarchy = self.getPreferences("MenuHierarchy") for menuTitle, entries in hierarchy: - if menuTitle == '--Separator--': + if menuTitle == "--Separator--": self.__menu.addSeparator() else: submenu = QMenu(menuTitle, self.__menu) for title, encString in entries: - if title == '--Separator--': + if title == "--Separator--": submenu.addSeparator() else: act = submenu.addAction(title) act.setData(encString) submenu.triggered.connect(self.__encloseSelection) self.__menu.addMenu(submenu) - + @pyqtSlot(QAction) def __encloseSelection(self, act): """ Private slot to enclose the selection with the selected string. - + @param act action that triggered @type QAction """ if act is None: return - + editor = ericApp().getObject("ViewManager").activeWindow() if editor is None: return - + if not editor.hasSelectedText(): return - + encloseString = act.data() if not encloseString: return - - if '%s' in encloseString: + + if "%s" in encloseString: newText = encloseString % editor.selectedText() - elif '{0}' in encloseString or '{}' in encloseString: + elif "{0}" in encloseString or "{}" in encloseString: # __IGNORE_WARNING_M613__ newText = encloseString.format(editor.selectedText()) else: @@ -383,5 +394,6 @@ editor.replaceSelectedText(newText) editor.endUndoAction() + # # eflag: noqa = M801
--- a/SelectionEncloser.epj Thu Dec 30 12:58:58 2021 +0100 +++ b/SelectionEncloser.epj Wed Sep 21 10:48:59 2022 +0200 @@ -1,19 +1,21 @@ { "header": { "comment": "eric project file for project SelectionEncloser", - "copyright": "Copyright (C) 2021 Detlev Offenbach, detlev@die-offenbachs.de" + "copyright": "Copyright (C) 2022 Detlev Offenbach, detlev@die-offenbachs.de" }, "project": { "AUTHOR": "Detlev Offenbach", "CHECKERSPARMS": { "Pep8Checker": { "AnnotationsChecker": { + "AllowStarArgAny": false, "AllowUntypedDefs": false, "AllowUntypedNested": false, "DispatchDecorators": [ "singledispatch", "singledispatchmethod" ], + "ForceFutureAnnotations": false, "MaximumComplexity": 3, "MaximumLength": 7, "MinimumCoverage": 75, @@ -59,20 +61,25 @@ }, "CopyrightAuthor": "", "CopyrightMinFileSize": 0, - "DocstringType": "eric", + "DocstringType": "eric_black", "EnabledCheckerCategories": "C, D, E, M, N, S, Y, W", "ExcludeFiles": "*/Ui_*.py, */*_rc.py", - "ExcludeMessages": "C101,E265,E266,E305,E402,M201,M301,M302,M303,M304,M305,M306,M307,M308,M311,M312,M313,M314,M315,M321,M701,M702,M811,M834,N802,N803,N807,N808,N821,W293,W504,Y119,Y401,Y402", + "ExcludeMessages": "C101,E265,E266,E305,E402,M201,M301,M302,M303,M304,M305,M306,M307,M308,M311,M312,M313,M314,M315,M321,M701,M702,M811,M834,N802,N803,N807,N808,N821,W293,W503,Y119,Y401,Y402", "FixCodes": "", "FixIssues": false, "FutureChecker": "", "HangClosing": false, + "ImportsChecker": { + "ApplicationPackageNames": [], + "BanRelativeImports": "", + "BannedModules": [] + }, "IncludeMessages": "", "LineComplexity": 25, "LineComplexityScore": 10, "MaxCodeComplexity": 10, - "MaxDocLineLength": 79, - "MaxLineLength": 79, + "MaxDocLineLength": 88, + "MaxLineLength": 88, "NoFixCodes": "E501", "RepeatMessages": true, "SecurityChecker": { @@ -129,6 +136,7 @@ } }, "EMAIL": "detlev@die-offenbachs.de", + "EMBEDDED_VENV": false, "EOL": -1, "FILETYPES": { "*.epj": "OTHERS", @@ -163,6 +171,7 @@ }, "INTERFACES": [], "LEXERASSOCS": {}, + "LICENSE": "GNU General Public License v3 or later (GPLv3+)", "MAINSCRIPT": "PluginSelectionEncloser.py", "MAKEPARAMS": { "MakeEnabled": false, @@ -178,6 +187,7 @@ "ChangeLog", "PKGLIST", "PluginSelectionEncloser.zip", + "SelectionEncloser.epj", "SelectionEncloser/Documentation/LICENSE.GPL3", "SelectionEncloser/Documentation/source", "SelectionEncloser/icons/edit-dark.svg", @@ -187,10 +197,26 @@ "SelectionEncloser/icons/separatorAdd-dark.svg", "SelectionEncloser/icons/separatorAdd-light.svg", "SelectionEncloser/icons/topAdd-dark.svg", - "SelectionEncloser/icons/topAdd-light.svg", - "SelectionEncloser.epj" + "SelectionEncloser/icons/topAdd-light.svg" ], - "OTHERTOOLSPARMS": {}, + "OTHERTOOLSPARMS": { + "Black": { + "exclude": "/(\\.direnv|\\.eggs|\\.git|\\.hg|\\.mypy_cache|\\.nox|\\.tox|\\.venv|venv|\\.svn|_build|buck-out|build|dist|__pypackages__)/", + "extend-exclude": "", + "force-exclude": "", + "line-length": 88, + "skip-magic-trailing-comma": false, + "skip-string-normalization": false, + "source": "project", + "target-version": [ + "py311", + "py310", + "py39", + "py38", + "py37" + ] + } + }, "PACKAGERSPARMS": {}, "PROGLANGUAGE": "Python3", "PROJECTTYPE": "E7Plugin", @@ -214,6 +240,7 @@ "SPELLEXCLUDES": "", "SPELLLANGUAGE": "en_US", "SPELLWORDS": "", + "TESTING_FRAMEWORK": "", "TRANSLATIONEXCEPTIONS": [], "TRANSLATIONPATTERN": "SelectionEncloser/i18n/selectionencloser_%language%.ts", "TRANSLATIONS": [
--- a/SelectionEncloser/ConfigurationPage/SelectionEncloserEditDialog.py Thu Dec 30 12:58:58 2021 +0100 +++ b/SelectionEncloser/ConfigurationPage/SelectionEncloserEditDialog.py Wed Sep 21 10:48:59 2022 +0200 @@ -17,10 +17,11 @@ """ Class implementing a dialog to edit an enclosing menu entry. """ + def __init__(self, title="", string="", parent=None): """ Constructor - + @param title menu entry title @type str @param string enclosing string or string format expression @@ -30,46 +31,47 @@ """ super().__init__(parent) self.setupUi(self) - + self.titleEdit.setText(title) self.stringEdit.setText(string) - + msh = self.minimumSizeHint() self.resize(max(self.size().width(), msh.width()), msh.height()) - + self.__updateOkButton() - + def __updateOkButton(self): """ Private slot to set the status of the OK button. """ self.buttonBox.button(QDialogButtonBox.StandardButton.Ok).setEnabled( - bool(self.titleEdit.text()) and bool(self.stringEdit.text())) - + bool(self.titleEdit.text()) and bool(self.stringEdit.text()) + ) + @pyqtSlot(str) def on_titleEdit_textChanged(self, txt): """ Private slot to react on changes of the title. - + @param txt title text @type str """ self.__updateOkButton() - + @pyqtSlot(str) def on_stringEdit_textChanged(self, txt): """ Private slot to react on changes of the string. - + @param txt enclosing string @type str """ self.__updateOkButton() - + def getData(self): """ Public method to get the dialog data. - + @return tuple with menu entry title and enclosing string or string format expression @rtype tuple of (str, str)
--- a/SelectionEncloser/ConfigurationPage/SelectionEncloserPage.py Thu Dec 30 12:58:58 2021 +0100 +++ b/SelectionEncloser/ConfigurationPage/SelectionEncloserPage.py Wed Sep 21 10:48:59 2022 +0200 @@ -14,9 +14,7 @@ from EricWidgets.EricApplication import ericApp -from Preferences.ConfigurationPages.ConfigurationPageBase import ( - ConfigurationPageBase -) +from Preferences.ConfigurationPages.ConfigurationPageBase import ConfigurationPageBase from .Ui_SelectionEncloserPage import Ui_SelectionEncloserPage import UI.PixmapCache @@ -26,56 +24,69 @@ """ Class implementing Selection Encloser configuration page. """ + def __init__(self, plugin): """ Constructor - + @param plugin reference to the plugin object @type SelectionEncloserPlugin """ super().__init__() self.setupUi(self) self.setObjectName("SelectionEncloserPage") - + usesDarkPalette = ericApp().usesDarkPalette() iconSuffix = "dark" if usesDarkPalette else "light" - - self.editButton.setIcon(UI.PixmapCache.getIcon( - os.path.join("SelectionEncloser", "icons", - "edit-{0}".format(iconSuffix)))) + + self.editButton.setIcon( + UI.PixmapCache.getIcon( + os.path.join( + "SelectionEncloser", "icons", "edit-{0}".format(iconSuffix) + ) + ) + ) self.addButton.setIcon(UI.PixmapCache.getIcon("plus")) self.deleteButton.setIcon(UI.PixmapCache.getIcon("minus")) self.upButton.setIcon(UI.PixmapCache.getIcon("1uparrow")) self.downButton.setIcon(UI.PixmapCache.getIcon("1downarrow")) - self.addMenuButton.setIcon(UI.PixmapCache.getIcon( - os.path.join("SelectionEncloser", "icons", - "topAdd-{0}".format(iconSuffix)))) - self.addSeparatorButton.setIcon(UI.PixmapCache.getIcon( - os.path.join("SelectionEncloser", "icons", - "separatorAdd-{0}".format(iconSuffix)))) - + self.addMenuButton.setIcon( + UI.PixmapCache.getIcon( + os.path.join( + "SelectionEncloser", "icons", "topAdd-{0}".format(iconSuffix) + ) + ) + ) + self.addSeparatorButton.setIcon( + UI.PixmapCache.getIcon( + os.path.join( + "SelectionEncloser", "icons", "separatorAdd-{0}".format(iconSuffix) + ) + ) + ) + self.editButton.setEnabled(False) self.addButton.setEnabled(False) self.addSeparatorButton.setEnabled(False) self.deleteButton.setEnabled(False) self.upButton.setEnabled(False) self.downButton.setEnabled(False) - + self.__plugin = plugin - + # set initial values hierarchy = self.__plugin.getPreferences("MenuHierarchy") for menuTitle, entries in hierarchy: - if menuTitle == '--Separator--': - menuTitle = self.tr('--Separator--') + if menuTitle == "--Separator--": + menuTitle = self.tr("--Separator--") top = QTreeWidgetItem(self.menuTree, [menuTitle]) for title, encString in entries: - if title == '--Separator--': - title = self.tr('--Separator--') + if title == "--Separator--": + title = self.tr("--Separator--") itm = QTreeWidgetItem(top, [title]) itm.setData(0, Qt.ItemDataRole.UserRole, encString) top.setExpanded(True) - + def save(self): """ Public slot to save the Selection Encloser configuration. @@ -84,20 +95,18 @@ for topIndex in range(self.menuTree.topLevelItemCount()): topItem = self.menuTree.topLevelItem(topIndex) menuTitle = topItem.text(0) - if menuTitle == self.tr('--Separator--'): - menuTitle = '--Separator--' + if menuTitle == self.tr("--Separator--"): + menuTitle = "--Separator--" topEntry = [menuTitle, []] for index in range(topItem.childCount()): itm = topItem.child(index) title = itm.text(0) - if title == self.tr('--Separator--'): - title = '--Separator--' - topEntry[1].append( - [title, itm.data(0, Qt.ItemDataRole.UserRole)] - ) + if title == self.tr("--Separator--"): + title = "--Separator--" + topEntry[1].append([title, itm.data(0, Qt.ItemDataRole.UserRole)]) hierarchy.append(topEntry) self.__plugin.setPreferences("MenuHierarchy", hierarchy) - + @pyqtSlot() def on_addMenuButton_clicked(self): """ @@ -107,23 +116,25 @@ self, self.tr("Menu Title"), self.tr("Enter menu title:"), - QLineEdit.EchoMode.Normal) + QLineEdit.EchoMode.Normal, + ) if ok and menuTitle: top = QTreeWidgetItem(self.menuTree, [menuTitle]) top.setExpanded(True) - + @pyqtSlot() def on_addButton_clicked(self): """ Private slot to add a menu entry. """ from .SelectionEncloserEditDialog import SelectionEncloserEditDialog + dlg = SelectionEncloserEditDialog(parent=self) if dlg.exec() == QDialog.DialogCode.Accepted: title, encString = dlg.getData() itm = QTreeWidgetItem(self.menuTree.selectedItems()[0], [title]) itm.setData(0, Qt.ItemDataRole.UserRole, encString) - + @pyqtSlot() def on_addSeparatorButton_clicked(self): """ @@ -131,7 +142,7 @@ """ selItm = self.menuTree.selectedItems()[0] parent = selItm.parent() - itm = QTreeWidgetItem([self.tr('--Separator--')]) + itm = QTreeWidgetItem([self.tr("--Separator--")]) if parent is None: # top level item index = self.menuTree.indexOfTopLevelItem(selItm) + 1 @@ -140,7 +151,7 @@ # sub item index = parent.indexOfChild(selItm) + 1 parent.insertChild(index, itm) - + @pyqtSlot() def on_deleteButton_clicked(self): """ @@ -155,25 +166,25 @@ index = parent.indexOfChild(itm) parent.takeChild(index) del itm - + @pyqtSlot() def on_upButton_clicked(self): """ Private slot to move an entry up. """ self.__moveSelectedEntry(True) - + @pyqtSlot() def on_downButton_clicked(self): """ Private slot to move an entry down. """ self.__moveSelectedEntry(False) - + def __moveSelectedEntry(self, moveUp): """ Private method to move the selected entry up or down. - + @param moveUp flag indicating to move the entry up @type bool """ @@ -195,7 +206,7 @@ for sitm in self.menuTree.selectedItems(): sitm.setSelected(False) itm.setSelected(True) - + @pyqtSlot() def on_editButton_clicked(self): """ @@ -209,20 +220,21 @@ self.tr("Menu Entry"), self.tr("Enter menu entry text:"), QLineEdit.EchoMode.Normal, - itm.text(0)) + itm.text(0), + ) if ok and menuTitle: itm.setText(0, menuTitle) else: - from .SelectionEncloserEditDialog import ( - SelectionEncloserEditDialog + from .SelectionEncloserEditDialog import SelectionEncloserEditDialog + + dlg = SelectionEncloserEditDialog( + itm.text(0), itm.data(0, Qt.ItemDataRole.UserRole), self ) - dlg = SelectionEncloserEditDialog( - itm.text(0), itm.data(0, Qt.ItemDataRole.UserRole), self) if dlg.exec() == QDialog.DialogCode.Accepted: title, encString = dlg.getData() itm.setText(0, title) itm.setData(0, Qt.ItemDataRole.UserRole, encString) - + @pyqtSlot() def on_menuTree_itemSelectionChanged(self): """ @@ -246,8 +258,8 @@ if self.menuTree.indexOfTopLevelItem(itm) == 0: upEnable = False if ( - self.menuTree.indexOfTopLevelItem(itm) == - self.menuTree.topLevelItemCount() - 1 + self.menuTree.indexOfTopLevelItem(itm) + == self.menuTree.topLevelItemCount() - 1 ): downEnable = False else: