1+ import sys
2+
3+
4+ def generateGraphML (inputFileName ):
5+
6+ oneIndent = ' '
7+ twoIndent = ' '
8+ threeIndent = ' '
9+ target = open (inputFileName + '.graphml' , 'w' )
10+
11+ target .write ('<?xml version="1.0" encoding="UTF-8"?>\n ' )
12+ target .write ('<graphml xmlns="http://graphml.graphdrawing.org/xmlns" '
13+ 'xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" '
14+ 'xsi:schemaLocation="http://graphml.graphdrawing.org/xmlns '
15+ 'http://graphml.graphdrawing.org/xmlns/1.0/graphml.xsd">\n ' )
16+
17+ target .write ('<key id="label" for="node" attr.name="label" attr.type="string"/>\n ' )
18+ target .write ('<key id="timeStamp" for="node" attr.name="timeStamp" attr.type="string"/>\n ' )
19+
20+ target .write ('<key id="label" for="edge" attr.name="label" attr.type="string"/>\n ' )
21+ target .write ('<key id="timeStamp" for="edge" attr.name="timeStamp" attr.type="string"/>\n ' )
22+ target .write ('<key id="directed" for="edge" attr.name="directed" attr.type="string"/>\n ' )
23+
24+ target .write ('<graph id="GSG_V1" edgedefault="directed">\n ' )
25+
26+ with open (inputFileName ) as infile :
27+ for line in infile :
28+ if line .startswith (' {"vertex": {' ):
29+ # write a node
30+ # use split instead of index+ substring because we
31+ idLine = next (infile ) # "id": "27",\n
32+ vid = idLine .split ('"id": ' )[1 ][:- 2 ] # "27"
33+ target .write (oneIndent + '<node id=' + vid + '>\n ' )
34+
35+ #Get Attributes
36+ attrLine = next (infile ) # "attributes": {"label": "v8"},\n
37+ allAttrs = attrLine .split ('"attributes": {' )[1 ][:- 3 ] # "label": "v8"
38+ allAttrArray = allAttrs .split (',' )
39+ for attr in allAttrArray :
40+ key , val = attr .split (':' ) # [0]="label" [1]="v8" with '"'
41+ target .write (twoIndent + '<data key=' + key .strip () + '>' + val .strip () + '</data>\n ' )
42+
43+ # Get Timestamp
44+ timestampLine = next (infile ) # "timeStamp": "5"}},\n
45+ vTimestamp = timestampLine .split ('"timeStamp": ' )[1 ][:- 4 ]
46+ # Very last timeStamp entry does not end with "," which make the subscript invalid
47+ if vTimestamp .endswith ('"' ) == False :
48+ vTimestamp = vTimestamp + '"'
49+ target .write (twoIndent + '<data key="timeStamp">' + vTimestamp + '</data>\n ' )
50+ target .write (oneIndent + '</node>\n ' )
51+
52+ elif line .startswith (' {"edge": {' ):
53+ # Write an edge Node
54+ idLine = next (infile )
55+ eId = idLine .split ('"id": ' )[1 ][:- 2 ]
56+
57+ # Get Source
58+ srcLine = next (infile )
59+ src = srcLine .split ('"source": ' )[1 ][:- 2 ]
60+
61+
62+ # Get Destination
63+ dstLine = next (infile )
64+ dst = dstLine .split ('"target": ' )[1 ][:- 2 ]
65+
66+ target .write (oneIndent + '<edge id=' + eId + ' source=' + src + ' target=' + dst + '>\n ' )
67+
68+ # Get Attributes
69+ attrLine = next (infile ) # {"foo": "bar", "label": "e89"},\n
70+ allAttrs = attrLine .split ('"attributes": {' )[1 ][:- 3 ] # "label": "v8", "label": "e89"
71+ allAttrArray = allAttrs .split (',' )
72+ for attr in allAttrArray :
73+ key , val = attr .split (':' ) # [0]="label" [1]="v8" with '"'
74+ target .write (twoIndent + '<data key=' + key .strip () + '>' + val .strip () + '</data>\n ' )
75+
76+ # Get Direction
77+ edirLine = next (infile )
78+ eDir = edirLine .split ('"directed": ' )[1 ][:- 2 ]
79+ target .write (twoIndent + '<data key="directed">' + eDir + '</data>\n ' )
80+
81+ # Get Timestamp
82+ timestampLine = next (infile ) # "timeStamp": "5"}},\n
83+ eTimestamp = timestampLine .split ('"timeStamp": ' )[1 ][:- 4 ]
84+
85+ # Very last timeStamp entry does not end with "," which make the subscript invalid
86+ if eTimestamp .endswith ('"' ) == False :
87+ eTimestamp = eTimestamp + '"'
88+ target .write (twoIndent + '<data key="timeStamp">' + eTimestamp + '</data>\n ' )
89+
90+ # End of Edge
91+ target .write (oneIndent + '</edge>\n ' )
92+
93+ else :
94+ continue
95+
96+ target .write ('</graph>\n ' )
97+ target .write ('</graphml>\n ' )
98+ print "****Finish: Export " + inputFileName + " to Format = " + exportFormat + ". " + inputFileName + ".grpahml generated \n "
99+
100+ def main ():
101+
102+ global exportFormat
103+ exportFormat = "GraphML"
104+
105+ if len (sys .argv ) < 2 :
106+ print "Usage: python gExportGraphML.py <input json file> [exportFormat=GraphMl]"
107+
108+ if len (sys .argv ) > 1 :
109+ inputFileName = sys .argv [1 ]
110+ if len (sys .argv ) > 2 :
111+ exportFormat = sys .argv [2 ]
112+
113+ print "****Start: Export " + inputFileName + " to Format = " + exportFormat + "\n "
114+
115+ if exportFormat == "GraphML" :
116+ generateGraphML (inputFileName )
117+
118+
119+ if __name__ == '__main__' :
120+ main ()
0 commit comments