def replace(self, oldString, newString): changesMade = 0 search = self.unoObjs.document.createSearchDescriptor() search.SearchString = oldString search.SearchAll = True search.SearchWords = True search.SearchCaseSensitive = False selsFound = self.unoObjs.document.findAll(search) self.selsCount = selsFound.getCount() if selsFound.getCount() == 0: logger.debug("Did not find anything.") return changesMade for selIndex, oSel in enumerate(iteruno.byIndex(selsFound)): logger.debug("Found selection %s", selIndex) if self.askEach: self.unoObjs.viewcursor.gotoRange(oSel.getStart(), False) self.unoObjs.viewcursor.gotoRange(oSel.getEnd(), True) result = self.msgboxFour.display( "Make this change? (%s -> %s)", oldString, newString) if result == "yes": # keep going pass elif result == "no": continue elif result == "yesToAll": self.askEach = False else: raise exceptions.UserInterrupt() self.unoObjs.viewcursor.goRight(0, False) # deselect oTextCursTmp = oSel.getText().createTextCursorByRange(oSel) changeString(oTextCursTmp, newString) changesMade += 1 return changesMade
def changeTextRange(self, txtRange): logger.debug(util.funcName('begin')) oSel = txtRange.sel try: oCursor = oSel.getText().createTextCursorByRange(oSel) except (RuntimeException, IllegalArgumentException): logger.warning("Failed to go to text range.") return logger.debug(u"String = '%r'", oCursor.getString()) if self.askEach: if self.unoObjs.viewcursor: self.unoObjs.viewcursor.gotoRange(oSel.getStart(), False) self.unoObjs.viewcursor.gotoRange(oSel.getEnd(), True) # select else: self.unoObjs.controller.select(oSel) result = self.msgboxFour.display("Make this change?") if not self.unoObjs.viewcursor: self.unoObjs.controller.select(None) if result == "yes": # keep going pass elif result == "no": return False elif result == "yesToAll": self.askEach = False else: raise exceptions.UserInterrupt() return self.convertString(oCursor)
def getFormResults(self, verify=True): """Reads form fields and gets settings. If verify is True, raises an exception if there is a problem. """ logger.debug(util.funcName('begin')) exportType = "" if self.dlgCtrls.optReplacementCCT.getState() == 1: # selected exportType = "ReplacementCCT" elif self.dlgCtrls.optSFM_CCT.getState() == 1: exportType = "SFM_CCT" sfMarkers = self.dlgCtrls.txtSFM.getText().strip() if verify and sfMarkers == "": ok = self.msgbox.displayOkCancel( "No SF markers were specified. Continue anyway?") if not ok: raise exceptions.UserInterrupt() self.userVars.store("SFM_Markers", sfMarkers) self.app.setSFM(sfMarkers) elif self.dlgCtrls.optXSLT.getState() == 1: exportType = "XSLT" if verify and self.dlgCtrls.listXpaths.getItemCount() == 0: ok = self.msgbox.displayOkCancel( "No Xpath expressions were specified. Continue anyway?") if not ok: raise exceptions.UserInterrupt() self.userVars.store("XSLT_MatchPartial", str(self.dlgCtrls.chkMatchPartial.getState())) self.app.setMatchPartial( self.dlgCtrls.chkMatchPartial.getState() == 1) self.userVars.store("XpathCount", str(self.dlgCtrls.listXpaths.getItemCount())) stringList = dutil.listbox_items(self.dlgCtrls.listXpaths) for exprNum, exprVal in enumerate(stringList): varname = "XpathExpr%02d" % exprNum self.userVars.store(varname, exprVal) self.app.setXpathExprs(stringList) self.userVars.store("ExportType", exportType) self.app.setExportType(exportType) filepath = self.dlgCtrls.txtFilePath.getText().strip() if verify and filepath == "": raise exceptions.ChoiceProblem("Please specify a file to export.") self.userVars.store("Filepath", filepath) self.app.setFilepath(filepath) logger.debug(util.funcName('end'))
def askInterrupt(self, refnumFound): """ Updated the same number twice. It might be an infinite loop. """ logger.debug("Repeated ex %d times", self.repeatedCount) self.repeatedCount += 1 MAX_REPETITIONS = 5 dummy_div, mod = divmod(self.repeatedCount, MAX_REPETITIONS) if self.repeatedCount > 0 and mod == 0: refnumDisplay = refnumFound.strip() if not self.msgbox.displayOkCancel( "Updated '%s' %d times in a row. Keep going?", refnumDisplay, self.repeatedCount): raise exceptions.UserInterrupt()
def applyCorrection(self, wordText): """Returns True if a change was made.""" newWord = self.goodList.changeDict[self.goodList.firstLower(wordText)] if self.askEach: result = self.msgboxFour.display( "Make this change? (%s -> %s)", wordText, newWord) if result == 'yes': pass elif result == 'no': return False elif result == 'yesToAll': self.askEach = False else: raise exceptions.UserInterrupt() self.rangeJumper.changeString(self.addPunct(newWord)) return True
def changeTextRange(self, txtRange): rangeJumper = RangeJumper(self.unoObjs) rangeJumper.setTextRange(txtRange) rangeTokens = getTokens(rangeJumper.getString()) tokenNum = -2 # because the loop starts by += 2 while True: tokenNum += 2 # tokens are in pairs: word, delim logger.debug("Token '%d' of %d", tokenNum, len(rangeTokens)) if tokenNum >= len(rangeTokens): break word = rangeTokens[tokenNum].strip(self.config.punctuation) #word = re.sub(self.config.punct_expr, "", rangeTokens[tokenNum]) wordLower = self.goodList.firstLower(word) wordNoAffix = self.wordAsker.removeAffixes(wordLower) suspect = True if not word: suspect = False elif word.isdigit() or word.isspace(): suspect = False elif wordLower in self.goodList or wordNoAffix in self.goodList: suspect = False elif wordLower in self.wordAsker.wordsToIgnore: suspect = False if self.config.whichTask == 'ApplyCorrections': suspect = wordLower in self.goodList.changeDict if suspect: logger.debug("Word '%s' is suspect", word) try: rangeJumper.selectWord( "".join(rangeTokens[:tokenNum]), rangeTokens[tokenNum]) except exceptions.RangeError: if self.msgbox.displayOkCancel( "Missed word '%s'. Keep going?", word): continue else: raise exceptions.UserInterrupt() if self.wordAsker.handleWord( word, rangeTokens, tokenNum, rangeJumper): self.numChanges += 1 rangeTokens = getTokens(rangeJumper.getString()) tokensBefore = getTokens(rangeJumper.getStringBefore()) tokenNum = len(tokensBefore) tokenNum -= tokenNum % 2 # make sure it's even
def checkSpelling(self, wordText, context): suggestList = self.goodList.suggestions.getSuggestions(wordText) if not self.config.matchCase: ## Suggest only words of the same case as the word found. # Non-roman characters will not be changed. firstChar = wordText[:1] if firstChar.isupper(): suggestList = util.uniqueList( [s.capitalize() for s in suggestList]) elif firstChar.islower(): suggestList = util.uniqueList( [s.lower() for s in suggestList]) if not self.dlgReplace: self.dlgReplace = DlgSpellingReplace(self.unoObjs) self.dlgReplace.makeDlg() self.dlgReplace.setContents(wordText, suggestList, context) self.dlgReplace.doExecute() action, changeTo = self.dlgReplace.getResults() if action == 'Ignore': # just keep going return False elif action == 'IgnoreAll': self.wordsToIgnore.add(self.goodList.firstLower(wordText)) return False elif action == 'Change': self.rangeJumper.changeString(self.addPunct(changeTo)) return True elif action == 'ChangeAll': self.rangeJumper.changeString(self.addPunct(changeTo)) replacer = FindAndReplace(self.unoObjs, False) replacer.replace(wordText, changeTo) return True elif action == 'Add': self.goodList.add(self.removeAffixes(wordText)) return False # user probably pressed Close or x'd out of the dialog raise exceptions.UserInterrupt()