a
    ތxdV                     @   s   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 z d dlmZ d d	lmZmZ W n ey   ed
Y n0 eG dd deZG dd de	ZdS )    N)urljoin)settings)ImproperlyConfigured)File)Storage)deconstructible)
get_driver)ObjectDoesNotExistErrorProviderzCould not load libcloudc                   @   sv   e Zd ZdZdddZdd Zdd Zd	d
 Zdd Zdd Z	dddZ
dd Zdd ZdddZdd Zdd ZdS ) LibCloudStoragezXDjango storage derived class using apache libcloud to operate
    on supported providersNc           	   
   C   s"  |d u rt tdd}tj|| _| js4td| i }d| jv rP| jd |d< d| jv rh| jd |d< zj| jd }t|tr|dd\}}|d	krt	d
t t
|}t|}|| jd | jd fi || _W n> ty } z$td| jd|f W Y d }~n
d }~0 0 | jd | _d S )NZ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 %s: %sbucket)getattrr   ZLIBCLOUD_PROVIDERSgetproviderr   
isinstancestrrsplit
ValueErrorr
   r   driver	Exceptionr   )	selfZprovider_nameoptionextra_kwargsprovider_typemodule_pathtagZDrivere r%   Y/var/www/html/Ranjet/env/lib/python3.9/site-packages/storages/backends/apache_libcloud.py__init__   sB    




zLibCloudStorage.__init__c                 C   s   | j | jS )z0Helper to get bucket object (libcloud container))r   Zget_containerr   r   r%   r%   r&   _get_bucket;   s    zLibCloudStorage._get_bucketc                 C   s   t j|ddS )z Clean name (windows directories)\/)ospathnormpathreplace)r   namer%   r%   r&   _clean_name?   s    zLibCloudStorage._clean_namec                 C   s6   |  |}z| j| j|W S  ty0   Y dS 0 dS )z7Get object by its name. Return None if object not foundN)r1   r   Z
get_objectr   r	   )r   r0   Z
clean_namer%   r%   r&   _get_objectC   s
    
zLibCloudStorage._get_objectc                 C   s&   |  |}|r| j|S tddS )zDelete object on remotez Object to delete does not existsN)r2   r   Zdelete_objectr   r   r0   objr%   r%   r&   deleteK   s    
zLibCloudStorage.deletec                 C   s   |  |}t|S N)r2   boolr3   r%   r%   r&   existsS   s    
zLibCloudStorage.existsr+   c           	      C   s  |   }| j|}| |}|ds0d| }g }g }|D ]}|dkr|jddkrf||j n8|jddkr|jd|jd }||vr|| q<|j	|r<|jd|dkr<|jdr|jdd }|t
|d }|| q<|jt
|d }|| q<||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.
        r+   z%s/r   r   Nz	_$folder$i)r)   r   Zlist_container_objectsr1   endswithr0   countappendindex
startswithlen)	r   r-   	containerobjectsfilesdirsodir_namer0   r%   r%   r&   listdirW   s0    

zLibCloudStorage.listdirc                 C   s   |  |}|r|jS dS )N)r2   sizer3   r%   r%   r&   rG   y   s    
zLibCloudStorage.sizec              
   C   s   | j d  }| |}|s d S z| j|}W n ty } zd| j|j}d|v rrd| jj	j
 }t||}nNd|v rtd|}n:d|v rd| j d	  }t||}nd
|v rtd|}n|W Y d }~n
d }~0 0 |S )Nr   z{}/{}Zs3z
https://%sZgooglezhttps://storage.googleapis.comZazurez https://%s.blob.core.windows.netr   Z	backblazezapi.backblaze.com/b2api/v1/)r   lowerr2   r   Zget_object_cdn_urlNotImplementedErrorformatr   r0   
connectionhostr   )r   r0   r!   r4   urlr$   Zobject_pathbase_urlr%   r%   r&   rM   }   s,    
zLibCloudStorage.urlrbc                 C   s   t || |d}|S )N)mode)LibCloudFile)r   r0   rP   Zremote_filer%   r%   r&   _open   s    zLibCloudStorage._openc                 C   s   |  |}t| j||jS r6   )r2   nextr   Zdownload_object_as_streamrG   r3   r%   r%   r&   _read   s    
zLibCloudStorage._readc                 C   s   | j t||  | |S r6   )r   Zupload_object_via_streamiterr)   )r   r0   filer%   r%   r&   _save   s    zLibCloudStorage._save)NN)r+   )rO   )__name__
__module____qualname____doc__r'   r)   r1   r2   r5   r8   rE   rG   rM   rR   rT   rW   r%   r%   r%   r&   r      s   
"
"
r   c                   @   sX   e Zd ZdZdd Zdd Zdd ZeeeZedd	 Z	dddZ
dd Zdd Zd
S )rQ   z@File inherited class for libcloud storage objects read and writec                 C   s"   || _ || _|| _d| _d | _d S )NF)r0   _storage_mode	_is_dirty_file)r   r0   ZstoragerP   r%   r%   r&   r'      s
    zLibCloudFile.__init__c                 C   s*   | j d u r$| j| j}t|| _ | j S r6   )r_   r\   rT   r0   ioBytesIO)r   datar%   r%   r&   	_get_file   s    
zLibCloudFile._get_filec                 C   s
   || _ d S r6   )r_   )r   valuer%   r%   r&   	_set_file   s    zLibCloudFile._set_filec                 C   s    t | ds| j| j| _| jS )N_size)hasattrr\   rG   r0   rf   r(   r%   r%   r&   rG      s    
zLibCloudFile.sizeNc                 C   s   | j |S r6   )rV   read)r   	num_bytesr%   r%   r&   rh      s    zLibCloudFile.readc                 C   s(   d| j vrtdt|| _d| _d S )Nwz%File was opened for read-only access.T)r]   AttributeErrorr`   ra   rV   r^   )r   contentr%   r%   r&   write   s    
zLibCloudFile.writec                 C   s&   | j r| j| j| j | j  d S r6   )r^   r\   rW   r0   rV   closer(   r%   r%   r&   rn      s    zLibCloudFile.close)N)rX   rY   rZ   r[   r'   rc   re   propertyrV   rG   rh   rm   rn   r%   r%   r%   r&   rQ      s   


rQ   )r`   r,   urllib.parser   Zdjango.confr   Zdjango.core.exceptionsr   Zdjango.core.files.baser   Zdjango.core.files.storager   Zdjango.utils.deconstructr   Zlibcloud.storage.providersr   Zlibcloud.storage.typesr	   r
   ImportErrorr   rQ   r%   r%   r%   r&   <module>   s     