示例#1
0
 def getRanges(self):
     progressBar = ProgressBar(self.unoObjs, "Finding text...")
     progressBar.show()
     progressBar.updateBeginning()
     textSearch = TextSearch(self.unoObjs, progressBar)
     textSearch.setConfig(self.config.searchConfig)
     try:
         if self.config.whichScope == 'WholeDoc':
             textSearch.scopeWholeDocTraverse()
         elif self.config.whichScope == 'Selection':
             textSearch.scopeSelection()
         elif self.config.whichScope == 'Language':
             textSearch.scopeLocale()
         elif self.config.whichScope == 'ParaStyle':
             textSearch.scopeParaStyle()
         elif self.config.whichScope == 'CharStyle':
             textSearch.scopeCharStyle()
         elif self.config.whichScope == 'Font':
             textSearch.scopeFont()
         elif self.config.whichScope == 'SFMs':
             textSearch.scopeSFMs()
         else:
             raise exceptions.LogicError(
                 "Unexpected value %s", self.config.whichScope)
         progressBar.updateFinishing()
     except exceptions.MessageError as exc:
         raise exc
     finally:
         progressBar.close()
     return textSearch.getRanges()
示例#2
0
 def __init__(self, unoObjs):
     if self.__class__ is FileReader:
         # The base class should not be instantiated.
         raise NotImplementedError()
     self.unoObjs = unoObjs
     self.msgbox = MessageBox(unoObjs)
     self.progressBar = ProgressBar(unoObjs, "Loading data...")
     self.data = None  # typically a list or dict
     self.dom = None
     self.filepath = ""
示例#3
0
 def setUp(self):
     unoObjs = testutil.unoObjsForCurrentDoc()
     fileconfig = fileitemlist.WordListFileItem(None)
     docReader = DocReader(fileconfig, unoObjs, -1)
     docReader.loadDoc(self.INFILE)
     self.unoObjs = docReader.doc
     testutil.stored.doc = self.unoObjs.document
     self.progressBar = ProgressBar(self.unoObjs, "TestSearch")
    def findOccurrences(self, abbrevList):
        """Modifies abbrevList."""
        progressBar = ProgressBar(self.unoObjs, "Searching for occurrences...")
        progressBar.show()
        progressBar.updateBeginning()
        progressRange = ProgressRange(ops=len(abbrevList), pbar=progressBar)
        for itemPos in range(0, len(abbrevList)):
            search = self.unoObjs.document.createSearchDescriptor()
            search.SearchString = abbrevList[itemPos].abbrevText
            search.SearchCaseSensitive = False
            search.SearchWords = True

            selectionsFound = self.unoObjs.document.findAll(search)
            occurrences = selectionsFound.getCount()
            abbrevList.setOccurrences(itemPos, occurrences)
            progressRange.update(itemPos)
        progressBar.updateFinishing()
        progressBar.close()
