<s> from __future__ import absolute_import <EOL> import struct <EOL> from io import BytesIO <EOL> from thriftpy . _compat import CYTHON <EOL> from . . import TTransportBase , readall <EOL> from . . buffered import TBufferedTransport <EOL> class TFramedTransport ( TTransportBase ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , trans ) : <EOL> self . _trans = trans <EOL> self . _rbuf = BytesIO ( ) <EOL> self . _wbuf = BytesIO ( ) <EOL> def is_open ( self ) : <EOL> return self . _trans . is_open ( ) <EOL> def open ( self ) : <EOL> return self . _trans . open ( ) <EOL> def close ( self ) : <EOL> return self . _trans . close ( ) <EOL> def read ( self , sz ) : <EOL> if sz == <NUM_LIT:0> : <EOL> return b'<STR_LIT>' <EOL> ret = self . _rbuf . read ( sz ) <EOL> if len ( ret ) != <NUM_LIT:0> : <EOL> return ret <EOL> self . read_frame ( ) <EOL> return self . _rbuf . read ( sz ) <EOL> def read_frame ( self ) : <EOL> buff = readall ( self . _trans . read , <NUM_LIT:4> ) <EOL> sz , = struct . unpack ( '<STR_LIT>' , buff ) <EOL> frame = readall ( self . _trans . read , sz ) <EOL> self . _rbuf = BytesIO ( frame ) <EOL> def write ( self , buf ) : <EOL> self . _wbuf . write ( buf ) <EOL> def flush ( self ) : <EOL> out = self . _wbuf . getvalue ( ) <EOL> self . _wbuf = BytesIO ( ) <EOL> self . _trans . write ( struct . pack ( "<STR_LIT>" , len ( out ) ) + out ) <EOL> self . _trans . flush ( ) <EOL> def getvalue ( self ) : <EOL> return self . _trans . getvalue ( ) <EOL> class TFramedTransportFactory ( object ) : <EOL> def get_transport ( self , trans ) : <EOL> return TBufferedTransport ( TFramedTransport ( trans ) ) <EOL> if CYTHON : <EOL> from . cyframed import TCyFramedTransport , TCyFramedTransportFactory </s>
