--- a/src/eric7/Plugins/WizardPlugins/PyRegExpWizard/PyRegExpWizardCharactersDialog.py Wed Jul 13 11:16:20 2022 +0200 +++ b/src/eric7/Plugins/WizardPlugins/PyRegExpWizard/PyRegExpWizardCharactersDialog.py Wed Jul 13 14:55:47 2022 +0200 @@ -10,47 +10,45 @@ from PyQt6.QtCore import QRegularExpression from PyQt6.QtGui import QRegularExpressionValidator from PyQt6.QtWidgets import ( - QSizePolicy, QSpacerItem, QWidget, QHBoxLayout, QLineEdit, QPushButton, - QDialog, QScrollArea, QComboBox, QVBoxLayout, QLabel + QSizePolicy, + QSpacerItem, + QWidget, + QHBoxLayout, + QLineEdit, + QPushButton, + QDialog, + QScrollArea, + QComboBox, + QVBoxLayout, + QLabel, ) -from .Ui_PyRegExpWizardCharactersDialog import ( - Ui_PyRegExpWizardCharactersDialog -) +from .Ui_PyRegExpWizardCharactersDialog import Ui_PyRegExpWizardCharactersDialog -class PyRegExpWizardCharactersDialog( - QDialog, Ui_PyRegExpWizardCharactersDialog): +class PyRegExpWizardCharactersDialog(QDialog, Ui_PyRegExpWizardCharactersDialog): """ Class implementing a dialog for entering character classes. """ - specialChars = { - 4: "\\a", - 5: "\\f", - 6: "\\n", - 7: "\\r", - 8: "\\t", - 9: "\\v" - } - + + specialChars = {4: "\\a", 5: "\\f", 6: "\\n", 7: "\\r", 8: "\\t", 9: "\\v"} + predefinedClasses = ["\\s", "\\S", "\\w", "\\W", "\\d", "\\D"] - + def __init__(self, parent=None): """ Constructor - + @param parent parent widget (QWidget) """ super().__init__(parent) self.setupUi(self) - + self.comboItems = [] - self.singleComboItems = [] # these are in addition to the above + self.singleComboItems = [] # these are in addition to the above self.comboItems.append(self.tr("Normal character")) - self.comboItems.append( - self.tr("Unicode character in hexadecimal notation")) - self.comboItems.append( - self.tr("Unicode character in octal notation")) + self.comboItems.append(self.tr("Unicode character in hexadecimal notation")) + self.comboItems.append(self.tr("Unicode character in octal notation")) self.singleComboItems.append(self.tr("---")) self.singleComboItems.append(self.tr("Bell character (\\a)")) self.singleComboItems.append(self.tr("Page break (\\f)")) @@ -58,14 +56,17 @@ self.singleComboItems.append(self.tr("Carriage return (\\r)")) self.singleComboItems.append(self.tr("Horizontal tabulator (\\t)")) self.singleComboItems.append(self.tr("Vertical tabulator (\\v)")) - + self.charValidator = QRegularExpressionValidator( - QRegularExpression(".{0,1}"), self) + QRegularExpression(".{0,1}"), self + ) self.hexValidator = QRegularExpressionValidator( - QRegularExpression("[0-9a-fA-F]{0,4}"), self) + QRegularExpression("[0-9a-fA-F]{0,4}"), self + ) self.octValidator = QRegularExpressionValidator( - QRegularExpression("[0-3]?[0-7]{0,2}"), self) - + QRegularExpression("[0-3]?[0-7]{0,2}"), self + ) + # generate dialog part for single characters self.singlesBoxLayout = QVBoxLayout(self.singlesBox) self.singlesBoxLayout.setObjectName("singlesBoxLayout") @@ -75,7 +76,7 @@ self.singlesView = QScrollArea(self.singlesBox) self.singlesView.setObjectName("singlesView") self.singlesBoxLayout.addWidget(self.singlesView) - + self.singlesItemsBox = QWidget(self) self.singlesView.setWidget(self.singlesItemsBox) self.singlesItemsBox.setObjectName("singlesItemsBox") @@ -85,21 +86,23 @@ self.singlesItemsBox.setLayout(self.singlesItemsBoxLayout) self.singlesEntries = [] self.__addSinglesLine() - + hlayout0 = QHBoxLayout() hlayout0.setContentsMargins(0, 0, 0, 0) hlayout0.setSpacing(6) hlayout0.setObjectName("hlayout0") self.moreSinglesButton = QPushButton( - self.tr("Additional Entries"), self.singlesBox) + self.tr("Additional Entries"), self.singlesBox + ) self.moreSinglesButton.setObjectName("moreSinglesButton") hlayout0.addWidget(self.moreSinglesButton) hspacer0 = QSpacerItem( - 30, 20, QSizePolicy.Policy.Expanding, QSizePolicy.Policy.Minimum) + 30, 20, QSizePolicy.Policy.Expanding, QSizePolicy.Policy.Minimum + ) hlayout0.addItem(hspacer0) self.singlesBoxLayout.addLayout(hlayout0) self.moreSinglesButton.clicked.connect(self.__addSinglesLine) - + # generate dialog part for character ranges self.rangesBoxLayout = QVBoxLayout(self.rangesBox) self.rangesBoxLayout.setObjectName("rangesBoxLayout") @@ -109,7 +112,7 @@ self.rangesView = QScrollArea(self.rangesBox) self.rangesView.setObjectName("rangesView") self.rangesBoxLayout.addWidget(self.rangesView) - + self.rangesItemsBox = QWidget(self) self.rangesView.setWidget(self.rangesItemsBox) self.rangesItemsBox.setObjectName("rangesItemsBox") @@ -119,21 +122,23 @@ self.rangesItemsBox.setLayout(self.rangesItemsBoxLayout) self.rangesEntries = [] self.__addRangesLine() - + hlayout1 = QHBoxLayout() hlayout1.setContentsMargins(0, 0, 0, 0) hlayout1.setSpacing(6) hlayout1.setObjectName("hlayout1") self.moreRangesButton = QPushButton( - self.tr("Additional Entries"), self.rangesBox) + self.tr("Additional Entries"), self.rangesBox + ) self.moreSinglesButton.setObjectName("moreRangesButton") hlayout1.addWidget(self.moreRangesButton) hspacer1 = QSpacerItem( - 30, 20, QSizePolicy.Policy.Expanding, QSizePolicy.Policy.Minimum) + 30, 20, QSizePolicy.Policy.Expanding, QSizePolicy.Policy.Minimum + ) hlayout1.addItem(hspacer1) self.rangesBoxLayout.addLayout(hlayout1) self.moreRangesButton.clicked.connect(self.__addRangesLine) - + def __addSinglesLine(self): """ Private slot to add a line of entry widgets for single characters. @@ -160,18 +165,16 @@ le2.setValidator(self.charValidator) hboxLayout.addWidget(le2) self.singlesItemsBoxLayout.addWidget(hbox) - - cb1.activated[int].connect( - lambda i: self.__singlesCharTypeSelected(i, cb1)) - cb2.activated[int].connect( - lambda i: self.__singlesCharTypeSelected(i, cb2)) + + cb1.activated[int].connect(lambda i: self.__singlesCharTypeSelected(i, cb1)) + cb2.activated[int].connect(lambda i: self.__singlesCharTypeSelected(i, cb2)) hbox.show() - + self.singlesItemsBox.adjustSize() - + self.singlesEntries.append([cb1, le1]) self.singlesEntries.append([cb2, le2]) - + def __addRangesLine(self): """ Private slot to add a line of entry widgets for character ranges. @@ -196,20 +199,19 @@ le2.setValidator(self.charValidator) hboxLayout.addWidget(le2) self.rangesItemsBoxLayout.addWidget(hbox) - - cb1.activated[int].connect( - lambda i: self.__rangesCharTypeSelected(i, cb1)) - + + cb1.activated[int].connect(lambda i: self.__rangesCharTypeSelected(i, cb1)) + hbox.show() - + self.rangesItemsBox.adjustSize() - + self.rangesEntries.append([cb1, le1, le2]) - + def __performSelectedAction(self, index, lineedit): """ Private method performing some actions depending on the input. - + @param index selected list index (integer) @param lineedit line edit widget to act on (QLineEdit) """ @@ -224,12 +226,12 @@ elif index > 3: lineedit.setEnabled(False) lineedit.clear() - + def __singlesCharTypeSelected(self, index, combo): """ Private slot to handle the activated(int) signal of the single chars combo boxes. - + @param index selected list index @type int @param combo reference to the combo box @@ -239,12 +241,12 @@ if combo == entriesList[0]: self.__performSelectedAction(index, entriesList[1]) break - + def __rangesCharTypeSelected(self, index, combo): """ Private slot to handle the activated(int) signal of the char ranges combo boxes. - + @param index selected list index @type int @param combo reference to the combo box @@ -255,11 +257,11 @@ self.__performSelectedAction(index, entriesList[1]) self.__performSelectedAction(index, entriesList[2]) break - + def __formatCharacter(self, index, char): """ Private method to format the characters entered into the dialog. - + @param index selected list index (integer) @param char character string enetered into the dialog (string) @return formated character string (string) @@ -275,19 +277,19 @@ return self.specialChars[index] except KeyError: return "" - + def getCharacters(self): """ Public method to return the character string assembled via the dialog. - + @return formatted string for character classes (string) """ regexp = "" - + # negative character range if self.negativeCheckBox.isChecked(): regexp += "^" - + # predefined character ranges if self.wordCharCheckBox.isChecked(): regexp += "\\w" @@ -301,34 +303,30 @@ regexp += "\\s" if self.nonWhitespaceCheckBox.isChecked(): regexp += "\\S" - + # single characters for entrieslist in self.singlesEntries: index = entrieslist[0].currentIndex() char = entrieslist[1].text() regexp += self.__formatCharacter(index, char) - + # character ranges for entrieslist in self.rangesEntries: - if ( - entrieslist[1].text() == "" or - entrieslist[2].text() == "" - ): + if entrieslist[1].text() == "" or entrieslist[2].text() == "": continue index = entrieslist[0].currentIndex() char1 = entrieslist[1].text() char2 = entrieslist[2].text() regexp += "{0}-{1}".format( self.__formatCharacter(index, char1), - self.__formatCharacter(index, char2)) - + self.__formatCharacter(index, char2), + ) + if regexp: if ( - (len(regexp) == 2 and - (regexp in self.predefinedClasses or - regexp in self.specialChars)) or - len(regexp) == 1 - ): + len(regexp) == 2 + and (regexp in self.predefinedClasses or regexp in self.specialChars) + ) or len(regexp) == 1: return regexp else: return "[{0}]".format(regexp)