parser.py
Go to the documentation of this file.
00001 import xml.etree.ElementTree as ET
00002 import xml.dom.minidom
00003 from topology import *
00004 """ v5 topology parser and serializer """
00005 
00006 def parseFile(filename):
00007     return parseTree(ET.parse(filename))
00008     
00009 def parseString(data):
00010     return parseTree(ET.fromstring(data))
00011 
00012 def parseTree(tree):
00013     # Get XML Tree root and initialize topology
00014     root = tree.getroot()
00015     t = Topology()
00016  
00017     # Populate Edges
00018     edges = root.find("edges").findall("edge")
00019 
00020 #     print "Num Edges Detected:",len(edges)
00021 
00022     # Keep track of edges for reference later
00023     edgeList = dict()
00024 
00025     for edge in edges:
00026         e = Edge(t)
00027         eid = int(edge.attrib['id'].strip())
00028         edgeList[eid] = e
00029         for band in edge.findall("band"):
00030             altitude = int(band.attrib["altitude"].strip())
00031             rank = int(band.attrib["rank"].strip())
00032             matches = filter(lambda x: x.rank==rank,t.bands.values())
00033             b = e.posBand if altitude > 0 else e.negBand
00034             b.altitude = altitude
00035             b.rank = rank
00036 
00037    
00038     # Populate Vertices
00039     vertices = root.find("vertices").findall("vertex")
00040 #     print "Num Vertices Detected: %d"%len(vertices)
00041     for vertex in vertices:
00042         index = int(vertex.attrib['index'].strip())
00043         v = Vertex(t)
00044 #         print "Creating Vertex with index=",index,v
00045         v.block.index = index
00046 
00047         # Make edge connections to this vertex
00048         for sink in vertex.find("collector").findall("sink"):
00049             order = int(sink.attrib["order"].strip())
00050             edgeid = int(sink.attrib["edge"].strip())
00051             e = edgeList[edgeid]
00052             if v in [s.vertex for s in e.sinks]:
00053                 pass
00054 #                 print "Existing Vertex found!"
00055             else:
00056                 tmp = Sink(t,v,e)
00057                 tmp.snap.order = order
00058 #                 print "Creating sink with order=",order,"altitude=",altitude,tmp
00059 
00060         for source in vertex.find("emitter").findall("source"):
00061             order = int(source.attrib["order"].strip())
00062             edgeid = int(source.attrib["edge"].strip())
00063             e = edgeList[edgeid]
00064             if v in [src.vertex for src in e.sources]:
00065                 pass
00066 #                 print "Existing Vertex found"
00067             else:
00068                 tmp = Source(t,v,e)
00069                 tmp.snap.order = order
00070 #                 print "Creating source with order=",order,"altitude=",altitude,tmp
00071     return t
00072 
00073 def serialize(topology):
00074     """ Generate xml from topology """
00075     xmlRoot = ET.Element('topology')
00076     xmlVertices = ET.SubElement(xmlRoot,'vertices')
00077     xmlEdges = ET.SubElement(xmlRoot,'edges')
00078 
00079     nextEdgeId = 0
00080     revEdgeList = dict()
00081 
00082     # Serialize Edges
00083     for edge in topology.edges:
00084         xmlEdge = ET.SubElement(xmlEdges,'edge')
00085         eid = nextEdgeId
00086         nextEdgeId+=1
00087         xmlEdge.attrib["id"] = eid
00088         for band in [edge.posBand,edge.negBand]:
00089             b = ET.SubElement(xmlEdge,'band')
00090             b.attrib["altitude"] = str(band.altitude)
00091             b.attrib["rank"] = str(band.rank)
00092             revEdgeList[band.altitude] = eid
00093 
00094     # Serialize Vertices
00095     for vertex in topology.vertices:
00096         xmlVertex = ET.SubElement(xmlVertices,'vertex')
00097 #         for source in vertex.sources:
00098 # 
00099 # 
00100 # 
00101 #         for sink in vertex.sinks:
00102 
00103     return xmlify(xmlRoot)
00104 
00105 
00106 
00107 
00108 # Search children of ETree 
00109 def find_element_by_attribute(root,elementname,attribname,attribval):
00110     element_list = root.findall(elementname)
00111     if element_list is None:
00112         raise Exception("No Elements of name %s found"%elementname)
00113     for tmp in element_list:
00114         try:
00115             if tmp.attrib[attribname] == attribval:
00116                 return tmp
00117         except:
00118             raise Exception("Element %s has not attribute %s"%(elementname,attribname))
00119     raise Exception("Could not find %s with %s=%s"%(elementname,attribname,attribval))
00120 
00121 
00122 def xmlify(root):
00123     # Split continuous string by newlines
00124     content = xml.dom.minidom.parseString(ET.tostring(root)).toprettyxml().split("\n")
00125     # Remove right hand whitespace
00126     content = [str(l).rstrip() for l in content]
00127     # Filter Blank lines
00128     content = filter(lambda x: not x == "",content)
00129     # Repack as a single string
00130     content = "\n".join(content)
00131     return content
00132 
00133 


rqt_graphprofiler
Author(s): Dan Brooks
autogenerated on Thu Jun 6 2019 20:29:31