
    ]jV                         d dl Z d dl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Z d d	lmZmZ e G d de             Z G d de	      Zy# e$ r	  ed
      w xY w)    N)urljoin)settings)ImproperlyConfigured)File)Storage)deconstructible)
get_driver)ObjectDoesNotExistErrorProviderzCould not load libcloudc                   ^    e Zd ZdZddZd Zd Zd Zd Zd Z	dd	Z
d
 Zd ZddZd Zd Zy)LibCloudStoragezXDjango storage derived class using apache libcloud to operate
    on supported providersNc                    |t        t        dd      }t        j                  j                  |      | _        | j                  st        d|z        i }d| j                  v r| j                  d   |d<   d| j                  v r| j                  d   |d<   	 | j                  d   }t        |t              r5|j                  dd      \  }}|d	k7  rt        d
      t        t        |      }t        |      } || j                  d   | j                  d   fi || _        | j                  d   | _        y # t        $ r/}t        d| j                  j                  d      d|      d }~ww xY w)NDEFAULT_LIBCLOUD_PROVIDERdefaultz,LIBCLOUD_PROVIDERS %s not defined or invalidregionprojecttype.   zlibcloud.storage.types.ProviderzInvalid module pathuserkeyz&Unable to create libcloud driver type z: bucket)getattrr   LIBCLOUD_PROVIDERSgetproviderr   
isinstancestrrsplit
ValueErrorr   r	   driver	Exceptionr   )	selfprovider_nameoptionextra_kwargsprovider_typemodule_pathtagDriveres	            K/root/env/lib/python3.12/site-packages/storages/backends/apache_libcloud.py__init__zLibCloudStorage.__init__   sg    #H.I9UM 3377F}}&>NP Pt}}$%)]]8%<L"%&*mmI&>L#	0 MM&1M--#0#7#7Q#? S"CC$%:;; '# 6.F f%e$ DK mmH-	  	0&""6*A/0 0	0s   BD2 2	E*;*E%%E*c                 L    | j                   j                  | j                        S )z0Helper to get bucket object (libcloud container))r!   get_containerr   r#   s    r,   _get_bucketzLibCloudStorage._get_bucket;   s    {{((55    c                 `    t         j                  j                  |      j                  dd      S )z Clean name (windows directories)\/)ospathnormpathreplace)r#   names     r,   _clean_namezLibCloudStorage._clean_name?   s$    ww%--dC88r2   c                     | j                  |      }	 | j                  j                  | j                  |      S # t        $ r Y yw xY w)z7Get object by its name. Return None if object not foundN)r;   r!   
get_objectr   r
   )r#   r:   
clean_names      r,   _get_objectzLibCloudStorage._get_objectC   sD    %%d+
	;;))$++zBB& 		s   %9 	AAc                 t    | j                  |      }|r| j                  j                  |      S t        d      )zDelete object on remotez Object to delete does not exists)r?   r!   delete_objectr"   r#   r:   objs      r,   deletezLibCloudStorage.deleteK   s6    t$;;,,S11>??r2   c                 :    | j                  |      }t        |      S N)r?   boolrB   s      r,   existszLibCloudStorage.existsS   s    t$Cyr2   c                 ~   | j                         }| j                  j                  |      }| j                  |      }|j	                  d      sd|z  }g }g }|D ]^  }|dk(  r|j
                  j                  d      dk(  r|j                  |j
                         C|j
                  j                  d      dk(  sb|j
                  d|j
                  j                  d       }||vs|j                  |       |j
                  j                  |      s|j
                  j                  d      |j                  d      k  s|j
                  j	                  d      r0|j
                  dd }|t        |      d }|j                  |       6|j
                  t        |      d }|j                  |       a ||fS )zLists the contents of the specified path,
        returning a 2-tuple of lists; the first item being
        directories, the second item being files.
        r5   z%s/r   r   Nz	_$folder$i)r1   r!   list_container_objectsr;   endswithr:   countappendindex
