î
ªÍ XÞI  ã               @   s²  d  d l  Z  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	 m
 Z
 d  d l m Z d  d l m Z d  d l m 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 m Z d  d l m Z m Z d  d l  m! Z! d  d l" m# Z# d  d l$ m% Z% d Z& Gd d „  d e' ƒ Z( d d „  Z) e Gd d „  d e( ƒ ƒ Z* d d d „ Z+ Gd d „  d e ƒ Z, e, ƒ  Z- d S)é    N)Údatetime)Úsettings)ÚSuspiciousFileOperation)ÚFileÚlocks)Úfile_move_safe)Úsetting_changed)Útimezone)ÚabspathuÚ	safe_join)Úget_random_string)Údeconstructible)ÚRemovedInDjango20Warning)Úfilepath_to_uriÚ
force_text)Ú
LazyObjectÚcached_property)Úimport_string)Úurljoin)Úget_valid_filenameÚStorageÚFileSystemStorageÚDefaultStorageÚdefault_storagec               @   së   e  Z d  Z d Z d d d „ Z d d d „ Z d d	 „  Z d d
 d „ Z d d „  Z d d „  Z	 d d „  Z
 d d „  Z d d „  Z d d „  Z d d „  Z d d „  Z d d „  Z d d „  Z d  d! „  Z d" d# „  Z d$ d% „  Z d S)&r   zŠ
    A base storage class, providing some default behaviors that all other
    storage systems can inherit or override, as necessary.
    Úrbc             C   s   |  j  | | ƒ S)z<
        Retrieves the specified file from storage.
        )Ú_open)ÚselfÚnameÚmode© r   úG/home/ubuntu/projects/ifolica/build/django/django/core/files/storage.pyÚopen"   s    zStorage.openNc             C   s^   | d k r | j  } n  t | d ƒ s9 t | | ƒ } n  |  j | d | ƒ} |  j | | ƒ S)zÂ
        Saves new content to the file specified by name. The content should be
        a proper File object or any python file-like object, ready to be read
        from the beginning.
        NÚchunksÚ
max_length)r   Úhasattrr   Úget_available_nameÚ_save)r   r   Úcontentr#   r   r   r    Úsave(   s    zStorage.savec             C   s
   t  | ƒ S)zƒ
        Returns a filename, based on the provided filename, that's suitable for
        use in the target storage system.
        )r   )r   r   r   r   r    Úget_valid_name:   s    zStorage.get_valid_namec             C   s  t  j j | ƒ \ } } t  j j | ƒ \ } } xÖ |  j | ƒ sZ | rt | ƒ | k rt  j j | d | t d ƒ | f ƒ } | d k r” q3 n  t | ƒ | } | d k r3 | d | … } | sÚ t d | ƒ ‚ n  t  j j | d | t d ƒ | f ƒ } q3 q3 W| S)z†
        Returns a filename that's free on the target storage system, and
        available for new content to be written to.
        z%s_%s%sé   Nr   z‡Storage can not find an available filename for "%s". Please make sure that the corresponding file field allows sufficient "max_length".)	ÚosÚpathÚsplitÚsplitextÚexistsÚlenÚjoinr   r   )r   r   r#   Zdir_nameÚ	file_nameZ	file_rootZfile_extZ
