U
    d0                     @   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
 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.    )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 )
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-9cz4377o/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   5   s    z AzureBlockBlobBackend._parse_urlc                 C   sh   t j| j| j| jd}z |j| jd d| j d}W n" tk
rX   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connection_timeoutread_timeout)namezContainer created with name .zContainer with name z* already.exists. This will not be created.)
r
   Zfrom_connection_stringr   r   r   Zcreate_containerr   r   LOGGERinfo)r   clientmsgr!   r!   r"   _blob_service_client=   s    
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   r-   get_blob_clientr   Zdownload_blobreadalldecoder	   r   keyblob_clientr!   r!   r"   r   U   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 /r.   T)	overwriteN)r   r)   r1   r   r-   r2   r   Zupload_blob)r   r6   valuer7   r!   r!   r"   seth   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   ).0r6   r   r!   r"   
<listcomp>   s     z.AzureBlockBlobBackend.mget.<locals>.<listcomp>r!   )r   keysr!   r=   r"   mgetz   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 r8   r.   N)r   r)   r1   r   r-   r2   r   Zdelete_blobr5   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"   r>      s   z0AzureBlockBlobBackend.as_uri.<locals>.<listcomp>)"AZURE_BLOCK_BLOB_CONNECTION_PREFIXr   splitjoin)r   Zinclude_passwordZconnection_string_partsZ redacted_connection_string_partsr!   rE   r"   as_uri   s    
zAzureBlockBlobBackend.as_uri)NN)F)__name__
__module____qualname____doc__r   classmethodrF   r   r   r-   r   r;   r@   rA   rI   __classcell__r!   r!   r   r"   r      s     
	r   )rM   Zkombu.utilsr   Zkombu.utils.encodingr   Zcelery.exceptionsr   Zcelery.utils.logr   baser   Zazure.storage.blobZstorager0   r   Zazure.core.exceptionsr   r	   r
   ImportError__all__rJ   r)   rF   r   r!   r!   r!   r"   <module>   s   
