o
    Df                     @   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_timeoutx   )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__ W/home/ubuntu/webapp/venv/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 )zReturn 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 )zStore 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"    