示例#5
0
    def outputList(self, wordList, progressBarWriter):
        """Sends output to the Calc spreadsheet.
        Takes a list of app.wordlist.WordInList.
        """
        logger.debug(util.funcName('begin'))
        outputter = SpreadsheetOutput(self.unoObjs)
        self.listDoc = outputter.createSpreadsheet()
        self.msgbox = MessageBox(self.listDoc)
        self.sheet = self.listDoc.sheets.getByIndex(0)

        progressBarCalc = ProgressBar(self.listDoc, "Generating List...")
        progressBarCalc.show()
        self.progressRanges = ProgressRanges(
            [progressBarWriter, progressBarCalc])
        self.progressRanges.initRanges(progressBarWriter.getPercent() + 20, 95,
                                       len(wordList))
        self.progressRanges.updateStart()
        try:
            self._outputList(wordList)
            self.progressRanges.updateFinishing()
        finally:
            self.progressRanges.closeBars()
        logger.debug(util.funcName('end'))
 def generateList(self, punctToRemove, normForm, outputToCalc=True):
     """Harvest words from various files.
     If outputToCalc is True, then output a word list in Calc.
     """
     logger.debug(util.funcName('begin'))
     all_words_read = []
     self.progressBar = ProgressBar(self.unoObjs, "Reading...")
     self.progressBar.show()
     self.progressBar.updateBeginning()
     progressRange = ProgressRange(
         ops=len(self.fileItems), pbar=self.progressBar)
     try:
         for fileItemIndex, fileItem in enumerate(self.fileItems):
             try:
                 new_words = self._harvestWords(fileItem)
                 all_words_read.extend(new_words)
                 logger.debug("Word count: %d", len(all_words_read))
             except (exceptions.DataNotFoundError,
                     exceptions.FileAccessError) as exc:
                 self.msgbox.displayExc(exc)
             progressRange.update(fileItemIndex)
         self.progressBar.updateFinishing()
     finally:
         self.progressBar.close()
     self.progressBar = ProgressBar(self.unoObjs, "Sorting...")
     self.progressBar.show()
     self.progressBar.updateBeginning()
     try:
         splitByWhitespace = True
         if len(self.fileItems) > 0:
             splitByWhitespace = self.fileItems[0].splitByWhitespace
         self.words = organizeList(
             all_words_read, punctToRemove, splitByWhitespace, normForm,
             self.progressBar)
         self.progressBar.updateFinishing()
     finally:
         self.progressBar.close()
     if self.words or len(self.fileItems) == 0:
         if outputToCalc:
             self.progressBar = ProgressBar(
                 self.unoObjs, "Generating List...")
             self.progressBar.show()
             self.progressBar.updateBeginning()
             try:
                 self._generateCalcList()
                 self.progressBar.updateFinishing()
             finally:
                 self.progressBar.close()
         else:
             self.msgbox.display("Found %d words.", len(self.words))
     else:
         self.msgbox.display("Did not find any words for the list.")
示例#7
0
class FileReader:
    """Abstract base class for XML file readers.
    The methods beginning with a single underscore are intended to only be
    called from the base class.
    """
    SUPPORTED_FORMATS = []  # list of tuples of name, text description

    def __init__(self, unoObjs):
        if self.__class__ is FileReader:
            # The base class should not be instantiated.
            raise NotImplementedError()
        self.unoObjs = unoObjs
        self.msgbox = MessageBox(unoObjs)
        self.progressBar = ProgressBar(unoObjs, "Loading data...")
        self.data = None  # typically a list or dict
        self.dom = None
        self.filepath = ""

    @classmethod
    def supportedNames(cls):
        names = [name for name, desc in cls.SUPPORTED_FORMATS]
        return names

    def read(self):
        logger.debug(util.funcName('begin'))
        if self.progressBar:
            self.progressBar.show()
            self.progressBar.updateBeginning()
            self.progressBar.updatePercent(20)
        self._initData()
        try:
            self._read()
            self._verifyDataFound()
            if self.progressBar:
                self.progressBar.updateFinishing()
        finally:
            if self.progressBar:
                self.progressBar.close()
        logger.debug(util.funcName('end'))
        return self.data

    def _initData(self):
        # All derived classes should implement this method.
        raise NotImplementedError()

    def _read(self):
        # All derived classes should implement this method.
        raise NotImplementedError()

    def _verifyDataFound(self):
        """Derived classes should override if they don't want this check to
        be performed.
        """
        if not self.data:
            raise exceptions.DataNotFoundError(
                "Did not find any data in file %s", self.filepath)

    def getSuggestions(self):
        """Get suggested ref numbers.  Intended for linguistic examples only,
        so derived classes are not required to override this method.
        """
        return []
 def make(self):
     logger.debug(util.funcName('begin'))
     progressBar = ProgressBar(self.unoObjs, "Getting data...")
     progressBar.show()
     progressBar.updateBeginning()
     try:
         columnOrder = ColumnOrder(self.userVars)
         columnOrder.loadUserVars()
         changeList = getChangeList(self.unoObjs, columnOrder)
         progressBar.updateFinishing()
     except exceptions.DocAccessError:
         self.msgbox.display("Error reading spreadsheet.")
     progressBar.close()
     progressBar = ProgressBar(self.unoObjs, "Saving file...")
     progressBar.show()
     progressBar.updatePercent(50)
     if self.exportType == "ReplacementCCT":
         outputter = CCT_Writer(self.filepath)
         outputter.writeSimpleReplacements(changeList)
     elif self.exportType == "SFM_CCT":
         outputter = CCT_Writer(self.filepath)
         outputter.writeComplete(changeList, self.sfMarkers)
     elif self.exportType == "XSLT":
         outputter = XSLT_Writer(self.filepath)
         outputter.write(changeList, self.xpathExprs, self.matchPartial)
     progressBar.updateFinishing()
     progressBar.close()
     logger.debug(util.funcName('end'))
