o
    h                     @   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 zdd	lm  mZ dd
lmZmZ ddlmZ W n eyJ   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.    )	Transport)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 )z
        Supported URL formats:

        azureblockblob://CONNECTION_STRING
        azureblockblob://DefaultAzureCredential@STORAGE_ACCOUNT_URL
        azureblockblob://ManagedIdentityCredential@STORAGE_ACCOUNT_URL
        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__ `/var/www/html/optinet_system/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   <   s   z AzureBlockBlobBackend._parse_urlc                 C   s   d| j v s
d| j v rt| j \}}t||| j| jd}ntj| j | j| jd}z|j| jd d| j d}W n t	yH   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.

        DefaultAzureCredentialManagedIdentityCredential)account_url
credentialconnection_timeoutread_timeout)r3   r4   )namezContainer created with name .zContainer with name z* already.exists. This will not be created.)r   AzureStorageQueuesTransport	parse_urir   r    r!   from_connection_stringcreate_containerr   r	   LOGGERinfo)r"   credential_r#   clientmsgr)   r)   r*   _blob_service_clientD   s0   
	

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   r;   debugr   r@   get_blob_clientr   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 /rA   T)	overwriteN)r   r;   rD   r   r@   rE   r   upload_blob)r"   rJ   valuerK   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   ).0rJ   r"   r)   r*   
<listcomp>   s    z.AzureBlockBlobBackend.mget.<locals>.<listcomp>r)   )r"   keysr)   rR   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 rL   rA   N)r   r;   rD   r   r@   rE   r   delete_blobrI   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)rQ   partaccount_key_prefixr)   r*   rS      s
    z0AzureBlockBlobBackend.as_uri.<locals>.<listcomp>)"AZURE_BLOCK_BLOB_CONNECTION_PREFIXr   splitjoin)r"   include_passwordconnection_string_parts redacted_connection_string_partsr)   r[   r*   as_uri   s   
zAzureBlockBlobBackend.as_uri)NN)F)__name__
__module____qualname____doc__r   classmethodr]   r   r   r@   r   rP   rU   rW   rc   __classcell__r)   r)   r'   r*   r      s    !
&	r   )rg   "kombu.transport.azurestoragequeuesr   r7   kombu.utilsr   kombu.utils.encodingr   celery.exceptionsr   celery.utils.logr   baser   azure.storage.blobstoragerC   r   azure.core.exceptionsr	   r
   r   ImportError__all__rd   r;   r]   r   r)   r)   r)   r*   <module>   s$    