o
    ›¨Êh  ã                   @   s®   d 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
 zddlm  mZ dd	lmZmZ dd
lmZ W n eyD   dZY nw dZeeƒZdZG dd„ de
ƒZdS )z0The Azure Storage Block Blob backend for Celery.é    )Úcached_property)Úbytes_to_str)ÚImproperlyConfigured)Ú
get_loggeré   )ÚKeyValueStoreBackendN)ÚResourceExistsErrorÚResourceNotFoundError)ÚBlobServiceClient)ÚAzureBlockBlobBackendzazureblockblob://c                       sl   e Zd ZdZ		d‡ fdd„	Zeefdd„ƒZedd„ ƒZ	d	d
„ Z
dd„ Zdd„ Zdd„ Zddd„Z‡  ZS )r   z,Azure Storage Block Blob backend for Celery.Nc                    s|   t ƒ j|i |¤Ž td u stjdk rtdƒ‚| jj}|  |¡| _|p%|d | _	| 
dd¡| _| 
dd¡| _| 
dd	¡| _d S )
NÚ12zWYou need to install the azure-storage-blob v12 library touse the AzureBlockBlob backendÚazureblockblob_container_nameÚazureblockblob_base_pathÚ Ú!azureblockblob_connection_timeouté   Úazureblockblob_read_timeoutéx   )ÚsuperÚ__init__ÚazurestorageÚ__version__r   ÚappÚconfÚ
_parse_urlÚ_connection_stringÚ_container_nameÚgetÚ	base_pathÚ_connection_timeoutÚ_read_timeout)ÚselfÚurlÚcontainer_nameÚargsÚkwargsr   ©Ú	__class__© úP/var/www/html/env/lib/python3.10/site-packages/celery/backends/azureblockblob.pyr      s   ÿþÿzAzureBlockBlobBackend.__init__c                 C   s    |t |ƒd … }|stdƒ‚|S )NzInvalid URL)Úlenr   )Úclsr"   ÚprefixÚconnection_stringr(   r(   r)   r   4   s   z AzureBlockBlobBackend._parse_urlc                 C   sf   t j| j| j| jd�}z|j| jd� d| j› d�}W n ty+   d| j› d�}Y nw t 	|¡ |S )z¿Return the Azure Storage Blob service client.

        If this is the first call to the property, the client is created and
        the container is created if it doesn't yet exist.

        )Úconnection_timeoutÚread_timeout)ÚnamezContainer created with name Ú.zContainer with name z* already.exists. This will not be created.)
r
   Úfrom_connection_stringr   r   r    Úcreate_containerr   r   ÚLOGGERÚinfo)r!   ÚclientÚmsgr(   r(   r)   Ú_blob_service_client<   s   ýÿ
z*AzureBlockBlobBackend._blob_service_clientc                 C   s\   t |ƒ}t d| j|¡ | jj| j| j› |› �d�}z	| ¡  ¡  	¡ W S  t
y-   Y dS w )zwRead the value stored at the given key.

        Args:
              key: The key for which to read the value.
        zGetting Azure Block Blob %s/%s©Ú	containerÚblobN)r   r4   Údebugr   r8   Úget_blob_clientr   Údownload_blobÚreadallÚdecoder	   ©r!   ÚkeyÚblob_clientr(   r(   r)   r   T   s   þÿzAzureBlockBlobBackend.getc                 C   sN   t |ƒ}t d| j› d|› �¡ | jj| j| j› |› �d�}|j|dd� dS )z˜Store a value for a given key.

        Args:
              key: The key at which to store the value.
              value: The value to store.

        zCreating azure blob at ú/r9   T)Ú	overwriteN)r   r4   r<   r   r8   r=   r   Úupload_blob)r!   rB   ÚvaluerC   r(   r(   r)   Úsetg   s   þzAzureBlockBlobBackend.setc                    s   ‡ fdd„|D ƒS )zqRead all the values for the provided keys.

        Args:
              keys: The list of keys to read.

        c                    s   g | ]}ˆ   |¡‘qS r(   )r   )Ú.0rB   ©r!   r(   r)   Ú
<listcomp>€   s    z.AzureBlockBlobBackend.mget.<locals>.<listcomp>r(   )r!   Úkeysr(   rJ   r)   Úmgety   s   zAzureBlockBlobBackend.mgetc                 C   sH   t |ƒ}t d| j› d|› �¡ | jj| j| j› |› �d�}| ¡  dS )zlDelete the value at a given key.

        Args:
              key: The key of the value to delete.

        zDeleting azure blob at rD   r9   N)r   r4   r<   r   r8   r=   r   Údelete_blobrA   r(   r(   r)   Údelete‚   s   þzAzureBlockBlobBackend.deleteFc                    sF   |r	t › | j› �S | j d¡}d‰ ‡ fdd„|D ƒ}t › d |¡› �S )Nú;zAccountKey=c                    s$   g | ]}|  ˆ ¡rˆ › d �n|‘qS )z**)Ú
startswith)rI   Úpart©Úaccount_key_prefixr(   r)   rK   œ   s
    þþz0AzureBlockBlobBackend.as_uri.<locals>.<listcomp>)Ú"AZURE_BLOCK_BLOB_CONNECTION_PREFIXr   ÚsplitÚjoin)r!   Úinclude_passwordÚconnection_string_partsÚ redacted_connection_string_partsr(   rS   r)   Úas_uri“   s   ÿÿ
ýÿÿzAzureBlockBlobBackend.as_uri)NN)F)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   ÚclassmethodrU   r   r   r8   r   rH   rM   rO   r[   Ú__classcell__r(   r(   r&   r)   r      s    þ
	r   )r_   Úkombu.utilsr   Úkombu.utils.encodingr   Úcelery.exceptionsr   Úcelery.utils.logr   Úbaser   Úazure.storage.blobÚstorager;   r   Úazure.core.exceptionsr   r	   r
   ÚImportErrorÚ__all__r\   r4   rU   r   r(   r(   r(   r)   Ú<module>   s"    ÿ