def get(self, item_tag, prop="", child=None): for in_text in self.xmlBlocks: # this step is done to avoid confusion due to the "<<" and ">>" signs root= ET.fromstring(in_text) if root.tag == root_tag: ### we use XPath features of the xml.etree library here root.findall('.%s' % child) propSet= SettingParser(child.text) if propSet.get(refProp) == refValue: propSet.set(prop, value) child.text= propText.tostring()
def set(self, item_tag, prop, value, refProp, refValue): for i in range(len(self.xmlBlocks)): # this step is done to avoid confusion due to the "<<" and ">>" signs in_text= self.xmlBlocks[i] root= ET.fromstring(in_text) #print root.text for item in root.iter(): #print item.tag if item.tag == item_tag: parser=SettingParser(item.text) if refValue != "": try: if parser.get(refProp) == refValue: parser.set(prop,value) except: pass else: parser.set(prop, value) item.text= parser.tostring() out_text = ET.tostring(root) self.xmlBlocks[i]= out_text
class CircosConfigParser: ''' circos implemented the apache config format which has no builtin parser in python. this class is intended to parse and manipulate the circos configuration files. ''' def __init__(self, configType): self.configType = configType self.xmlParser=None self.txtParser=None self.conf_file= file_provider['circos_config'][configType] self.parse() def parse(self): ''' returns a tuple with the first item as a dictionary of attributes outside xml blocks AND the second item as a list of xml blocks. the first item is straight forward. Second item should be further parsed iteratively by using the iPaseXML function ''' with open(self.conf_file) as f: text=f.read() self.xmlParser= MyXMLParser(text) xml_text= self.xmlParser.tostring() non_xml_text= text.replace(xml_text, "") self.txtParser= SettingParser(non_xml_text) def set(self, prop, value, item_tag="", refProp="index", refValue= ""): if item_tag != "": ## means coming from the xml part self.xmlParser.set(item_tag, prop, value, refProp, refValue) else: ### means coming from the text part self.txtParser.set(prop, value) self.write() def write(self): ### !!! TODO: this part should be rewritten ### There is still a problem with the xmlparser. with open(self.conf_file,'w') as f: f.write(self.tostring()) def tostring(self): if self.configType == 'plots': self.doc= self.xmlParser.tostring() else: self.doc= self.txtParser.tostring() + self.xmlParser.tostring() return self.doc
def parse(self): ''' returns a tuple with the first item as a dictionary of attributes outside xml blocks AND the second item as a list of xml blocks. the first item is straight forward. Second item should be further parsed iteratively by using the iPaseXML function ''' with open(self.conf_file) as f: text=f.read() self.xmlParser= MyXMLParser(text) xml_text= self.xmlParser.tostring() non_xml_text= text.replace(xml_text, "") self.txtParser= SettingParser(non_xml_text)