ó FRTc@s@dZddlmZddlmZdefd„ƒYZdS(s Represents an SQS Queue iÿÿÿÿ(turllib(tMessagetQueuecBsµeZd&d&ed„Zd„Zd„ZeeƒZd„Z ee ƒZ d„Z ee ƒZ d„Z d„Zd„Zdd „Zd „Zd „Zd „Zd „Zd„Zd&d&d&d„Zd&d„Zd„Zdd„Zdd&d&d&d&d„Zd„Zd„Zd„Zd„Zddd„Zddd„Z ddd„Z!dddd„Z"dd „Z#dd!„Z$e$Z%d"„Z&d&d#„Z'dd$„Z(dd%„Z)e)Z*RS('cCs(||_||_||_d|_dS(N(t connectionturlt message_classtNonetvisibility_timeout(tselfRRR((s;/opt/freeware/lib/python2.7/site-packages/boto/sqs/queue.pyt__init__s   cCs d|jS(Ns Queue(%s)(R(R((s;/opt/freeware/lib/python2.7/site-packages/boto/sqs/queue.pyt__repr__%scCs2|jr%tjj|jƒd}n |j}|S(Ni(RRtparseturlparse(Rtval((s;/opt/freeware/lib/python2.7/site-packages/boto/sqs/queue.pyt_id(s  cCs?|jr2tjj|jƒdjdƒd}n |j}|S(Nit/(RRR R tsplit(RR ((s;/opt/freeware/lib/python2.7/site-packages/boto/sqs/queue.pyt_name0s ) cCs4|jjdƒ}d|jjj|d|dfS(NRsarn:aws:sqs:%s:%s:%sii(tidRRtregiontname(Rtparts((s;/opt/freeware/lib/python2.7/site-packages/boto/sqs/queue.pyt_arn8scCsdS(N(R(RRtattrsR((s;/opt/freeware/lib/python2.7/site-packages/boto/sqs/queue.pyt startElement>scCsJ|dkr||_n.|dkr6t|ƒ|_nt|||ƒdS(NtQueueUrltVisibilityTimeout(RtintRtsetattr(RRtvalueR((s;/opt/freeware/lib/python2.7/site-packages/boto/sqs/queue.pyt endElementAs    cCs ||_dS(sq Set the message class that should be used when instantiating messages read from the queue. By default, the class :class:`boto.sqs.message.Message` is used but this can be overriden with any class that behaves like a message. :type message_class: Message-like class :param message_class: The new Message class N(R(RR((s;/opt/freeware/lib/python2.7/site-packages/boto/sqs/queue.pytset_message_classIs tAllcCs|jj||ƒS(sÛ Retrieves attributes about this queue object and returns them in an Attribute instance (subclass of a Dictionary). :type attributes: string :param attributes: String containing one of: ApproximateNumberOfMessages, ApproximateNumberOfMessagesNotVisible, VisibilityTimeout, CreatedTimestamp, LastModifiedTimestamp, Policy ReceiveMessageWaitTimeSeconds :rtype: Attribute object :return: An Attribute object which is a mapping type holding the requested name/value pairs (Rtget_queue_attributes(Rt attributes((s;/opt/freeware/lib/python2.7/site-packages/boto/sqs/queue.pytget_attributesUscCs|jj|||ƒS(s Set a new value for an attribute of the Queue. :type attribute: String :param attribute: The name of the attribute you want to set. The only valid value at this time is: VisibilityTimeout :type value: int :param value: The new value for the attribute. For VisibilityTimeout the value must be an integer number of seconds from 0 to 86400. :rtype: bool :return: True if successful, otherwise False. (Rtset_queue_attribute(Rt attributeR((s;/opt/freeware/lib/python2.7/site-packages/boto/sqs/queue.pyt set_attributeiscCs|jdƒ}t|dƒS(s† Get the visibility timeout for the queue. :rtype: int :return: The number of seconds as an integer. R(R#R(Rta((s;/opt/freeware/lib/python2.7/site-packages/boto/sqs/queue.pyt get_timeoutzscCs(|jd|ƒ}|r$||_n|S(s¤ Set the visibility timeout for the queue. :type visibility_timeout: int :param visibility_timeout: The desired timeout in seconds R(R&R(RRtretval((s;/opt/freeware/lib/python2.7/site-packages/boto/sqs/queue.pyt set_timeout„s cCs|jj||||ƒS(ss Add a permission to a queue. :type label: str or unicode :param label: A unique identification of the permission you are setting. Maximum of 80 characters ``[0-9a-zA-Z_-]`` Example, AliceSendMessage :type aws_account_id: str or unicode :param principal_id: The AWS account number of the principal who will be given permission. The principal must have an AWS account, but does not need to be signed up for Amazon SQS. For information about locating the AWS account identification. :type action_name: str or unicode :param action_name: The action. Valid choices are: SendMessage|ReceiveMessage|DeleteMessage| ChangeMessageVisibility|GetQueueAttributes|* :rtype: bool :return: True if successful, False otherwise. (Rtadd_permission(Rtlabeltaws_account_idt action_name((s;/opt/freeware/lib/python2.7/site-packages/boto/sqs/queue.pyR+scCs|jj||ƒS(s Remove a permission from a queue. :type label: str or unicode :param label: The unique label associated with the permission being removed. :rtype: bool :return: True if successful, False otherwise. (Rtremove_permission(RR,((s;/opt/freeware/lib/python2.7/site-packages/boto/sqs/queue.pyR/«s cCs@|jd|d|d|ƒ}t|ƒdkr8|dSdSdS(sD Read a single message from the queue. :type visibility_timeout: int :param visibility_timeout: The timeout for this message in seconds :type wait_time_seconds: int :param wait_time_seconds: The duration (in seconds) for which the call will wait for a message to arrive in the queue before returning. If a message is available, the call will return sooner than wait_time_seconds. :type message_attributes: list :param message_attributes: The name(s) of additional message attributes to return. The default is to return no additional message attributes. Use ``['All']`` or ``['.*']`` to return all. :rtype: :class:`boto.sqs.message.Message` :return: A single message or None if queue is empty itwait_time_secondstmessage_attributesiN(t get_messagestlenR(RRR0R1trs((s;/opt/freeware/lib/python2.7/site-packages/boto/sqs/queue.pytread¸s  cCsF|jj||jƒd|d|jƒ}|j|_|j|_|S(s Add a single message to the queue. :type message: Message :param message: The message to be written to the queue :rtype: :class:`boto.sqs.message.Message` :return: The :class:`boto.sqs.message.Message` object that was written. t delay_secondsR1(Rt send_messagetget_body_encodedR1Rtmd5(RtmessageR6tnew_msg((s;/opt/freeware/lib/python2.7/site-packages/boto/sqs/queue.pytwriteÖs    cCs|jj||ƒS(sÄ Delivers up to 10 messages in a single request. :type messages: List of lists. :param messages: A list of lists or tuples. Each inner tuple represents a single message to be written and consists of and ID (string) that must be unique within the list of messages, the message body itself which can be a maximum of 64K in length, an integer which represents the delay time (in seconds) for the message (0-900) before the message will be delivered to the queue, and an optional dict of message attributes like those passed to ``send_message`` in the connection class. (Rtsend_message_batch(Rtmessages((s;/opt/freeware/lib/python2.7/site-packages/boto/sqs/queue.pyt write_batchçstcKs"|j|||}||_|S(sü Create new message of appropriate class. :type body: message body :param body: The body of the newly created message (optional). :rtype: :class:`boto.sqs.message.Message` :return: A new Message object (Rtqueue(Rtbodytkwargstm((s;/opt/freeware/lib/python2.7/site-packages/boto/sqs/queue.pyt new_messageùs  ic Cs.|jj|d|d|d|d|d|ƒS(s Get a variable number of messages. :type num_messages: int :param num_messages: The maximum number of messages to read from the queue. :type visibility_timeout: int :param visibility_timeout: The VisibilityTimeout for the messages read. :type attributes: str :param attributes: The name of additional attribute to return with response or All if you want all attributes. The default is to return no additional attributes. Valid values: All SenderId SentTimestamp ApproximateReceiveCount ApproximateFirstReceiveTimestamp :type wait_time_seconds: int :param wait_time_seconds: The duration (in seconds) for which the call will wait for a message to arrive in the queue before returning. If a message is available, the call will return sooner than wait_time_seconds. :type message_attributes: list :param message_attributes: The name(s) of additional message attributes to return. The default is to return no additional message attributes. Use ``['All']`` or ``['.*']`` to return all. :rtype: list :return: A list of :class:`boto.sqs.message.Message` objects. tnumber_messagesRR"R0R1(Rtreceive_message(Rt num_messagesRR"R0R1((s;/opt/freeware/lib/python2.7/site-packages/boto/sqs/queue.pyR2s "   cCs|jj||ƒS(s Delete a message from the queue. :type message: :class:`boto.sqs.message.Message` :param message: The :class:`boto.sqs.message.Message` object to delete. :rtype: bool :return: True if successful, False otherwise (Rtdelete_message(RR:((s;/opt/freeware/lib/python2.7/site-packages/boto/sqs/queue.pyRI0s cCs|jj||ƒS(sÁ Deletes a list of messages in a single request. :type messages: List of :class:`boto.sqs.message.Message` objects. :param messages: A list of message objects. (Rtdelete_message_batch(RR>((s;/opt/freeware/lib/python2.7/site-packages/boto/sqs/queue.pyRJ<scCs|jj||ƒS(sr A batch version of change_message_visibility that can act on up to 10 messages at a time. :type messages: List of tuples. :param messages: A list of tuples where each tuple consists of a :class:`boto.sqs.message.Message` object and an integer that represents the new visibility timeout for that message. (Rtchange_message_visibility_batch(RR>((s;/opt/freeware/lib/python2.7/site-packages/boto/sqs/queue.pyRKEs cCs|jj|ƒS(s# Delete the queue. (Rt delete_queue(R((s;/opt/freeware/lib/python2.7/site-packages/boto/sqs/queue.pytdeleteQsi cCscd}|j||ƒ}xD|r^x%|D]}|j|ƒ|d7}q(W|j||ƒ}qW|S(s4Utility function to remove all messages from a queueii(R2RI(Rt page_sizetvtimeouttntlRD((s;/opt/freeware/lib/python2.7/site-packages/boto/sqs/queue.pytclearWs   cCs|jdƒ}t|dƒS(sÖ Utility function to count the number of messages in a queue. Note: This function now calls GetQueueAttributes to obtain an 'approximate' count of the number of messages in a queue. tApproximateNumberOfMessages(R#R(RRNROR'((s;/opt/freeware/lib/python2.7/site-packages/boto/sqs/queue.pytcountbscCsVd}|j||ƒ}x7|rQx|D]}|d7}q(W|j||ƒ}qW|S(s Deprecated. This is the old 'count' method that actually counts the messages by reading them all. This gives an accurate count but is very slow for queues with non-trivial number of messasges. Instead, use get_attributes('ApproximateNumberOfMessages') to take advantage of the new SQS capability. This is retained only for the unit tests. ii(R2(RRNRORPRQRD((s;/opt/freeware/lib/python2.7/site-packages/boto/sqs/queue.pyt count_slowks   s c Cs˜t|dƒ}d}|j||ƒ}x`|r‰xA|D]9}|j|jƒƒ|rf|j|ƒn|d7}q7W|j||ƒ}q*W|jƒ|S(soUtility function to dump the messages in a queue to a file NOTE: Page size must be < 10 else SQS errorstwbii(topenR2R<tget_bodytclose( Rt file_nameRNROtseptfpRPRQRD((s;/opt/freeware/lib/python2.7/site-packages/boto/sqs/queue.pytdump|s   cCsod}|jƒ}xV|rj|d7}|j|jƒƒ|rN|j|ƒn|j|ƒ|jƒ}qW|S(s: Read all messages from the queue and persist them to file-like object. Messages are written to the file and the 'sep' string is written in between messages. Messages are deleted from the queue after being written to the file. Returns the number of messages saved. ii(R5R<RXRI(RR\R[RPRD((s;/opt/freeware/lib/python2.7/site-packages/boto/sqs/queue.pyt save_to_fileŒs    cCs/t|dƒ}|j||ƒ}|jƒ|S(s4 Read all messages from the queue and persist them to local file. Messages are written to the file and the 'sep' string is written in between messages. Messages are deleted from the queue after being written to the file. Returns the number of messages saved. RV(RWR^RY(RRZR[R\RP((s;/opt/freeware/lib/python2.7/site-packages/boto/sqs/queue.pytsave_to_filenameŸs cCsxd}|jƒ}x_|rs|d7}|jd|j|jfƒ}|j|jƒƒ|j|ƒ|jƒ}qW|S(s+ Read all messages from the queue and persist them to S3. Messages are stored in the S3 bucket using a naming scheme of:: / Messages are deleted from the queue after being saved to S3. Returns the number of messages saved. iis%s/%s(R5tnew_keyRtset_contents_from_stringRXRI(RtbucketRPRDtkey((s;/opt/freeware/lib/python2.7/site-packages/boto/sqs/queue.pyt save_to_s3¯s     cCs}d}|rd|}nd|jd}|jd|ƒ}x:|D]2}|d7}|j|jƒƒ}|j|ƒqCW|S(s7 Load messages previously saved to S3. is%s/itprefix(RtlistREtget_contents_as_stringR<(RRbReRPR4RcRD((s;/opt/freeware/lib/python2.7/site-packages/boto/sqs/queue.pyt load_from_s3Ãs   cCsƒd}d}|jƒ}xd|r~||kret||ƒ}|j|ƒ|d7}d|GHd}n ||}|jƒ}qW|S(sDUtility function to load messages from a file-like object to a queueiR@iswriting message %d(treadlineRR<(RR\R[RPRBRQRD((s;/opt/freeware/lib/python2.7/site-packages/boto/sqs/queue.pytload_from_fileÓs        cCs/t|dƒ}|j||ƒ}|jƒ|S(sBUtility function to load messages from a local filename to a queuetrb(RWRjRY(RRZR[R\RP((s;/opt/freeware/lib/python2.7/site-packages/boto/sqs/queue.pytload_from_filenameäs N(+t__name__t __module__RRR R RtpropertyRRRRtarnRRRR#R&R(R*R+R/R5R<R?RER2RIRJRKRMRRRTRUR]R^R_tsaveRdRhRjRltload(((s;/opt/freeware/lib/python2.7/site-packages/boto/sqs/queue.pyRsR                  &         N(t__doc__t boto.compatRtboto.sqs.messageRtobjectR(((s;/opt/freeware/lib/python2.7/site-packages/boto/sqs/queue.pyts