
    h                         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 	 dd	lmc mZ dd
lmZmZ ddlmZ dZ ee      ZdZ G d de      Zy	# e$ r d	ZY !w xY w)z0The Azure Storage Block Blob backend for Celery.    )	Transport)cached_property)bytes_to_str)ImproperlyConfigured)
get_logger   )KeyValueStoreBackendN)ResourceExistsErrorResourceNotFoundError)BlobServiceClient)AzureBlockBlobBackendzazureblockblob://c                   l     e Zd ZdZ	 	 d
 fd	Zeefd       Zed        Z	d Z
d Zd Zd Zdd	Z xZS )r   z,Azure Storage Block Blob backend for Celery.c                 j   t        |   |i | t        t        j                  dk  rt	        d      | j
                  j                  }| j                  |      | _        |xs |d   | _	        |j                  dd      | _        |j                  dd      | _        |j                  d	d
      | _        y)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__s         Z/var/www/Befach/backend/env/lib/python3.12/site-packages/celery/backends/azureblockblob.pyr   zAzureBlockBlobBackend.__init__   s     	$)&)<#;#;d#B&12 2 xx}}"&//#"6  201 	 "<bA#'88/$
  "XX&CSI    c                 <    |t        |      d  }|st        d      |S )NzInvalid URL)lenr   )clsr&   prefixconnection_strings       r+   r   z AzureBlockBlobBackend._parse_url<   s'    F- &}55  r,   c                    d| j                   v sd| j                   v rFt        j                  | j                         \  }}t        ||| j                  | j
                        }n6t        j                  | j                   | j                  | j
                        }	 |j                  | j                         d| j                   d}t        j                  |       |S # t        $ r d| j                   d	}Y 2w xY w)
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)r7   r8   )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msgs        r+   _blob_service_clientz*AzureBlockBlobBackend._blob_service_clientD   s     %(?(??'4+B+BB  ;DD'' K '&#'#;#;!//	F '==''#'#;#;!//F	4##)=)=#>01E1E0FaHC 	C # 	4()=)=(> ?4 4C	4s   ,C C98C9c                 <   t        |      }t        j                  d| j                  |       | j                  j                  | j                  | j                   |       }	 |j                         j                         j                         S # t        $ r Y yw xY 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    rD   get_blob_clientr"   download_blobreadalldecoder   r%   keyblob_clients      r+   r!   zAzureBlockBlobBackend.getk   s     35t7K7KSQ//??**NN#C5) @ 

	,,.668??AA$ 		s   #+B 	BBc                     t        |      }t        j                  d| j                   d|        | j                  j                  | j                  | j                   |       }|j                  |d       y)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 /rF   T)	overwriteN)r   r?   rI   r    rD   rJ   r"   upload_blob)r%   rO   valuerP   s       r+   setzAzureBlockBlobBackend.set~   sz     3.t/C/C.DAcUKL//??**NN#C5) @ 

 	6r,   c                 J    |D cg c]  }| j                  |       c}S c c}w )zqRead all the values for the provided keys.

        Args:
              keys: The list of keys to read.

        )r!   )r%   keysrO   s      r+   mgetzAzureBlockBlobBackend.mget   s      *..#...s    c                     t        |      }t        j                  d| j                   d|        | j                  j                  | j                  | j                   |       }|j                          y)zlDelete the value at a given key.

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

        zDeleting azure blob at rR   rF   N)r   r?   rI   r    rD   rJ   r"   delete_blobrN   s      r+   deletezAzureBlockBlobBackend.delete   ss     3.t/C/C.DAcUKL//??**NN#C5) @ 

 	!r,   c                     |rt          | j                   S | j                  j                  d      }d}|D cg c]  }|j                  |      r| dn| }}t          dj	                  |       S c c}w )N;zAccountKey=z**)"AZURE_BLOCK_BLOB_CONNECTION_PREFIXr   split
startswithjoin)r%   include_passwordconnection_string_partsaccount_key_prefixpart redacted_connection_string_partss         r+   as_urizAzureBlockBlobBackend.as_uri   s    56**+-
 #'"9"9"?"?"D* 0,
  *.9K)L!""%,
( ,
 22xx89:<	
,
s   A2)NN)F)__name__
__module____qualname____doc__r   classmethodr_   r   r   rD   r!   rV   rY   r\   rh   __classcell__)r*   s   @r+   r   r      sZ    6  $JB $F ! ! $ $L&7$/""
r,   r   )rl   "kombu.transport.azurestoragequeuesr   r;   kombu.utilsr   kombu.utils.encodingr   celery.exceptionsr   celery.utils.logr   baser	   azure.storage.blobstoragerH   r   azure.core.exceptionsr
   r   r   ImportError__all__ri   r?   r_   r    r,   r+   <module>r{      sg    6 W ' - 2 ' &--P4 %	H	%8 "d
0 d
  Ls   A A! A!