truncationr   r   r    r%   A   s    *(/zStorage.get_available_namec             C   s@   t  j j | ƒ \ } } t  j j t  j j | |  j | ƒ ƒ ƒ S)z„
        Validate the filename by calling get_valid_name() and return a filename
        to be passed to the save() method.
        )r+   r,   r-   Únormpathr1   r)   )r   ÚfilenameÚdirnamer   r   r    Úgenerate_filename`   s    zStorage.generate_filenamec             C   s   t  d ƒ ‚ d S)zâ
        Returns a local filesystem path where the file can be retrieved using
        Python's built-in open() function. Storage systems that can't be
        accessed using open() should *not* implement this method.
        z,This backend doesn't support absolute paths.N)ÚNotImplementedError)r   r   r   r   r    r,   i   s    zStorage.pathc             C   s   t  d ƒ ‚ d S)zE
        Deletes the specified file from the storage system.
        z4subclasses of Storage must provide a delete() methodN)r7   )r   r   r   r   r    Údeletet   s    zStorage.deletec             C   s   t  d ƒ ‚ d S)z¥
        Returns True if a file referenced by the given name already exists in the
        storage system, or False if the name is available for a new file.
        z5subclasses of Storage must provide an exists() methodN)r7   )r   r   r   r   r    r/   z   s    zStorage.existsc             C   s   t  d ƒ ‚ d S)z 
        Lists the contents of the specified path, returning a 2-tuple of lists;
        the first item being directories, the second item being files.
        z5subclasses of Storage must provide a listdir() methodN)r7   )r   r,   r   r   r    Úlistdir   s    zStorage.listdirc             C   s   t  d ƒ ‚ d S)zR
        Returns the total size, in bytes, of the file specified by name.
        z2subclasses of Storage must provide a size() methodN)r7   )r   r   r   r   r    Úsizeˆ   s    zStorage.sizec             C   s   t  d ƒ ‚ d S)zv
        Returns an absolute URL where the file's contents can be accessed
        directly by a Web browser.
        z1subclasses of Storage must provide a url() methodN)r7   )r   r   r   r   r    ÚurlŽ   s    zStorage.urlc             C   s&   t  j d t d d ƒt d ƒ ‚ d S)z™
        Returns the last accessed time (as datetime object) of the file
        specified by name. Deprecated: use get_accessed_time() instead.
        zFStorage.accessed_time() is deprecated in favor of get_accessed_time().Ú
stacklevelé   z<subclasses of Storage must provide an accessed_time() methodN)ÚwarningsÚwarnr   r7   )r   r   r   r   r    Úaccessed_time•   s
    zStorage.accessed_timec             C   s&   t  j d t d d ƒt d ƒ ‚ d S)z“
        Returns the creation time (as datetime object) of the file
        specified by name. Deprecated: use get_created_time() instead.
        zDStorage.created_time() is deprecated in favor of get_created_time().r<   r=   z:subclasses of Storage must provide a created_time() methodN)r>   r?   r   r7   )r   r   r   r   r    Úcreated_time¡   s
    zStorage.created_timec             C   s&   t  j d t d d ƒt d ƒ ‚ d S)z™
        Returns the last modified time (as datetime object) of the file
        specified by name. Deprecated: use get_modified_time() instead.
        zFStorage.modified_time() is deprecated in favor of get_modified_time().r<   r=   z;subclasses of Storage must provide a modified_time() methodN)r>   r?   r   r7   )r   r   r   r   r    Úmodified_time­   s
    zStorage.modified_timec             C   s/   t  j d t d d ƒ|  j | ƒ } t | ƒ S)zš
        Return the last accessed time (as a datetime) of the file specified by
        name. The datetime will be timezone-aware if USE_TZ=True.
        z]Storage.accessed_time() is deprecated. Storage backends should implement get_accessed_time().r<   r=   )r>   r?   r   r@   Ú_possibly_make_aware)r   r   Údtr   r   r    Úget_accessed_time¹   s    zStorage.get_accessed_timec             C   s/   t  j d t d d ƒ|  j | ƒ } t | ƒ S)z•
        Return the creation time (as a datetime) of the file specified by name.
        The datetime will be timezone-aware if USE_TZ=True.
        z[Storage.created_time() is deprecated. Storage backends should implement get_created_time().r<   r=   )r>   r?   r   rA   rC   )r   r   rD   r   r   r    Úget_created_timeÉ   s    zStorage.get_created_timec             C   s/   t  j d t d d ƒ|  j | ƒ } t | ƒ S)zš
        Return the last modified time (as a datetime) of the file specified by
        name. The datetime will be timezone-aware if USE_TZ=True.
        z]Storage.modified_time() is deprecated. Storage backends should implement get_modified_time().r<   r=   )r>   r?   r   rB   rC   )r   r   rD   r   r   r    Úget_modified_timeÙ   s    zStorage.get_modified_time)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r!   r(   r)   r%   r6   r,   r8   r/   r9   r:   r;   r@   rA   rB   rE   rF   rG   r   r   r   r    r      s$   	c             C   s9   t  j r1 t j ƒ  } t j |  | ƒ j t j ƒ S|  Sd S)za
    Convert a datetime object in the local timezone to aware
    in UTC, if USE_TZ is True.
    N)r   ÚUSE_TZr	   Zget_default_timezoneZ
make_awareÚ
astimezoneÚutc)rD   Útzr   r   r    rC   ê   s    	rC   c               @   sW  e  Z d  Z d Z d d d d d d „ Z d d „  Z d d „  Z e d	 d
 „  ƒ Z e d d „  ƒ Z	 e d d „  ƒ Z
 e d d „  ƒ Z e d d „  ƒ Z d d d „ Z d d „  Z d d „  Z d d „  Z d d „  Z d d „  Z d  d! „  Z d" d# „  Z d$ d% „  Z d& d' „  Z d( d) „  Z d* d+ „  Z d, d- „  Z d. d/ „  Z d0 d1 „  Z d S)2r   z%
    Standard filesystem storage
    Nc             C   s8   | |  _  | |  _ | |  _ | |  _ t j |  j ƒ d  S)N)Ú	_locationÚ	_base_urlÚ_file_permissions_modeÚ_directory_permissions_moder   ÚconnectÚ_clear_cached_properties)r   ÚlocationÚbase_urlÚfile_permissions_modeÚdirectory_permissions_moder   r   r    Ú__init__þ   s
    				zFileSystemStorage.__init__c             K   sŸ   | d k r5 |  j  j d d ƒ |  j  j d d ƒ nf | d k rW |  j  j d d ƒ nD | d k ry |  j  j d d ƒ n" | d	 k r› |  j  j d
 d ƒ n  d S)z$Reset setting based property values.Ú
MEDIA_ROOTÚbase_locationNrV   Ú	MEDIA_URLrW   ÚFILE_UPLOAD_PERMISSIONSrX   Ú!FILE_UPLOAD_DIRECTORY_PERMISSIONSrY   )Ú__dict__Úpop)r   ÚsettingÚkwargsr   r   r    rU     s    z*FileSystemStorage._clear_cached_propertiesc             C   s   | d  k r | S| S)Nr   )r   Úvaluerb   r   r   r    Ú_value_or_setting  s    z#FileSystemStorage._value_or_settingc             C   s   |  j  |  j t j ƒ S)N)re   rP   r   r[   )r   r   r   r    r\     s    zFileSystemStorage.base_locationc             C   s   t  |  j ƒ S)N)r
   r\   )r   r   r   r    rV     s    zFileSystemStorage.locationc             C   sJ   |  j  d  k	 r4 |  j  j d ƒ r4 |  j  d 7_  n  |  j |  j  t j ƒ S)Nú/)rQ   Úendswithre   r   r]   )r   r   r   r    rW     s    "zFileSystemStorage.base_urlc             C   s   |  j  |  j t j ƒ S)N)re   rR   r   r^   )r   r   r   r    rX   #  s    z'FileSystemStorage.file_permissions_modec             C   s   |  j  |  j t j ƒ S)N)re   rS   r   r_   )r   r   r   r    rY   '  s    z,FileSystemStorage.directory_permissions_moder   c             C   s   t  t |  j | ƒ | ƒ ƒ S)N)r   r!   r,   )r   r   r   r   r   r    r   +  s    zFileSystemStorage._openc             C   s¯  |  j  | ƒ } t j  j | ƒ } t j  j | ƒ sÐ yZ |  j d  k	 r t j d ƒ } z t j | |  j ƒ Wd  t j | ƒ Xn t j | ƒ WqÐ t k
 rÌ } z | j t j	 k rº ‚  n  WYd  d  } ~ XqÐ Xn  t j  j
 | ƒ sõ t d | ƒ ‚ n  x|yt | d ƒ r t | j ƒ  | ƒ nð t j t j Bt j Bt t d d ƒ B} t j | | d ƒ } d  }	 zw t j | t j ƒ x] | j ƒ  D]O }
 |	 d  k rÇt |
 t ƒ r¬d n d } t j | | ƒ }	 n  |	 j |
 ƒ q…WWd  t j | ƒ |	 d  k	 r|	 j ƒ  n t j | ƒ XWn[ t k
 rn} z; | j t j	 k rY|  j | ƒ } |  j  | ƒ } n ‚  WYd  d  } ~ Xqø XPqø W|  j d  k	 r™t j  | |  j ƒ n  t! | j" d d	 ƒ ƒ S)
Nr   z!%s exists and is not a directory.Útemporary_file_pathÚO_BINARYi¶  ÚwbZwtú\rf   )#r,   r+   r5   r/   rY   ÚumaskÚmakedirsÚOSErrorÚerrnoÚEEXISTÚisdirÚIOErrorr$   r   rh   ÚO_WRONLYÚO_CREATÚO_EXCLÚgetattrr!   r   ÚlockÚLOCK_EXr"   Ú
isinstanceÚbytesÚfdopenÚwriteZunlockÚcloser%   rX   Úchmodr   Úreplace)r   r   r'   Ú	full_pathÚ	directoryZ	old_umaskÚeÚflagsÚfdÚ_fileÚchunkr   r   r   r    r&   .  sV    zFileSystemStorage._savec             C   s‹   | s t  d ƒ ‚ |  j | ƒ } t j j | ƒ r‡ y t j | ƒ Wq‡ t k
 rƒ } z | j t j k rq ‚  n  WYd  d  } ~ Xq‡ Xn  d  S)Nz-The name argument is not allowed to be empty.)ÚAssertionErrorr,   r+   r/   Úremovern   ro   ÚENOENT)r   r   r‚   r   r   r    r8   {  s    zFileSystemStorage.deletec             C   s   t  j j |  j | ƒ ƒ S)N)r+   r,   r/   )r   r   r   r   r    r/   ‰  s    zFileSystemStorage.existsc             C   s~   |  j  | ƒ } g  g  } } xU t j | ƒ D]D } t j  j t j  j | | ƒ ƒ rc | j | ƒ q, | j | ƒ q, W| | f S)N)r,   r+   r9   rq   r1   Úappend)r   r,   ÚdirectoriesÚfilesÚentryr   r   r    r9   Œ  s    !zFileSystemStorage.listdirc             C   s   t  |  j | ƒ S)N)r   rV   )r   r   r   r   r    r,   –  s    zFileSystemStorage.pathc             C   s   t  j j |  j | ƒ ƒ S)N)r+   r,   Úgetsize)r   r   r   r   r    r:   ™  s    zFileSystemStorage.sizec             C   sX   |  j  d  k r t d ƒ ‚ n  t | ƒ } | d  k	 rH | j d ƒ } n  t |  j  | ƒ S)Nz&This file is not accessible via a URL.rf   )rW   Ú
ValueErrorr   Úlstripr   )r   r   r;   r   r   r    r;   œ  s    zFileSystemStorage.urlc             C   s8   t  j d t d d ƒt j t j j |  j | ƒ ƒ ƒ S)NzPFileSystemStorage.accessed_time() is deprecated in favor of get_accessed_time().r<   r=   )r>   r?   r   r   Úfromtimestampr+   r,   Úgetatime)r   r   r   r   r    r@   ¤  s
    zFileSystemStorage.accessed_timec             C   s8   t  j d t d d ƒt j t j j |  j | ƒ ƒ ƒ S)NzNFileSystemStorage.created_time() is deprecated in favor of get_created_time().r<   r=   )r>   r?   r   r   r‘   r+   r,   Úgetctime)r   r   r   r   r    rA   ­  s
    zFileSystemStorage.created_timec             C   s8   t  j d t d d ƒt j t j j |  j | ƒ ƒ ƒ S)NzPFileSystemStorage.modified_time() is deprecated in favor of get_modified_time().r<   r=   )r>   r?   r   r   r‘   r+   r,   Úgetmtime)r   r   r   r   r    rB   ¶  s
    zFileSystemStorage.modified_timec             C   s6   t  j r% t j | ƒ j d t j ƒ St j | ƒ Sd S)z‘
        If timezone support is enabled, make an aware datetime object in UTC;
        otherwise make a naive one in the local timezone.
        ÚtzinfoN)r   rL   r   Úutcfromtimestampr   r	   rN   r‘   )r   Útsr   r   r    Ú_datetime_from_timestamp¿  s    	z*FileSystemStorage._datetime_from_timestampc             C   s"   |  j  t j j |  j | ƒ ƒ ƒ S)N)r˜   r+   r,   r’   )r   r   r   r   r    rE   Ê  s    z#FileSystemStorage.get_accessed_timec             C   s"   |  j  t j j |  j | ƒ ƒ ƒ S)N)r˜   r+   r,   r“   )r   r   r   r   r    rF   Í  s    z"FileSystemStorage.get_created_timec             C   s"   |  j  t j j |  j | ƒ ƒ ƒ S)N)r˜   r+   r,   r”   )r   r   r   r   r    rG   Ð  s    z#FileSystemStorage.get_modified_time)rH   rI   rJ   rK   rZ   rU   re   r   r\   rV   rW   rX   rY   r   r&   r8   r/   r9   r,   r:   r;   r@   rA   rB   r˜   rE   rF   rG   r   r   r   r    r   ø   s2   	M
			c             C   s   t  |  p t j ƒ S)N)r   r   ZDEFAULT_FILE_STORAGE)Zimport_pathr   r   r    Úget_storage_classÔ  s    r™   c               @   s   e  Z d  Z d d „  Z d S)r   c             C   s   t  ƒ  ƒ  |  _ d  S)N)r™   Z_wrapped)r   r   r   r    Ú_setupÙ  s    zDefaultStorage._setupN)rH   rI   rJ   rš   r   r   r   r    r   Ø  s   )zStoragezFileSystemStoragezDefaultStoragezdefault_storage).ro   r+   r>   r   Zdjango.confr   Zdjango.core.exceptionsr   Zdjango.core.filesr   r   Zdjango.core.files.mover   Zdjango.core.signalsr   Zdjango.utilsr	   Zdjango.utils._osr
   r   Zdjango.utils.cryptor   Zdjango.utils.deconstructr   Zdjango.utils.deprecationr   Zdjango.utils.encodingr   r   Zdjango.utils.functionalr   r   Zdjango.utils.module_loadingr   Z#django.utils.six.moves.urllib.parser   Zdjango.utils.textr   Ú__all__Úobjectr   rC   r   r™   r   r   r   r   r   r    Ú<module>   s4   ÑÛ