<s> from time import gmtime , strftime <EOL> from data_types import * <EOL> from packet_structs import * <EOL> import packet_extensions <EOL> class Packet : <EOL> def __init__ ( self , ** kargs ) : <EOL> self . ident = kargs . get ( '<STR_LIT>' , <NUM_LIT:0> ) <EOL> self . direction = kargs . get ( '<STR_LIT>' , CLIENT_TO_SERVER ) <EOL> self . data = kargs . get ( '<STR_LIT:data>' , { } ) <EOL> def clone ( self ) : <EOL> return Packet ( ident = self . ident , direction = self . direction , data = dict ( self . data ) ) <EOL> def decode ( self , bbuff ) : <EOL> self . ident = unpack ( bbuff , '<STR_LIT>' ) <EOL> for data_type , name in structs [ self . ident ] [ self . direction ] : <EOL> self . data [ name ] = unpack ( bbuff , data_type ) <EOL> if self . ident in packet_extensions . extensions : <EOL> packet_extensions . extensions [ self . ident ] . decode_extra ( self , bbuff ) <EOL> def encode ( self ) : <EOL> output = pack ( '<STR_LIT>' , self . ident ) <EOL> if self . ident in packet_extensions . extensions : <EOL> append = packet_extensions . extensions [ self . ident ] . encode_extra ( self ) <EOL> else : <EOL> append = '<STR_LIT>' <EOL> for data_type , name in structs [ self . ident ] [ self . direction ] : <EOL> output += pack ( data_type , self . data [ name ] ) <EOL> return output + append <EOL> def __repr__ ( self ) : <EOL> if self . direction == TO_SERVER : s = "<STR_LIT>" <EOL> else : s = "<STR_LIT>" <EOL> format = "<STR_LIT>" + str ( max ( [ len ( i ) for i in names . values ( ) ] ) + <NUM_LIT:1> ) + "<STR_LIT>" <EOL> return format % ( strftime ( "<STR_LIT>" , gmtime ( ) ) , s , self . ident , names [ self . ident ] , str ( self . data ) ) <EOL> def read_packet ( bbuff , direction ) : <EOL> p = Packet ( direction = direction ) <EOL> p . decode ( bbuff ) <EOL> return p </s>
