1"""This is like pexpect, but will work on any file descriptor that you pass it.
2You are reponsible for opening and close the file descriptor.
3
4PEXPECT LICENSE
5
6    This license is approved by the OSI and FSF as GPL-compatible.
7        http://opensource.org/licenses/isc-license.txt
8
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.
20
21"""
22
23from pexpect import *
24import os
25
26__all__ = ['fdspawn']
27
28class fdspawn (spawn):
29
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. """
33
34    def __init__ (self, fd, args=[], timeout=30, maxread=2000, searchwindowsize=None, logfile=None):
35
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
38        support fileno(). """
39
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.
42
43        if type(fd) != type(0) and hasattr(fd, 'fileno'):
44            fd = fd.fileno()
45
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.')
48
49        try: # make sure fd is a valid file descriptor
50            os.fstat(fd)
51        except OSError:
52            raise ExceptionPexpect, 'The fd argument is not a valid file descriptor.'
53
54        self.args = None
55        self.command = None
56        spawn.__init__(self, None, args, timeout, maxread, searchwindowsize, logfile)
57        self.child_fd = fd
58        self.own_fd = False
59        self.closed = False
60        self.name = '<file descriptor %d>' % fd
61
62    def __del__ (self):
63
64        return
65
66    def close (self):
67
68        if self.child_fd == -1:
69            return
70        if self.own_fd:
71            self.close (self)
72        else:
73            self.flush()
74            os.close(self.child_fd)
75            self.child_fd = -1
76            self.closed = True
77
78    def isalive (self):
79
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. """
82
83        if self.child_fd == -1:
84            return False
85        try:
86            os.fstat(self.child_fd)
87            return True
88        except:
89            return False
90
91    def terminate (self, force=False):
92
93        raise ExceptionPexpect ('This method is not valid for file descriptors.')
94
95    def kill (self, sig):
96
97        return
98
99