예제 #1
0
    def Search(self):
        # Place all matching strings to the search into the tree widget
        self.parent.WriteDatabaseStorageToHdd()

        newSearchTab = self.generateSearchTab()
        matchString = unicode(self.original.toPlainText())
        exceptString = unicode(self.exceptions.text())

        if matchString.count(unicode('<', 'UTF-8')) != matchString.count(
                unicode('>', 'UTF-8')):
            reply = QtGui.QMessageBox.information(
                self, "Questionable Search Usage",
                "Warning:\n\nPart of a variable: Be sure you know what you're doing."
            )
            #return

        tabNameString = matchString
        matchString = Globals.VariableRemove(matchString)

        searchDebug = self.searchDebug.isChecked()
        matchCase = self.matchCase.isChecked()
        matchFullEntry = self.matchEntryCheckbox.isChecked()
        matchJapanese = self.matchJpnCheckbox.isChecked()
        matchEnglish = self.matchEngCheckbox.isChecked()

        if not matchFullEntry:
            if len(matchString) == 1:
                if ord(matchString) <= 0x20:
                    reply = QtGui.QMessageBox.question(
                        self, "Questionable Search Usage",
                        "Warning:\n\nYour search only consists of a space, a form feed, a newline, or a tab.\nAre you sure you want to search for this?",
                        QtGui.QMessageBox.Yes, QtGui.QMessageBox.No)
                    if reply != QtGui.QMessageBox.Yes:
                        return
            elif len(matchString) == 0:
                reply = QtGui.QMessageBox.information(
                    self, "Incorrect Search Usage",
                    "Warning:\n\nYour search can not be empty. Please enter text in the search bar."
                )
                return

        MatchedEntries = []

        # turn on case sensitive checking
        if matchCase:
            Globals.CursorGracesJapanese.execute(
                u"PRAGMA case_sensitive_like = ON")

        JPmatches = set()
        if matchFullEntry:
            ReplacementType = 'Entry'
            if matchJapanese:
                Globals.CursorGracesJapanese.execute(
                    u"SELECT ID FROM Japanese WHERE string LIKE ?",
                    (unicode(matchString), ))
                for match in Globals.CursorGracesJapanese.fetchall():
                    JPmatches.add(int(match[0]))
        else:
            ReplacementType = 'Substr'
            if matchJapanese:
                Globals.CursorGracesJapanese.execute(
                    u"SELECT ID FROM Japanese WHERE string LIKE ?",
                    ('%' + unicode(matchString) + '%', ))
                for match in Globals.CursorGracesJapanese.fetchall():
                    JPmatches.add(int(match[0]))

        dbFilter = unicode(self.fileFilter.text()).lower()
        for File in Globals.configData.FileList:
            if dbFilter in File.lower(
            ) or dbFilter in Globals.GetDatabaseDescriptionString(
                    File).lower():
                data = Globals.Cache.GetDatabase(File)
                for i in xrange(len(data)):
                    if ( matchJapanese and data[i].stringId in JPmatches ) \
                    or ( matchEnglish and matchFullEntry and data[i].english == matchString ) \
                    or ( matchEnglish and not matchFullEntry and matchCase and matchString in data[i].english ) \
                    or ( matchEnglish and not matchFullEntry and not matchCase and matchString.upper() in data[i].english.upper() > -1 ):
                        if searchDebug or data[i].status >= 0:
                            Globals.CursorGracesJapanese.execute(
                                'SELECT string FROM Japanese WHERE ID={0}'.
                                format(data[i].stringId))
                            JPString = Globals.CursorGracesJapanese.fetchall(
                            )[0][0]
                            MatchedEntries.append([
                                File, i + 1, data[i].english, JPString,
                                data[i].IdentifyString, data[i].status,
                                Globals.GetDatabaseDescriptionString(File),
                                data[i].comment
                            ])

        if len(MatchedEntries) == 0:
            return

        if len(exceptString) >= 1:
            checkForExceptions = True
        else:
            checkForExceptions = False

        for item in MatchedEntries:
            try:
                filename = item[0]
                entryID = item[1]
                englishString = Globals.VariableReplace(item[2])
                japaneseString = Globals.VariableReplace(item[3])
                infoString = item[4]
                status = item[5]
                databaseDescriptor = item[6]
                comment = item[7]

                if checkForExceptions:
                    if exceptString in englishString or exceptString in japaneseString:
                        continue

                treeItem = QtGui.QTreeWidgetItem([
                    databaseDescriptor,
                    str(entryID),
                    str(infoString), "", englishString, englishString,
                    japaneseString,
                    str(int(status)), filename, ReplacementType, comment
                ])
                treeItem.setCheckState(3, QtCore.Qt.Checked)
                newSearchTab.addTopLevelItem(treeItem)
            except:
                Globals.MainWindow.displayStatusMessage(
                    "Mass Replace: Failed adding file [" + filename +
                    "], entry [" + str(entryID) + "]")

        # turn case sensitiveness back off
        if matchCase:
            Globals.CursorGracesJapanese.execute(
                u"PRAGMA case_sensitive_like = OFF")

        self.tabwidget.addTab(newSearchTab, tabNameString)
        self.tabwidget.setCurrentIndex(self.tabwidget.count() - 1)
        self.UpdateReplacementText()
