<s> """<STR_LIT>""" <EOL> import base64 <EOL> import StringIO <EOL> from boto . sqs . attributes import Attributes <EOL> from boto . exception import SQSDecodeError <EOL> import boto <EOL> class RawMessage : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , queue = None , body = '<STR_LIT>' ) : <EOL> self . queue = queue <EOL> self . set_body ( body ) <EOL> self . id = None <EOL> self . receipt_handle = None <EOL> self . md5 = None <EOL> self . attributes = Attributes ( self ) <EOL> def __len__ ( self ) : <EOL> return len ( self . encode ( self . _body ) ) <EOL> def startElement ( self , name , attrs , connection ) : <EOL> if name == '<STR_LIT>' : <EOL> return self . attributes <EOL> return None <EOL> def endElement ( self , name , value , connection ) : <EOL> if name == '<STR_LIT>' : <EOL> self . set_body ( self . decode ( value ) ) <EOL> elif name == '<STR_LIT>' : <EOL> self . id = value <EOL> elif name == '<STR_LIT>' : <EOL> self . receipt_handle = value <EOL> elif name == '<STR_LIT>' : <EOL> self . md5 = value <EOL> else : <EOL> setattr ( self , name , value ) <EOL> def encode ( self , value ) : <EOL> """<STR_LIT>""" <EOL> return value <EOL> def decode ( self , value ) : <EOL> """<STR_LIT>""" <EOL> return value <EOL> def set_body ( self , body ) : <EOL> """<STR_LIT>""" <EOL> self . _body = body <EOL> def get_body ( self ) : <EOL> return self . _body <EOL> def get_body_encoded ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . encode ( self . get_body ( ) ) <EOL> def delete ( self ) : <EOL> if self . queue : <EOL> return self . queue . delete_message ( self ) <EOL> def change_visibility ( self , visibility_timeout ) : <EOL> if self . queue : <EOL> self . queue . connection . change_message_visibility ( self . queue , <EOL> self . receipt_handle , <EOL> visibility_timeout ) <EOL> class Message ( RawMessage ) : <EOL> """<STR_LIT>""" <EOL> def encode ( self , value ) : <EOL> return base64 . b64encode ( value ) <EOL> def decode ( self , value ) : <EOL> try : <EOL> value = base64 . b64decode ( value ) <EOL> except : <EOL> boto . log . warning ( '<STR_LIT>' ) <EOL> return value <EOL> return value <EOL> class MHMessage ( Message ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , queue = None , body = None , xml_attrs = None ) : <EOL> if body == None or body == '<STR_LIT>' : <EOL> body = { } <EOL> Message . __init__ ( self , queue , body ) <EOL> def decode ( self , value ) : <EOL> try : <EOL> msg = { } <EOL> fp = StringIO . StringIO ( value ) <EOL> line = fp . readline ( ) <EOL> while line : <EOL> delim = line . find ( '<STR_LIT::>' ) <EOL> key = line [ <NUM_LIT:0> : delim ] <EOL> value = line [ delim + <NUM_LIT:1> : ] . strip ( ) <EOL> msg [ key . strip ( ) ] = value . strip ( ) <EOL> line = fp . readline ( ) <EOL> except : <EOL> raise SQSDecodeError ( '<STR_LIT>' , self ) <EOL> return msg <EOL> def encode ( self , value ) : <EOL> s = '<STR_LIT>' <EOL> for item in value . items ( ) : <EOL> s = s + '<STR_LIT>' % ( item [ <NUM_LIT:0> ] , item [ <NUM_LIT:1> ] ) <EOL> return s <EOL> def __getitem__ ( self , key ) : <EOL> if self . _body . has_key ( key ) : <EOL> return self . _body [ key ] <EOL> else : <EOL> raise KeyError ( key ) <EOL> def __setitem__ ( self , key , value ) : <EOL> self . _body [ key ] = value <EOL> self . set_body ( self . _body ) <EOL> def keys ( self ) : <EOL> return self . _body . keys ( ) <EOL> def values ( self ) : <EOL> return self . _body . values ( ) <EOL> def items ( self ) : <EOL> return self . _body . items ( ) <EOL> def has_key ( self , key ) : <EOL> return self . _body . has_key ( key ) <EOL> def update ( self , d ) : <EOL> self . _body . update ( d ) <EOL> self . set_body ( self . _body ) <EOL> def get ( self , key , default = None ) : <EOL> return self . _body . get ( key , default ) <EOL> class EncodedMHMessage ( MHMessage ) : <EOL> """<STR_LIT>""" <EOL> def decode ( self , value ) : <EOL> try : <EOL> value = base64 . b64decode ( value ) <EOL> except : <EOL> raise SQSDecodeError ( '<STR_LIT>' , self ) <EOL> return MHMessage . decode ( self , value ) <EOL> def encode ( self , value ) : <EOL> value = MHMessage . encode ( self , value ) <EOL> return base64 . b64encode ( value ) </s>
