o
    rMf`                     @   st   d dl Z d dlmZ d dlmZ d dlmZ d dlmZ d dl	m
Z
 d dlmZ d dlmZmZ G d	d
 d
eZdS )    N)AWSQueryConnection)SQSRegionInfo)Queue)Message)
Attributes)BatchResults)SQSErrorBotoServerErrorc                       s  e Zd ZdZejdddZejdddZejdddZ	d	Z
eZd
Z					d; fdd	Zdd Zd<ddZd=ddZdd Zd>ddZdd Z			d?d d!Zd"d# Zd$d% Zd&d' Z		d@d(d)Zd*d+ Zd,d- Zd.d/ ZdAd1d2Zd<d3d4ZeZd5d6 Z d7d8 Z!d9d: Z"  Z#S )BSQSConnectionz*
    A Connection to the SQS Service.
    Botosqs_region_namez	us-east-1sqs_region_endpointzqueue.amazonaws.comsqs_versionz
2012-11-05z
text/plainsqsNTr   /c                    sZ   |s
t | | j| j}|| _tt| j||||||||| jj|	|
||||d | jj| _	d S )N)security_tokenvalidate_certsprofile_name)
r   DefaultRegionNameDefaultRegionEndpointregionsuperr
   __init__endpointnameauth_region_name)selfaws_access_key_idaws_secret_access_key	is_secureportproxy
proxy_port
proxy_user
proxy_passdebughttps_connection_factoryr   pathr   r   r   	__class__ U/var/www/html/analyze/labelStudio/lib/python3.10/site-packages/boto/sqs/connection.pyr   ,   s"   
zSQSConnection.__init__c                 C   s   dgS )Nzhmac-v4r*   )r   r*   r*   r+   _required_auth_capabilityA   s   z'SQSConnection._required_auth_capabilityc                 C   s.   d|i}|rd|d< t ||d< | d|tS )a  
        Create an SQS Queue.

        :type queue_name: str or unicode
        :param queue_name: The name of the new queue.  Names are
            scoped to an account and need to be unique within that
            account.  Calling this method on an existing queue name
            will not return an error from SQS unless the value for
            visibility_timeout is different than the value of the
            existing queue of that name.  This is still an expensive
            operation, though, and not the preferred way to check for
            the existence of a queue.  See the
            :func:`boto.sqs.connection.SQSConnection.lookup` method.

        :type visibility_timeout: int
        :param visibility_timeout: The default visibility timeout for
            all messages written in the queue.  This can be overridden
            on a per-message.

        :rtype: :class:`boto.sqs.queue.Queue`
        :return: The newly created queue.

        	QueueNameVisibilityTimeoutzAttribute.1.NamezAttribute.1.ValueCreateQueue)int
get_objectr   )r   
queue_namevisibility_timeoutparamsr*   r*   r+   create_queueD   s
   zSQSConnection.create_queueFc                 C      |  dd|jS )aa  
        Delete an SQS Queue.

        :type queue: A Queue object
        :param queue: The SQS queue to be deleted

        :type force_deletion: Boolean
        :param force_deletion: A deprecated parameter that is no longer used by
            SQS's API.

        :rtype: bool
        :return: True if the command succeeded, False otherwise
        DeleteQueueN
get_statusid)r   queueforce_deletionr*   r*   r+   delete_queueb   s   zSQSConnection.delete_queuec                 C   r6   )z
        Purge all messages in an SQS Queue.

        :type queue: A Queue object
        :param queue: The SQS queue to be purged

        :rtype: bool
        :return: True if the command succeeded, False otherwise
        
PurgeQueueNr8   )r   r;   r*   r*   r+   purge_queuer   s   
zSQSConnection.purge_queueAllc                 C   s   d|i}|  d|t|jS )a  
        Gets one or all attributes of a Queue

        :type queue: A Queue object
        :param queue: The SQS queue to get attributes for

        :type attribute: str
        :param attribute: The specific attribute requested.  If not
            supplied, the default is to return all attributes.  Valid
            attributes are:

            * All
            * ApproximateNumberOfMessages
            * ApproximateNumberOfMessagesNotVisible
            * VisibilityTimeout
            * CreatedTimestamp
            * LastModifiedTimestamp
            * Policy
            * MaximumMessageSize
            * MessageRetentionPeriod
            * QueueArn
            * ApproximateNumberOfMessagesDelayed
            * DelaySeconds
            * ReceiveMessageWaitTimeSeconds
            * RedrivePolicy

        :rtype: :class:`boto.sqs.attributes.Attributes`
        :return: An Attributes object containing request value(s).
        AttributeNameGetQueueAttributes)r1   r   r:   )r   r;   	attributer4   r*   r*   r+   get_queue_attributes~   s   z"SQSConnection.get_queue_attributesc                 C      ||d}|  d||jS )a;  
        Set a new value for an attribute of a Queue.

        :type queue: A Queue object
        :param queue: The SQS queue to get attributes for

        :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).
                >>> connection.set_queue_attribute(queue, 'DelaySeconds', 900)

            * For `MaximumMessageSize` the value must be an integer number of
            bytes from 1024 (1 KiB) to 262144 (256 KiB).
                >>> connection.set_queue_attribute(queue, 'MaximumMessageSize', 262144)

            * For `MessageRetentionPeriod` the value must be an integer number of
            seconds from 60 (1 minute) to 1209600 (14 days).
                >>> connection.set_queue_attribute(queue, 'MessageRetentionPeriod', 1209600)

            * For `Policy` the value must be an string that contains JSON formatted
            parameters and values.
                >>> connection.set_queue_attribute(queue, '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.
                >>> connection.set_queue_attribute(queue, 'ReceiveMessageWaitTimeSeconds', 20)

            * For `VisibilityTimeout` the value must be an integer number of
            seconds from 0 to 43200 (12 hours).
                >>> connection.set_queue_attribute(queue, '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.
                >>> connection.set_queue_attribute(queue, 'RedrivePolicy', json.dumps({
                ...    'maxReceiveCount': 5,
                ...    'deadLetterTargetArn': "arn:aws:aws:sqs:us-east-1:123456789012:testDeadLetterQueue"
                ... }))
        )zAttribute.NamezAttribute.ValueSetQueueAttributesr8   )r   r;   rC   valuer4   r*   r*   r+   set_queue_attribute   s   
<z!SQSConnection.set_queue_attribute   c                 C   sp   d|i}|dur||d< |dur|  ||d |dur||d< |dur*|  ||d | d|d|jfg|j|S )	a,  
        Read messages from an SQS Queue.

        :type queue: A Queue object
        :param queue: The Queue from which messages are read.

        :type number_messages: int
        :param number_messages: The maximum number of messages to read
                                (default=1)

        :type visibility_timeout: int
        :param visibility_timeout: The number of seconds the message should
            remain invisible to other queue readers
            (default=None which uses the Queues default)

        :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.

        MaxNumberOfMessagesNr.   rA   WaitTimeSecondsMessageAttributeNameReceiveMessager   )build_list_paramsget_listmessage_classr:   )r   r;   number_messagesr3   
attributeswait_time_secondsmessage_attributesr4   r*   r*   r+   receive_message   s   ,
zSQSConnection.receive_messagec                 C   s   d|j i}| d||jS )al  
        Delete a message from a queue.

        :type queue: A :class:`boto.sqs.queue.Queue` object
        :param queue: The Queue from which messages are read.

        :type message: A :class:`boto.sqs.message.Message` object
        :param message: The Message to be deleted

        :rtype: bool
        :return: True if successful, False otherwise.
        ReceiptHandleDeleteMessage)receipt_handler9   r:   )r   r;   messager4   r*   r*   r+   delete_message  s   
zSQSConnection.delete_messagec                 C   sd   i }t |D ] \}}d}d||d f }|j||< d||d f }|j||< q| jd|t|jddS )aS  
        Deletes a list of messages from a queue in a single request.

        :type queue: A :class:`boto.sqs.queue.Queue` object.
        :param queue: The Queue to which the messages will be written.

        :type messages: List of :class:`boto.sqs.message.Message` objects.
        :param messages: A list of message objects.
        DeleteMessageBatchRequestEntry%s.%i.IdrI   %s.%i.ReceiptHandleDeleteMessageBatchPOSTverb	enumerater:   rX   r1   r   )r   r;   messagesr4   imsgprefixp_namer*   r*   r+   delete_message_batch)  s   


z"SQSConnection.delete_message_batchc                 C      d|i}|  d||jS )at  
        Delete a message from a queue, given a receipt handle.

        :type queue: A :class:`boto.sqs.queue.Queue` object
        :param queue: The Queue from which messages are read.

        :type receipt_handle: str
        :param receipt_handle: The receipt handle for the message

        :rtype: bool
        :return: True if successful, False otherwise.
        rV   rW   r8   )r   r;   rX   r4   r*   r*   r+   delete_message_from_handle=  s   z(SQSConnection.delete_message_from_handlec           
      C   s   d|i}|rt ||d< |durgt| }t|ddD ]J\}}|| }	||d| < d|	v r6|	d |d| < d	|	v rB|	d	 |d
| < d|	v rN|	d |d| < d|	v rZ|	d |d| < d|	v rf|	d |d| < q| jd|t|jddS )aT  
        Send a new message to the queue.

        :type queue: A :class:`boto.sqs.queue.Queue` object.
        :param queue: The Queue to which the messages will be written.

        :type message_content: string
        :param message_content: The body of the message

        :type delay_seconds: int
        :param delay_seconds: Number of seconds (0 - 900) to delay this
            message from being processed.

        :type message_attributes: dict
        :param message_attributes: Message attributes to set. Should be
            of the form:

            {
                "name1": {
                    "data_type": "Number",
                    "string_value": "1"
                },
                "name2": {
                    "data_type": "String",
                    "string_value": "Bob"
                }
            }

        MessageBodyDelaySecondsNrI   )startzMessageAttribute.%s.Name	data_typez"MessageAttribute.%s.Value.DataTypestring_valuez%MessageAttribute.%s.Value.StringValuebinary_valuez%MessageAttribute.%s.Value.BinaryValuestring_list_valuez)MessageAttribute.%s.Value.StringListValuebinary_list_valuez)MessageAttribute.%s.Value.BinaryListValueSendMessager_   r`   )r0   sortedkeysrc   r1   r   r:   )