class WordList:

    def __init__(self, writerUnoObjs, fileItems, columnOrder, userVars):
        self.unoObjs = writerUnoObjs
        self.fileItems = fileItems    # FileItemList of WordListFileItem
        self.columnOrder = columnOrder
        self.userVars = userVars
        self.msgbox = MessageBox(self.unoObjs)
        self.words = []
        self.progressBar = None

    def generateList(self, punctToRemove, normForm, outputToCalc=True):
        """Harvest words from various files.
        If outputToCalc is True, then output a word list in Calc.
        """
        logger.debug(util.funcName('begin'))
        all_words_read = []
        self.progressBar = ProgressBar(self.unoObjs, "Reading...")
        self.progressBar.show()
        self.progressBar.updateBeginning()
        progressRange = ProgressRange(
            ops=len(self.fileItems), pbar=self.progressBar)
        try:
            for fileItemIndex, fileItem in enumerate(self.fileItems):
                try:
                    new_words = self._harvestWords(fileItem)
                    all_words_read.extend(new_words)
                    logger.debug("Word count: %d", len(all_words_read))
                except (exceptions.DataNotFoundError,
                        exceptions.FileAccessError) as exc:
                    self.msgbox.displayExc(exc)
                progressRange.update(fileItemIndex)
            self.progressBar.updateFinishing()
        finally:
            self.progressBar.close()
        self.progressBar = ProgressBar(self.unoObjs, "Sorting...")
        self.progressBar.show()
        self.progressBar.updateBeginning()
        try:
            splitByWhitespace = True
            if len(self.fileItems) > 0:
                splitByWhitespace = self.fileItems[0].splitByWhitespace
            self.words = organizeList(
                all_words_read, punctToRemove, splitByWhitespace, normForm,
                self.progressBar)
            self.progressBar.updateFinishing()
        finally:
            self.progressBar.close()
        if self.words or len(self.fileItems) == 0:
            if outputToCalc:
                self.progressBar = ProgressBar(
                    self.unoObjs, "Generating List...")
                self.progressBar.show()
                self.progressBar.updateBeginning()
                try:
                    self._generateCalcList()
                    self.progressBar.updateFinishing()
                finally:
                    self.progressBar.close()
            else:
                self.msgbox.display("Found %d words.", len(self.words))
        else:
            self.msgbox.display("Did not find any words for the list.")

    def _harvestWords(self, fileItem):
        """Harvest words from the specified file."""
        fileType = fileItem.filetype  # short variable name
        logger.debug(util.funcName(args=fileType))
        words = []
        if fileType in WordsReader.supportedNames():
            reader = WordsReader(fileItem, self.unoObjs)
            words = reader.read()
        elif fileType in SFM_Reader.supportedNames():
            reader = SFM_Reader(fileItem, self.unoObjs)
            words = reader.read()
        elif fileType in InterlinReader.supportedNames():
            config = fileitemlist.InterlinInputSettings(self.userVars)
            config.showMorphLine2 = True
            config.separateMorphColumns = True
            lingExFileItem = fileitemlist.LingExFileItem(self.userVars)
            lingExFileItem.filepath = fileItem.filepath
            config.fileList.addItem(lingExFileItem)
            reader = InterlinReader(self.unoObjs, self.userVars, config)
            words = reader.grabWords(fileItem.thingsToGrab)
        elif fileType in PhonReader.supportedNames():
            config = lingex_structs.PhonInputSettings(self.userVars)
            config.filepath = fileItem.filepath
            config.phoneticWS = fileItem.writingSystem
            config.isLexemePhonetic = True
            phonUserVars = UserVars(
                Prefix.PHONOLOGY, self.unoObjs.document, logger)
            if phonUserVars.get("FlexLexeme") == 'phonemic':
                config.isLexemePhonetic = False
            reader = PhonReader(self.unoObjs, self.userVars, config)
            words = reader.grabWords(fileItem.thingsToGrab)
        elif fileType in DocReader.supportedNames():
            settings = TextSearchSettings()
            settings.load_userVars(self.userVars)
            reader = DocReader(fileItem, self.unoObjs, settings.matchesLimit)
            words = reader.read()
        elif fileType in CalcFileReader.supportedNames():
            reader = CalcFileReader(self.unoObjs)
            reader.setFileConfig(fileItem)
            words = reader.read()
        return words


    def _generateCalcList(self):
        """Generate list in calc."""
        listOutput = WordlistIO(self.unoObjs, self.columnOrder)
        listOutput.outputList(self.words, self.progressBar)
        msgbox = listOutput.getMsgbox()  # for Calc spreadsheet

        ## Copy some user vars for the Spelling component.

        userVarsSp = UserVars(
            Prefix.SPELLING, self.unoObjs.document, logger)
        varname = "HasSettings"
        userVarsSp.store(varname, self.userVars.get(varname))
        columnOrderSp = ColumnOrder(userVarsSp)
        columnOrderSp.sortOrder = self.columnOrder.sortOrder
        columnOrderSp.storeUserVars()

        # Initialize some user vars for Calc dialogs.  We do this here
        # to reset properly if a new word list is made.
        self.userVars.store("ConvSourceColumn",
                            self.columnOrder.getColLetter('colWord'))
        self.userVars.store("ConvTargetColumn",
                            self.columnOrder.getColLetter('colConv1'))
        userVarsSp.store("CurrentRow", "")
        msgbox.display("Made list of %d words.", len(self.words))
    def doConversions_draw(self):
        """For converting data in a Draw doc."""
        logger.debug(util.funcName('begin'))

        ## Start progress bar

        progressBar = ProgressBar(self.unoObjs, "Converting...")
        progressBar.show()
        progressBar.updateBeginning()

        ## Find the text ranges

        shapeSearch = ShapeSearch(self.unoObjs, progressBar)
        shapeSearch.setConfig(self.config.searchConfig)
        try:
            if self.config.whichScope == 'WholeDoc':
                shapeSearch.scopeWholeDoc()
            elif self.config.whichScope == 'Selection':
                shapeSearch.scopeSelection()
            elif self.config.whichScope == 'Font':
                shapeSearch.scopeFont()
            else:
                raise exceptions.LogicError("Unexpected value %s",
                                            self.config.whichScope)
        except (exceptions.RangeError, exceptions.LogicError) as exc:
            self.msgbox.displayExc(exc)
            progressBar.close()
            return
        rangesFound = shapeSearch.getRanges()

        if progressBar.getPercent() < 40:
            progressBar.updatePercent(40)

        ## Do the changes to those ranges

        textChanger = TextChanger(self.unoObjs, progressBar,
                                  self.changerSettings)
        if self.secCall.config.convName:
            textChanger.setConverterCall(self.secCall)
        textChanger.setFontToChange(self.config.targetFont)
        # Apparently, if we change text in front of a range in Draw,
        # the range can move.  So, start from the end and work backwards.
        rangesFound.reverse()
        numDataChanges, numStyleChanges = textChanger.doChanges(
            rangesFound, self.config.askEach)

        progressBar.updateFinishing()
        progressBar.close()

        ## Display results

        paragraphsFound = len(rangesFound)
        if paragraphsFound == 0:
            self.msgbox.display("Did not find scope of change.")
        elif numDataChanges == 0:
            if numStyleChanges == 0:
                self.msgbox.display("No changes.")
            else:
                plural = "" if numStyleChanges == 1 else "s"
                # add "s" if plural
                self.msgbox.display(
                    "No changes, but modified style of %d paragraph%s.",
                    numStyleChanges, plural)
        elif paragraphsFound == 1:
            plural = "" if numDataChanges == 1 else "s"  # add "s" if plural
            self.msgbox.display("Made %d change%s.", numDataChanges, plural)
        else:
            plural = "" if numDataChanges == 1 else "s"  # add "s" if plural
            self.msgbox.display("Found %d paragraphs and made %d change%s.",
                                paragraphsFound, numDataChanges, plural)
    def doConversions_calc(self, sourceCol, destCol, skipFirstRow):
        """For converting data in a Calc spreadsheet."""
        logger.debug(util.funcName('begin'))

        ## Start progress bar

        progressBar = ProgressBar(self.unoObjs, "Converting...")
        progressBar.show()
        progressBar.updateBeginning()

        ## Get list of words from source column
        #  (just strings are enough, no need for a special object)

        reader = SpreadsheetReader(self.unoObjs)
        try:
            inputList = reader.getColumnStringList(sourceCol, skipFirstRow)
        except exceptions.DocAccessError:
            self.msgbox.display("Error reading spreadsheet.")
            progressBar.close()
        if len(inputList) == 0:
            self.msgbox.display("Did not find anything in column %s.",
                                sourceCol)
            progressBar.close()
            return

        if progressBar.getPercent() < 40:
            progressBar.updatePercent(40)

        ## Convert

        outList = []
        problems = False
        numDataChanges = 0
        for inValue in inputList:
            try:
                outValue = self.secCall.convert(inValue)
                outList.append(outValue)
                if outValue != inValue:
                    numDataChanges += 1
            except exceptions.MessageError as exc:
                self.msgbox.displayExc(exc)
                problems = True
                outList.append("")
                break

        ## Output results

        outputter = SpreadsheetOutput(self.unoObjs)
        try:
            outputter.outputToColumn(destCol, outList, skipFirstRow)
        except exceptions.DocAccessError:
            self.msgbox.display("Error writing to spreadsheet.")

        progressBar.updateFinishing()
        progressBar.close()

        ## Display results

        if not problems:
            if numDataChanges == 0:
                self.msgbox.display("No changes.")
            else:
                self.msgbox.display("Successfully finished conversion.")
    def doConversions_writer(self):
        """For converting data in a Writer doc."""
        logger.debug(util.funcName('begin'))

        ## Start progress bar

        progressBar = ProgressBar(self.unoObjs, "Converting...")
        progressBar.show()
        progressBar.updateBeginning()

        ## Find the text ranges

        textSearch = TextSearch(self.unoObjs, progressBar)
        textSearch.setConfig(self.config.searchConfig)
        try:
            if self.config.whichScope == 'WholeDoc':
                textSearch.scopeWholeDoc()
            elif self.config.whichScope == 'Selection':
                textSearch.scopeSelection()
            elif self.config.whichScope == 'ParaStyle':
                textSearch.scopeParaStyle()
            elif self.config.whichScope == 'CharStyle':
                textSearch.scopeCharStyle()
            elif self.config.whichScope == 'Font':
                textSearch.scopeFont()
            elif self.config.whichScope == 'SFMs':
                textSearch.scopeSFMs()
            else:
                raise exceptions.LogicError("Unexpected value %s",
                                            self.config.whichScope)
        except (exceptions.RangeError, exceptions.LogicError) as exc:
            self.msgbox.displayExc(exc)
            progressBar.close()
            return
        rangesFound = textSearch.getRanges()

        if progressBar.getPercent() < 40:
            progressBar.updatePercent(40)

        ## Do the changes to those ranges

        textChanger = TextChanger(self.unoObjs, progressBar,
                                  self.changerSettings)
        if self.secCall.config.convName:
            textChanger.setConverterCall(self.secCall)
        if self.config.whichTarget == "ParaStyle":
            textChanger.setStyleToChange("ParaStyleName",
                                         self.config.targetStyle)
        elif self.config.whichTarget == "CharStyle":
            textChanger.setStyleToChange("CharStyleName",
                                         self.config.targetStyle)
        elif self.config.whichTarget == "FontOnly":
            textChanger.setFontToChange(self.config.targetFont)
        numDataChanges, numStyleChanges = textChanger.doChanges(
            rangesFound, self.config.askEach)

        progressBar.updateFinishing()
        progressBar.close()

        ## Display results

        paragraphsFound = len(rangesFound)
        if paragraphsFound == 0:
            self.msgbox.display("Did not find scope of change.")
        elif numDataChanges == 0:
            if numStyleChanges == 0:
                self.msgbox.display("No changes.")
            else:
                plural = "" if numStyleChanges == 1 else "s"
                # add "s" if plural
                self.msgbox.display(
                    "No changes, but modified style of %d paragraph%s.",
                    numStyleChanges, plural)
        elif paragraphsFound == 1:
            plural = "" if numDataChanges == 1 else "s"  # add "s" if plural
            self.msgbox.display("Made %d change%s.", numDataChanges, plural)
        else:
            plural = "" if numDataChanges == 1 else "s"  # add "s" if plural
            self.msgbox.display("Found %d paragraphs and made %d change%s.",
                                paragraphsFound, numDataChanges, plural)