1 | # ---------------------------------------------------------------------- |
---|
2 | # COMPONENT: interface - used in the main program of a simulator |
---|
3 | # |
---|
4 | # Rappture.interface() is called at the start of a simulator. You pass |
---|
5 | # in arguments and one or more interface packages. If the -gui flag |
---|
6 | # is included, then the interfaces are loaded and interrogated, and |
---|
7 | # interface() writes out a tool.xml file, then invokes the Rappture GUI |
---|
8 | # to collect input. The Rappture GUI writes out a driver.xml file, |
---|
9 | # and then invokes this tool again with the -driver flag to process |
---|
10 | # the input. |
---|
11 | # |
---|
12 | # ====================================================================== |
---|
13 | # AUTHOR: Michael McLennan, Purdue University |
---|
14 | # Copyright (c) 2005 Purdue Research Foundation, West Lafayette, IN |
---|
15 | # ====================================================================== |
---|
16 | import getopt, sys |
---|
17 | import atexit |
---|
18 | import Rappture |
---|
19 | |
---|
20 | def interface(argv,*args): |
---|
21 | """ |
---|
22 | Clients call this near the top of their simulator. It looks |
---|
23 | at command line arguments and decides whether the Rappture GUI |
---|
24 | should be invoked, or perhaps the GUI has already been invoked |
---|
25 | and the simulator should process a driver file. |
---|
26 | |
---|
27 | The argv is a list of command-line arguments. All remaining |
---|
28 | arguments represent packages that should be imported and |
---|
29 | interrogated for Rappture components, to feed the GUI. |
---|
30 | """ |
---|
31 | |
---|
32 | try: |
---|
33 | opts,rest = getopt.getopt(argv[1:],'d:g',["driver","gui"]) |
---|
34 | except getopt.GetoptError: |
---|
35 | print 'usage:' |
---|
36 | print ' %s --gui (launch the graphical interface)' % argv[0] |
---|
37 | print ' %s --driver file (use the input stored in file)' % argv[0] |
---|
38 | sys.exit(2) |
---|
39 | |
---|
40 | for opt,val in opts: |
---|
41 | if opt in ('-g','--gui'): |
---|
42 | # |
---|
43 | # Launch the GUI. Start by loading the various modules |
---|
44 | # and interrogating them for symbols. |
---|
45 | # |
---|
46 | lib = Rappture.library('<?xml version="1.0"?><run></run>') |
---|
47 | |
---|
48 | for module in args: |
---|
49 | for symbol in dir(module): |
---|
50 | s = module.__dict__[symbol] |
---|
51 | if isinstance(s, Rappture.number): |
---|
52 | s.put(lib) |
---|
53 | |
---|
54 | f = open("driver.xml","w") |
---|
55 | f.write( lib.xml() ) |
---|
56 | f.close() |
---|
57 | |
---|
58 | print 'launch the gui...' |
---|
59 | sys.exit(0) |
---|
60 | |
---|
61 | elif opt in ('-d','--driver'): |
---|
62 | # |
---|
63 | # Continue, using input from the driver file. |
---|
64 | # Register a hook to write out results upon exit. |
---|
65 | # |
---|
66 | Rappture.driver = Rappture.library(val) |
---|
67 | |
---|
68 | atexit.register(finish) |
---|
69 | return |
---|
70 | |
---|
71 | |
---|
72 | def finish(): |
---|
73 | """ |
---|
74 | Called automatically whenever the program exits. Writes out |
---|
75 | any results in the global driver to the "run.xml" file |
---|
76 | representing the entire run. |
---|
77 | """ |
---|
78 | f = open("run.xml","w") |
---|
79 | f.write( Rappture.driver.xml() ) |
---|
80 | f.close() |
---|