source: trunk/python/Rappture/interface.py @ 96

Last change on this file since 96 was 96, checked in by dkearney, 19 years ago

added units conversion to coerce to the number.py
added a command to exec a new process to launch the gui in interface.py
these might make it possible to run examples/app-fermi/2.0

File size: 3.5 KB
Line 
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# ======================================================================
16import getopt, sys, os
17import atexit
18import Rappture
19
20def 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><tool>' +
47                    '<about>Press Simulate to view results.</about>' +
48                    '<command>python ' +argv[0]+ ' -d @driver</command></tool></run>')
49
50            for module in args:
51                for symbol in dir(module):
52                    s = module.__dict__[symbol]
53                    if isinstance(s, Rappture.number):
54                        s.put(lib)
55
56            toolFileName = "tool.xml"
57            f = open(toolFileName,"w")
58            f.write( lib.xml() )
59            f.close()
60
61            print 'launch the gui...'
62            #
63            # this doesnt work, but needs to. when we
64            # os.execvp('driver', driverArgs ) with following definition of
65            # driverArgs (and we change the name of toolFileName), program
66            # does not send the driver program the arguments as expected.
67            #
68            #driverArgs = ('-tool', toolFileName)
69            driverArgs = ()
70            os.execvp('driver', driverArgs )
71            sys.exit(0)
72
73        elif opt in ('-d','--driver'):
74            #
75            # Continue, using input from the driver file.
76            # Register a hook to write out results upon exit.
77            #
78            Rappture.driver = Rappture.library(val)
79
80            atexit.register(finish)
81            return
82
83
84def finish():
85    """
86    Called automatically whenever the program exits.  Writes out
87    any results in the global driver to the "run.xml" file
88    representing the entire run.
89    """
90#    f = open("run.xml","w")
91#    f.write( Rappture.driver.xml() )
92#    f.close()
93
94    Rappture.result(Rappture.driver)
Note: See TracBrowser for help on using the repository browser.