예제 #2
0
    def Search(self):
        self.parent.WriteDatabaseStorageToHdd()

        newSearchTab = self.generateSearchTab()

        dbFilter = unicode(self.fileFilter.text()).lower()
        tabNameString = dbFilter

        searchDebug = self.searchDebug.isChecked()

        sortedfiles = []
        for File in Globals.configData.FileList:
            sortedfiles.append(File)
        sortedfiles.sort()

        MatchedEntries = []
        for File in sortedfiles:
            if dbFilter in File.lower(
            ) or dbFilter in Globals.GetDatabaseDescriptionString(
                    File).lower():
                data = Globals.Cache.GetDatabase(File)
                for i in xrange(len(data)):
                    if searchDebug or data[i].status >= 0:
                        for word in self.tokenizer(
                                Globals.VariableReplace(data[i].english)):
                            if not self.dict.check(word[0]):
                                MatchedEntries.append([
                                    File, i + 1, data[i].english, word[0],
                                    data[i].IdentifyString, data[i].status,
                                    Globals.GetDatabaseDescriptionString(File)
                                ])

        if len(MatchedEntries) == 0:
            return

        def html_escape(text):
            """Produce entities within text."""
            html_escape_table = {
                "&": "&amp;",
                '"': "&quot;",
                "'": "&apos;",
                ">": "&gt;",
                "<": "&lt;",
            }
            return "".join(html_escape_table.get(c, c) for c in text)

        html = open('spellcheck.html', 'w')
        html.write('<html>')
        html.write('<head>')
        html.write('<style>')
        html.write(
            'body { background-color: #68504F; color: #EFD1AE; font-size: 16; }'
        )
        html.write(
            'table, tr, td, th { padding: 0px 4px 0px 0px; border-spacing: 0px; }'
        )
        html.write('td, td > a { padding-right: 16px; padding-bottom: 16px; }')
        html.write(
            'span.mis { font-weight: bold; text-decoration: underline; }')
        html.write('</style>')
        html.write('</head>')
        html.write('<body>')
        html.write('<table>')
        html.write('\n')

        MisWords = {}
        for item in MatchedEntries:
            try:
                filename = item[0]
                entryID = item[1]
                englishString = Globals.VariableReplace(item[2])
                misspelledWord = Globals.VariableReplace(item[3])
                infoString = item[4]
                status = item[5]
                databaseDescriptor = item[6]

                treeItem = QtGui.QTreeWidgetItem([
                    databaseDescriptor,
                    str(entryID),
                    str(infoString), englishString, misspelledWord,
                    str(int(status)), filename
                ])
                newSearchTab.addTopLevelItem(treeItem)

            except:
                Globals.MainWindow.displayStatusMessage(
                    "Mass Spellcheck: Failed adding file [" + filename +
                    "], entry [" + str(entryID) + "]")

            html.write('<tr>')
            html.write('<td>' + html_escape(unicode(filename)) + '</td>')
            html.write('<td>' + html_escape(unicode(databaseDescriptor)) +
                       '</td>')
            html.write('<td>' + html_escape(unicode(entryID)) + '</td>')
            html.write('<td>' + html_escape(unicode(infoString)) + '</td>')
            html.write('<td>' + html_escape(unicode(status)) + '</td>')
            html.write('<td>' +
                       html_escape(unicode(misspelledWord)).encode('utf8') +
                       '</td>')
            html.write('<td>' + html_escape(unicode(englishString)).replace(
                html_escape(misspelledWord), '<span class="mis">' +
                html_escape(misspelledWord) + '</span>').encode('utf8') +
                       '</td>')
            html.write('</tr>')
            html.write('\n')

            if misspelledWord not in MisWords:
                MisWords[misspelledWord] = 1
            else:
                MisWords[misspelledWord] += 1

        html.write('</table>')
        html.write('</body>')
        html.write('</html>')
        html.close()
        xml = open('dictionary.xml', 'w')

        import operator
        for misspelledWord, count in sorted(MisWords.items(),
                                            key=operator.itemgetter(1),
                                            reverse=True):
            xml.write('\t\t<!-- ' + str(count) + ' --> <Entry Word="' +
                      html_escape(unicode(misspelledWord)).encode('utf8') +
                      '" />\n')
        xml.close()

        self.tabwidget.addTab(newSearchTab, tabNameString)
        self.tabwidget.setCurrentIndex(self.tabwidget.count() - 1)