o
    wvXjÍ  ã                   @   sÈ   d Z ddlmZ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 zdd	lmZ dd
lmZ ddlmZ ddlmZ W n eyS   d	 Z Z ZZY nw dZeeƒZG dd„ deƒZd	S )z0The Azure Storage Block Blob backend for Celery.é    )Úabsolute_importÚunicode_literals)Úcached_property)Úbytes_to_str)ÚImproperlyConfigured)Ú
get_loggeré   )ÚKeyValueStoreBackendN)ÚAzureMissingResourceHttpError)ÚBlockBlobService)ÚExponentialRetry)ÚAzureBlockBlobBackendc                       sf   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           	         sv   t t| ƒj|i |¤Ž td u rtdƒ‚| jj}|  |¡| _|p"|d | _	|p)|d | _
|p0|d | _|p7|d | _d S )NzOYou need to install the azure-storage library to use the AzureBlockBlob backendÚazureblockblob_container_nameÚ(azureblockblob_retry_initial_backoff_secÚ#azureblockblob_retry_increment_baseÚ!azureblockblob_retry_max_attempts)Úsuperr   Ú__init__Úazurestorager   ÚappÚconfÚ
_parse_urlÚ_connection_stringÚ_container_nameÚ_retry_initial_backoff_secÚ_retry_increment_baseÚ_retry_max_attempts)	ÚselfÚurlÚcontainer_nameÚretry_initial_backoff_secÚretry_increment_baseÚretry_max_attemptsÚargsÚkwargsr   ©Ú	__class__© ú[/var/www/html/myproject/venv/lib/python3.10/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   ÚprefixÚconnection_stringr'   r'   r(   r   @   s   z AzureBlockBlobBackend._parse_urlc                 C   sJ   t | jd�}|j| jdd�}|rt d| j¡ t| j| j| j	d�j
|_
|S )z¾Return the Azure Storage Block Blob service.

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

        )r-   F)r   Úfail_on_existz'Created Azure Blob Storage container %s)Úinitial_backoffÚincrement_baseÚmax_attempts)r   r   Úcreate_containerr   ÚLOGGERÚinfor   r   r   r   Úretry)r   ÚclientÚcreatedr'   r'   r(   Ú_clientH   s    ÿÿýýzAzureBlockBlobBackend._clientc                 C   sB   t |ƒ}t d| j|¡ z
| j | j|¡jW S  ty    Y dS w )zxRead the value stored at the given key.

        Args:
              key: The key for which to read the value.

        zGetting Azure Block Blob %s/%sN)r   r3   Údebugr   r8   Úget_blob_to_textÚcontentr
   ©r   Úkeyr'   r'   r(   Úget`   s   ÿÿÿÿzAzureBlockBlobBackend.getc                 C   s*   t |ƒ}t d| j|¡ | j | j||¡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 Block Blob at %s/%s)r   r3   r9   r   r8   Úcreate_blob_from_text)r   r=   ÚvalueÚstater'   r'   r(   Úsetq   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'   rD   r(   Úmget€   s   zAzureBlockBlobBackend.mgetc                 C   s,   t |ƒ}t d| j|¡ | j | j|¡ dS )zlDelete the value at a given key.

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

        z"Deleting Azure Block Blob at %s/%sN)r   r3   r9   r   r8   Údelete_blobr<   r'   r'   r(   Údelete‰   s
   ÿzAzureBlockBlobBackend.delete)NNNNN)r)   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   Úclassmethodr   r   r8   r>   rB   rG   rI   Ú__classcell__r'   r'   r%   r(   r      s     û#
	r   )rM   Ú
__future__r   r   Úkombu.utilsr   Úkombu.utils.encodingr   Úcelery.exceptionsr   Úcelery.utils.logr   Úbaser	   Úazure.storageÚstorager   Úazure.commonr
   Úazure.storage.blobr   Úazure.storage.common.retryr   ÚImportErrorÚ__all__rJ   r3   r   r'   r'   r'   r(   Ú<module>   s(    ÿþ