1
2
3
4 """object_to_rtgraph
5
6 Load a serialized object in memory and generate 3D dynamic graph.
7 This module uses RealTime 3D Graph.
8
9 It is necessary to have the modules :
10 - python-visual ;
11 - povexport ;
12 - PyInline for better performance.
13
14 PyInline and povexport are already included with IP-Link.
15 """
16
17 __author__ = "Jerome Hussenet, Cedric Bonhomme"
18 __version__ = "$Revision: 0.1 $"
19 __date__ = "$Date: 2009/03/05 $"
20 __copyright__ = "Copyright (c) 2009 Jerome Hussenet, Copyright (c) 2009 Cedric Bonhomme"
21 __license__ = "Python"
22
23 import os
24 import sys
25 import threading
26
27 import pickle
28
29 from rtgraph3d.rtg_cli import *
30 from rtgraph3d.rtgraph3d import *
31
32
34 """Generates a 3D dynamic graphics."""
35 dic_obj = open(obj_file, "r")
36 if options.verbose:
37 print "Loading dictionary..."
38 dic_ip = pickle.load(dic_obj)
39
40 if options.verbose:
41 print "Creation of the command for RealTime 3D Graph..."
42 command = ""
43 edges = []
44 for ip_src in dic_ip:
45 for ip_dst in dic_ip[ip_src]:
46 if (ip_src, ip_dst) not in edges:
47 edges.extend([(ip_src, ip_dst), (ip_dst, ip_src)])
48 command += "edge " + ip_src + " " + ip_dst + "\n"
49 command += "quit"
50
51 if options.verbose:
52 print "Launching the RTgraph3D window thread"
53 a = threading.Thread(None, rtggraph_launch, None)
54 a.start()
55
56 cmd = 'echo "' + command + '" | python ./rtgraph3d/rtg_cli.py'
57 if options.verbose:
58 print "command to execute :"
59 print cmd
60
61 (child_stdin, child_stdout, child_stderr) = os.popen3(cmd)
62 stderr = child_stderr.readlines()
63 stdout = child_stdout.readlines()
64 child_stdin.close()
65 child_stdout.close()
66 child_stderr.close()
67
68 if options.verbose:
69 print "Problem(s) :"
70 print stderr
71 print "\nOutput :"
72 print stdout
73
75 INPUT = [sys.stdin]
76 STARTFILE = None
77 SAVEFILE = None
78 STEREO = None
79 CINEMATIC = Cinematic.dynamic
80 POVDUMP = None
81 MODE = 0
82 try:
83 opts = getopt.getopt(sys.argv[1:], "htr:w:s:c:P:m:q")
84
85 for opt,optarg in opts[0]:
86 if opt == "-h":
87 usage()
88 elif opt == "-t":
89 MODE = socket.SOCK_STREAM
90 elif opt == "-r":
91 STARTFILE = optarg
92 elif opt == "-s":
93 STEREO = optarg
94 elif opt == "-w":
95 SAVEFILE = optarg
96 elif opt == "-m":
97 try:
98 MODE = {"p":1, "c":2, "s":3}[optarg.lower()]
99 except KeyError:
100 raise getopt.GetoptError("unkonwn mode [%s]" % optarg)
101 elif opt == "-P":
102 POVDUMP = optarg
103 elif opt == "-c":
104 if optarg.startswith("s"):
105 CINEMATIC = Cinematic.static
106 elif optarg.startswith("d"):
107 CINEMATIC = Cinematic.dynamic
108 except getopt.GetoptError, msg:
109 log.exception(msg)
110 sys.exit(-1)
111
112 gobject.threads_init()
113 dbus.glib.init_threads()
114 dbus.mainloop.glib.DBusGMainLoop(set_as_default = True)
115
116 bus = dbus.SessionBus()
117 name = dbus.service.BusName("org.secdev.rtgraph3d", bus)
118 svc = RTGraphService(bus, "/control",
119 get_physics_engine(MODE), INPUT,
120 startfile = STARTFILE,
121 savefile = SAVEFILE, stereo = STEREO)
122
123 if CINEMATIC == Cinematic.dynamic:
124 thread.start_new_thread(cinematic_thread, (svc, POVDUMP,))
125
126 mainloop = gobject.MainLoop()
127 log.info("Entering main loop")
128 mainloop.run()
129
130
131 if __name__ == "__main__":
132
133 from optparse import OptionParser
134 parser = OptionParser()
135 parser.add_option("-i", "--input", dest="obj_file",
136 help="Python serialized object")
137 parser.add_option("-q", "--quiet",
138 action="store_false", dest="verbose",
139 help="be vewwy quiet (I'm hunting wabbits)")
140 parser.set_defaults(obj_file = './data/dic.pyobj', verbose = True)
141
142 (options, args) = parser.parse_args()
143
144 object_to_rtgraph(options.obj_file)
145