Module object_to_graphviz
|
|
1
2
3
4 """object_to_graphviz
5
6 Load a serialized object in memory and generate a DOT file for GraphViz.
7 """
8
9 __author__ = "Jerome Hussenet, Cedric Bonhomme"
10 __version__ = "$Revision: 0.1 $"
11 __date__ = "$Date: 2009/03/06 $"
12 __copyright__ = "Copyright (c) 2009 Jerome Hussenet, Copyright (c) 2009 Cedric Bonhomme"
13 __license__ = "Python"
14
15 import os
16 import sys
17
18 import pickle
19
20
22 """Generate a DOT file for GraphViZ.
23 """
24 dic_obj = open(obj_file, "r")
25 if options.verbose:
26 print "Loading dictionary..."
27 dic_ip = pickle.load(dic_obj)
28
29
30 liste_ip = []
31 for i in dic_ip:
32 if i not in liste_ip:
33 liste_ip.append(i)
34 for j in dic_ip[i].keys():
35 if j not in liste_ip:
36 liste_ip.append(j)
37
38 if options.verbose:
39 print "Creating GraphViz DOT file..."
40 gv_txt = "digraph G {\n"
41 gv_txt += '\tbgcolor=azure;\n'
42 gv_txt += '\tranksep=10;\n'
43
44 gv_txt += '\tcompound=true;\n'
45
46
47 gv_txt += '\tedge [arrowsize=2, color=gold];\n'
48 for ip in liste_ip:
49 gv_txt += '"' + ip + '"' + ";\n"
50
51 for ip_src in dic_ip:
52 for ip_dst in dic_ip[ip_src]:
53 gv_txt += '\t"' + ip_src + '" -> ' + ' "' + ip_dst + \
54 '" [label = "' + str(dic_ip[ip_src][ip_dst]) + '"];\n'
55 gv_txt += "}"
56
57 if options.verbose:
58 print "Writting file."
59 gv = open(gv_file, "w")
60 gv.write(gv_txt)
61 gv.close()
62
63
64 if __name__ == "__main__":
65
66 from optparse import OptionParser
67 parser = OptionParser()
68 parser.add_option("-i", "--input", dest="obj_file",
69 help="Python serialized object")
70 parser.add_option("-o", "--output", dest="gv_file",
71 help="GraphViz file")
72 parser.add_option("-q", "--quiet",
73 action="store_false", dest="verbose",
74 help="be vewwy quiet (I'm hunting wabbits)")
75 parser.set_defaults(obj_file = './data/dic.pyobj',
76 gv_file = './data/ip.dot',
77 verbose = True)
78
79 (options, args) = parser.parse_args()
80
81 object_to_graphviz(options.obj_file, options.gv_file)
82