o ckF[tN@s0dZddlmZddlmZGdddeZdS)z Represents an SQS Queue )urllib)Messagec@sjeZdZddefddZddZddZeeZdd Z ee Z d d Z ee Z d d Z ddZddZdKddZddZddZddZddZddZ  dLdd ZdMd!d"Zd#d$ZdNd&d'Z   dOd)d*Zd+d,Zd-d.Zd/d0Zd1d2Zd3d4ZdPd6d7Z dPd8d9Z!dPd:d;Z"dQd=d>Z#dRd?d@Z$dRdAdBZ%e%Z&dCdDZ'dMdEdFZ(dRdGdHZ)dRdIdJZ*e*Z+dS)SQueueNcCs||_||_||_d|_dSN) connectionurl message_classvisibility_timeout)selfrrrr 0/usr/lib/python3/dist-packages/boto/sqs/queue.py__init__s zQueue.__init__cCs d|jS)Nz Queue(%s))rr r r r __repr__%s zQueue.__repr__cCs&|jrtj|jd}|S|j}|S)N)rrparseurlparser valr r r _id(s z Queue._idcCs0|jrtj|jddd}|S|j}|S)Nr/)rrrrsplitrr r r _name0s z Queue._namecCsB|jd}|jjjdkrd}nd}d||jjj|d|dfS)Nrz cn-north-1zaws-cnawszarn:%s:sqs:%s:%s:%sr)idrrregionname)r parts partitionr r r _arn8s z Queue._arncCsdSrr )r rattrsrr r r startElementBszQueue.startElementcCs8|dkr ||_dS|dkrt||_dSt|||dS)NQueueUrlVisibilityTimeout)rintr setattr)r rvaluerr r r endElementEs  zQueue.endElementcCs ||_dS)aq 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)r rr r r set_message_classMs zQueue.set_message_classAllcC|j||S)a 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 )rget_queue_attributes)r attributesr r r get_attributesYszQueue.get_attributescCs|j|||S)a Set a new value for an attribute of the Queue. :type attribute: String :param attribute: The name of the attribute you want to set. :param value: The new value for the attribute must be: * For `DelaySeconds` the value must be an integer number of seconds from 0 to 900 (15 minutes). >>> queue.set_attribute('DelaySeconds', 900) * For `MaximumMessageSize` the value must be an integer number of bytes from 1024 (1 KiB) to 262144 (256 KiB). >>> queue.set_attribute('MaximumMessageSize', 262144) * For `MessageRetentionPeriod` the value must be an integer number of seconds from 60 (1 minute) to 1209600 (14 days). >>> queue.set_attribute('MessageRetentionPeriod', 1209600) * For `Policy` the value must be an string that contains JSON formatted parameters and values. >>> queue.set_attribute('Policy', json.dumps({ ... 'Version': '2008-10-17', ... 'Id': '/123456789012/testQueue/SQSDefaultPolicy', ... 'Statement': [ ... { ... 'Sid': 'Queue1ReceiveMessage', ... 'Effect': 'Allow', ... 'Principal': { ... 'AWS': '*' ... }, ... 'Action': 'SQS:ReceiveMessage', ... 'Resource': 'arn:aws:aws:sqs:us-east-1:123456789012:testQueue' ... } ... ] ... })) * For `ReceiveMessageWaitTimeSeconds` the value must be an integer number of seconds from 0 to 20. >>> queue.set_attribute('ReceiveMessageWaitTimeSeconds', 20) * For `VisibilityTimeout` the value must be an integer number of seconds from 0 to 43200 (12 hours). >>> queue.set_attribute('VisibilityTimeout', 43200) * For `RedrivePolicy` the value must be an string that contains JSON formatted parameters and values. You can set maxReceiveCount to a value between 1 and 1000. The deadLetterTargetArn value is the Amazon Resource Name (ARN) of the queue that will receive the dead letter messages. >>> queue.set_attribute('RedrivePolicy', json.dumps({ ... 'maxReceiveCount': 5, ... 'deadLetterTargetArn': "arn:aws:aws:sqs:us-east-1:123456789012:testDeadLetterQueue" ... })) :rtype: bool :return: True if successful, otherwise False. )rset_queue_attribute)r attributer'r r r set_attributems9s "zQueue.get_messagescCr+)a 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 )rdelete_message)r rFr r r rVa zQueue.delete_messagecCr+)z 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. )rdelete_message_batchrJr r r rXmszQueue.delete_message_batchcCr+)ar 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. )rchange_message_visibility_batchrJr r r rYvrWz%Queue.change_message_visibility_batchcC |j|S)z# Delete the queue. )r delete_queuerr r r delete z Queue.deletecCrZ)z2 Purge all messages in the queue. )r purge_queuerr r r purger]z Queue.purge cCs|S)z?Deprecated utility function to remove all messages from a queue)r_)r page_sizevtimeoutr r r clearsz Queue.clearcCs|d}t|dS)z 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. ApproximateNumberOfMessagesr2)r rarbr3r r r counts  z Queue.countcCs:d}|||}|r|D]}|d7}q |||}|s |S)a 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. rr)r>)r rarbnlrQr r r count_slows   zQueue.count_slow c Csht|d}d}|||}|r.|D]}|||r!|||d7}q|||}|s||S)zoUtility function to dump the messages in a queue to a file NOTE: Page size must be < 10 else SQS errorswbrr)openr>rHget_bodyclose) r file_namerarbsepfprfrgrQr r r dumps     z Queue.dumpcCsNd}|}|r%|d7}|||r|||||}|s|S)a: 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. rr)rArHrlrV)r rprorfrQr r r save_to_files  zQueue.save_to_filecC"t|d}|||}||S)a4 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. rj)rkrrrmr rnrorprfr r r save_to_filenames  zQueue.save_to_filenamecCsVd}|}|r)|d7}|d|j|jf}|||||}|s|S)a+ 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. rrz%s/%s)rAnew_keyrset_contents_from_stringrlrV)r bucketrfrQkeyr r r save_to_s3s  zQueue.save_to_s3cCs^d}|r d|}n d|jdd}|j|d}|D]}|d7}||}||q|S)z7 Load messages previously saved to S3. rz%s/rN)prefix)rlistrRget_contents_as_stringrH)r rxr{rfr@ryrQr r r load_from_s3s   zQueue.load_from_s3cCsbd}d}|}|r/||kr%t||}|||d7}td|d}n||}|}|s |S)zDUtility function to load messages from a file-like object to a queuerrMrzwriting message %d)readlinerrHprint)r rprorfrOrgrQr r r load_from_files    zQueue.load_from_filecCrs)zBUtility function to load messages from a local filename to a queuerb)rkrrmrtr r r load_from_filenames  zQueue.load_from_filename)r*)NNNr)rM)rNNNN)r`r`)r`r`ri)ri),__name__ __module__ __qualname__rr rrpropertyrrrr arnr"r(r)r.r1r4r6r7r;rArHrLrRr>rVrXrYr\r_rcrerhrqrrrusaverzr~rrloadr r r r rsZ >      (           rN)__doc__ boto.compatrboto.sqs.messagerobjectrr r r r s