1 """This is like pexpect, but will work on any file descriptor that you pass it.
2 You are reponsible for opening and close the file descriptor.
6 This license is approved by the OSI and FSF as GPL-compatible.
7 http://opensource.org/licenses/isc-license.txt
9 Copyright (c) 2012, Noah Spurrier <noah@noah.org>
10 PERMISSION TO USE, COPY, MODIFY, AND/OR DISTRIBUTE THIS SOFTWARE FOR ANY
11 PURPOSE WITH OR WITHOUT FEE IS HEREBY GRANTED, PROVIDED THAT THE ABOVE
12 COPYRIGHT NOTICE AND THIS PERMISSION NOTICE APPEAR IN ALL COPIES.
13 THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
14 WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
15 MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
16 ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
17 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
18 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
19 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
28 class fdspawn (spawn
):
30 """This is like pexpect.spawn but allows you to supply your own open file
31 descriptor. For example, you could use it to read through a file looking
32 for patterns, or to control a modem or serial device. """
34 def __init__ (self
, fd
, args
=[], timeout
=30, maxread
=2000, searchwindowsize
=None, logfile
=None):
36 """This takes a file descriptor (an int) or an object that support the
37 fileno() method (returning an int). All Python file-like objects
40 ### TODO: Add better handling of trying to use fdspawn in place of spawn
41 ### TODO: (overload to allow fdspawn to also handle commands as spawn does.
43 if type(fd
) != type(0) and hasattr(fd
, 'fileno'):
46 if type(fd
) != type(0):
47 raise ExceptionPexpect ('The fd argument is not an int. If this is a command string then maybe you want to use pexpect.spawn.')
49 try: # make sure fd is a valid file descriptor
52 raise ExceptionPexpect
, 'The fd argument is not a valid file descriptor.'
56 spawn
.__init
__(self
, None, args
, timeout
, maxread
, searchwindowsize
, logfile
)
60 self
.name
= '<file descriptor %d>' % fd
68 if self
.child_fd
== -1:
74 os
.close(self
.child_fd
)
80 """This checks if the file descriptor is still valid. If os.fstat()
81 does not raise an exception then we assume it is alive. """
83 if self
.child_fd
== -1:
86 os
.fstat(self
.child_fd
)
91 def terminate (self
, force
=False):
93 raise ExceptionPexpect ('This method is not valid for file descriptors.')