pytty

changeset 25:219b08e4217b

Added TTY.bits property.
author Arc Riley <arcriley@gmail.com>
date Mon, 09 Jul 2012 04:35:36 -0400
parents c44498ba0d72
children 381984878a49
files src/__init__.py
diffstat 1 files changed, 21 insertions(+), 6 deletions(-) [+]
line diff
     1.1 --- a/src/__init__.py	Mon Jan 02 17:33:32 2012 -0500
     1.2 +++ b/src/__init__.py	Mon Jul 09 04:35:36 2012 -0400
     1.3 @@ -28,10 +28,8 @@
     1.4  
     1.5  __version__ = '0.3'
     1.6  
     1.7 -
     1.8  import io
     1.9  
    1.10 -
    1.11  class TTY (io.BufferedRWPair) :
    1.12      '''TTY io class
    1.13  
    1.14 @@ -54,7 +52,6 @@
    1.15      # This is intended to be overridden by some subclasses
    1.16      _iobase = io.FileIO
    1.17  
    1.18 -
    1.19      def __init__ (self, name) :
    1.20          from fcntl import fcntl, F_SETFL, F_GETFL
    1.21          from os import O_NONBLOCK
    1.22 @@ -79,7 +76,6 @@
    1.23          # initialize self with BufferedRWPair
    1.24          super(TTY, self).__init__(reader, writer)
    1.25  
    1.26 -
    1.27      @property
    1.28      def baud (self) :
    1.29          '''Baud rate
    1.30 @@ -103,7 +99,6 @@
    1.31              termios.B230400 : 230400, termios.B460800 : 460800,
    1.32          }[termios.tcgetattr(self._fdi)[4]]
    1.33  
    1.34 -
    1.35      @baud.setter
    1.36      def baud (self, value) :
    1.37          import termios
    1.38 @@ -128,7 +123,27 @@
    1.39          termios.tcsetattr(self._fdi, termios.TCSANOW, tci)
    1.40          termios.tcsetattr(self._fdo, termios.TCSANOW, tco)
    1.41  
    1.42 +    @property
    1.43 +    def bits (self) :
    1.44 +        '''Number of bits per byte
    1.45 +
    1.46 +    This property determines how many bits are in a byte, between 5 and 8.
    1.47 +        '''
    1.48 +        import termios
    1.49 +        cflag = termios.tcgetattr(self._fdi)[2]
    1.50 +        return ((cflag & termios.CS8 and 8) or (cflag & termios.CS7 and 7) or
    1.51 +                (cflag & termios.CS6 and 6) or (cflag & termios.CS5 and 5))
    1.52 +
    1.53 +    @bits.setter
    1.54 +    def bits (self, value) :
    1.55 +        import termios
    1.56 +        if value < 5 or value > 8 :
    1.57 +            raise IOError('Byte size must be between 5 and 8 bits.')
    1.58 +        cs = (termios.CS5, termios.CS6, termios.CS7, termios.CS8)[value-5]
    1.59 +        for fd in (self._fdi, self._fdo) :
    1.60 +            tc = termios.tcgetattr(fd)
    1.61 +            tc[2] &= cs
    1.62 +            termios.tcsetattr(fd, termios.TCSANOW, tc)
    1.63  
    1.64  # Clean up package namespace
    1.65  del(io)
    1.66 -