U
    bh                     @   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 ddlmZ z2dd	lm  mZ dd
lmZmZ ddlmZ W n ek
r   d	ZY nX dZeeZdZG dd deZd	S )z0The Azure Storage Block Blob backend for Celery.    )	Transport)cached_property)bytes_to_str)ImproperlyConfigured)
get_logger   )KeyValueStoreBackendN)ResourceExistsErrorResourceNotFoundError)BlobServiceClient)AzureBlockBlobBackendzazureblockblob://c                       sh   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                    sx   t  j|| tdks tjdk r(td| jj}| || _|pF|d | _	|
dd| _|
dd| _|
d	d
| _dS )z
        Supported URL formats:

        azureblockblob://CONNECTION_STRING
        azureblockblob://DefaultAzureCredential@STORAGE_ACCOUNT_URL
        azureblockblob://ManagedIdentityCredential@STORAGE_ACCOUNT_URL
        NZ12zWYou need to install the azure-storage-blob v12 library touse the AzureBlockBlob backendZazureblockblob_container_nameZazureblockblob_base_path Z!azureblockblob_connection_timeout   Zazureblockblob_read_timeoutx   )super__init__azurestorage__version__r   Zappconf
_parse_url_connection_string_container_nameget	base_path_connection_timeout_read_timeout)selfurlZcontainer_nameargskwargsr   	__class__ B/tmp/pip-unpacked-wheel-kcem4wq5/celery/backends/azureblockblob.pyr      s      zAzureBlockBlobBackend.__init__c                 C   s    |t |d  }|std|S )NzInvalid URL)lenr   )clsr   prefixZconnection_stringr"   r"   r#   r   <   s    z AzureBlockBlobBackend._parse_urlc                 C   s   d| j ksd| j kr:t| j \}}t||| j| jd}ntj| j | j| jd}z |j| jd d| j d}W n" t	k
r   d| j d	}Y nX 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.

        ZDefaultAzureCredentialZManagedIdentityCredential)Zaccount_urlZ
credentialconnection_timeoutread_timeout)r'   r(   )namezContainer created with name .zContainer with name z* already.exists. This will not be created.)r   AzureStorageQueuesTransport	parse_urir   r   r   Zfrom_connection_stringZcreate_containerr   r	   LOGGERinfo)r   Zcredential_r   clientmsgr"   r"   r#   _blob_service_clientD   s2    	
z*AzureBlockBlobBackend._blob_service_clientc                 C   sb   t |}td| j| | jj| j| j | d}z|  	 W S  t
k
r\   Y dS X dS )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   r-   debugr   r1   get_blob_clientr   Zdownload_blobreadalldecoder
   r   keyblob_clientr"   r"   r#   r   k   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 /r2   T)	overwriteN)r   r-   r5   r   r1   r6   r   Zupload_blob)r   r:   valuer;   r"   r"   r#   set~   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   ).0r:   r   r"   r#   
<listcomp>   s     z.AzureBlockBlobBackend.mget.<locals>.<listcomp>r"   )r   keysr"   rA   r#   mget   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 r<   r2   N)r   r-   r5   r   r1   r6   r   Zdelete_blobr9   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)r@   partZaccount_key_prefixr"   r#   rB      s   z0AzureBlockBlobBackend.as_uri.<locals>.<listcomp>)"AZURE_BLOCK_BLOB_CONNECTION_PREFIXr   splitjoin)r   Zinclude_passwordZconnection_string_partsZ redacted_connection_string_partsr"   rI   r#   as_uri   s    
zAzureBlockBlobBackend.as_uri)NN)F)__name__
__module____qualname____doc__r   classmethodrJ   r   r   r1   r   r?   rD   rE   rM   __classcell__r"   r"   r    r#   r      s     !
&	r   )rQ   Z"kombu.transport.azurestoragequeuesr   r+   Zkombu.utilsr   Zkombu.utils.encodingr   Zcelery.exceptionsr   Zcelery.utils.logr   baser   Zazure.storage.blobZstorager4   r   Zazure.core.exceptionsr	   r
   r   ImportError__all__rN   r-   rJ   r   r"   r"   r"   r#   <module>   s    
