o
    —¨Êh¾"  ã                   @  s  d Z ddlmZ ddlZddlmZ ddlmZmZ ddl	m
Z
 ddlmZ ddlmZmZ dd	lmZ d
dlmZ zddlmZ W n eyM   dZY nw z
ddlmZmZ W n eye   dZdZY nw dd„ ejD ƒZG dd„ dejƒZG dd„ dejƒZdS )až  Azure Storage Queues transport module for kombu.

More information about Azure Storage Queues:
https://azure.microsoft.com/en-us/services/storage/queues/

Features
========
* Type: Virtual
* Supports Direct: *Unreviewed*
* Supports Topic: *Unreviewed*
* Supports Fanout: *Unreviewed*
* Supports Priority: *Unreviewed*
* Supports TTL: *Unreviewed*

Connection String
=================

Connection string has the following formats:

.. code-block::

    azurestoragequeues://<STORAGE_ACCOUNT_ACCESS_KEY>@<STORAGE_ACCOUNT_URL>
    azurestoragequeues://<SAS_TOKEN>@<STORAGE_ACCOUNT_URL>
    azurestoragequeues://DefaultAzureCredential@<STORAGE_ACCOUNT_URL>
    azurestoragequeues://ManagedIdentityCredential@<STORAGE_ACCOUNT_URL>

Note that if the access key for the storage account contains a forward slash
(``/``), it will have to be regenerated before it can be used in the connection
URL.

.. code-block::

    azurestoragequeues://DefaultAzureCredential@<STORAGE_ACCOUNT_URL>
    azurestoragequeues://ManagedIdentityCredential@<STORAGE_ACCOUNT_URL>

If you wish to use an `Azure Managed Identity` you may use the
``DefaultAzureCredential`` format of the connection string which will use
``DefaultAzureCredential`` class in the azure-identity package. You may want to
read the `azure-identity documentation` for more information on how the
``DefaultAzureCredential`` works.

.. _azure-identity documentation:
https://learn.microsoft.com/en-us/python/api/overview/azure/identity-readme?view=azure-python
.. _Azure Managed Identity:
https://learn.microsoft.com/en-us/azure/active-directory/managed-identities-azure-resources/overview

Transport Options
=================

* ``queue_name_prefix``
é    )ÚannotationsN)ÚEmpty)ÚAnyÚOptional)ÚResourceExistsError)Úsafe_str)ÚdumpsÚloads)Úcached_propertyé   )Úvirtual)ÚQueueServiceClient)ÚDefaultAzureCredentialÚManagedIdentityCredentialc                 C  s   i | ]}t |ƒd “qS )é-   )Úord)Ú.0Úc© r   úT/var/www/html/env/lib/python3.10/site-packages/kombu/transport/azurestoragequeues.pyÚ
<dictcomp>Q   s    
ÿr   c                      sà   e Zd ZU dZdZded< dZded< i Zded	< d
Zded< e	ƒ Z
ded< ‡ fdd„Z‡ fdd„Zefd+dd„Zdd„ Zdd„ Zdd„ Zd,dd„Zdd„ Zd d!„ Zed-d#d$„ƒZed%d&„ ƒZed'd(„ ƒZed+d)d*„ƒZ‡  ZS ).ÚChannelzAzure Storage Queues channel.zkombu%(vhost)sÚstrÚdomain_formatNzOptional[QueueServiceClient]Ú_queue_servicezdict[Any, Any]Ú_queue_name_cacheTÚboolÚno_ackzset[Any]Ú_noack_queuesc                   sZ   t d u rtdƒ‚tƒ j|i |¤Ž t | jj¡\| _| _	| j
 ¡ D ]	}|| j|d < q!d S )NzGAzure Storage Queues transport requires the azure-storage-queue libraryÚname)r   ÚImportErrorÚsuperÚ__init__Ú	TransportÚ	parse_uriÚconninfoÚhostnameÚ_credentialÚ_urlÚqueue_serviceÚlist_queuesr   )ÚselfÚargsÚkwargsÚqueue©Ú	__class__r   r   r"   _   s   ÿÿzChannel.__init__c                   s,   |r| j  |¡ tƒ j||g|¢R i |¤ŽS ©N)r   Úaddr!   Úbasic_consume)r+   r.   r   r,   r-   r/   r   r   r3   m   s   ÿÿzChannel.basic_consumeÚreturnc                 C  s   t t|ƒƒ |¡S )z=Format AMQP queue name into a valid Azure Storage Queue name.)r   r   Ú	translate)r+   r   Útabler   r   r   Úentity_namet   s   zChannel.entity_namec                 C  s€   |   | j| ¡}z| jj| j| d�}W |S  ty?   z| j |¡}W n ty4   | jj|d�}Y nw | 	¡ | j|< Y |S w )zEnsure a queue exists.)r.   )