r   r;   message_contentdelay_secondsrT   r4   rv   re   r   rC   r*   r*   r+   send_messageM  s6   





zSQSConnection.send_messagec                 C   sv  i }t |D ]\}}d|d  }|d |d| < |d |d| < |d |d| < t|dkr|d	7 }t|d  }t |D ]p\}}	|d |	 }
d
||d f }|	||< d|
v rfd||d f }|
d ||< d|
v rxd||d f }|
d ||< d|
v rd||d f }|
d ||< d|
v rd||d f }|
d ||< d|
v rd||d f }|
d ||< q>q| jd|t|jddS )aC  
        Delivers up to 10 messages to a queue in a single request.

        :type queue: A :class:`boto.sqs.queue.Queue` object.
        :param queue: The Queue to which the messages will be written.

        :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``
            above.

        zSendMessageBatchRequestEntry.%irI   r   z%s.Idz%s.MessageBody   z%s.DelaySeconds   z.MessageAttributez
%s.%i.Namero   z%s.%i.Value.DataTyperp   z%s.%i.Value.StringValuerq   z%s.%i.Value.BinaryValuerr   z%s.%i.Value.StringListValuers   z%s.%i.Value.BinaryListValueSendMessageBatchr_   r`   )rc   lenru   rv   r1   r   r:   )r   r;   rd   r4   re   rf   baserv   jr   rC   rh   r*   r*   r+   send_message_batch  s@   
z SQSConnection.send_message_batchc                 C   rE   )ab  
        Extends the read lock timeout for the specified message from
        the specified queue to the specified value.

        :type queue: A :class:`boto.sqs.queue.Queue` object
        :param queue: The Queue from which messages are read.

        :type receipt_handle: str
        :param receipt_handle: The receipt handle associated with the message
                               whose visibility timeout will be changed.

        :type visibility_timeout: int
        :param visibility_timeout: The new value of the message's visibility
                                   timeout in seconds.
        )rV   r.   ChangeMessageVisibilityr8   )r   r;   rX   r3   r4   r*   r*   r+   change_message_visibility  s   z'SQSConnection.change_message_visibilityc                 C   s   i }t |D ]2\}}d}d||d f }|d j||< d||d f }|d j||< d||d f }|d ||< q| jd|t|jdd	S )
a  
        A batch version of change_message_visibility that can act
        on up to 10 messages at a time.

        :type queue: A :class:`boto.sqs.queue.Queue` object.
        :param queue: The Queue to which the messages will be written.

        :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.
        (ChangeMessageVisibilityBatchRequestEntryr\   rI   r   r]   z%s.%i.VisibilityTimeoutChangeMessageVisibilityBatchr_   r`   rb   )r   r;   rd   r4   re   trg   rh   r*   r*   r+   change_message_visibility_batch  s   z-SQSConnection.change_message_visibility_batch c                 C   s$   i }|r||d< |  d|dtfgS )z
        Retrieves all queues.

        :keyword str prefix: Optionally, only return queues that start with
            this value.
        :rtype: list
        :returns: A list of :py:class:`boto.sqs.queue.Queue` instances.
        QueueNamePrefix
