source: trunk/lang/python/Rappture/interface.py @ 4346

Last change on this file since 4346 was 3177, checked in by mmc, 9 years ago

Updated all of the copyright notices to reference the transfer to
the new HUBzero Foundation, LLC.

File size: 3.6 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) 2004-2012  HUBzero Foundation, LLC
15#
16#  See the file "license.terms" for information on usage and
17#  redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES.
18# ======================================================================
19import getopt, sys, os
20import atexit
21import Rappture
22
23def interface(argv,*args):
24    """
25    Clients call this near the top of their simulator.  It looks
26    at command line arguments and decides whether the Rappture GUI
27    should be invoked, or perhaps the GUI has already been invoked
28    and the simulator should process a driver file.
29
30    The argv is a list of command-line arguments.  All remaining
31    arguments represent packages that should be imported and
32    interrogated for Rappture components, to feed the GUI.
33    """
34
35    try:
36        opts,rest = getopt.getopt(argv[1:],'d:g',["driver","gui"])
37    except getopt.GetoptError:
38        print 'usage:'
39        print '  %s --gui (launch the graphical interface)' % argv[0]
40        print '  %s --driver file (use the input stored in file)' % argv[0]
41        sys.exit(2)
42
43    for opt,val in opts:
44        if opt in ('-g','--gui'):
45            #
46            # Launch the GUI.  Start by loading the various modules
47            # and interrogating them for symbols.
48            #
49            lib = Rappture.library('<?xml version="1.0"?><run><tool>' +
50                    '<about>Press Simulate to view results.</about>' +
51                    '<command>python ' +argv[0]+ ' -d @driver</command></tool></run>')
52
53            for module in args:
54                for symbol in dir(module):
55                    s = module.__dict__[symbol]
56                    if isinstance(s, Rappture.number):
57                        s.put(lib)
58
59            toolFileName = "tool.xml"
60            f = open(toolFileName,"w")
61            f.write( lib.xml() )
62            f.close()
63
64            print 'launch the gui...'
65            #
66            # this doesnt work, but needs to. when we
67            # os.execvp('driver', driverArgs ) with following definition of
68            # driverArgs (and we change the name of toolFileName), program
69            # does not send the driver program the arguments as expected.
70            #
71            #driverArgs = ('-tool', toolFileName)
72            driverArgs = ()
73            os.execvp('driver', driverArgs )
74            sys.exit(0)
75
76        elif opt in ('-d','--driver'):
77            #
78            # Continue, using input from the driver file.
79            # Register a hook to write out results upon exit.
80            #
81            Rappture.driver = Rappture.library(val)
82
83            atexit.register(finish)
84            return
85
86
87def finish():
88    """
89    Called automatically whenever the program exits.  Writes out
90    any results in the global driver to the "run.xml" file
91    representing the entire run.
92    """
93#    f = open("run.xml","w")
94#    f.write( Rappture.driver.xml() )
95#    f.close()
96
97    Rappture.result(Rappture.driver)
Note: See TracBrowser for help on using the repository browser.