pytty

changeset 31:34b161a8e076

Now supports whatever baud rates are offered by the termios module, which differs from platform to platform. This uses dict comprehension, so PyTTY no longer supports Python 2.6.
author Arc Riley <arcriley@gmail.com>
date Fri, 20 Jul 2012 18:21:54 -0400
parents 873320419f15
children 6dc760b9f35e
files setup.py src/__init__.py
diffstat 2 files changed, 26 insertions(+), 32 deletions(-) [+]
line diff
     1.1 --- a/setup.py	Tue Jul 10 03:53:06 2012 -0400
     1.2 +++ b/setup.py	Fri Jul 20 18:21:54 2012 -0400
     1.3 @@ -1,8 +1,13 @@
     1.4  #!/usr/bin/env python
     1.5 +# -*- coding: utf-8 -*-
     1.6 +
     1.7 +# PyTTY works with Python 2.7 and above only
     1.8 +import sys
     1.9 +if sys.version_info[0:2] < (2, 7) :
    1.10 +    raise RuntimeError('Python 2.7 or above is required for this package.')
    1.11  
    1.12  
    1.13  from src import __author__, __credits__, __doc__, __version__
    1.14 -import sys
    1.15  from distutils.core import setup
    1.16  
    1.17  
    1.18 @@ -27,7 +32,6 @@
    1.19          'License :: OSI Approved :: GNU Library or Lesser General Public License (LGPL)',
    1.20          'Natural Language :: English',
    1.21          'Operating System :: POSIX',
    1.22 -        'Programming Language :: Python :: 2.6',
    1.23          'Programming Language :: Python :: 2.7',
    1.24          'Programming Language :: Python :: 3',
    1.25          'Programming Language :: Python :: 3.1',
     2.1 --- a/src/__init__.py	Tue Jul 10 03:53:06 2012 -0400
     2.2 +++ b/src/__init__.py	Fri Jul 20 18:21:54 2012 -0400
     2.3 @@ -52,6 +52,19 @@
     2.4      # This is intended to be overridden by some subclasses
     2.5      _iobase = io.FileIO
     2.6  
     2.7 +    # Generator to compile our baudrate/integer lookup dicts
     2.8 +    def _baudrates() :
     2.9 +        import termios
    2.10 +        for member in termios.__dict__ :
    2.11 +            if member[0] == 'B' and member[1:].isdigit() :
    2.12 +                yield (int(member[1:]), termios.__dict__[member])
    2.13 +
    2.14 +    # {termios.B9600 : 9600, termios.B19200 : 19200, etc}
    2.15 +    _baud2int = {b[1] : b[0] for b in _baudrates()}
    2.16 +
    2.17 +    # {9600 : termios.B9600, 19200 : termios.B19200, etc}
    2.18 +    _int2baud = {b[0] : b[1] for b in _baudrates()}
    2.19 +
    2.20      def __init__ (self, name) :
    2.21          from fcntl import fcntl, F_SETFL, F_GETFL
    2.22          from os import O_NONBLOCK
    2.23 @@ -86,43 +99,20 @@
    2.24      def baud (self) :
    2.25          '''Baud rate
    2.26  
    2.27 -    Value must be supported by the tty device and in this list::
    2.28 -
    2.29 -        [0, 50, 75, 110, 134, 150, 200, 300, 600, 1200, 1800, 2400, 4800,
    2.30 -        9600, 19200, 38400, 57600, 115200, 230400, 460800]
    2.31 -        '''
    2.32 +    Value must be supported by the tty device and the termios module.'''
    2.33          import termios
    2.34 -        return {
    2.35 -            termios.B0 : 0,           termios.B50 : 50,
    2.36 -            termios.B75 : 75,         termios.B110 : 110,
    2.37 -            termios.B134 : 134,       termios.B150 : 150,
    2.38 -            termios.B200 : 200,       termios.B300 : 300,
    2.39 -            termios.B600 : 600,       termios.B1200 : 1200,
    2.40 -            termios.B1800 : 1800,     termios.B2400 : 2400,
    2.41 -            termios.B4800 : 4800,     termios.B9600 : 9600,
    2.42 -            termios.B19200 : 19200,   termios.B38400 : 38400,
    2.43 -            termios.B57600 : 57600,   termios.B115200 : 115200,
    2.44 -            termios.B230400 : 230400, termios.B460800 : 460800,
    2.45 -        }[termios.tcgetattr(self._fdi)[4]]
    2.46 +        try :
    2.47 +            return self._baud2int[termios.tcgetattr(self._fdi)[4]]
    2.48 +        except KeyError :
    2.49 +            raise IOError('Current baud rate not supported by termios.')
    2.50  
    2.51      @baud.setter
    2.52      def baud (self, value) :
    2.53          import termios
    2.54          try :
    2.55 -            tv = {
    2.56 -                0 : termios.B0,           50 : termios.B50,
    2.57 -                75 : termios.B75,         110 : termios.B110,
    2.58 -                134 : termios.B134,       150 : termios.B150,
    2.59 -                200 : termios.B200,       300 : termios.B300,
    2.60 -                600 : termios.B600,       1200 : termios.B1200,
    2.61 -                1800 : termios.B1800,     2400 : termios.B2400,
    2.62 -                4800 : termios.B4800,     9600 : termios.B9600,
    2.63 -                19200 : termios.B19200,   38400 : termios.B38400,
    2.64 -                57600 : termios.B57600,   115200 : termios.B115200,
    2.65 -                230400 : termios.B230400, 460800 : termios.B460800,
    2.66 -            }[value]
    2.67 +            tv = self._int2baud[value]
    2.68          except KeyError :
    2.69 -            raise IOError('Baud rate not supported by PyTTY.')
    2.70 +            raise IOError('Baud rate not supported by termios.')
    2.71          tci = termios.tcgetattr(self._fdi)
    2.72          tco = termios.tcgetattr(self._fdo)
    2.73          tci[4], tci[5], tco[4], tco[5] = (tv,)*4