def pre_commit_check(self): definitions = self.get_definitions() # Verify the search/replace in the edit widgets has been # included to the list of search/replace definitions edit_search = self.sr_search.regex if edit_search: edit_replace = str(self.sr_replace.text()) found = False for search, replace in definitions: if search == edit_search and replace == edit_replace: found = True break if not found and not question_dialog( self, _('Unused search & replace definition'), _('The search/replace definition being edited ' ' has not been added to the list of definitions. ' 'Do you wish to continue with the conversion ' '(the definition will not be used)?')): return False # Verify all search expressions are valid for search, replace in definitions: try: compile_regular_expression(search) except Exception as err: error_dialog(self, _('Invalid regular expression'), _('Invalid regular expression: %s') % err, show=True) return False return True
def pre_commit_check(self): definitions = self.get_definitions() # Verify the search/replace in the edit widgets has been # included to the list of search/replace definitions edit_search = self.sr_search.regex if edit_search: edit_replace = unicode_type(self.sr_replace.text()) found = False for search, replace in definitions: if search == edit_search and replace == edit_replace: found = True break if not found and not question_dialog(self, _('Unused search & replace definition'), _('The search/replace definition being edited ' ' has not been added to the list of definitions. ' 'Do you wish to continue with the conversion ' '(the definition will not be used)?')): return False # Verify all search expressions are valid for search, replace in definitions: try: compile_regular_expression(search) except Exception as err: error_dialog(self, _('Invalid regular expression'), _('Invalid regular expression: %s')%err, show=True) return False return True
def do_test(self): selections = [] self.match_locs = [] if self.regex_valid(): text = unicode_type(self.preview.toPlainText()) regex = unicode_type(self.regex.text()) cursor = QTextCursor(self.preview.document()) extsel = QTextEdit.ExtraSelection() extsel.cursor = cursor extsel.format.setBackground(QBrush(Qt.yellow)) try: for match in compile_regular_expression(regex).finditer(text): es = QTextEdit.ExtraSelection(extsel) es.cursor.setPosition(match.start(), QTextCursor.MoveAnchor) es.cursor.setPosition(match.end(), QTextCursor.KeepAnchor) selections.append(es) self.match_locs.append((match.start(), match.end())) except: pass self.preview.setExtraSelections(selections) if self.match_locs: self.next.setEnabled(True) self.previous.setEnabled(True) self.occurrences.setText(unicode_type(len(self.match_locs)))
def do_test(self): selections = [] self.match_locs = [] class Pos: python: int = 0 qt: int = 0 if self.regex_valid(): text = str(self.preview.toPlainText()) regex = str(self.regex.text()) cursor = QTextCursor(self.preview.document()) extsel = QTextEdit.ExtraSelection() extsel.cursor = cursor extsel.format.setBackground(QBrush(Qt.GlobalColor.yellow)) with suppress(Exception): prev = Pos() for match in compile_regular_expression(regex).finditer(text): es = QTextEdit.ExtraSelection(extsel) qtchars_to_start = utf16_length( text[prev.python:match.start()]) qt_pos = prev.qt + qtchars_to_start prev.python = match.end() prev.qt = qt_pos + utf16_length(match.group()) es.cursor.setPosition(qt_pos, QTextCursor.MoveMode.MoveAnchor) es.cursor.setPosition(prev.qt, QTextCursor.MoveMode.KeepAnchor) selections.append(es) self.match_locs.append((qt_pos, prev.qt)) self.preview.setExtraSelections(selections) if self.match_locs: self.next.setEnabled(True) self.previous.setEnabled(True) self.occurrences.setText(str(len(self.match_locs)))
def regex_valid(self): regex = unicode_type(self.regex.text()) if regex: try: compile_regular_expression(regex) self.regex.setStyleSheet('QLineEdit { color: black; background-color: rgba(0,255,0,20%); }') return True except: self.regex.setStyleSheet('QLineEdit { color: black; background-color: rgb(255,0,0,20%); }') else: self.regex.setStyleSheet('QLineEdit { color: black; background-color: white; }') self.preview.setExtraSelections([]) self.match_locs = [] self.next.setEnabled(False) self.previous.setEnabled(False) self.occurrences.setText('0') return False
def regex_valid(self): regex = str(self.regex.text()) if regex: try: compile_regular_expression(regex) self.regex.setStyleSheet('QLineEdit { color: black; background-color: rgba(0,255,0,20%); }') return True except: self.regex.setStyleSheet('QLineEdit { color: black; background-color: rgba(255,0,0,20%); }') else: self.regex.setStyleSheet('QLineEdit { color: black; background-color: white; }') self.preview.setExtraSelections([]) self.match_locs = [] self.next.setEnabled(False) self.previous.setEnabled(False) self.occurrences.setText('0') return False
def do_search_replace(search_pattern, replace_txt): from calibre.ebooks.conversion.search_replace import compile_regular_expression try: search_re = compile_regular_expression(search_pattern) if not replace_txt: replace_txt = '' rules.insert(0, (search_re, replace_txt)) user_sr_rules[(search_re, replace_txt)] = search_pattern except Exception as e: self.log.error('Failed to parse %r regexp because %s' % (search, as_unicode(e)))
def do_test(self): selections = [] self.match_locs = [] if self.regex_valid(): text = unicode_type(self.preview.toPlainText()) regex = unicode_type(self.regex.text()) cursor = QTextCursor(self.preview.document()) extsel = QTextEdit.ExtraSelection() extsel.cursor = cursor extsel.format.setBackground(QBrush(Qt.yellow)) try: for match in compile_regular_expression(regex).finditer(text): es = QTextEdit.ExtraSelection(extsel) es.cursor.setPosition(match.start(), QTextCursor.MoveAnchor) es.cursor.setPosition(match.end(), QTextCursor.KeepAnchor) selections.append(es) self.match_locs.append((match.start(), match.end())) except: pass self.preview.setExtraSelections(selections) if self.match_locs: self.next.setEnabled(True) self.previous.setEnabled(True) self.occurrences.setText(str(len(self.match_locs)))