o ckF[&@sdZddlZddlZddlmZddlmZddlmZddlm Z ddl m Z Gdd d e Z Gd d d e ZGd d d eZGdddeZdS)aX SQS Message A Message represents the data stored in an SQS queue. The rules for what is allowed within an SQS Message are here: http://docs.amazonwebservices.com/AWSSimpleQueueService/2008-01-01/SQSDeveloperGuide/Query_QuerySendMessage.html So, at it's simplest level a Message just needs to allow a developer to store bytes in it and get the bytes back out. However, to allow messages to have richer semantics, the Message class must support the following interfaces: The constructor for the Message class must accept a keyword parameter "queue" which is an instance of a boto Queue object and represents the queue that the message will be stored in. The default value for this parameter is None. The constructor for the Message class must accept a keyword parameter "body" which represents the content or body of the message. The format of this parameter will depend on the behavior of the particular Message subclass. For example, if the Message subclass provides dictionary-like behavior to the user the body passed to the constructor should be a dict-like object that can be used to populate the initial state of the message. The Message class must provide an encode method that accepts a value of the same type as the body parameter of the constructor and returns a string of characters that are able to be stored in an SQS message body (see rules above). The Message class must provide a decode method that accepts a string of characters that can be stored (and probably were stored!) in an SQS message and return an object of a type that is consistent with the "body" parameter accepted on the class constructor. The Message class must provide a __len__ method that will return the size of the encoded message that would be stored in SQS based on the current state of the Message object. The Message class must provide a get_body method that will return the body of the message in the same format accepted in the constructor of the class. The Message class must provide a set_body method that accepts a message body in the same format accepted by the constructor of the class. This method should alter to the internal state of the Message object to reflect the state represented in the message body parameter. The Message class must provide a get_body_encoded method that returns the current body of the message in the format in which it would be stored in SQS. N)StringIO)six) Attributes)MessageAttributes)SQSDecodeErrorc@sreZdZdZdddZddZdd Zd d Zd d ZddZ ddZ ddZ ddZ ddZ ddZddZdS) RawMessagea Base class for SQS messages. RawMessage does not encode the message in any way. Whatever you store in the body of the message is what will be written to SQS and whatever is returned from SQS is stored directly into the body of the message. NcCs@||_||d|_d|_d|_t||_t||_d|_ dSN) queueset_bodyidreceipt_handlemd5r attributesrmessage_attributesmd5_message_attributes)selfr bodyr2/usr/lib/python3/dist-packages/boto/sqs/message.py__init__Ts    zRawMessage.__init__cCst||jSr )lenencode_bodyrrrr__len__^szRawMessage.__len__cCs |dkr|jS|dkr|jSdS)N AttributeMessageAttribute)rr)rnameattrs connectionrrr startElementas zRawMessage.startElementcCsn|dkr ||dS|dkr||_dS|dkr||_dS|dkr&||_dS|dkr/||_dSt|||dS)NBody MessageId ReceiptHandle MD5OfBodyMD5OfMessageAttributes)r r r rrsetattr)rrvaluer rrr endElemenths    zRawMessage.endElementcCs|||dSr )r decodeget_body)rr rrrendNodevszRawMessage.endNodecC|S)z8Transform body object into serialized byte array format.rrr(rrrryzRawMessage.encodecCr-)z/Transform seralized byte array into any object.rr.rrrr*}r/zRawMessage.decodecCs ||_dS)z@Override the current body for this object, using decoded format.Nr)rrrrrr s zRawMessage.set_bodycCs|jSr r0rrrrr+szRawMessage.get_bodycCs||S)z This method is really a semi-private method used by the Queue.write method when writing the contents of the message to SQS. You probably shouldn't need to call this method in the normal course of events. )rr+rrrrget_body_encodedszRawMessage.get_body_encodedcCs|jr |j|SdSr )r delete_messagerrrrdeletes zRawMessage.deletecCs$|jr|jj|j|j|dSdSr )r r change_message_visibilityr )rvisibility_timeoutrrrchange_visibilitys  zRawMessage.change_visibilityNr)__name__ __module__ __qualname____doc__rrr!r)r,rr*r r+r1r3r6rrrrrLs   rc@s eZdZdZddZddZdS)Messageaz The default Message class used for SQS queues. This class automatically encodes/decodes the message body using Base64 encoding to avoid any illegal characters in the message body. See: https://forums.aws.amazon.com/thread.jspa?threadID=13067 for details on why this is a good idea. The encode/decode is meant to be transparent to the end-user. cCs&t|tjs |d}t|dSNutf-8) isinstancer binary_typerbase64 b64encoder*r.rrrrs  zMessage.encodecCs8zt|dd}W|Stjd|YSNr>Unable to decode message)rA b64decoderr*botologwarningr.rrrr*s  zMessage.decodeN)r8r9r:r;rr*rrrrr<s r<cs|eZdZdZdfdd ZddZddZd d Zd d Zd dZ ddZ ddZ ddZ ddZ ddZdddZZS) MHMessageaX The MHMessage class provides a message that provides RFC821-like headers like this: HeaderName: HeaderValue The encoding/decoding of this is handled automatically and after the message body has been read, the message instance can be treated like a mapping object, i.e. m['HeaderName'] would return 'HeaderValue'. Ncs*|dus|dkr i}tt|||dSr7)superrIr)rr r xml_attrs __class__rrrszMHMessage.__init__cCs|z5i}t|}|}|r3|d}|d|}||dd}|||<|}|s W|SW|Std|)N:rrD)rreadlinefindstripr)rr(msgfplinedelimkeyrrrr*s    zMHMessage.decodecCs.d}|D]}|d|d|df}q|S)Nrz%s: %s rrO)items)rr(sitemrrrrs zMHMessage.encodecC ||jvSr r0rrWrrr __contains__ zMHMessage.__contains__cCs||jvr |j|St|r )rKeyErrorr\rrr __getitem__s  zMHMessage.__getitem__cCs||j|<||jdSr )rr )rrWr(rrr __setitem__s zMHMessage.__setitem__cC |jSr )rkeysrrrrrcr^zMHMessage.keyscCrbr )rvaluesrrrrrdr^zMHMessage.valuescCrbr )rrXrrrrrXr^zMHMessage.itemscCr[r r0r\rrrhas_keyr^zMHMessage.has_keycCs|j|||jdSr )rupdater )rdrrrrfs zMHMessage.updatecCs|j||Sr )rget)rrWdefaultrrrrhsz MHMessage.get)NNNr )r8r9r:r;rr*rr]r`rarcrdrXrerfrh __classcell__rrrLrrIs rIcs,eZdZdZfddZfddZZS)EncodedMHMessageaN The EncodedMHMessage class provides a message that provides RFC821-like headers like this: HeaderName: HeaderValue This variation encodes/decodes the body of the message in base64 automatically. The message instance can be treated like a mapping object, i.e. m['HeaderName'] would return 'HeaderValue'. cs<z t|dd}Wntd|tt||SrC)rArErr*rrJrkr.rLrrr*s  zEncodedMHMessage.decodecs&tt||}t|ddSr=)rJrkrrArBr*r.rLrrr szEncodedMHMessage.encode)r8r9r:r;r*rrjrrrLrrks rk)r;rArF boto.compatrrboto.sqs.attributesrboto.sqs.messageattributesrboto.exceptionrobjectrr<rIrkrrrrs,     NF