<s> try : <EOL> bytes <EOL> bytearray <EOL> except ( NameError , AttributeError ) : <EOL> bytes = str <EOL> class bytearray ( list ) : <EOL> def __str__ ( self ) : return '<STR_LIT>' . join ( self ) <EOL> def __repr__ ( self ) : return '<STR_LIT>' % '<STR_LIT>' . join ( self ) <EOL> def append ( self , item ) : <EOL> if isinstance ( item , str ) : <EOL> list . append ( self , item ) <EOL> else : <EOL> list . append ( self , chr ( item ) ) <EOL> def __iadd__ ( self , other ) : <EOL> for byte in other : <EOL> self . append ( byte ) <EOL> return self <EOL> def __getslice__ ( self , i , j ) : <EOL> return bytearray ( list . __getslice__ ( self , i , j ) ) <EOL> def __getitem__ ( self , item ) : <EOL> if isinstance ( item , slice ) : <EOL> return bytearray ( list . __getitem__ ( self , item ) ) <EOL> else : <EOL> return ord ( list . __getitem__ ( self , item ) ) <EOL> def __eq__ ( self , other ) : <EOL> if isinstance ( other , basestring ) : <EOL> other = bytearray ( other ) <EOL> return list . __eq__ ( self , other ) <EOL> try : <EOL> memoryview <EOL> except ( NameError , AttributeError ) : <EOL> class memoryview : <EOL> pass <EOL> def to_bytes ( seq ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( seq , bytes ) : <EOL> return seq <EOL> elif isinstance ( seq , bytearray ) : <EOL> return bytes ( seq ) <EOL> elif isinstance ( seq , memoryview ) : <EOL> return seq . tobytes ( ) <EOL> else : <EOL> b = bytearray ( ) <EOL> for item in seq : <EOL> b . append ( item ) <EOL> return bytes ( b ) <EOL> XON = to_bytes ( [ <NUM_LIT> ] ) <EOL> XOFF = to_bytes ( [ <NUM_LIT> ] ) <EOL> CR = to_bytes ( [ <NUM_LIT> ] ) <EOL> LF = to_bytes ( [ <NUM_LIT:10> ] ) <EOL> PARITY_NONE , PARITY_EVEN , PARITY_ODD , PARITY_MARK , PARITY_SPACE = '<STR_LIT:N>' , '<STR_LIT:E>' , '<STR_LIT:O>' , '<STR_LIT:M>' , '<STR_LIT:S>' <EOL> STOPBITS_ONE , STOPBITS_ONE_POINT_FIVE , STOPBITS_TWO = ( <NUM_LIT:1> , <NUM_LIT> , <NUM_LIT:2> ) <EOL> FIVEBITS , SIXBITS , SEVENBITS , EIGHTBITS = ( <NUM_LIT:5> , <NUM_LIT:6> , <NUM_LIT:7> , <NUM_LIT:8> ) <EOL> PARITY_NAMES = { <EOL> PARITY_NONE : '<STR_LIT:None>' , <EOL> PARITY_EVEN : '<STR_LIT>' , <EOL> PARITY_ODD : '<STR_LIT>' , <EOL> PARITY_MARK : '<STR_LIT>' , <EOL> PARITY_SPACE : '<STR_LIT>' , <EOL> } <EOL> class SerialException ( IOError ) : <EOL> """<STR_LIT>""" <EOL> class SerialTimeoutException ( SerialException ) : <EOL> """<STR_LIT>""" <EOL> writeTimeoutError = SerialTimeoutException ( '<STR_LIT>' ) <EOL> portNotOpenError = SerialException ( '<STR_LIT>' ) <EOL> class FileLike ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self ) : <EOL> self . closed = True <EOL> def close ( self ) : <EOL> self . closed = True <EOL> def __del__ ( self ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> self . close ( ) <EOL> except : <EOL> pass <EOL> def writelines ( self , sequence ) : <EOL> for line in sequence : <EOL> self . write ( line ) <EOL> def flush ( self ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> def next ( self ) : <EOL> line = self . readline ( ) <EOL> if not line : raise StopIteration <EOL> return line <EOL> def __iter__ ( self ) : <EOL> return self <EOL> def readline ( self , size = None , eol = LF ) : <EOL> """<STR_LIT>""" <EOL> leneol = len ( eol ) <EOL> line = bytearray ( ) <EOL> while True : <EOL> c = self . read ( <NUM_LIT:1> ) <EOL> if c : <EOL> line += c <EOL> if line [ - leneol : ] == eol : <EOL> break <EOL> if size is not None and len ( line ) >= size : <EOL> break <EOL> else : <EOL> break <EOL> return bytes ( line ) <EOL> def readlines ( self , sizehint = None , eol = LF ) : <EOL> """<STR_LIT>""" <EOL> if self . timeout is None : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> leneol = len ( eol ) <EOL> lines = [ ] <EOL> while True : <EOL> line = self . readline ( eol = eol ) <EOL> if line : <EOL> lines . append ( line ) <EOL> if line [ - leneol : ] != eol : <EOL> break <EOL> else : <EOL> break <EOL> return lines <EOL> def xreadlines ( self , sizehint = None ) : <EOL> """<STR_LIT>""" <EOL> while True : <EOL> line = self . readline ( ) <EOL> if not line : break <EOL> yield line <EOL> def seek ( self , pos , whence = <NUM_LIT:0> ) : <EOL> raise IOError ( "<STR_LIT>" ) <EOL> def tell ( self ) : <EOL> raise IOError ( "<STR_LIT>" ) <EOL> def truncate ( self , n = None ) : <EOL> raise IOError ( "<STR_LIT>" ) <EOL> def isatty ( self ) : <EOL> return False <EOL> class SerialBase ( object ) : <EOL> """<STR_LIT>""" <EOL> BAUDRATES = ( <NUM_LIT:50> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT:200> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <EOL> <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <EOL> <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <EOL> <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ) <EOL> BYTESIZES = ( FIVEBITS , SIXBITS , SEVENBITS , EIGHTBITS ) <EOL> PARITIES = ( PARITY_NONE , PARITY_EVEN , PARITY_ODD , PARITY_MARK , PARITY_SPACE ) <EOL> STOPBITS = ( STOPBITS_ONE , STOPBITS_ONE_POINT_FIVE , STOPBITS_TWO ) <EOL> def __init__ ( self , <EOL> port = None , <EOL> baudrate = <NUM_LIT> , <EOL> bytesize = EIGHTBITS , <EOL> parity = PARITY_NONE , <EOL> stopbits = STOPBITS_ONE , <EOL> timeout = None , <EOL> xonxoff = False , <EOL> rtscts = False , <EOL> writeTimeout = None , <EOL> dsrdtr = False , <EOL> interCharTimeout = None <EOL> ) : <EOL> """<STR_LIT>""" <EOL> self . _isOpen = False <EOL> self . _port = None <EOL> self . _baudrate = None <EOL> self . _bytesize = None <EOL> self . _parity = None <EOL> self . _stopbits = None <EOL> self . _timeout = None <EOL> self . _writeTimeout = None <EOL> self . _xonxoff = None <EOL> self . _rtscts = None <EOL> self . _dsrdtr = None <EOL> self . _interCharTimeout = None <EOL> self . port = port <EOL> self . baudrate = baudrate <EOL> self . bytesize = bytesize <EOL> self . parity = parity <EOL> self . stopbits = stopbits <EOL> self . timeout = timeout <EOL> self . writeTimeout = writeTimeout <EOL> self . xonxoff = xonxoff <EOL> self . rtscts = rtscts <EOL> self . dsrdtr = dsrdtr <EOL> self . interCharTimeout = interCharTimeout <EOL> if port is not None : <EOL> self . open ( ) <EOL> def isOpen ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _isOpen <EOL> def getSupportedBaudrates ( self ) : <EOL> return [ ( str ( b ) , b ) for b in self . BAUDRATES ] <EOL> def getSupportedByteSizes ( self ) : <EOL> return [ ( str ( b ) , b ) for b in self . BYTESIZES ] <EOL> def getSupportedStopbits ( self ) : <EOL> return [ ( str ( b ) , b ) for b in self . STOPBITS ] <EOL> def getSupportedParities ( self ) : <EOL> return [ ( PARITY_NAMES [ b ] , b ) for b in self . PARITIES ] <EOL> def setPort ( self , port ) : <EOL> """<STR_LIT>""" <EOL> was_open = self . _isOpen <EOL> if was_open : self . close ( ) <EOL> if port is not None : <EOL> if isinstance ( port , basestring ) : <EOL> self . portstr = port <EOL> else : <EOL> self . portstr = self . makeDeviceName ( port ) <EOL> else : <EOL> self . portstr = None <EOL> self . _port = port <EOL> self . name = self . portstr <EOL> if was_open : self . open ( ) <EOL> def getPort ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _port <EOL> port = property ( getPort , setPort , doc = "<STR_LIT>" ) <EOL> def setBaudrate ( self , baudrate ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> b = int ( baudrate ) <EOL> except TypeError : <EOL> raise ValueError ( "<STR_LIT>" % ( baudrate , ) ) <EOL> else : <EOL> if b <= <NUM_LIT:0> : <EOL> raise ValueError ( "<STR_LIT>" % ( baudrate , ) ) <EOL> self . _baudrate = b <EOL> if self . _isOpen : self . _reconfigurePort ( ) <EOL> def getBaudrate ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _baudrate <EOL> baudrate = property ( getBaudrate , setBaudrate , doc = "<STR_LIT>" ) <EOL> def setByteSize ( self , bytesize ) : <EOL> """<STR_LIT>""" <EOL> if bytesize not in self . BYTESIZES : raise ValueError ( "<STR_LIT>" % ( bytesize , ) ) <EOL> self . _bytesize = bytesize <EOL> if self . _isOpen : self . _reconfigurePort ( ) <EOL> def getByteSize ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _bytesize <EOL> bytesize = property ( getByteSize , setByteSize , doc = "<STR_LIT>" ) <EOL> def setParity ( self , parity ) : <EOL> """<STR_LIT>""" <EOL> if parity not in self . PARITIES : raise ValueError ( "<STR_LIT>" % ( parity , ) ) <EOL> self . _parity = parity <EOL> if self . _isOpen : self . _reconfigurePort ( ) <EOL> def getParity ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _parity <EOL> parity = property ( getParity , setParity , doc = "<STR_LIT>" ) <EOL> def setStopbits ( self , stopbits ) : <EOL> """<STR_LIT>""" <EOL> if stopbits not in self . STOPBITS : raise ValueError ( "<STR_LIT>" % ( stopbits , ) ) <EOL> self . _stopbits = stopbits <EOL> if self . _isOpen : self . _reconfigurePort ( ) <EOL> def getStopbits ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _stopbits <EOL> stopbits = property ( getStopbits , setStopbits , doc = "<STR_LIT>" ) <EOL> def setTimeout ( self , timeout ) : <EOL> """<STR_LIT>""" <EOL> if timeout is not None : <EOL> try : <EOL> timeout + <NUM_LIT:1> <EOL> except TypeError : <EOL> raise ValueError ( "<STR_LIT>" % ( timeout , ) ) <EOL> if timeout < <NUM_LIT:0> : raise ValueError ( "<STR_LIT>" % ( timeout , ) ) <EOL> self . _timeout = timeout <EOL> if self . _isOpen : self . _reconfigurePort ( ) <EOL> def getTimeout ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _timeout <EOL> timeout = property ( getTimeout , setTimeout , doc = "<STR_LIT>" ) <EOL> def setWriteTimeout ( self , timeout ) : <EOL> """<STR_LIT>""" <EOL> if timeout is not None : <EOL> if timeout < <NUM_LIT:0> : raise ValueError ( "<STR_LIT>" % ( timeout , ) ) <EOL> try : <EOL> timeout + <NUM_LIT:1> <EOL> except TypeError : <EOL> raise ValueError ( "<STR_LIT>" % timeout ) <EOL> self . _writeTimeout = timeout <EOL> if self . _isOpen : self . _reconfigurePort ( ) <EOL> def getWriteTimeout ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _writeTimeout <EOL> writeTimeout = property ( getWriteTimeout , setWriteTimeout , doc = "<STR_LIT>" ) <EOL> def setXonXoff ( self , xonxoff ) : <EOL> """<STR_LIT>""" <EOL> self . _xonxoff = xonxoff <EOL> if self . _isOpen : self . _reconfigurePort ( ) <EOL> def getXonXoff ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _xonxoff <EOL> xonxoff = property ( getXonXoff , setXonXoff , doc = "<STR_LIT>" ) <EOL> def setRtsCts ( self , rtscts ) : <EOL> """<STR_LIT>""" <EOL> self . _rtscts = rtscts <EOL> if self . _isOpen : self . _reconfigurePort ( ) <EOL> def getRtsCts ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _rtscts <EOL> rtscts = property ( getRtsCts , setRtsCts , doc = "<STR_LIT>" ) <EOL> def setDsrDtr ( self , dsrdtr = None ) : <EOL> """<STR_LIT>""" <EOL> if dsrdtr is None : <EOL> self . _dsrdtr = self . _rtscts <EOL> else : <EOL> self . _dsrdtr = dsrdtr <EOL> if self . _isOpen : self . _reconfigurePort ( ) <EOL> def getDsrDtr ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _dsrdtr <EOL> dsrdtr = property ( getDsrDtr , setDsrDtr , "<STR_LIT>" ) <EOL> def setInterCharTimeout ( self , interCharTimeout ) : <EOL> """<STR_LIT>""" <EOL> if interCharTimeout is not None : <EOL> if interCharTimeout < <NUM_LIT:0> : raise ValueError ( "<STR_LIT>" % interCharTimeout ) <EOL> try : <EOL> interCharTimeout + <NUM_LIT:1> <EOL> except TypeError : <EOL> raise ValueError ( "<STR_LIT>" % interCharTimeout ) <EOL> self . _interCharTimeout = interCharTimeout <EOL> if self . _isOpen : self . _reconfigurePort ( ) <EOL> def getInterCharTimeout ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _interCharTimeout <EOL> interCharTimeout = property ( getInterCharTimeout , setInterCharTimeout , doc = "<STR_LIT>" ) <EOL> _SETTINGS = ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> def getSettingsDict ( self ) : <EOL> """<STR_LIT>""" <EOL> return dict ( [ ( key , getattr ( self , '<STR_LIT:_>' + key ) ) for key in self . _SETTINGS ] ) <EOL> def applySettingsDict ( self , d ) : <EOL> """<STR_LIT>""" <EOL> for key in self . _SETTINGS : <EOL> if d [ key ] != getattr ( self , '<STR_LIT:_>' + key ) : <EOL> setattr ( self , key , d [ key ] ) <EOL> def __repr__ ( self ) : <EOL> """<STR_LIT>""" <EOL> return "<STR_LIT>" % ( <EOL> self . __class__ . __name__ , <EOL> id ( self ) , <EOL> self . _isOpen , <EOL> self . portstr , <EOL> self . baudrate , <EOL> self . bytesize , <EOL> self . parity , <EOL> self . stopbits , <EOL> self . timeout , <EOL> self . xonxoff , <EOL> self . rtscts , <EOL> self . dsrdtr , <EOL> ) <EOL> def readable ( self ) : return True <EOL> def writable ( self ) : return True <EOL> def seekable ( self ) : return False <EOL> def readinto ( self , b ) : <EOL> data = self . read ( len ( b ) ) <EOL> n = len ( data ) <EOL> try : <EOL> b [ : n ] = data <EOL> except TypeError , err : <EOL> import array <EOL> if not isinstance ( b , array . array ) : <EOL> raise err <EOL> b [ : n ] = array . array ( '<STR_LIT:b>' , data ) <EOL> return n <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> import sys <EOL> s = SerialBase ( ) <EOL> sys . stdout . write ( '<STR_LIT>' % s . portstr ) <EOL> sys . stdout . write ( '<STR_LIT>' % s . getSupportedBaudrates ( ) ) <EOL> sys . stdout . write ( '<STR_LIT>' % s . getSupportedByteSizes ( ) ) <EOL> sys . stdout . write ( '<STR_LIT>' % s . getSupportedParities ( ) ) <EOL> sys . stdout . write ( '<STR_LIT>' % s . getSupportedStopbits ( ) ) <EOL> sys . stdout . write ( '<STR_LIT>' % s ) </s>
