a
    xd                     @   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y   dZY n0 dZee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)AzureBlockBlobBackendc                       s\   e Zd ZdZd fdd	ZedddZedd	 Zd
d Z	dd Z
dd Z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}| || _|pJ|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 )super__init__azurestorage__version__r   Zappconf
_parse_url_connection_string_container_nameget	base_path)selfurlZcontainer_nameargskwargsr   	__class__ V/var/www/html/Ranjet/env/lib/python3.9/site-packages/celery/backends/azureblockblob.pyr      s    zAzureBlockBlobBackend.__init__azureblockblob://c                 C   s    |t |d  }|std|S )NzInvalid URL)lenr   )clsr   prefixZconnection_stringr   r   r   r   0   s    z AzureBlockBlobBackend._parse_urlc                 C   s\   t | j}z |j| jd d| j d}W n  tyL   d| j d}Y n0 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.

        )namezContainer created with name .zContainer with name z* already.exists. This will not be created.)r
   Zfrom_connection_stringr   Zcreate_containerr   r   LOGGERinfo)r   clientmsgr   r   r   _blob_service_client8   s    
z*AzureBlockBlobBackend._blob_service_clientc                 C   s`   t |}td| j| | jj| j| j | d}z|  	 W S  t
yZ   Y dS 0 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   L   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%   r-   r   r)   r.   r   Zupload_blob)r   r2   valuer3   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   ).0r2   r   r   r   
<listcomp>x       z.AzureBlockBlobBackend.mget.<locals>.<listcomp>r   )r   keysr   r9   r   mgetq   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 r4   r*   N)r   r%   r-   r   r)   r.   r   Zdelete_blobr1   r   r   r   deletez   s    zAzureBlockBlobBackend.delete)NN)r   )__name__
__module____qualname____doc__r   classmethodr   r   r)   r   r7   r=   r>   __classcell__r   r   r   r   r      s     
	r   )rB   Zkombu.utilsr   Zkombu.utils.encodingr   Zcelery.exceptionsr   Zcelery.utils.logr   baser   Zazure.storage.blobZstorager,   r   Zazure.core.exceptionsr   r	   r
   ImportError__all__r?   r%   r   r   r   r   r   <module>   s   
