<s> from __future__ import absolute_import <EOL> import struct <EOL> from . . thrift import TType <EOL> from . exc import TProtocolException <EOL> VERSION_MASK = - <NUM_LIT> <EOL> VERSION_1 = - <NUM_LIT> <EOL> TYPE_MASK = <NUM_LIT> <EOL> def pack_i8 ( byte ) : <EOL> return struct . pack ( "<STR_LIT>" , byte ) <EOL> def pack_i16 ( i16 ) : <EOL> return struct . pack ( "<STR_LIT>" , i16 ) <EOL> def pack_i32 ( i32 ) : <EOL> return struct . pack ( "<STR_LIT>" , i32 ) <EOL> def pack_i64 ( i64 ) : <EOL> return struct . pack ( "<STR_LIT>" , i64 ) <EOL> def pack_double ( dub ) : <EOL> return struct . pack ( "<STR_LIT>" , dub ) <EOL> def pack_string ( string ) : <EOL> return struct . pack ( "<STR_LIT>" % len ( string ) , len ( string ) , string ) <EOL> def unpack_i8 ( buf ) : <EOL> return struct . unpack ( "<STR_LIT>" , buf ) [ <NUM_LIT:0> ] <EOL> def unpack_i16 ( buf ) : <EOL> return struct . unpack ( "<STR_LIT>" , buf ) [ <NUM_LIT:0> ] <EOL> def unpack_i32 ( buf ) : <EOL> return struct . unpack ( "<STR_LIT>" , buf ) [ <NUM_LIT:0> ] <EOL> def unpack_i64 ( buf ) : <EOL> return struct . unpack ( "<STR_LIT>" , buf ) [ <NUM_LIT:0> ] <EOL> def unpack_double ( buf ) : <EOL> return struct . unpack ( "<STR_LIT>" , buf ) [ <NUM_LIT:0> ] <EOL> def write_message_begin ( outbuf , name , ttype , seqid , strict = True ) : <EOL> if strict : <EOL> outbuf . write ( pack_i32 ( VERSION_1 | ttype ) ) <EOL> outbuf . write ( pack_string ( name . encode ( '<STR_LIT:utf-8>' ) ) ) <EOL> else : <EOL> outbuf . write ( pack_string ( name . encode ( '<STR_LIT:utf-8>' ) ) ) <EOL> outbuf . write ( pack_i8 ( ttype ) ) <EOL> outbuf . write ( pack_i32 ( seqid ) ) <EOL> def write_field_begin ( outbuf , ttype , fid ) : <EOL> outbuf . write ( pack_i8 ( ttype ) + pack_i16 ( fid ) ) <EOL> def write_field_stop ( outbuf ) : <EOL> outbuf . write ( pack_i8 ( TType . STOP ) ) <EOL> def write_list_begin ( outbuf , etype , size ) : <EOL> outbuf . write ( pack_i8 ( etype ) + pack_i32 ( size ) ) <EOL> def write_map_begin ( outbuf , ktype , vtype , size ) : <EOL> outbuf . write ( pack_i8 ( ktype ) + pack_i8 ( vtype ) + pack_i32 ( size ) ) <EOL> def write_val ( outbuf , ttype , val , spec = None ) : <EOL> if ttype == TType . BOOL : <EOL> if val : <EOL> outbuf . write ( pack_i8 ( <NUM_LIT:1> ) ) <EOL> else : <EOL> outbuf . write ( pack_i8 ( <NUM_LIT:0> ) ) <EOL> elif ttype == TType . BYTE : <EOL> outbuf . write ( pack_i8 ( val ) ) <EOL> elif ttype == TType . I16 : <EOL> outbuf . write ( pack_i16 ( val ) ) <EOL> elif ttype == TType . I32 : <EOL> outbuf . write ( pack_i32 ( val ) ) <EOL> elif ttype == TType . I64 : <EOL> outbuf . write ( pack_i64 ( val ) ) <EOL> elif ttype == TType . DOUBLE : <EOL> outbuf . write ( pack_double ( val ) ) <EOL> elif ttype == TType . STRING : <EOL> if not isinstance ( val , bytes ) : <EOL> val = val . encode ( '<STR_LIT:utf-8>' ) <EOL> outbuf . write ( pack_string ( val ) ) <EOL> elif ttype == TType . SET or ttype == TType . LIST : <EOL> if isinstance ( spec , tuple ) : <EOL> e_type , t_spec = spec [ <NUM_LIT:0> ] , spec [ <NUM_LIT:1> ] <EOL> else : <EOL> e_type , t_spec = spec , None <EOL> val_len = len ( val ) <EOL> write_list_begin ( outbuf , e_type , val_len ) <EOL> for e_val in val : <EOL> write_val ( outbuf , e_type , e_val , t_spec ) <EOL> elif ttype == TType . MAP : <EOL> if isinstance ( spec [ <NUM_LIT:0> ] , int ) : <EOL> k_type = spec [ <NUM_LIT:0> ] <EOL> k_spec = None <EOL> else : <EOL> k_type , k_spec = spec [ <NUM_LIT:0> ] <EOL> if isinstance ( spec [ <NUM_LIT:1> ] , int ) : <EOL> v_type = spec [ <NUM_LIT:1> ] <EOL> v_spec = None <EOL> else : <EOL> v_type , v_spec = spec [ <NUM_LIT:1> ] <EOL> write_map_begin ( outbuf , k_type , v_type , len ( val ) ) <EOL> for k in iter ( val ) : <EOL> write_val ( outbuf , k_type , k , k_spec ) <EOL> write_val ( outbuf , v_type , val [ k ] , v_spec ) <EOL> elif ttype == TType . STRUCT : <EOL> for fid in iter ( val . thrift_spec ) : <EOL> f_spec = val . thrift_spec [ fid ] <EOL> if len ( f_spec ) == <NUM_LIT:3> : <EOL> f_type , f_name , f_req = f_spec <EOL> f_container_spec = None <EOL> else : <EOL> f_type , f_name , f_container_spec , f_req = f_spec <EOL> v = getattr ( val , f_name ) <EOL> if v is None : <EOL> continue <EOL> write_field_begin ( outbuf , f_type , fid ) <EOL> write_val ( outbuf , f_type , v , f_container_spec ) <EOL> write_field_stop ( outbuf ) <EOL> def read_message_begin ( inbuf , strict = True ) : <EOL> sz = unpack_i32 ( inbuf . read ( <NUM_LIT:4> ) ) <EOL> if sz < <NUM_LIT:0> : <EOL> version = sz & VERSION_MASK <EOL> if version != VERSION_1 : <EOL> raise TProtocolException ( <EOL> type = TProtocolException . BAD_VERSION , <EOL> message = '<STR_LIT>' % ( sz ) ) <EOL> name_sz = unpack_i32 ( inbuf . read ( <NUM_LIT:4> ) ) <EOL> name = inbuf . read ( name_sz ) . decode ( '<STR_LIT:utf-8>' ) <EOL> type_ = sz & TYPE_MASK <EOL> else : <EOL> if strict : <EOL> raise TProtocolException ( type = TProtocolException . BAD_VERSION , <EOL> message = '<STR_LIT>' ) <EOL> name = inbuf . read ( sz ) . decode ( '<STR_LIT:utf-8>' ) <EOL> type_ = unpack_i8 ( inbuf . read ( <NUM_LIT:1> ) ) <EOL> seqid = unpack_i32 ( inbuf . read ( <NUM_LIT:4> ) ) <EOL> return name , type_ , seqid <EOL> def read_field_begin ( inbuf ) : <EOL> f_type = unpack_i8 ( inbuf . read ( <NUM_LIT:1> ) ) <EOL> if f_type == TType . STOP : <EOL> return f_type , <NUM_LIT:0> <EOL> return f_type , unpack_i16 ( inbuf . read ( <NUM_LIT:2> ) ) <EOL> def read_list_begin ( inbuf ) : <EOL> e_type = unpack_i8 ( inbuf . read ( <NUM_LIT:1> ) ) <EOL> sz = unpack_i32 ( inbuf . read ( <NUM_LIT:4> ) ) <EOL> return e_type , sz <EOL> def read_map_begin ( inbuf ) : <EOL> k_type , v_type = unpack_i8 ( inbuf . read ( <NUM_LIT:1> ) ) , unpack_i8 ( inbuf . read ( <NUM_LIT:1> ) ) <EOL> sz = unpack_i32 ( inbuf . read ( <NUM_LIT:4> ) ) <EOL> return k_type , v_type , sz <EOL> def read_val ( inbuf , ttype , spec = None , decode_response = True ) : <EOL> if ttype == TType . BOOL : <EOL> return bool ( unpack_i8 ( inbuf . read ( <NUM_LIT:1> ) ) ) <EOL> elif ttype == TType . BYTE : <EOL> return unpack_i8 ( inbuf . read ( <NUM_LIT:1> ) ) <EOL> elif ttype == TType . I16 : <EOL> return unpack_i16 ( inbuf . read ( <NUM_LIT:2> ) ) <EOL> elif ttype == TType . I32 : <EOL> return unpack_i32 ( inbuf . read ( <NUM_LIT:4> ) ) <EOL> elif ttype == TType . I64 : <EOL> return unpack_i64 ( inbuf . read ( <NUM_LIT:8> ) ) <EOL> elif ttype == TType . DOUBLE : <EOL> return unpack_double ( inbuf . read ( <NUM_LIT:8> ) ) <EOL> elif ttype == TType . STRING : <EOL> sz = unpack_i32 ( inbuf . read ( <NUM_LIT:4> ) ) <EOL> byte_payload = inbuf . read ( sz ) <EOL> if decode_response : <EOL> try : <EOL> return byte_payload . decode ( '<STR_LIT:utf-8>' ) <EOL> except UnicodeDecodeError : <EOL> pass <EOL> return byte_payload <EOL> elif ttype == TType . SET or ttype == TType . LIST : <EOL> if isinstance ( spec , tuple ) : <EOL> v_type , v_spec = spec [ <NUM_LIT:0> ] , spec [ <NUM_LIT:1> ] <EOL> else : <EOL> v_type , v_spec = spec , None <EOL> result = [ ] <EOL> r_type , sz = read_list_begin ( inbuf ) <EOL> if r_type != v_type : <EOL> for _ in range ( sz ) : <EOL> skip ( inbuf , r_type ) <EOL> return [ ] <EOL> for i in range ( sz ) : <EOL> result . append ( read_val ( inbuf , v_type , v_spec , decode_response ) ) <EOL> return result <EOL> elif ttype == TType . MAP : <EOL> if isinstance ( spec [ <NUM_LIT:0> ] , int ) : <EOL> k_type = spec [ <NUM_LIT:0> ] <EOL> k_spec = None <EOL> else : <EOL> k_type , k_spec = spec [ <NUM_LIT:0> ] <EOL> if isinstance ( spec [ <NUM_LIT:1> ] , int ) : <EOL> v_type = spec [ <NUM_LIT:1> ] <EOL> v_spec = None <EOL> else : <EOL> v_type , v_spec = spec [ <NUM_LIT:1> ] <EOL> result = { } <EOL> sk_type , sv_type , sz = read_map_begin ( inbuf ) <EOL> if sk_type != k_type or sv_type != v_type : <EOL> for _ in range ( sz ) : <EOL> skip ( inbuf , sk_type ) <EOL> skip ( inbuf , sv_type ) <EOL> return { } <EOL> for i in range ( sz ) : <EOL> k_val = read_val ( inbuf , k_type , k_spec , decode_response ) <EOL> v_val = read_val ( inbuf , v_type , v_spec , decode_response ) <EOL> result [ k_val ] = v_val <EOL> return result <EOL> elif ttype == TType . STRUCT : <EOL> obj = spec ( ) <EOL> read_struct ( inbuf , obj , decode_response ) <EOL> return obj <EOL> def read_struct ( inbuf , obj , decode_response = True ) : <EOL> while True : <EOL> f_type , fid = read_field_begin ( inbuf ) <EOL> if f_type == TType . STOP : <EOL> break <EOL> if fid not in obj . thrift_spec : <EOL> skip ( inbuf , f_type ) <EOL> continue <EOL> if len ( obj . thrift_spec [ fid ] ) == <NUM_LIT:3> : <EOL> sf_type , f_name , f_req = obj . thrift_spec [ fid ] <EOL> f_container_spec = None <EOL> else : <EOL> sf_type , f_name , f_container_spec , f_req = obj . thrift_spec [ fid ] <EOL> if f_type != sf_type : <EOL> skip ( inbuf , f_type ) <EOL> continue <EOL> setattr ( obj , f_name , <EOL> read_val ( inbuf , f_type , f_container_spec , decode_response ) ) <EOL> def skip ( inbuf , ftype ) : <EOL> if ftype == TType . BOOL or ftype == TType . BYTE : <EOL> inbuf . read ( <NUM_LIT:1> ) <EOL> elif ftype == TType . I16 : <EOL> inbuf . read ( <NUM_LIT:2> ) <EOL> elif ftype == TType . I32 : <EOL> inbuf . read ( <NUM_LIT:4> ) <EOL> elif ftype == TType . I64 : <EOL> inbuf . read ( <NUM_LIT:8> ) <EOL> elif ftype == TType . DOUBLE : <EOL> inbuf . read ( <NUM_LIT:8> ) <EOL> elif ftype == TType . STRING : <EOL> inbuf . read ( unpack_i32 ( inbuf . read ( <NUM_LIT:4> ) ) ) <EOL> elif ftype == TType . SET or ftype == TType . LIST : <EOL> v_type , sz = read_list_begin ( inbuf ) <EOL> for i in range ( sz ) : <EOL> skip ( inbuf , v_type ) <EOL> elif ftype == TType . MAP : <EOL> k_type , v_type , sz = read_map_begin ( inbuf ) <EOL> for i in range ( sz ) : <EOL> skip ( inbuf , k_type ) <EOL> skip ( inbuf , v_type ) <EOL> elif ftype == TType . STRUCT : <EOL> while True : <EOL> f_type , fid = read_field_begin ( inbuf ) <EOL> if f_type == TType . STOP : <EOL> break <EOL> skip ( inbuf , f_type ) <EOL> class TBinaryProtocol ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , trans , <EOL> strict_read = True , strict_write = True , <EOL> decode_response = True ) : <EOL> self . trans = trans <EOL> self . strict_read = strict_read <EOL> self . strict_write = strict_write <EOL> self . decode_response = decode_response <EOL> def skip ( self , ttype ) : <EOL> skip ( self . trans , ttype ) <EOL> def read_message_begin ( self ) : <EOL> api , ttype , seqid = read_message_begin ( <EOL> self . trans , strict = self . strict_read ) <EOL> return api , ttype , seqid <EOL> def read_message_end ( self ) : <EOL> pass <EOL> def write_message_begin ( self , name , ttype , seqid ) : <EOL> write_message_begin ( self . trans , name , ttype , seqid , <EOL> strict = self . strict_write ) <EOL> def write_message_end ( self ) : <EOL> pass <EOL> def read_struct ( self , obj ) : <EOL> return read_struct ( self . trans , obj , self . decode_response ) <EOL> def write_struct ( self , obj ) : <EOL> write_val ( self . trans , TType . STRUCT , obj ) <EOL> class TBinaryProtocolFactory ( object ) : <EOL> def __init__ ( self , strict_read = True , strict_write = True , <EOL> decode_response = True ) : <EOL> self . strict_read = strict_read <EOL> self . strict_write = strict_write <EOL> self . decode_response = decode_response <EOL> def get_protocol ( self , trans ) : <EOL> return TBinaryProtocol ( trans , <EOL> self . strict_read , self . strict_write , <EOL> self . decode_response ) </s>