ListQueuesQueueUrl)rO   r   )r   rg   r4   r*   r*   r+   get_all_queues  s   	zSQSConnection.get_all_queuesc                 C   s:   d|i}|r
||d< z|  d|tW S  ty   Y dS w )a  
        Retrieves the queue with the given name, or ``None`` if no match
        was found.

        :param str queue_name: The name of the queue to retrieve.
        :param str owner_acct_id: Optionally, the AWS account ID of the account that created the queue.
        :rtype: :py:class:`boto.sqs.queue.Queue` or ``None``
        :returns: The requested queue, or ``None`` if no match was found.
        r-   QueueOwnerAWSAccountIdGetQueueUrlN)r1   r   r   )r   r2   owner_acct_idr4   r*   r*   r+   	get_queue  s   
zSQSConnection.get_queuec                 C   s   d|j i}| d|dtfgS )a)  
        Retrieves the dead letter source queues for a given queue.

        :type queue: A :class:`boto.sqs.queue.Queue` object.
        :param queue: The queue for which to get DL source queues
        :rtype: list
        :returns: A list of :py:class:`boto.sqs.queue.Queue` instances.
        r   ListDeadLetterSourceQueues)urlrO   r   )r   r;   r4   r*   r*   r+   get_dead_letter_source_queues  s   
	z+SQSConnection.get_dead_letter_source_queuesc                 C   s   |||d}|  d||jS )a  
        Add a permission to a queue.

        :type queue: :class:`boto.sqs.queue.Queue`
        :param queue: The queue object

        :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.

        )LabelAWSAccountId
