<s> import base64 <EOL> import json as _json <EOL> import six <EOL> ( OPEN , CLOSE , PING , PONG , MESSAGE , UPGRADE , NOOP ) = ( <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> , <NUM_LIT:4> , <NUM_LIT:5> , <NUM_LIT:6> ) <EOL> packet_names = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> class Packet ( object ) : <EOL> """<STR_LIT>""" <EOL> json = _json <EOL> def __init__ ( self , packet_type = NOOP , data = None , binary = None , <EOL> encoded_packet = None ) : <EOL> self . packet_type = packet_type <EOL> self . data = data <EOL> if binary is not None : <EOL> self . binary = binary <EOL> elif isinstance ( data , six . text_type ) : <EOL> self . binary = False <EOL> elif isinstance ( data , six . binary_type ) : <EOL> self . binary = True <EOL> else : <EOL> self . binary = False <EOL> if encoded_packet : <EOL> self . decode ( encoded_packet ) <EOL> def encode ( self , b64 = False , always_bytes = True ) : <EOL> """<STR_LIT>""" <EOL> if self . binary and not b64 : <EOL> encoded_packet = six . int2byte ( self . packet_type ) <EOL> else : <EOL> encoded_packet = six . text_type ( self . packet_type ) <EOL> if self . binary and b64 : <EOL> encoded_packet = '<STR_LIT:b>' + encoded_packet <EOL> if self . binary : <EOL> if b64 : <EOL> encoded_packet += base64 . b64encode ( self . data ) . decode ( '<STR_LIT:utf-8>' ) <EOL> else : <EOL> encoded_packet += self . data <EOL> elif isinstance ( self . data , six . string_types ) : <EOL> encoded_packet += self . data <EOL> elif isinstance ( self . data , dict ) or isinstance ( self . data , list ) : <EOL> encoded_packet += self . json . dumps ( self . data , <EOL> separators = ( '<STR_LIT:U+002C>' , '<STR_LIT::>' ) ) <EOL> elif self . data is not None : <EOL> encoded_packet += str ( self . data ) <EOL> if always_bytes and not isinstance ( encoded_packet , six . binary_type ) : <EOL> encoded_packet = encoded_packet . encode ( '<STR_LIT:utf-8>' ) <EOL> return encoded_packet <EOL> def decode ( self , encoded_packet ) : <EOL> """<STR_LIT>""" <EOL> b64 = False <EOL> if not isinstance ( encoded_packet , six . binary_type ) : <EOL> encoded_packet = encoded_packet . encode ( '<STR_LIT:utf-8>' ) <EOL> self . packet_type = six . byte2int ( encoded_packet [ <NUM_LIT:0> : <NUM_LIT:1> ] ) <EOL> if self . packet_type == <NUM_LIT> : <EOL> self . binary = True <EOL> encoded_packet = encoded_packet [ <NUM_LIT:1> : ] <EOL> self . packet_type = six . byte2int ( encoded_packet [ <NUM_LIT:0> : <NUM_LIT:1> ] ) <EOL> self . packet_type -= <NUM_LIT> <EOL> b64 = True <EOL> elif self . packet_type >= <NUM_LIT> : <EOL> self . packet_type -= <NUM_LIT> <EOL> self . binary = False <EOL> else : <EOL> self . binary = True <EOL> self . data = None <EOL> if len ( encoded_packet ) > <NUM_LIT:1> : <EOL> if self . binary : <EOL> if b64 : <EOL> self . data = base64 . b64decode ( encoded_packet [ <NUM_LIT:1> : ] ) <EOL> else : <EOL> self . data = encoded_packet [ <NUM_LIT:1> : ] <EOL> else : <EOL> try : <EOL> self . data = self . json . loads ( <EOL> encoded_packet [ <NUM_LIT:1> : ] . decode ( '<STR_LIT:utf-8>' ) ) <EOL> except ValueError : <EOL> self . data = encoded_packet [ <NUM_LIT:1> : ] . decode ( '<STR_LIT:utf-8>' ) </s>