r7   Úqueue_name_prefixr   Úget_queue_clientr   ÚKeyErrorr)   Úcreate_queuer   Úget_queue_properties©r+   r.   Úqr   r   r   Ú_ensure_queuex   s   ÿ
ùÿùzChannel._ensure_queuec                 O  s(   |   |¡}| j |d¡ | j |¡ dS )zDelete queue by name.N)r7   r   Úpopr)   Údelete_queue)r+   r.   r,   r-   Ú
queue_namer   r   r   Ú_deleteˆ   s   
zChannel._deletec                 K  s    |   |¡}t|ƒ}| |¡ dS )zPut message onto queue.N)r?   r   Úsend_message)r+   r.   Úmessager-   r>   Úencoded_messager   r   r   Ú_putŽ   s   
zChannel._putc                 C  sT   |   |¡}|jd|d�}zt|ƒ}W n
 ty   tƒ ‚w t|jƒ}|j|d� |S )z/Try to retrieve a single message off ``queue``.r   )Úmessages_per_pageÚtimeout)rE   )r?   Úreceive_messagesÚnextÚStopIterationr   r	   ÚcontentÚdelete_message)r+   r.   rI   r>   ÚmessagesrE   rM   r   r   r   Ú_get”   s   
ÿ
zChannel._getc                 C  s   |   |¡}| ¡ jS )z)Return the number of messages in a queue.)r?   r<   Úapproximate_message_countr=   r   r   r   Ú_size¤   s   

zChannel._sizec                 C  s"   |   |¡}|  |j¡}| ¡  |S )z'Delete all current messages in a queue.)r?   rR   rB   Úclear_messages)r+   r.   r>   Únr   r   r   Ú_purge©   s   
zChannel._purger   c                 C  s"   | j d u rt| j| jd�| _ | j S )N)Úaccount_urlÚ
credential)r   r   r(   r'   ©r+   r   r   r   r)   °   s
   
ÿzChannel.queue_servicec                 C  s   | j jS r1   )Ú
connectionÚclientrX   r   r   r   r%   ¹   s   zChannel.conninfoc                 C  s
   | j jjS r1   )rY   rZ   Útransport_optionsrX   r   r   r   r[   ½   s   
zChannel.transport_optionsc                 C  s   | j  dd¡S )Nr8   Ú )r[   ÚgetrX   r   r   r   r8   Á   s   zChannel.queue_name_prefix)r4   r   r1   )r4   r   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   Ú__annotations__r   r   r   Úsetr   r"   r3   ÚCHARS_REPLACE_TABLEr7   r?   rC   rG   rP   rR   rU   Úpropertyr)   r%   r[   r
   r8   Ú__classcell__r   r   r/   r   r   V   s0   
 


r   c                   @  sZ   e Zd ZU dZeZdZded< dZded< dZd	ed
< e	ddd„ƒZ
e	dddd„ƒZdS )r#   zAzure Storage Queues transport.r   ÚintÚpolling_intervalNzOptional[int]Údefault_portTr   Úcan_parse_urlÚurir   r4   útuple[str | dict, str]c                 C  s¸   zP|   dd¡} |  dd¡\}}d ¡ | ¡ kr#td u rtdƒ‚tƒ }n!d ¡ | ¡ kr7td u r3tdƒ‚tƒ }nd	|v rDd
|vrDd	|dœ}t||gƒsLJ ‚W ||fS  ty[   tdƒ‚w )Nzazurestoragequeues://r\   ú@r   r   z`Azure Storage Queues transport with a DefaultAzureCredential requires the azure-identity libraryr   zcAzure Storage Queues transport with a ManagedIdentityCredential requires the azure-identity libraryÚdevstoreaccount1z.core.windows.net)Úaccount_nameÚaccount_keyz¬Need a URI like azurestoragequeues://{SAS or access key}@{URL}, azurestoragequeues://DefaultAzureCredential@{URL}, , or azurestoragequeues://ManagedIdentityCredential@{URL})	ÚreplaceÚrsplitÚlowerr   r    r   ÚallÚ	ExceptionÚ
ValueError)rk   rW   Úurlr   r   r   r$   Ï   s,   þ
÷ÿÿzTransport.parse_uriFú**Úinclude_passwordÚmaskc                 C  s&   |   |¡\}}d |r||¡S ||¡S )Nzazurestoragequeues://{}@{})r$   Úformat)Úclsrk   ry   rz   rW   rw   r   r   r   Úas_uri   s   ÿÿzTransport.as_uri)rk   r   r4   rl   )Frx   )rk   r   ry   r   rz   r   r4   r   )r^   r_   r`   ra   r   rh   rb   ri   rj   Ústaticmethodr$   Úclassmethodr}   r   r   r   r   r#   Æ   s   
 0ÿr#   )ra   Ú
__future__r   Ústringr.   r   Útypingr   r   Úazure.core.exceptionsr   Úkombu.utils.encodingr   Úkombu.utils.jsonr   r	   Úkombu.utils.objectsr
   r\   r   Úazure.storage.queuer   r    Úazure.identityr   r   Úpunctuationrd   r   r#   r   r   r   r   Ú<module>   s4    4ÿþÿp