def eDuplicate(fldbk): '''duplicate entry''' oldNode = dataIndex.exDict[dataIndex.currentCard] newID = idGenerator.generateID('EX', dataIndex.exDict) newNode = deepcopy(oldNode) newNode.set('ExID', newID) k = dataIndex.root.find('Ex[@ExID="%s"]' % dataIndex.currentCard) i = list(dataIndex.root).index(k) + 1 dataIndex.root.insert(i, newNode) dataIndex.currentCard = newID dataIndex.exDict[newID] = newNode cardLoader.loadExCard(newNode)
def newDataset(fldbk, title, newText=''): #1 create DSNode tDate = setSessionDate() today = SessionDate.dateFinder() newID = idGenerator.generateID('DS', dataIndex.dataDict) newNode = etree.Element('Dset') newNode.set('DsetID', newID) newNode.set('Date', tDate) newNode.set('Update', today) if dataIndex.lastSpeaker != None: newNode.set('Spkr', dataIndex.lastSpeaker) else: newNode.set('Spkr', '') if dataIndex.lastRschr != None: newNode.set('Rschr', dataIndex.lastRschr) else: newNode.set('Rschr', '') etree.SubElement(newNode, 'Title') etree.SubElement(newNode, 'Data') newNode.find('Title').text = title newNode.find('Data').text = newText #2 add new DS to XML and dataDict and set current DSet dataIndex.dataDict[newID] = newNode dataIndex.lastDset = newID dList = list(dataIndex.dataDict.keys()) lastDCard = dataIndex.dataDict[dList[len(dataIndex.dataDict) - 1]] try: i = list(dataIndex.root).index(lastDCard) except ValueError: lastDCard = dataIndex.dataDict[dList[len(dataIndex.dataDict) - 2]] i = list(dataIndex.root).index(lastDCard) if len(dataIndex.dataDict.keys()) == 1: badNode = dataIndex.root.find('Dset') badID = badNode.attrib.get('DsetID') if badID == 'DS00': dataIndex.root.remove(badNode) del dataIndex.dataDict[badID] dataIndex.root.insert(i, newNode) #3 add new DS to listnav navLists.navListBuilderData(fldbk) cardLoader.resetNavBars(fldbk.dDataNav, newID) dataIndex.currentCard = newID dataIndex.lastDset = newID dataIndex.root.set('LastDset', newID) return newNode
def updateMediaInfo(fldbk, item, newName): '''create new media node''' tree = dataIndex.root medID = idGenerator.generateID("MC",dataIndex.mediaDict) elemList = list(tree) elemList.reverse() for i, node in enumerate(elemList): if node.tag == 'Abbreviations': break i = len(elemList) - i newNode = etree.Element('Media') tree.insert(i,newNode) '''add attributes to media element''' newNode.set('MedID',medID) newNode.set('Filename',newName) dataIndex.mediaDict[medID] = newNode '''update media table''' mediaTable = fldbk.mMediaTable nextRow = mediaTable.rowCount() mediaTable.setRowCount(nextRow+1) firstItem = QtWidgets.QTableWidgetItem(1001) firstItem.setText(newName) '''data 36 is the new XML node''' firstItem.setData(36,newNode) '''data 37 is the path to the sound file''' firstItem.setData(37, item) firstItem.setFlags(QtCore.Qt.ItemIsEnabled | QtCore.Qt.ItemIsSelectable) mediaTable.setItem(nextRow,0,firstItem) secondItem = QtWidgets.QTableWidgetItem(1001) secondItem.setText('XX') secondItem.setFlags(QtCore.Qt.ItemIsEnabled | QtCore.Qt.ItemIsSelectable) mediaTable.setItem(nextRow,1,secondItem) thirdItem = QtWidgets.QTableWidgetItem(1001) thirdItem.setText("???") thirdItem.setFlags(QtCore.Qt.ItemIsEnabled | QtCore.Qt.ItemIsSelectable) mediaTable.setItem(nextRow,2,thirdItem) fourthItem = QtWidgets.QTableWidgetItem(1001) fourthItem.setIcon(QtGui.QIcon(":/new/infoBtn.png")) mediaTable.setItem(nextRow,3,fourthItem) mediaTable.sortItems(0,QtCore.Qt.AscendingOrder) mediaTable.scrollToItem(fourthItem, QtWidgets.QAbstractItemView.PositionAtCenter) mediaTable.selectRow(fourthItem.row()) return medID, newNode
def newData(self, oldID): self.fldbk = dataIndex.fldbk line1 = self.firstLine.toPlainText() line2 = self.secondLine.toPlainText() gloss11 = self.firstL1.toPlainText() gloss12 = self.secondL1.toPlainText() if len(self.firstL2.toPlainText()) !=0: gloss21 = self.firstL2.toPlainText() gloss22 = self.secondL2.toPlainText() if self.egTable.columnCount() != 0: mrphList1 = [] mrphList2 = [] ilegList1 = [] ilegList2 = [] mrph1 = None mrph2 = None ileg1 = None ileg2 = None for i in range(0,self.egTable.currentColumn()): mrphList1.append(self.egTable.item(0,i).text()) ilegList1.append(self.egTable.item(1,i).text()) for i in range(self.egTable.currentColumn(),self.egTable.columnCount()): mrphList2.append(self.egTable.item(0,i).text()) ilegList2.append(self.egTable.item(1,i).text()) for item in mrphList1: if mrph1 == None: mrph1 = item else: mrph1 += '\t' + item for item in mrphList2: if mrph2 == None: mrph2 = item else: mrph2 += '\t' + item for item in ilegList1: if ileg1 == None: ileg1 = item else: ileg1 += '\t' + item for item in ilegList2: if ileg2 == None: ileg2 = item else: ileg2 += '\t' + item update = SessionDate.dateFinder() tDate = self.fldbk.tDate.toPlainText() spkr = self.fldbk.tSource.toPlainText() rschr = self.fldbk.tResearcher.toPlainText() newID = idGenerator.generateID('Ex', dataIndex.exDict) ##revised node node = dataIndex.exDict[oldID] node.find('Line').text = line1 d = i if self.egTable.columnCount() != 0: node.find('Mrph').text = mrph1 node.find('ILEG').text = ileg1 node.find('L1Gloss').text = gloss11 node.find('L2Gloss').text = gloss21 node.set('Update',update) k = dataIndex.root.find('Ex[@ExID="%s"]'%oldID) d = list(dataIndex.root).index(k) + 1 ##new node newNode = etree.Element('Ex') etree.SubElement(newNode,'Line') newNode.find('Line').text = line2 if self.egTable.columnCount() != 0: etree.SubElement(newNode,'Mrph') etree.SubElement(newNode,'ILEG') newNode.find('Mrph').text = mrph2 newNode.find('ILEG').text = ileg2 etree.SubElement(newNode,'L1Gloss') newNode.find('L1Gloss').text = gloss12 etree.SubElement(newNode,'L2Gloss') newNode.find('L2Gloss').text = gloss22 newNode.set('Date',tDate) newNode.set('Update',update) newNode.set('Spkr',spkr) newNode.set('Rschr',rschr) newNode.set('ExID',newID) if node.attrib.get('SourceText') != None: newNode.set('SourceText', node.attrib.get('SourceText')) dataIndex.root.insert(d,newNode) dataIndex.exDict[newID] = newNode idList = [oldID,newID] return idList
def tAnalyzeLine(fldbk): '''take user to the example card for parsing/editing''' if dataIndex.currentTextTable != None: ExNode = dataIndex.currentTextTable.verticalHeaderItem(0).data(35) if ExNode == None: thisText = dataIndex.textDict[dataIndex.currentCard] msgbox = QtWidgets.QMessageBox() msgbox.setIcon(QtWidgets.QMessageBox.Question) msgbox.setText("Parse line?") msgbox.setInformativeText( "This line was previously treated as “unparseable”. Parse it now?" ) msgbox.setStandardButtons(QtWidgets.QMessageBox.Ok | QtWidgets.QMessageBox.Cancel) msgbox.setDefaultButton(QtWidgets.QMessageBox.Ok) reply = msgbox.exec_() if reply == QtWidgets.QMessageBox.Cancel: return else: lineNode = dataIndex.currentTextTable.verticalHeaderItem( 0).data(36) text = lineNode.text if text == None or len(text) == 0: text = "new line" if len(text.split('\n')) == 2: line = text.split('\n')[0] line2 = text.split('\n')[1].strip() else: line = text line2 = None date = fldbk.tDate.toPlainText() spkr = fldbk.tSource.toPlainText() rschr = fldbk.tResearcher.toPlainText() ExNode = etree.Element('Ex') etree.SubElement(ExNode, 'Line') etree.SubElement(ExNode, 'Mrph') etree.SubElement(ExNode, 'ILEG') etree.SubElement(ExNode, 'L1Gloss') ExNode.find('Line').text = line ExNode.find('L1Gloss').text = line2 ExID = idGenerator.generateID('Ex', dataIndex.exDict) ExNode.set('ExID', ExID) ExNode.set('Date', date) ExNode.set('Update', date) ExNode.set('Spkr', spkr) ExNode.set('Rschr', rschr) ExNode.set('SourceText', dataIndex.currentCard) if lineNode.attrib.get('Time') != None: ExNode.set('Time', lineNode.attrib.get('Time')) ##insert ex nodes into XML and add to dicts dataIndex.exDict[ExID] = ExNode lineNo = list(thisText).index(lineNode) if lineNo == 0: #first, make sure this isn't the first line of text a = tryAfter(thisText, lineNo) if a != False: prevID = thisText[lineNo + a].attrib.get('LnRef') k = dataIndex.root.find('Ex[@ExID="%s"]' % prevID) d = list(dataIndex.root).index(k) dataIndex.root.insert(d, ExNode) elif thisText[lineNo - 1].attrib.get('LnRef') != None: #next, check to see if this is the normal case where the previous line has been analyzed prevID = thisText[lineNo - 1].attrib.get('LnRef') a = True k = dataIndex.root.find('Ex[@ExID="%s"]' % prevID) d = list(dataIndex.root).index(k) dataIndex.root.insert(d + 1, ExNode) else: #if the previous line is not parsed, find first previous line that has been parsed a = tryBefore(thisText, lineNo) if a != False: prevID = thisText[lineNo - a].attrib.get('LnRef') k = dataIndex.root.find('Ex[@ExID="%s"]' % prevID) d = list(dataIndex.root).index(k) dataIndex.root.insert(d + 1, ExNode) else: #if no previous lines have been parsed, look for a subsequent line a = tryAfter(thisText, lineNo) if a != False: prevID = thisText[lineNo + a].attrib.get('LnRef') k = dataIndex.root.find('Ex[@ExID="%s"]' % prevID) d = list(dataIndex.root).index(k) dataIndex.root.insert(d, ExNode) if a == False: egList = list(dataIndex.exDict.keys()) prevID = egList[-1] lineNode.set('LnRef', ExID) if len(fldbk.iIndex.toPlainText()) == 0: buildIndex = autoparsing.askToBuildIndex() if buildIndex == False: return cardLoader.loadExCard(ExNode) fldbk.tabWidget.setCurrentIndex(3) fldbk.eAnalysis.setColumnCount(1) autoparsing.doParse() updateText(fldbk, ExNode)
def tNewLine(fldbk): ''' adds a new line to a text immediately following the currently selected line ''' if dataIndex.currentTextTable == None: return update = SessionDate.dateFinder() scroll = fldbk.tFullText.verticalScrollBar().value() timeCode = None spokenBy = None newLineDialog = AddTextLine.AddLineDialog() if newLineDialog.exec_(): values = newLineDialog.returnValues() if len(values[0]) == 0: #bial if there is no text supplied return else: return if values[1] != None: spokenBy, values[1] = codeExtractor.getSpokenBy(values[1]) if '[' in values[ 1]: # check to see if timeCode is contained in the gloss timeCode, endTime, values[1] = codeExtractor.getTime(values[1]) ##step one: generate nodes (Ln in both cases, EX if possible) newLineNode = etree.Element('Ln') if spokenBy != None: newLineNode.set('SpokenBy', spokenBy) if values[2] == True: #if unparseable if values[1] == None: newLineNode.text = values[0] else: newLineNode.text = values[0] + '\n' + values[1] if timeCode != None: newLineNode.set('Time', timeCode) if endTime != None: newLineNode.set('EndTime', endTime) else: #if parseable, we need to set up an Ex Node for it tDate = fldbk.tDate.toPlainText() spkr = fldbk.tSource.toPlainText() rschr = fldbk.tResearcher.toPlainText() newExNode = etree.Element('Ex') etree.SubElement(newExNode, 'Line') etree.SubElement(newExNode, 'Mrph') etree.SubElement(newExNode, 'ILEG') etree.SubElement(newExNode, 'L1Gloss') newExNode.find('Line').text = values[0] newExNode.find('L1Gloss').text = values[1] ExID = idGenerator.generateID('Ex', dataIndex.exDict) newExNode.set('ExID', ExID) newExNode.set('Date', tDate) newExNode.set('Update', update) newExNode.set('Spkr', spkr) newExNode.set('Rschr', rschr) newExNode.set('SourceText', dataIndex.currentCard) if timeCode != None: newExNode.set('Time', timeCode) ##insert ex nodes into XML and add to dicts dataIndex.exDict[ExID] = newExNode prevID = dataIndex.currentTextTable.verticalHeaderItem(0).data( 35).attrib.get('ExID') k = dataIndex.root.find('Ex[@ExID="%s"]' % prevID) d = list(dataIndex.root).index(k) dataIndex.root.insert(d + 1, newExNode) newLineNode.set('LnRef', ExID) ##insert Ln node textRoot = dataIndex.currentText textViewIndex = int( dataIndex.currentTextTable.verticalHeaderItem(0).text()) if textViewIndex == 1: msgbox = QtWidgets.QMessageBox() msgbox.setIcon(QtWidgets.QMessageBox.Question) msgbox.setText("Add line at beginning?") msgbox.setInformativeText( "Add this line before the first line of the text?") msgbox.setStandardButtons(QtWidgets.QMessageBox.Yes | QtWidgets.QMessageBox.No) msgbox.setDefaultButton(QtWidgets.QMessageBox.No) reply = msgbox.exec_() if reply == QtWidgets.QMessageBox.Yes: textRoot.insert(0, newLineNode) scroll -= 65 else: textRoot.insert(1, newLineNode) else: textRoot.insert(textViewIndex + 1, newLineNode) ##step 5: rebuild tText cardLoader.addTextWidget(fldbk, dataIndex.currentText) fldbk.tUpdated.setPlainText(update) dataIndex.unsavedEdit = 1 dataIndex.currentTextTable = None fldbk.tFullText.verticalScrollBar().setValue(scroll + 65)
def newCard(fldbk): today = SessionDate.dateFinder() tDate = setSessionDate() dataIndex.unsavedEdit = 1 if fldbk.tabWidget.currentIndex() == 1: #lexicon card newID = idGenerator.generateID('LX', dataIndex.lexDict) newCdWindow = NewLexDialog.NewLexDialog(fldbk) navBar = fldbk.lNavBar if newCdWindow.exec_(): clearCard(fldbk) fldbk.lL1Definition.clear() fldbk.lL2Definition.clear() fldbk.lDerivatives.clear() fldbk.lSound.Recordings.clear() fldbk.lSound.SoundFileMeta.clear() fldbk.lDoneBtn.setChecked(0) data = newCdWindow.getData() '''returns a list [speaker,researcher,entry word,gloss]''' newNode = etree.Element('Lex', {"LexID": newID}) newNode.set('Date', tDate) newNode.set('Update', today) newNode.set('Spkr', data[0]) newNode.set('Rschr', data[1]) newNode.set('Done', '0') newOrth = etree.SubElement(newNode, 'Orth') newOrth.text = data[2] '''generate IPA node if Auto checked''' if fldbk.lAutoBtn.isChecked() == True: IPA = Orthographies.toIPA(data[2]) newIPA = etree.SubElement(newNode, 'IPA') newIPA.text = IPA fldbk.lIPA.setText(IPA) newDef = etree.SubElement(newNode, 'Def', {'Index': '1'}) newL1 = etree.SubElement(newDef, 'L1') newL1.text = data[3] fldbk.lSource.setPlainText(data[0]) fldbk.lResearcher.setPlainText(data[1]) fldbk.lDate.setPlainText(tDate) fldbk.lUpdated.setPlainText(tDate) fldbk.lOrthography.setText(data[2]) '''if new word in data[2] is homophonous with another entry''' homList = fldbk.lLexNav.findChildren(QtGui.QStandardItem, data[2]) if homList != None: homList.append(newID) update.manageHomonyms(homList) cardLoader.loadDefinitions(fldbk, newNode) if len(dataIndex.lexDict.keys()) == 1: badNode = dataIndex.root.find('Lex') badID = badNode.attrib.get('LexID') if badID == 'LX00': dataIndex.root.remove(badNode) del dataIndex.lexDict[badID] dataIndex.root.insert(1, newNode) dataIndex.lexDict[newID] = newNode navLists.navListBuilderLex(fldbk) cardLoader.resetNavBars(fldbk.lLexNav, newID) dataIndex.currentCard = newID dataIndex.lastLex = newID dataIndex.lexDict[newID] = newNode dataIndex.root.set('LastLex', newID) if fldbk.tabWidget.currentIndex() == 2: # text card textOnlyBtns.enterNewText(fldbk) navBar = fldbk.tNavBar if fldbk.tabWidget.currentIndex() == 3: # example card navBar = fldbk.eNavBar fldbk.eAnalysis.clear() fldbk.eAnalysis.setColumnCount(2) fldbk.eAnalysis.setRowCount(2) lastHeadWidget = QtWidgets.QTableWidgetItem(1001) lastHeadWidget.setText('+') fldbk.eAnalysis.setHorizontalHeaderItem(1, lastHeadWidget) fldbk.eAnalysis.resizeColumnToContents(1) rowHeader = QtWidgets.QTableWidgetItem(1001) rowHeader.setText('Morph') fldbk.eAnalysis.setVerticalHeaderItem(0, rowHeader) rowHeader = QtWidgets.QTableWidgetItem(1001) rowHeader.setText('ILEG') fldbk.eAnalysis.setVerticalHeaderItem(1, rowHeader) clearCard(fldbk) fldbk.eLinksList.clear() newID = idGenerator.generateID('EX', dataIndex.exDict) newNode = etree.Element('Ex', {"ExID": newID}) newNode.set('Date', tDate) newNode.set('Update', today) if dataIndex.lastRschr != None and len(dataIndex.lastRschr) != 0: newNode.set('Rschr', dataIndex.lastRschr) else: newNode.set('Rschr', 'YYY') if dataIndex.lastSpeaker != None and len(dataIndex.lastSpeaker) != 0: newNode.set('Spkr', dataIndex.lastSpeaker) else: newNode.set('Spkr', 'XX') etree.SubElement(newNode, 'Line') etree.SubElement(newNode, 'Mrph') etree.SubElement(newNode, 'ILEG') etree.SubElement(newNode, 'L1Gloss') egList = list(dataIndex.exDict.keys()) lastExCard = dataIndex.exDict[egList[len(dataIndex.exDict) - 1]] i = list(dataIndex.root).index(lastExCard) if len(dataIndex.exDict.keys()) == 1: badNode = dataIndex.root.find('Ex') badID = badNode.attrib.get('ExID') if badID == 'EX00': dataIndex.root.remove(badNode) del dataIndex.exDict[badID] dataIndex.root.insert(i, newNode) dataIndex.currentCard = newID dataIndex.exDict[newID] = newNode dataIndex.lastEx = newID dataIndex.root.set('lastEx', newID) cardLoader.loadExCard(newNode) if fldbk.tabWidget.currentIndex() == 4: # dataset card nameBox = StyledInputDialog.StyledInputDialog(fldbk) nameBox.setWindowTitle('New dataset.') nameBox.inputLabel.setText('Give the dataset a name.') navBar = fldbk.dNavBar if nameBox.exec_(): title = nameBox.returnInput() newNode = newDataset(fldbk, title) clearCard(fldbk) cardLoader.loadDataCard(newNode) navBar.stack.append(dataIndex.currentCard) navBar.index = navBar.index + 1
def copyCard(fldbk): tDate = setSessionDate() dataIndex.unsavedEdit = 1 if fldbk.tabWidget.currentIndex() == 1: #lexicon card cardType = 'LX' newID = idGenerator.generateID(cardType, dataIndex.lexDict) ID = 'LexID' elemListType = 'Lex[@LexID="%s"]' % dataIndex.currentCard dict = dataIndex.lexDict elif fldbk.tabWidget.currentIndex() == 2: #text card cardType = 'TX' newID = idGenerator.generateID(cardType, dataIndex.lexDict) ID = 'TextID' elemListType = 'Text[@TextID="%s"]' % dataIndex.currentCard dict = dataIndex.textDict elif fldbk.tabWidget.currentIndex() == 3: #example card cardType = 'EX' newID = idGenerator.generateID(cardType, dataIndex.lexDict) ID = 'ExID' elemListType = 'Ex[@ExID="%s"]' % dataIndex.currentCard dict = dataIndex.exDict elif fldbk.tabWidget.currentIndex() == 4: #dataset card cardType = 'DS' newID = idGenerator.generateID(cardType, dataIndex.lexDict) ID = 'DsetID' elemListType = 'Dset[@DsetID="%s"]' % dataIndex.currentCard dict = dataIndex.dataDict newID = idGenerator.generateID(cardType, dict) sourceCard = dict[dataIndex.currentCard] newElem = copy.deepcopy(sourceCard) newElem.set(ID, newID) newElem.set('Date', tDate) newElem.set('Update', tDate) k = dataIndex.root.find(elemListType) i = list(dataIndex.root).index(k) + 1 dataIndex.root.insert(i, newElem) ##delink sounds and media files## soundList = newElem.findall('Sound') if len(soundList) != 0: for sound in soundList: newElem.remove(sound) if cardType == 'LX': dataIndex.lexDict[newID] = newElem cardLoader.loadLexCard(newElem) navLists.navListBuilderLex(fldbk) cardLoader.resetNavBars(fldbk.lLexNav, newID) dataIndex.currentCard = newID dataIndex.lastLex = newID dataIndex.root.set('LastLex', newID) fldbk.lSound.Recordings.clear() fldbk.lSound.SoundFileMeta.clear() ##clear derivations## fldbk.lDerivatives.clear() drvnList = newElem.findall('Drvn') if len(drvnList) != 0: for drvn in drvnList: newElem.remove(drvn) ##clear root## root = newElem.find('Root') try: newElem.remove(root) fldbk.lBase.clear() fldbk.lBreakLnkBtn.setEnabled(0) except TypeError: pass ##deal with existing homonyms## if newElem.attrib.get('Hom') != None: syn = newElem.attrib.get('Hom') synList = syn.split(', ') synList.append(newID) try: synList.index(sourceCard.attrib.get('LexID')) except ValueError: synList.append(sourceCard.attrib.get('LexID')) update.manageHomonyms(synList) elif cardType == 'TX': dataIndex.textDict[newID] = newElem cardLoader.loadTextCard(newElem) navLists.navListBuilderText(fldbk) cardLoader.resetNavBars(fldbk.tTextNav, newID) dataIndex.currentCard = newID dataIndex.lastText = newID dataIndex.root.set('LastText', newID) fldbk.tRecordings.clear() fldbk.tSoundFileMeta.clear() elif cardType == 'EX': dataIndex.exDict[newID] = newElem cardLoader.loadExCard(newElem) dataIndex.currentCard = newID dataIndex.lastText = newID dataIndex.root.set('lastEx', newID) if newElem.attrib['Links'] != None: del newElem.attrib['Links'] fldbk.eLinksList.clear() fldbk.eSound.Recordings.clear() fldbk.eSound.SoundFileMeta.clear() elif cardType == 'DS': dataIndex.dataDict[newID] = newElem cardLoader.loadDataCard(newElem) navLists.navListBuilderData(fldbk) cardLoader.resetNavBars(fldbk.dDataNav, newID) dataIndex.currentCard = newID dataIndex.lastText = newID dataIndex.root.set('LastDset', newID) fldbk.dSound.Recordings.clear() fldbk.dSound.SoundFileMeta.clear()