[7c3008e] | 1 | #!/usr/bin/python -u |
---|
| 2 | import libxml2 |
---|
| 3 | |
---|
| 4 | class federated_virtual_topology: |
---|
| 5 | class callback: |
---|
| 6 | def __init__(self): |
---|
| 7 | self.experiment = { }; |
---|
| 8 | self.in_node=0; |
---|
| 9 | self.in_lan=0; |
---|
| 10 | self.rows = [] |
---|
| 11 | self.row = { } |
---|
| 12 | self.node_fields = set(["vname", "ips"]); |
---|
| 13 | self.lan_fields = set(["vname", "vnode", "bandwidth", "delay", "member", "ip"]); |
---|
| 14 | self.long_fields = set(["bandwidth"]); |
---|
| 15 | self.float_fields = set(["delay"]); |
---|
| 16 | self.last_chars = ""; |
---|
| 17 | pass |
---|
| 18 | |
---|
| 19 | def startDocument(self): |
---|
| 20 | pass |
---|
| 21 | |
---|
| 22 | def endDocument(self): |
---|
| 23 | pass |
---|
| 24 | |
---|
| 25 | def startElement(self, tag, attrs): |
---|
| 26 | if ( tag == "node"): |
---|
| 27 | self.in_node = 1 |
---|
| 28 | elif tag == "lan": |
---|
| 29 | self.in_lan = 1 |
---|
| 30 | pass |
---|
| 31 | |
---|
| 32 | def endElement(self, tag): |
---|
| 33 | if self.in_node >0 : |
---|
| 34 | if tag in self.node_fields : |
---|
| 35 | if tag in self.long_fields: |
---|
| 36 | self.row[tag] = long(self.last_chars); |
---|
| 37 | elif tag in self.float_fields: |
---|
| 38 | self.row[tag] = float(self.last_chars); |
---|
| 39 | else: |
---|
| 40 | self.row[tag] = self.last_chars; |
---|
| 41 | pass |
---|
| 42 | self.last_chars = ""; |
---|
| 43 | pass |
---|
| 44 | if tag == "node": |
---|
| 45 | # end of a row |
---|
| 46 | self.rows.append(self.row); |
---|
| 47 | self.row = { }; |
---|
| 48 | self.in_node=0 |
---|
| 49 | pass |
---|
| 50 | pass |
---|
| 51 | if tag == "nodes": |
---|
| 52 | self.experiment["nodes"] = self.rows; |
---|
| 53 | self.rows = []; |
---|
| 54 | pass |
---|
| 55 | if self.in_lan >0 : |
---|
| 56 | if tag in self.lan_fields : |
---|
| 57 | if tag in self.long_fields: |
---|
| 58 | self.row[tag] = long(self.last_chars); |
---|
| 59 | elif tag in self.float_fields: |
---|
| 60 | self.row[tag] = float(self.last_chars); |
---|
| 61 | else: |
---|
| 62 | self.row[tag] = self.last_chars; |
---|
| 63 | pass |
---|
| 64 | self.last_chars = ""; |
---|
| 65 | pass |
---|
| 66 | if tag == "lan": |
---|
| 67 | # end of a row |
---|
| 68 | self.rows.append(self.row); |
---|
| 69 | self.row = { }; |
---|
| 70 | self.in_lan=0 |
---|
| 71 | pass |
---|
| 72 | pass |
---|
| 73 | if tag == "lans": |
---|
| 74 | self.experiment["lans"] = self.rows; |
---|
| 75 | self.rows = []; |
---|
| 76 | pass |
---|
| 77 | |
---|
| 78 | def characters(self, data): |
---|
| 79 | self.last_chars = data; |
---|
| 80 | pass |
---|
| 81 | |
---|
| 82 | def warning(self, msg): |
---|
| 83 | print "Warning: %s!" % msg |
---|
| 84 | pass |
---|
| 85 | |
---|
| 86 | def error(self, msg): |
---|
| 87 | print "Error: %s!" % msg |
---|
| 88 | pass |
---|
| 89 | |
---|
| 90 | def fatalError(self, msg): |
---|
| 91 | global log |
---|
| 92 | print "fatalError: %s:" % (msg) |
---|
| 93 | |
---|
| 94 | def __init__(self, file): |
---|
| 95 | self.handler = self.callback() |
---|
| 96 | self.rv = { }; |
---|
| 97 | libxml2.SAXParseFile(self.handler, file, 0); |
---|
| 98 | self.rv["experiment"] = self.handler.experiment; |
---|
| 99 | libxml2.cleanupParser() |
---|
| 100 | pass |
---|
| 101 | pass |
---|
| 102 | |
---|
| 103 | if __name__ == "__main__": |
---|
| 104 | import sys; |
---|
| 105 | parse = federated_virtual_topology(sys.argv[1]); |
---|
| 106 | print parse.rv |
---|
| 107 | pass |
---|