1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336
|
<!doctype html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html><head><title>Python: module fdpexpect</title>
</head><body bgcolor="#f0f0f8">
<table width="100%" cellspacing=0 cellpadding=2 border=0 summary="heading">
<tr bgcolor="#7799ee">
<td valign=bottom> <br>
<font color="#ffffff" face="helvetica, arial"> <br><big><big><strong>fdpexpect</strong></big></big> (version 2.1)</font></td
><td align=right valign=bottom
><font color="#ffffff" face="helvetica, arial"><a href=".">index</a><br><a href="file:/home/noah/documents/export/engineering/source/python/pexpect/trunk/pexpect/fdpexpect.py">/home/noah/documents/export/engineering/source/python/pexpect/trunk/pexpect/fdpexpect.py</a></font></td></tr></table>
<p><tt>TODO: This is BETA. When it gets stable I will move it into the pexpect.py file.</tt></p>
<p>
<table width="100%" cellspacing=0 cellpadding=2 border=0 summary="section">
<tr bgcolor="#aa55cc">
<td colspan=3 valign=bottom> <br>
<font color="#fffff" face="helvetica, arial"><big><strong>Modules</strong></big></font></td></tr>
<tr><td bgcolor="#aa55cc"><tt> </tt></td><td> </td>
<td width="100%"><table width="100%" summary="list"><tr><td width="25%" valign=top><a href="os.html">os</a><br>
</td><td width="25%" valign=top></td><td width="25%" valign=top></td><td width="25%" valign=top></td></tr></table></td></tr></table><p>
<table width="100%" cellspacing=0 cellpadding=2 border=0 summary="section">
<tr bgcolor="#ee77aa">
<td colspan=3 valign=bottom> <br>
<font color="#ffffff" face="helvetica, arial"><big><strong>Classes</strong></big></font></td></tr>
<tr><td bgcolor="#ee77aa"><tt> </tt></td><td> </td>
<td width="100%"><dl>
<dt><font face="helvetica, arial"><a href="pexpect.html#spawn">pexpect.spawn</a>(<a href="__builtin__.html#object">__builtin__.object</a>)
</font></dt><dd>
<dl>
<dt><font face="helvetica, arial"><a href="fdpexpect.html#fdspawn">fdspawn</a>
</font></dt></dl>
</dd>
</dl>
<p>
<table width="100%" cellspacing=0 cellpadding=2 border=0 summary="section">
<tr bgcolor="#ffc8d8">
<td colspan=3 valign=bottom> <br>
<font color="#000000" face="helvetica, arial"><a name="fdspawn">class <strong>fdspawn</strong></a>(<a href="pexpect.html#spawn">pexpect.spawn</a>)</font></td></tr>
<tr bgcolor="#ffc8d8"><td rowspan=2><tt> </tt></td>
<td colspan=2><tt>This is like pexpect.<a href="pexpect.html#spawn">spawn</a> but allows you to supply your own,<br>
already open file descriptor. For example, you could use it to<br>
read through a file looking for patterns, or to control a modem or<br>
serial device.<br> </tt></td></tr>
<tr><td> </td>
<td width="100%"><dl><dt>Method resolution order:</dt>
<dd><a href="fdpexpect.html#fdspawn">fdspawn</a></dd>
<dd><a href="pexpect.html#spawn">pexpect.spawn</a></dd>
<dd><a href="__builtin__.html#object">__builtin__.object</a></dd>
</dl>
<hr>
Methods defined here:<br>
<dl><dt><a name="fdspawn-__del__"><strong>__del__</strong></a>(self)</dt></dl>
<dl><dt><a name="fdspawn-__init__"><strong>__init__</strong></a>(self, fd, args<font color="#909090">=[]</font>, timeout<font color="#909090">=30</font>, maxread<font color="#909090">=2000</font>, searchwindowsize<font color="#909090">=None</font>, logfile<font color="#909090">=None</font>)</dt><dd><tt>This takes a file descriptor (an int) or an object that support the <a href="#fdspawn-fileno">fileno</a>() method<br>
(returning an int). All Python file-like objects support <a href="#fdspawn-fileno">fileno</a>().</tt></dd></dl>
<dl><dt><a name="fdspawn-close"><strong>close</strong></a>(self)</dt></dl>
<dl><dt><a name="fdspawn-isalive"><strong>isalive</strong></a>(self)</dt><dd><tt>This checks if the file descriptor is still valid.<br>
If os.fstat() does not raise an exception then we assume it is alive.</tt></dd></dl>
<dl><dt><a name="fdspawn-kill"><strong>kill</strong></a>(self, sig)</dt></dl>
<dl><dt><a name="fdspawn-terminate"><strong>terminate</strong></a>(self, force<font color="#909090">=False</font>)</dt></dl>
<hr>
Methods inherited from <a href="pexpect.html#spawn">pexpect.spawn</a>:<br>
<dl><dt><a name="fdspawn-__iter__"><strong>__iter__</strong></a>(self)</dt><dd><tt>This is to support iterators over a file-like object.</tt></dd></dl>
<dl><dt><a name="fdspawn-__str__"><strong>__str__</strong></a>(self)</dt><dd><tt>This returns the current state of the pexpect object as a string.</tt></dd></dl>
<dl><dt><a name="fdspawn-compile_pattern_list"><strong>compile_pattern_list</strong></a>(self, patterns)</dt><dd><tt>This compiles a pattern-string or a list of pattern-strings.<br>
Patterns must be a StringType, EOF, TIMEOUT, SRE_Pattern, or <br>
a list of those. Patterns may also be None which results in<br>
an empty list.<br>
<br>
This is used by <a href="#fdspawn-expect">expect</a>() when calling <a href="#fdspawn-expect_list">expect_list</a>().<br>
Thus <a href="#fdspawn-expect">expect</a>() is nothing more than::<br>
cpl = <a href="#fdspawn-compile_pattern_list">compile_pattern_list</a>(pl)<br>
return <a href="#fdspawn-expect_list">expect_list</a>(clp, timeout)<br>
<br>
If you are using <a href="#fdspawn-expect">expect</a>() within a loop it may be more<br>
efficient to compile the patterns first and then call <a href="#fdspawn-expect_list">expect_list</a>().<br>
This avoid calls in a loop to <a href="#fdspawn-compile_pattern_list">compile_pattern_list</a>():<br>
cpl = <a href="#fdspawn-compile_pattern_list">compile_pattern_list</a>(my_pattern)<br>
while some_condition:<br>
...<br>
i = <a href="#fdspawn-expect_list">expect_list</a>(clp, timeout)<br>
...</tt></dd></dl>
<dl><dt><a name="fdspawn-eof"><strong>eof</strong></a>(self)</dt><dd><tt>This returns True if the EOF exception was ever raised.</tt></dd></dl>
<dl><dt><a name="fdspawn-expect"><strong>expect</strong></a>(self, pattern, timeout<font color="#909090">=-1</font>, searchwindowsize<font color="#909090">=None</font>)</dt><dd><tt>This seeks through the stream until a pattern is matched.<br>
The pattern is overloaded and may take several types including a list.<br>
The pattern can be a StringType, EOF, a compiled re, or a list of<br>
those types. Strings will be compiled to re types. This returns the<br>
index into the pattern list. If the pattern was not a list this<br>
returns index 0 on a successful match. This may raise exceptions for<br>
EOF or TIMEOUT. To avoid the EOF or TIMEOUT exceptions add<br>
EOF or TIMEOUT to the pattern list.<br>
<br>
After a match is found the instance attributes<br>
'before', 'after' and 'match' will be set.<br>
You can see all the data read before the match in 'before'.<br>
You can see the data that was matched in 'after'.<br>
The re.MatchObject used in the re match will be in 'match'.<br>
If an error occured then 'before' will be set to all the<br>
data read so far and 'after' and 'match' will be None.<br>
<br>
If timeout is -1 then timeout will be set to the self.<strong>timeout</strong> value.<br>
<br>
Note: A list entry may be EOF or TIMEOUT instead of a string.<br>
This will catch these exceptions and return the index<br>
of the list entry instead of raising the exception.<br>
The attribute 'after' will be set to the exception type.<br>
The attribute 'match' will be None.<br>
This allows you to write code like this:<br>
index = p.expect (['good', 'bad', pexpect.EOF, pexpect.TIMEOUT])<br>
if index == 0:<br>
do_something()<br>
elif index == 1:<br>
do_something_else()<br>
elif index == 2:<br>
do_some_other_thing()<br>
elif index == 3:<br>
do_something_completely_different()<br>
instead of code like this:<br>
try:<br>
index = p.expect (['good', 'bad'])<br>
if index == 0:<br>
do_something()<br>
elif index == 1:<br>
do_something_else()<br>
except EOF:<br>
do_some_other_thing()<br>
except TIMEOUT:<br>
do_something_completely_different()<br>
These two forms are equivalent. It all depends on what you want.<br>
You can also just expect the EOF if you are waiting for all output<br>
of a child to finish. For example:<br>
p = pexpect.<a href="pexpect.html#spawn">spawn</a>('/bin/ls')<br>
p.expect (pexpect.EOF)<br>
print p.before<br>
<br>
If you are trying to optimize for speed then see <a href="#fdspawn-expect_list">expect_list</a>().</tt></dd></dl>
<dl><dt><a name="fdspawn-expect_exact"><strong>expect_exact</strong></a>(self, pattern_list, timeout<font color="#909090">=-1</font>)</dt><dd><tt>This method is no longer supported or allowed.<br>
It was too hard to maintain and keep it up to date with expect_list.<br>
Few people used this method. Most people favored reliability over speed.<br>
The implementation is left in comments in case anyone needs to hack this<br>
feature back into their copy.<br>
If someone wants to diff this with expect_list and make them work<br>
nearly the same then I will consider adding this make in.</tt></dd></dl>
<dl><dt><a name="fdspawn-expect_list"><strong>expect_list</strong></a>(self, pattern_list, timeout<font color="#909090">=-1</font>, searchwindowsize<font color="#909090">=-1</font>)</dt><dd><tt>This takes a list of compiled regular expressions and returns <br>
the index into the pattern_list that matched the child output.<br>
The list may also contain EOF or TIMEOUT (which are not<br>
compiled regular expressions). This method is similar to<br>
the <a href="#fdspawn-expect">expect</a>() method except that <a href="#fdspawn-expect_list">expect_list</a>() does not<br>
recompile the pattern list on every call.<br>
This may help if you are trying to optimize for speed, otherwise<br>
just use the <a href="#fdspawn-expect">expect</a>() method. This is called by <a href="#fdspawn-expect">expect</a>().<br>
If timeout==-1 then the self.<strong>timeout</strong> value is used.<br>
If searchwindowsize==-1 then the self.<strong>searchwindowsize</strong> value is used.</tt></dd></dl>
<dl><dt><a name="fdspawn-fileno"><strong>fileno</strong></a>(self)</dt><dd><tt>This returns the file descriptor of the pty for the child.</tt></dd></dl>
<dl><dt><a name="fdspawn-flush"><strong>flush</strong></a>(self)</dt><dd><tt>This does nothing. It is here to support the interface for a File-like object.</tt></dd></dl>
<dl><dt><a name="fdspawn-getwinsize"><strong>getwinsize</strong></a>(self)</dt><dd><tt>This returns the terminal window size of the child tty.<br>
The return value is a tuple of (rows, cols).</tt></dd></dl>
<dl><dt><a name="fdspawn-interact"><strong>interact</strong></a>(self, escape_character<font color="#909090">='<font color="#c040c0">\x1d</font>'</font>, input_filter<font color="#909090">=None</font>, output_filter<font color="#909090">=None</font>)</dt><dd><tt>This gives control of the child process to the interactive user<br>
(the human at the keyboard).<br>
Keystrokes are sent to the child process, and the stdout and stderr<br>
output of the child process is printed.<br>
This simply echos the child stdout and child stderr to the real<br>
stdout and it echos the real stdin to the child stdin.<br>
When the user types the escape_character this method will stop.<br>
The default for escape_character is ^]. This should not be confused<br>
with ASCII 27 -- the ESC character. ASCII 29 was chosen<br>
for historical merit because this is the character used<br>
by 'telnet' as the escape character. The escape_character will<br>
not be sent to the child process.<br>
<br>
You may pass in optional input and output filter functions.<br>
These functions should take a string and return a string.<br>
The output_filter will be passed all the output from the child process.<br>
The input_filter will be passed all the keyboard input from the user.<br>
The input_filter is run BEFORE the check for the escape_character.<br>
<br>
Note that if you change the window size of the parent<br>
the SIGWINCH signal will not be passed through to the child.<br>
If you want the child window size to change when the parent's<br>
window size changes then do something like the following example:<br>
import pexpect, struct, fcntl, termios, signal, sys<br>
def sigwinch_passthrough (sig, data):<br>
s = struct.pack("HHHH", 0, 0, 0, 0)<br>
a = struct.unpack('hhhh', fcntl.ioctl(sys.stdout.<a href="#fdspawn-fileno">fileno</a>(), termios.TIOCGWINSZ , s))<br>
global p<br>
p.<a href="#fdspawn-setwinsize">setwinsize</a>(a[0],a[1])<br>
p = pexpect.<a href="pexpect.html#spawn">spawn</a>('/bin/bash') # Note this is global and used in sigwinch_passthrough.<br>
signal.signal(signal.SIGWINCH, sigwinch_passthrough)<br>
p.<a href="#fdspawn-interact">interact</a>()</tt></dd></dl>
<dl><dt><a name="fdspawn-isatty"><strong>isatty</strong></a>(self)</dt><dd><tt>This returns True if the file descriptor is open and connected to a tty(-like) device, else False.</tt></dd></dl>
<dl><dt><a name="fdspawn-next"><strong>next</strong></a>(self)</dt><dd><tt>This is to support iterators over a file-like object.</tt></dd></dl>
<dl><dt><a name="fdspawn-read"><strong>read</strong></a>(self, size<font color="#909090">=-1</font>)</dt><dd><tt>This reads at most "size" bytes from the file <br>
(less if the read hits EOF before obtaining size bytes). <br>
If the size argument is negative or omitted, <br>
read all data until EOF is reached. <br>
The bytes are returned as a string object. <br>
An empty string is returned when EOF is encountered immediately.</tt></dd></dl>
<dl><dt><a name="fdspawn-read_nonblocking"><strong>read_nonblocking</strong></a>(self, size<font color="#909090">=1</font>, timeout<font color="#909090">=-1</font>)</dt><dd><tt>This reads at most size characters from the child application.<br>
It includes a timeout. If the read does not complete within the<br>
timeout period then a TIMEOUT exception is raised.<br>
If the end of file is read then an EOF exception will be raised.<br>
If a log file was set using <a href="#fdspawn-setlog">setlog</a>() then all data will<br>
also be written to the log file.<br>
<br>
If timeout==None then the read may block indefinitely.<br>
If timeout==-1 then the self.<strong>timeout</strong> value is used.<br>
If timeout==0 then the child is polled and <br>
if there was no data immediately ready then this will raise a TIMEOUT exception.<br>
<br>
The "timeout" refers only to the amount of time to read at least one character.<br>
This is not effected by the 'size' parameter, so if you call<br>
<a href="#fdspawn-read_nonblocking">read_nonblocking</a>(size=100, timeout=30) and only one character is<br>
available right away then one character will be returned immediately. <br>
It will not wait for 30 seconds for another 99 characters to come in.<br>
<br>
This is a wrapper around os.<a href="#fdspawn-read">read</a>().<br>
It uses select.select() to implement a timeout.</tt></dd></dl>
<dl><dt><a name="fdspawn-readline"><strong>readline</strong></a>(self, size<font color="#909090">=-1</font>)</dt><dd><tt>This reads and returns one entire line. A trailing newline is kept in<br>
the string, but may be absent when a file ends with an incomplete line. <br>
Note: This <a href="#fdspawn-readline">readline</a>() looks for a \r\n pair even on UNIX because<br>
this is what the pseudo tty device returns. So contrary to what you<br>
may expect you will receive the newline as \r\n.<br>
An empty string is returned when EOF is hit immediately.<br>
Currently, the size agument is mostly ignored, so this behavior is not<br>
standard for a file-like object. If size is 0 then an empty string<br>
is returned.</tt></dd></dl>
<dl><dt><a name="fdspawn-readlines"><strong>readlines</strong></a>(self, sizehint<font color="#909090">=-1</font>)</dt><dd><tt>This reads until EOF using <a href="#fdspawn-readline">readline</a>() and returns a list containing <br>
the lines thus read. The optional "sizehint" argument is ignored.</tt></dd></dl>
<dl><dt><a name="fdspawn-send"><strong>send</strong></a>(self, str)</dt><dd><tt>This sends a string to the child process.<br>
This returns the number of bytes written.<br>
If a log file was set then the data is also written to the log.</tt></dd></dl>
<dl><dt><a name="fdspawn-sendeof"><strong>sendeof</strong></a>(self)</dt><dd><tt>This sends an EOF to the child.<br>
This sends a character which causes the pending parent output<br>
buffer to be sent to the waiting child program without<br>
waiting for end-of-line. If it is the first character of the<br>
line, the <a href="#fdspawn-read">read</a>() in the user program returns 0, which<br>
signifies end-of-file. This means to work as expected <br>
a <a href="#fdspawn-sendeof">sendeof</a>() has to be called at the begining of a line. <br>
This method does not send a newline. It is the responsibility<br>
of the caller to ensure the eof is sent at the beginning of a line.</tt></dd></dl>
<dl><dt><a name="fdspawn-sendline"><strong>sendline</strong></a>(self, str<font color="#909090">=''</font>)</dt><dd><tt>This is like <a href="#fdspawn-send">send</a>(), but it adds a line feed (os.linesep).<br>
This returns the number of bytes written.</tt></dd></dl>
<dl><dt><a name="fdspawn-setecho"><strong>setecho</strong></a>(self, state)</dt><dd><tt>This sets the terminal echo mode on or off.<br>
Note that anything the child sent before the echo will be lost, so<br>
you should be sure that your input buffer is empty before you setecho.<br>
For example, the following will work as expected.<br>
p = pexpect.<a href="pexpect.html#spawn">spawn</a>('cat')<br>
p.sendline ('1234') # We will see this twice (once from tty echo and again from cat).<br>
p.expect (['1234'])<br>
p.expect (['1234'])<br>
p.<a href="#fdspawn-setecho">setecho</a>(False) # Turn off tty echo<br>
p.sendline ('abcd') # We will set this only once (echoed by cat).<br>
p.sendline ('wxyz') # We will set this only once (echoed by cat)<br>
p.expect (['abcd'])<br>
p.expect (['wxyz'])<br>
The following WILL NOT WORK because the lines sent before the setecho<br>
will be lost:<br>
p = pexpect.<a href="pexpect.html#spawn">spawn</a>('cat')<br>
p.sendline ('1234') # We will see this twice (once from tty echo and again from cat).<br>
p.<a href="#fdspawn-setecho">setecho</a>(False) # Turn off tty echo<br>
p.sendline ('abcd') # We will set this only once (echoed by cat).<br>
p.sendline ('wxyz') # We will set this only once (echoed by cat)<br>
p.expect (['1234'])<br>
p.expect (['1234'])<br>
p.expect (['abcd'])<br>
p.expect (['wxyz'])</tt></dd></dl>
<dl><dt><a name="fdspawn-setlog"><strong>setlog</strong></a>(self, fileobject)</dt><dd><tt>This method is no longer supported or allowed.</tt></dd></dl>
<dl><dt><a name="fdspawn-setmaxread"><strong>setmaxread</strong></a>(self, maxread)</dt><dd><tt>This method is no longer supported or allowed.<br>
I don't like getters and setters without a good reason.</tt></dd></dl>
<dl><dt><a name="fdspawn-setwinsize"><strong>setwinsize</strong></a>(self, r, c)</dt><dd><tt>This sets the terminal window size of the child tty.<br>
This will cause a SIGWINCH signal to be sent to the child.<br>
This does not change the physical window size.<br>
It changes the size reported to TTY-aware applications like<br>
vi or curses -- applications that respond to the SIGWINCH signal.</tt></dd></dl>
<dl><dt><a name="fdspawn-wait"><strong>wait</strong></a>(self)</dt><dd><tt>This waits until the child exits. This is a blocking call.<br>
This will not read any data from the child, so this will block forever<br>
if the child has unread output and has terminated. In other words, the child<br>
may have printed output then called exit(); but, technically, the child is<br>
still alive until its output is read.</tt></dd></dl>
<dl><dt><a name="fdspawn-write"><strong>write</strong></a>(self, str)</dt><dd><tt>This is similar to <a href="#fdspawn-send">send</a>() except that there is no return value.</tt></dd></dl>
<dl><dt><a name="fdspawn-writelines"><strong>writelines</strong></a>(self, sequence)</dt><dd><tt>This calls <a href="#fdspawn-write">write</a>() for each element in the sequence.<br>
The sequence can be any iterable object producing strings, <br>
typically a list of strings. This does not add line separators<br>
There is no return value.</tt></dd></dl>
<hr>
Data and other attributes inherited from <a href="pexpect.html#spawn">pexpect.spawn</a>:<br>
<dl><dt><strong>__dict__</strong> = <dictproxy object><dd><tt>dictionary for instance variables (if defined)</tt></dl>
<dl><dt><strong>__weakref__</strong> = <attribute '__weakref__' of 'spawn' objects><dd><tt>list of weak references to the object (if defined)</tt></dl>
</td></tr></table></td></tr></table><p>
<table width="100%" cellspacing=0 cellpadding=2 border=0 summary="section">
<tr bgcolor="#55aa55">
<td colspan=3 valign=bottom> <br>
<font color="#ffffff" face="helvetica, arial"><big><strong>Data</strong></big></font></td></tr>
<tr><td bgcolor="#55aa55"><tt> </tt></td><td> </td>
<td width="100%"><strong>__all__</strong> = ['fdspawn']<br>
<strong>__revision__</strong> = '$Revision: 395 $'<br>
<strong>__version__</strong> = '2.1'</td></tr></table>
</body></html>
|