def rem(num,self): from generateButtons import generateButtons varDB=open('varDB.txt','r') lines = [line.strip() for line in varDB] varDB.close() entries=getinfo() varDB2=open('varDB.txt','w') for i in reversed(range(self.left.verticalLayoutScroll.count())): #Remove buttons self.left.verticalLayoutScroll.itemAt(i).widget().setParent(None) i=0 while i < len(lines): if i<num: varDB2.write(lines[i]+"\n") if i>num: #Prevent data shift varDB2.write(lines[i]+"\n") i+=1 varDB2.close() generateButtons(self,self.searchText.text(),0) print "Remove succesful: ",lines[num]
def initUI(self): self.setGeometry(500, 300, 800, 600) self.setWindowTitle('varDB GUI') #Create option buttons addButton=QtGui.QPushButton("Add Variable") addButton.pressed.connect(lambda: newSig(self)) self.saveButton=QtGui.QPushButton("Save Changes") self.deleteButton=QtGui.QPushButton("Delete Selected variable") quitButton=QtGui.QPushButton("Quit") quitButton.clicked.connect(QtCore.QCoreApplication.instance().quit) #Creates left and right sections, search bar, scroll area hbox = QtGui.QHBoxLayout(self) self.left = QtGui.QFrame(self) self.upright = QtGui.QFrame(self) self.downright = QtGui.QFrame(self) self=setup(hbox,self,"VDB.xml") self.searchText.textEdited.connect(lambda: generateButtons(self,str(self.searchText.text()),0)) self.setLayout(hbox) #Populates scroll area generateButtons(self,'',-1) #Option buttons layout hlox=QtGui.QHBoxLayout() hlox2=QtGui.QHBoxLayout() hlox.addStretch(1) hlox.addWidget(addButton,1) hlox.addWidget(self.saveButton,1) hlox.addWidget(self.deleteButton,1) hlox.addWidget(quitButton,1) hlox.addStretch() vbox=QtGui.QVBoxLayout() vbox.addStretch(1) vbox.addLayout(hlox) vbox.addLayout(hlox2) self.downright.setLayout(vbox)
def addsignal(signals,self,num,instructions): parser = etree.XMLParser(remove_blank_text=True) doc=etree.parse(fileName(),parser) root=doc.getroot() #+++++++++++ if instructions['action']=='edit': #Expecting signals to be of form [ [attribute,new valie] * n ] #Edits existing attributes added=False newAtt=[] j=0 for elm in root.iter('variable'): if elm.attrib['name'] == signals[0][1].upper(): #variable is already in vardb added=True #edit exiting entries i=0 while i<len(elm): for sig in signals: if elm[i].tag == sig[0] and elm[i].text != sig[1]: elm[i].text=sig[1] print 'changed ', elm.attrib['name'],": ",elm[i].tag,' to ',elm[i].text i+=1 #Check for new entries for sig in signals: inThere=False wasRemoved=False i=0 while i<len(elm): if elm[i].tag==sig[0]: inThere=True i+=1 if not inThere and sig[0] != 'name' and sig[1]!='': newAtt.append(etree.SubElement(elm,sig[0])) newAtt[j].text=sig[1] for att in elm: if getDictionary(fileName()).index(att.tag)>getDictionary(fileName()).index(sig[0]): att.addnext(newAtt[j]) break j+=1 print 'Added ',sig[0],' to ',elm.attrib['name'],' set as ',sig[1] #check for blank entries, remove them for att in elm: if str(att.text)=='' and str(att.tag) not in [s[0] for s in self.catelogList]: print 'removed ',att.tag,' from ',elm.attrib['name'] elm.remove(att) if added==False: instructions['action']='new signal' print str(signals[0][1]).upper(),' not found in VDB. Creating new entry.' #++++++++++++ if instructions['action']=='delete': for elm in root.iter('variable'): if elm.attrib['name']==signals: elm.getparent().remove(elm) print 'removed ',signals #+++++++++++ if instructions['action']=='new signal': #Expecting signals to be list of for [ [attribute,value]*n] #Inserts a new signal into varDB in alphabetical order #Check for name input if signals[0][0]!='name' : print 'exit code 1 in addsignal.py: no name was input' quit() #Insert element elms=[] for elm in root.iter('variable'): elms.append(str(elm.attrib['name'])) elms.append(str(signals[0][1]).upper()) elms.sort() added=False i=0 for elm in root.iter('variable'): if str(elm.attrib['name'])!=elms[i] and added==False: new = etree.Element('variable',name=signals[0][1].upper()) l=1 while l<len(signals): if signals[l][1]!='': subtext = etree.SubElement(new, signals[l][0]) subtext.text = signals[l][1] l+=1 elm.addprevious(new) added=True num=i print 'added ',signals[0][1] i+=1 if added==False: new = etree.Element('variable',name=signals[0][1].upper()) l=1 while l<len(signals): subtext = etree.SubElement(new, signals[l][0]) subtext.text = signals[l][1] l+=1 elm.addnext(new) added=True num=i print 'appended ',signals[0][1] #============= doc.write('VDB.xml',pretty_print=True) generateButtons(self,str(self.searchText.text()),num) return