startswithlen)	r#   r7   	containerobjectsfilesdirsodir_namer:   s	            r,   listdirzLibCloudStorage.listdirW   so   
 $$&	++44Y?%}}S!4<D  	+As{66<<$)LL(VV\\#&!+ vv&8qvv||C'89Ht+H-""4(66<<$

37vv{3 vvcr{#CIJ/D) vvc$ij1T*#	+$ e}r2   c                 D    | j                  |      }|r|j                  S dS )N)r?   sizerB   s      r,   rZ   zLibCloudStorage.sizey   s#    t$sxx&B&r2   c                    | j                   d   j                         }| j                  |      }|sy 	 | j                  j	                  |      }|S # t
        $ r}dj                  | j                  |j                        }d|v r0d| j                  j                  j                  z  }t        ||      }nGd|v rt        d|      }n6d|v rd| j                   d	   z  }t        ||      }nd
|v rt        d|      }n|Y d }~|S d }~ww xY w)Nr   z{}/{}s3z
https://%sgooglezhttps://storage.googleapis.comazurez https://%s.blob.core.windows.netr   	backblazezapi.backblaze.com/b2api/v1/)r   lowerr?   r!   get_object_cdn_urlNotImplementedErrorformatr   r:   
connectionhostr   )r#   r:   r'   rC   urlr+   object_pathbase_urls           r,   rf   zLibCloudStorage.url}   s   f-335t$	++005C  
 # 	!..chh?K}$'$++*@*@*E*EEh4]*>LM)> MM&12h4-;[I
	s   A 	DB!D  Dc                 "    t        || |      }|S )N)mode)LibCloudFile)r#   r:   rj   remote_files       r,   _openzLibCloudStorage._open   s    "4D9r2   c                     | j                  |      }t        | j                  j                  ||j                              S rF   )r?   nextr!   download_object_as_streamrZ   rB   s      r,   _readzLibCloudStorage._read   s2    t$DKK99#sxxHIIr2   c                 n    | j                   j                  t        |      | j                         |       |S rF   )r!   upload_object_via_streamiterr1   )r#   r:   files      r,   _savezLibCloudStorage._save   s*    ,,T$Z9I9I9KTRr2   )NN)r5   )rb)__name__
__module____qualname____doc__r-   r1   r;   r?   rD   rH   rW   rZ   rf   rm   rq   rv    r2   r,   r   r      sG     .D69@ D'0J
r2   r   c                   X    e Zd ZdZd Zd Zd Z eee      Zed        Z	d
dZ
d Zd	 Zy)rk   z@File inherited class for libcloud storage objects read and writec                 J    || _         || _        || _        d| _        d | _        y )NF)r:   _storage_mode	_is_dirty_file)r#   r:   storagerj   s       r,   r-   zLibCloudFile.__init__   s%    	

r2   c                     | j                   ?| j                  j                  | j                        }t	        j
                  |      | _         | j                   S rF   )r   r   rq   r:   ioBytesIO)r#   datas     r,   	_get_filezLibCloudFile._get_file   s>    ::==&&tyy1DD)DJzzr2   c                     || _         y rF   )r   )r#   values     r,   	_set_filezLibCloudFile._set_file   s	    
r2   c                     t        | d      s*| j                  j                  | j                        | _        | j                  S )N_size)hasattrr   rZ   r:   r   r0   s    r,   rZ   zLibCloudFile.size   s0    tW%++DII6DJzzr2   Nc                 8    | j                   j                  |      S rF   )ru   read)r#   	num_bytess     r,   r   zLibCloudFile.read   s    yy~~i((r2   c                 x    d| j                   vrt        d      t        j                  |      | _        d| _        y )Nwz%File was opened for read-only access.T)r   AttributeErrorr   r   ru   r   )r#   contents     r,   writezLibCloudFile.write   s1    djj  !HIIJJw'	r2   c                     | j                   r0| j                  j                  | j                  | j                         | j                  j                          y rF   )r   r   rv   r:   ru   closer0   s    r,   r   zLibCloudFile.close   s3    >>MM		4995		r2   rF   )rx   ry   rz   r{   r-   r   r   propertyru   rZ   r   r   r   r|   r2   r,   rk   rk      sC    J Iy)D 
)r2   rk   )r   r6   urllib.parser   django.confr   django.core.exceptionsr   django.core.files.baser   django.core.files.storager   django.utils.deconstructr   libcloud.storage.providersr	   libcloud.storage.typesr
   r   ImportErrorr   rk   r|   r2   r,   <module>r      ss    
 	     7 ' - 4:5H
 Jg J JZ&4 &e  :
8
99:s   A A&