#!/usr/bin/python -u import libxml2 class federated_virtual_topology: class callback: def __init__(self): self.experiment = { }; self.in_node=0; self.in_lan=0; self.rows = [] self.row = { } self.node_fields = set(["vname", "ips"]); self.lan_fields = set(["vname", "vnode", "bandwidth", "delay", "member", "ip"]); self.long_fields = set(["bandwidth"]); self.float_fields = set(["delay"]); self.last_chars = ""; pass def startDocument(self): pass def endDocument(self): pass def startElement(self, tag, attrs): if ( tag == "node"): self.in_node = 1 elif tag == "lan": self.in_lan = 1 pass def endElement(self, tag): if self.in_node >0 : if tag in self.node_fields : if tag in self.long_fields: self.row[tag] = long(self.last_chars); elif tag in self.float_fields: self.row[tag] = float(self.last_chars); else: self.row[tag] = self.last_chars; pass self.last_chars = ""; pass if tag == "node": # end of a row self.rows.append(self.row); self.row = { }; self.in_node=0 pass pass if tag == "nodes": self.experiment["nodes"] = self.rows; self.rows = []; pass if self.in_lan >0 : if tag in self.lan_fields : if tag in self.long_fields: self.row[tag] = long(self.last_chars); elif tag in self.float_fields: self.row[tag] = float(self.last_chars); else: self.row[tag] = self.last_chars; pass self.last_chars = ""; pass if tag == "lan": # end of a row self.rows.append(self.row); self.row = { }; self.in_lan=0 pass pass if tag == "lans": self.experiment["lans"] = self.rows; self.rows = []; pass def characters(self, data): self.last_chars = data; pass def warning(self, msg): print "Warning: %s!" % msg pass def error(self, msg): print "Error: %s!" % msg pass def fatalError(self, msg): global log print "fatalError: %s:" % (msg) def __init__(self, file): self.handler = self.callback() self.rv = { }; libxml2.SAXParseFile(self.handler, file, 0); self.rv["experiment"] = self.handler.experiment; libxml2.cleanupParser() pass pass if __name__ == "__main__": import sys; parse = federated_virtual_topology(sys.argv[1]); print parse.rv pass