ActionNameAddPermissionr8   )r   r;   labelaws_account_idaction_namer4   r*   r*   r+   add_permission   s
    zSQSConnection.add_permissionc                 C   rj   )aj  
        Remove a permission from a queue.

        :type queue: :class:`boto.sqs.queue.Queue`
        :param queue: The queue object

        :type label: str or unicode
        :param label: The unique label associated with the permission
                      being removed.

        :rtype: bool
        :return: True if successful, False otherwise.
        r   RemovePermissionr8   )r   r;   r   r4   r*   r*   r+   remove_permissionE  s   zSQSConnection.remove_permission)NNTNNNNNr   NNr   NTN)N)F)r@   )rI   NNNN)NN)r   )$__name__
__module____qualname____doc__botoconfiggetr   r   
APIVersionDefaultContentTyper   ResponseErrorAuthServiceNamer   r,   r5   r=   r?   rD   rH   rU   rZ   ri   rk   ry   r   r   r   r   r   lookupr   r   r   __classcell__r*   r*   r(   r+   r
       sR    



"?
:
;6

%r
   )r   boto.connectionr   boto.sqs.regioninfor   boto.sqs.queuer   boto.sqs.messager   boto.sqs.attributesr   boto.sqs.batchresultsr   boto.exceptionr   r	   r
   r*   r*   r*   r+   <module>   s   