pytty

changeset 18:27c8ec364325

Switched src/__init__.py to 4-space indentation
author Arc Riley <arcriley@gmail.com>
date Tue, 13 Dec 2011 12:48:40 -0500
parents c9c57104c858
children 69b68fa5f779
files src/__init__.py
diffstat 1 files changed, 67 insertions(+), 62 deletions(-) [+]
line diff
     1.1 --- a/src/__init__.py	Tue Dec 13 11:51:00 2011 -0500
     1.2 +++ b/src/__init__.py	Tue Dec 13 12:48:40 2011 -0500
     1.3 @@ -33,85 +33,90 @@
     1.4  
     1.5  
     1.6  class TTY (io.BufferedRWPair) :
     1.7 -  ''' TTY io class
     1.8 +    '''TTY io class
     1.9  
    1.10      This is a subclass of io.BufferedRWPair from the Python standard library
    1.11      which opens a tty device, sets nonblock mode on the device, and allows the
    1.12      user to change baud rate, flow control, and other settings often available
    1.13      to tty devices.
    1.14 -  '''
    1.15 +    '''
    1.16  
    1.17 -  # This is intended to be overridden by some subclasses
    1.18 -  _iobase = io.FileIO
    1.19 +    # This is intended to be overridden by some subclasses
    1.20 +    _iobase = io.FileIO
    1.21  
    1.22 -  def __init__ (self, name) :
    1.23 -    from fcntl import fcntl, F_SETFL, F_GETFL
    1.24 -    from os import O_NONBLOCK
    1.25  
    1.26 -    reader = self._iobase(name, 'r')
    1.27 +    def __init__ (self, name) :
    1.28 +        from fcntl import fcntl, F_SETFL, F_GETFL
    1.29 +        from os import O_NONBLOCK
    1.30  
    1.31 -    # ensure this is actually a tty device
    1.32 -    if not reader.isatty() :
    1.33 -      raise IOError('%s is not a tty device' % name)
    1.34 +        reader = self._iobase(name, 'r')
    1.35  
    1.36 -    # set non-blocking mode on the reader
    1.37 -    self._fdo = reader.fileno()
    1.38 -    fcntl(self._fdo, F_SETFL, (fcntl(self._fdo, F_GETFL) | O_NONBLOCK))
    1.39 +        # ensure this is actually a tty device
    1.40 +        if not reader.isatty() :
    1.41 +            raise IOError('%s is not a tty device' % name)
    1.42  
    1.43 -    # open a separate writer device
    1.44 -    writer = self._iobase(name, 'w')
    1.45 +        # set non-blocking mode on the reader
    1.46 +        self._fdo = reader.fileno()
    1.47 +        fcntl(self._fdo, F_SETFL, (fcntl(self._fdo, F_GETFL) | O_NONBLOCK))
    1.48  
    1.49 -    # set non-blocking mode on the reader
    1.50 -    self._fdi = reader.fileno()
    1.51 -    fcntl(self._fdi, F_SETFL, (fcntl(self._fdi, F_GETFL) | O_NONBLOCK))
    1.52 +        # open a separate writer device
    1.53 +        writer = self._iobase(name, 'w')
    1.54  
    1.55 -    # initialize self with BufferedRWPair
    1.56 -    super(TTY, self).__init__(reader, writer)
    1.57 +        # set non-blocking mode on the reader
    1.58 +        self._fdi = reader.fileno()
    1.59 +        fcntl(self._fdi, F_SETFL, (fcntl(self._fdi, F_GETFL) | O_NONBLOCK))
    1.60  
    1.61 +        # initialize self with BufferedRWPair
    1.62 +        super(TTY, self).__init__(reader, writer)
    1.63  
    1.64 -  @property
    1.65 -  def baud (self) :
    1.66 -    '''Baud rate must be supported by the tty device and in this list:
    1.67 -    [0, 50, 75, 110, 134, 150, 200, 300, 600, 1200, 1800, 2400, 4800, 9600,
    1.68 -     19200, 38400, 57600, 115200, 230400, 460800]'''
    1.69 -    import termios
    1.70 -    return {
    1.71 -      termios.B0 : 0,           termios.B50 : 50,
    1.72 -      termios.B75 : 75,         termios.B110 : 110,
    1.73 -      termios.B134 : 134,       termios.B150 : 150,
    1.74 -      termios.B200 : 200,       termios.B300 : 300,
    1.75 -      termios.B600 : 600,       termios.B1200 : 1200,
    1.76 -      termios.B1800 : 1800,     termios.B2400 : 2400,
    1.77 -      termios.B4800 : 4800,     termios.B9600 : 9600,
    1.78 -      termios.B19200 : 19200,   termios.B38400 : 38400,
    1.79 -      termios.B57600 : 57600,   termios.B115200 : 115200,
    1.80 -      termios.B230400 : 230400, termios.B460800 : 460800,
    1.81 -    }[termios.tcgetattr(self._fdi)[4]]
    1.82  
    1.83 +    @property
    1.84 +    def baud (self) :
    1.85 +        '''Baud rate
    1.86  
    1.87 -  @baud.setter
    1.88 -  def baud (self, value) :
    1.89 -    import termios
    1.90 -    try :
    1.91 -      tv = {
    1.92 -        0 : termios.B0,           50 : termios.B50,
    1.93 -        75 : termios.B75,         110 : termios.B110,
    1.94 -        134 : termios.B134,       150 : termios.B150,
    1.95 -        200 : termios.B200,       300 : termios.B300,
    1.96 -        600 : termios.B600,       1200 : termios.B1200,
    1.97 -        1800 : termios.B1800,     2400 : termios.B2400,
    1.98 -        4800 : termios.B4800,     9600 : termios.B9600,
    1.99 -        19200 : termios.B19200,   38400 : termios.B38400,
   1.100 -        57600 : termios.B57600,   115200 : termios.B115200,
   1.101 -        230400 : termios.B230400, 460800 : termios.B460800,
   1.102 -      }[value]
   1.103 -    except KeyError :
   1.104 -      raise IOError('Baud rate not supported by PyTTY.')
   1.105 -    tci = termios.tcgetattr(self._fdi)
   1.106 -    tco = termios.tcgetattr(self._fdo)
   1.107 -    tci[4], tci[5], tco[4], tco[5] = (tv,)*4
   1.108 -    termios.tcsetattr(self._fdi, termios.TCSANOW, tci)
   1.109 -    termios.tcsetattr(self._fdo, termios.TCSANOW, tco)
   1.110 +    Value must be supported by the tty device and in this list::
   1.111 +
   1.112 +        [0, 50, 75, 110, 134, 150, 200, 300, 600, 1200, 1800, 2400, 4800,
   1.113 +        9600, 19200, 38400, 57600, 115200, 230400, 460800]
   1.114 +        '''
   1.115 +        import termios
   1.116 +        return {
   1.117 +            termios.B0 : 0,           termios.B50 : 50,
   1.118 +            termios.B75 : 75,         termios.B110 : 110,
   1.119 +            termios.B134 : 134,       termios.B150 : 150,
   1.120 +            termios.B200 : 200,       termios.B300 : 300,
   1.121 +            termios.B600 : 600,       termios.B1200 : 1200,
   1.122 +            termios.B1800 : 1800,     termios.B2400 : 2400,
   1.123 +            termios.B4800 : 4800,     termios.B9600 : 9600,
   1.124 +            termios.B19200 : 19200,   termios.B38400 : 38400,
   1.125 +            termios.B57600 : 57600,   termios.B115200 : 115200,
   1.126 +            termios.B230400 : 230400, termios.B460800 : 460800,
   1.127 +        }[termios.tcgetattr(self._fdi)[4]]
   1.128 +
   1.129 +
   1.130 +    @baud.setter
   1.131 +    def baud (self, value) :
   1.132 +        import termios
   1.133 +        try :
   1.134 +            tv = {
   1.135 +                0 : termios.B0,           50 : termios.B50,
   1.136 +                75 : termios.B75,         110 : termios.B110,
   1.137 +                134 : termios.B134,       150 : termios.B150,
   1.138 +                200 : termios.B200,       300 : termios.B300,
   1.139 +                600 : termios.B600,       1200 : termios.B1200,
   1.140 +                1800 : termios.B1800,     2400 : termios.B2400,
   1.141 +                4800 : termios.B4800,     9600 : termios.B9600,
   1.142 +                19200 : termios.B19200,   38400 : termios.B38400,
   1.143 +                57600 : termios.B57600,   115200 : termios.B115200,
   1.144 +                230400 : termios.B230400, 460800 : termios.B460800,
   1.145 +            }[value]
   1.146 +        except KeyError :
   1.147 +            raise IOError('Baud rate not supported by PyTTY.')
   1.148 +        tci = termios.tcgetattr(self._fdi)
   1.149 +        tco = termios.tcgetattr(self._fdo)
   1.150 +        tci[4], tci[5], tco[4], tco[5] = (tv,)*4
   1.151 +        termios.tcsetattr(self._fdi, termios.TCSANOW, tci)
   1.152 +        termios.tcsetattr(self._fdo, termios.TCSANOW, tco)
   1.153  
   1.154  
   1.155  # Clean up package namespace