<s> """<STR_LIT>""" <EOL> import logging <EOL> import struct <EOL> from pika import amqp_object <EOL> from pika import exceptions <EOL> from pika import spec <EOL> from pika . compat import byte <EOL> LOGGER = logging . getLogger ( __name__ ) <EOL> class Frame ( amqp_object . AMQPObject ) : <EOL> """<STR_LIT>""" <EOL> NAME = '<STR_LIT>' <EOL> def __init__ ( self , frame_type , channel_number ) : <EOL> """<STR_LIT>""" <EOL> self . frame_type = frame_type <EOL> self . channel_number = channel_number <EOL> def _marshal ( self , pieces ) : <EOL> """<STR_LIT>""" <EOL> payload = b'<STR_LIT>' . join ( pieces ) <EOL> return struct . pack ( '<STR_LIT>' , self . frame_type , self . channel_number , <EOL> len ( payload ) ) + payload + byte ( spec . FRAME_END ) <EOL> def marshal ( self ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError <EOL> class Method ( Frame ) : <EOL> """<STR_LIT>""" <EOL> NAME = '<STR_LIT>' <EOL> def __init__ ( self , channel_number , method ) : <EOL> """<STR_LIT>""" <EOL> Frame . __init__ ( self , spec . FRAME_METHOD , channel_number ) <EOL> self . method = method <EOL> def marshal ( self ) : <EOL> """<STR_LIT>""" <EOL> pieces = self . method . encode ( ) <EOL> pieces . insert ( <NUM_LIT:0> , struct . pack ( '<STR_LIT>' , self . method . INDEX ) ) <EOL> return self . _marshal ( pieces ) <EOL> class Header ( Frame ) : <EOL> """<STR_LIT>""" <EOL> NAME = '<STR_LIT>' <EOL> def __init__ ( self , channel_number , body_size , props ) : <EOL> """<STR_LIT>""" <EOL> Frame . __init__ ( self , spec . FRAME_HEADER , channel_number ) <EOL> self . body_size = body_size <EOL> self . properties = props <EOL> def marshal ( self ) : <EOL> """<STR_LIT>""" <EOL> pieces = self . properties . encode ( ) <EOL> pieces . insert ( <NUM_LIT:0> , struct . pack ( '<STR_LIT>' , self . properties . INDEX , <EOL> self . body_size ) ) <EOL> return self . _marshal ( pieces ) <EOL> class Body ( Frame ) : <EOL> """<STR_LIT>""" <EOL> NAME = '<STR_LIT>' <EOL> def __init__ ( self , channel_number , fragment ) : <EOL> """<STR_LIT>""" <EOL> Frame . __init__ ( self , spec . FRAME_BODY , channel_number ) <EOL> self . fragment = fragment <EOL> def marshal ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _marshal ( [ self . fragment ] ) <EOL> class Heartbeat ( Frame ) : <EOL> """<STR_LIT>""" <EOL> NAME = '<STR_LIT>' <EOL> def __init__ ( self ) : <EOL> """<STR_LIT>""" <EOL> Frame . __init__ ( self , spec . FRAME_HEARTBEAT , <NUM_LIT:0> ) <EOL> def marshal ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _marshal ( list ( ) ) <EOL> class ProtocolHeader ( amqp_object . AMQPObject ) : <EOL> """<STR_LIT>""" <EOL> NAME = '<STR_LIT>' <EOL> def __init__ ( self , major = None , minor = None , revision = None ) : <EOL> """<STR_LIT>""" <EOL> self . frame_type = - <NUM_LIT:1> <EOL> self . major = major or spec . PROTOCOL_VERSION [ <NUM_LIT:0> ] <EOL> self . minor = minor or spec . PROTOCOL_VERSION [ <NUM_LIT:1> ] <EOL> self . revision = revision or spec . PROTOCOL_VERSION [ <NUM_LIT:2> ] <EOL> def marshal ( self ) : <EOL> """<STR_LIT>""" <EOL> return b'<STR_LIT>' + struct . pack ( '<STR_LIT>' , <NUM_LIT:0> , self . major , self . minor , <EOL> self . revision ) <EOL> def decode_frame ( data_in ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> if data_in [ <NUM_LIT:0> : <NUM_LIT:4> ] == b'<STR_LIT>' : <EOL> major , minor , revision = struct . unpack_from ( '<STR_LIT>' , data_in , <NUM_LIT:5> ) <EOL> return <NUM_LIT:8> , ProtocolHeader ( major , minor , revision ) <EOL> except ( IndexError , struct . error ) : <EOL> return <NUM_LIT:0> , None <EOL> try : <EOL> ( frame_type , channel_number , <EOL> frame_size ) = struct . unpack ( '<STR_LIT>' , data_in [ <NUM_LIT:0> : <NUM_LIT:7> ] ) <EOL> except struct . error : <EOL> return <NUM_LIT:0> , None <EOL> frame_end = spec . FRAME_HEADER_SIZE + frame_size + spec . FRAME_END_SIZE <EOL> if frame_end > len ( data_in ) : <EOL> return <NUM_LIT:0> , None <EOL> if data_in [ frame_end - <NUM_LIT:1> : frame_end ] != byte ( spec . FRAME_END ) : <EOL> raise exceptions . InvalidFrameError ( "<STR_LIT>" ) <EOL> frame_data = data_in [ spec . FRAME_HEADER_SIZE : frame_end - <NUM_LIT:1> ] <EOL> if frame_type == spec . FRAME_METHOD : <EOL> method_id = struct . unpack_from ( '<STR_LIT>' , frame_data ) [ <NUM_LIT:0> ] <EOL> method = spec . methods [ method_id ] ( ) <EOL> method . decode ( frame_data , <NUM_LIT:4> ) <EOL> return frame_end , Method ( channel_number , method ) <EOL> elif frame_type == spec . FRAME_HEADER : <EOL> class_id , weight , body_size = struct . unpack_from ( '<STR_LIT>' , frame_data ) <EOL> properties = spec . props [ class_id ] ( ) <EOL> out = properties . decode ( frame_data [ <NUM_LIT:12> : ] ) <EOL> return frame_end , Header ( channel_number , body_size , properties ) <EOL> elif frame_type == spec . FRAME_BODY : <EOL> return frame_end , Body ( channel_number , frame_data ) <EOL> elif frame_type == spec . FRAME_HEARTBEAT : <EOL> return frame_end , Heartbeat ( ) <EOL> raise exceptions . InvalidFrameError ( "<STR_LIT>" % frame_type ) </s>
