File: binary2plain.py

package info (click to toggle)
sumo 0.15.0~dfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 31,740 kB
  • sloc: cpp: 109,360; xml: 49,743; ansic: 41,570; python: 20,769; java: 17,071; sh: 10,413; makefile: 1,377; perl: 450
file content (108 lines) | stat: -rwxr-xr-x 3,098 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#!/usr/bin/env python
"""
@file    binary2plain.py
@author  Michael Behrisch
@date    2012-03-11
@version $Id: binary2plain.py 12093 2012-03-13 22:11:37Z behrisch $

Converter between SUMO's binary XML and plain XML

SUMO, Simulation of Urban MObility; see http://sumo.sourceforge.net/
Copyright (C) 2012-2012 DLR (http://www.dlr.de/) and contributors
All rights reserved
"""
import sys, struct

BYTE = 0
INTEGER = 1
FLOAT = 2
STRING = 3
LIST = 4
XML_TAG_START = 5
XML_TAG_END = 6
XML_ATTRIBUTE = 7
EDGE = 8
LANE = 9
POSITION_2D = 10
POSITION_3D = 11
COLOR = 12
NODE_TYPE = 13
EDGE_FUNCTION = 14

def read(content, format):
    return struct.unpack(format, content.read(struct.calcsize(format)))

def readInt(content):
    return read(content, "i")[0]

def readDouble(content):
    return read(content, "d")[0]

def readString(content):
    length = readInt(content)
    return read(content, "%ss" % length)[0]

def readStringList(content):
    n = read(content, "i")[0]
    list = []
    for i in range(n):
        read(content, "B") #type
        list.append(readString(content))
    return list


out = sys.stdout
content = open(sys.argv[1], 'rb')
print read(content, "BBB") #sbx version
print readString(content) #sumo version
read(content, "B") #type
elements = readStringList(content)
read(content, "B") #type
attributes = readStringList(content)
read(content, "B") #type
nodeTypes = readStringList(content)
read(content, "B") #type
edgeTypes = readStringList(content)
stack = []
startOpen = False
while True:
    typ = read(content, "B")
    if typ == XML_TAG_START:
        if startOpen:
            out.write(">\n")
        out.write("    " * len(stack))
        stack.append(readInt(content))
        out.write("<" + elements[stack[-1]])
        startOpen = True
    elif typ == XML_TAG_END:
        if startOpen:
            out.write(">")
        out.write("</%s>" % elements[stack.pop()])
        readInt(content)
    elif typ == XML_ATTRIBUTE:
        out.write(" %s=" % attributes[readInt(content)])
        valType = read(content, "B")
        if typ == BYTE:
            out.write('"%s"' % read(content, "B"))
        elif typ == INTEGER:
            out.write('"%s"' % readInt(content))
        elif typ == FLOAT:
            out.write('"%s"' % readDouble(content))
        elif typ == STRING:
            out.write('"%s"' % readInt(content))
        elif typ == LIST:
            out.write('"%s"' % readInt(content))
        elif typ == EDGE:
            out.write('"%s"' % readInt(content))
        elif typ == LANE:
            out.write('"%s"' % readInt(content))
        elif typ == POSITION_2D:
            out.write('"%s,%s"' % (readDouble(content),readDouble(content)))
        elif typ == POSITION_3D:
            out.write('"%s,%s,%s"' % (readInt(content),readDouble(content),readDouble(content)))
        elif typ == COLOR:
            out.write('"%s,%s,%s"' % read(content, "BBB"))
        elif typ == NODE_TYPE:
            out.write('"%s"' % nodeTypes[readInt(content)])
        elif typ == EDGE_FUNCTION:
            out.write('"%s"' % edgeTypes[readInt(content)])