home *** CD-ROM | disk | FTP | other *** search
/ Clickx 115 / Clickx 115.iso / software / tools / windows / tails-i386-0.16.iso / live / filesystem.squashfs / usr / share / pyshared / fdpexpect.py < prev    next >
Encoding:
Python Source  |  2007-12-26  |  2.4 KB  |  83 lines

  1. """This is like pexpect, but will work on any file descriptor that you pass it.
  2. So you are reponsible for opening and close the file descriptor.
  3.  
  4. $Id: fdpexpect.py 505 2007-12-26 21:33:50Z noah $
  5. """
  6.  
  7. from pexpect import *
  8. import os
  9.  
  10. __all__ = ['fdspawn']
  11.  
  12. class fdspawn (spawn):
  13.  
  14.     """This is like pexpect.spawn but allows you to supply your own open file
  15.     descriptor. For example, you could use it to read through a file looking
  16.     for patterns, or to control a modem or serial device. """
  17.  
  18.     def __init__ (self, fd, args=[], timeout=30, maxread=2000, searchwindowsize=None, logfile=None):
  19.  
  20.         """This takes a file descriptor (an int) or an object that support the
  21.         fileno() method (returning an int). All Python file-like objects
  22.         support fileno(). """
  23.  
  24.         ### TODO: Add better handling of trying to use fdspawn in place of spawn
  25.         ### TODO: (overload to allow fdspawn to also handle commands as spawn does.
  26.  
  27.         if type(fd) != type(0) and hasattr(fd, 'fileno'):
  28.             fd = fd.fileno()
  29.  
  30.         if type(fd) != type(0):
  31.             raise ExceptionPexpect ('The fd argument is not an int. If this is a command string then maybe you want to use pexpect.spawn.')
  32.  
  33.         try: # make sure fd is a valid file descriptor
  34.             os.fstat(fd)
  35.         except OSError:
  36.             raise ExceptionPexpect, 'The fd argument is not a valid file descriptor.'
  37.  
  38.         self.args = None
  39.         self.command = None
  40.         spawn.__init__(self, None, args, timeout, maxread, searchwindowsize, logfile)
  41.         self.child_fd = fd
  42.         self.own_fd = False
  43.         self.closed = False
  44.         self.name = '<file descriptor %d>' % fd
  45.  
  46.     def __del__ (self):
  47.  
  48.         return
  49.  
  50.     def close (self):
  51.  
  52.         if self.child_fd == -1:
  53.             return
  54.         if self.own_fd:
  55.             self.close (self)
  56.         else:
  57.             self.flush()
  58.             os.close(self.child_fd)
  59.             self.child_fd = -1
  60.             self.closed = True
  61.  
  62.     def isalive (self):
  63.  
  64.         """This checks if the file descriptor is still valid. If os.fstat()
  65.         does not raise an exception then we assume it is alive. """
  66.  
  67.         if self.child_fd == -1:
  68.             return False
  69.         try:
  70.             os.fstat(self.child_fd)
  71.             return True
  72.         except:
  73.             return False
  74.  
  75.     def terminate (self, force=False):
  76.  
  77.         raise ExceptionPexpect ('This method is not valid for file descriptors.')
  78.  
  79.     def kill (self, sig):
  80.  
  81.         return
  82.  
  83.