Initial commit of OpenSPARC T2 architecture model.
[OpenSPARC-T2-SAM] / sam-t2 / sam / docs / ui-cmds / cmd_run-python-file.html
CommitLineData
920dae64
AT
1<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML//EN">
2<html>
3<head>
4<title>SPARC Architectural Model: User Commands</title>
5</head>
6<body bgcolor="#FFFFFF" LANG="en-US">
7<div STYLE="margin-bottom: 0cm"></div>&nbsp;
8
9<h2>
10run-python-file
11</h2>
12
13<h4>
14NAME
15</h4>
16<ul>
17run-python-file - execute python script file
18</ul>
19
20<h4>SYNOPSIS</h4>
21<tt>
22<ul>
23run-python-file <i>filename</i>
24</ul>
25</tt>
26
27<h4>DESCRIPTION</h4>
28<ul>
29Read Python code from <i>filename</i>. Any definitions are entered
30into the top
31level name-space in the Python environment. This command can be used to
32start Python scripts inside simulation.
33<p>
34SAM provides a python method, sam.ui_register(), to register python
35methods with the user interface.
36This method can be used to add arbitrary functionality to the SAM UI.
37<p>
38The ui_exec() method allows SamUI commands to be executed from python.
39<p>
40The sam python object provides the following methods to access the
41SamUI, registers and unregister methods as commands with the SamUI.
42<br><br>
43<table>
44<tr>
45<th>Method Name</th><th>Description</th>
46</tr>
47<tr valign="top">
48<td>ui_exec ("<i>cmd</i>")</td>
49<td>Sends <i>cmd</i> to the SamUI.</td>
50</tr>
51<tr valign="top">
52<td>ui_register&nbsp;("<i>cmd_name</i>",&nbsp;"<i>help_string</i>",&nbsp;<i>cmd_function<i>)&nbsp;&nbsp;</td>
53<td>Registers the python function <i>cmd_function</i> with the SamUI
54as "<i>cmd_name</i>". The commands help string is
55"<i>help_string</i>".</td>
56</tr>
57<tr valign="top">
58<td>ui_unregister ("<i>cmd_name</i>")</td>
59<td>Unregisters the command registered with the SamUI,
60"<i>cmd_name</i>".</td>
61</tr>
62</table><br>
63<p>
64Here is an example that registers the python method
65<code>echo()</code> as "py_echo".
66<div>
67<code>
68def echo(*argv):<br>
69&nbsp;&nbsp;print argv<br>
70<br>
71sam.ui_register("py_echo", "Prints its arguments", echo)<br>
72</code>
73</div>
74<p>
75Here is another example that runs python code from the SamUI:
76<div>
77<code>
78def exec_py(*argv):<br>
79&nbsp;&nbsp;cmd = " ".join(argv[1:])<br>
80&nbsp;&nbsp;try:<br>
81&nbsp;&nbsp;&nbsp;&nbsp;exec(cmd)<br>
82&nbsp;&nbsp;except:<br>
83&nbsp;&nbsp;&nbsp;&nbsp;print "ERROR"<br>
84<br>
85sam.ui_register("exec_py","exec_py runs python code",exec_py)
86</code>
87</div>
88<p>
89Now from the SamUI, one can type:
90<br>
91<div>
92<code>
93exec_py xyz = 10<br>
94exec_py print "xyz is %d" % xyz<br>
95</code>
96</div>
97<p>
98and see
99<br>
100<div>
101<code>
102xyz is 10
103</code>
104</div>
105<h4>RETURN VALUES</h4>
106<ul>
107None.
108</ul>
109
110<h4>SEE ALSO</h4>
111<ul><a href="cmd_run-cmd-file.html">run-command-file</a></ul>
112<ul><a href="http://www.python.org">python</a></ul>
113
114<hr>
115</body>
116</html>