Initial commit of OpenSPARC T2 architecture model.
[OpenSPARC-T2-SAM] / sam-t2 / sam / docs / ui-cmds / cmd_run-python-file.html
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML//EN">
<html>
<head>
<title>SPARC Architectural Model: User Commands</title>
</head>
<body bgcolor="#FFFFFF" LANG="en-US">
<div STYLE="margin-bottom: 0cm"></div>&nbsp;
<h2>
run-python-file
</h2>
<h4>
NAME
</h4>
<ul>
run-python-file - execute python script file
</ul>
<h4>SYNOPSIS</h4>
<tt>
<ul>
run-python-file <i>filename</i>
</ul>
</tt>
<h4>DESCRIPTION</h4>
<ul>
Read Python code from <i>filename</i>. Any definitions are entered
into the top
level name-space in the Python environment. This command can be used to
start Python scripts inside simulation.
<p>
SAM provides a python method, sam.ui_register(), to register python
methods with the user interface.
This method can be used to add arbitrary functionality to the SAM UI.
<p>
The ui_exec() method allows SamUI commands to be executed from python.
<p>
The sam python object provides the following methods to access the
SamUI, registers and unregister methods as commands with the SamUI.
<br><br>
<table>
<tr>
<th>Method Name</th><th>Description</th>
</tr>
<tr valign="top">
<td>ui_exec ("<i>cmd</i>")</td>
<td>Sends <i>cmd</i> to the SamUI.</td>
</tr>
<tr valign="top">
<td>ui_register&nbsp;("<i>cmd_name</i>",&nbsp;"<i>help_string</i>",&nbsp;<i>cmd_function<i>)&nbsp;&nbsp;</td>
<td>Registers the python function <i>cmd_function</i> with the SamUI
as "<i>cmd_name</i>". The commands help string is
"<i>help_string</i>".</td>
</tr>
<tr valign="top">
<td>ui_unregister ("<i>cmd_name</i>")</td>
<td>Unregisters the command registered with the SamUI,
"<i>cmd_name</i>".</td>
</tr>
</table><br>
<p>
Here is an example that registers the python method
<code>echo()</code> as "py_echo".
<div>
<code>
def echo(*argv):<br>
&nbsp;&nbsp;print argv<br>
<br>
sam.ui_register("py_echo", "Prints its arguments", echo)<br>
</code>
</div>
<p>
Here is another example that runs python code from the SamUI:
<div>
<code>
def exec_py(*argv):<br>
&nbsp;&nbsp;cmd = " ".join(argv[1:])<br>
&nbsp;&nbsp;try:<br>
&nbsp;&nbsp;&nbsp;&nbsp;exec(cmd)<br>
&nbsp;&nbsp;except:<br>
&nbsp;&nbsp;&nbsp;&nbsp;print "ERROR"<br>
<br>
sam.ui_register("exec_py","exec_py runs python code",exec_py)
</code>
</div>
<p>
Now from the SamUI, one can type:
<br>
<div>
<code>
exec_py xyz = 10<br>
exec_py print "xyz is %d" % xyz<br>
</code>
</div>
<p>
and see
<br>
<div>
<code>
xyz is 10
</code>
</div>
<h4>RETURN VALUES</h4>
<ul>
None.
</ul>
<h4>SEE ALSO</h4>
<ul><a href="cmd_run-cmd-file.html">run-command-file</a></ul>
<ul><a href="http://www.python.org">python</a></ul>
<hr>
</body>
</html>