î
2!ìV  ã               @   sí   d  d l  m Z d  d l Z d  d l Z d  d l Z d  d l m Z d  d l Z d  d l m Z m	 Z	 d  d l
 m Z m Z d  d l m Z Gd d „  d e ƒ Z e d	 d
 „  ƒ Z Gd d „  d e ƒ Z Gd d „  d e ƒ Z e ƒ  Z d S)é    )Úunicode_literalsN)Údatetime)ÚFileSystemStorageÚget_storage_class)Ú
LazyObjectÚSimpleLazyObject)Úsettingsc                   ss   e  Z d  Z d Z d d ‡  f d d † Z d d „  Z d d „  Z d	 d
 „  Z d d d „ Z ‡  f d d †  Z	 ‡  S)ÚCompressorFileStoragez³
    Standard file system storage for files handled by django-compressor.

    The defaults for ``location`` and ``base_url`` are ``COMPRESS_ROOT`` and
    ``COMPRESS_URL``.

    Nc                sS   | d  k r t  j } n  | d  k r0 t  j } n  t t |  ƒ j | | | | Ž d  S)N)r   ZCOMPRESS_ROOTZCOMPRESS_URLÚsuperr	   Ú__init__)ÚselfÚlocationÚbase_urlÚargsÚkwargs)Ú	__class__© ú?/tmp/pip-build-0sboevxh/django-compressor/compressor/storage.pyr      s    zCompressorFileStorage.__init__c             C   s"   t  j t j j |  j | ƒ ƒ ƒ S)N)r   ÚfromtimestampÚosÚpathÚgetatime)r   Únamer   r   r   Úaccessed_time   s    z#CompressorFileStorage.accessed_timec             C   s"   t  j t j j |  j | ƒ ƒ ƒ S)N)r   r   r   r   Úgetctime)r   r   r   r   r   Úcreated_time!   s    z"CompressorFileStorage.created_timec             C   s"   t  j t j j |  j | ƒ ƒ ƒ S)N)r   r   r   r   Úgetmtime)r   r   r   r   r   Úmodified_time$   s    z#CompressorFileStorage.modified_timec             C   s#   |  j  | ƒ r |  j | ƒ n  | S)z6
        Deletes the given file if it exists.
        )ÚexistsÚdelete)r   r   Ú
max_lengthr   r   r   Úget_available_name'   s    z(CompressorFileStorage.get_available_namec                s^   y t  t |  ƒ j | ƒ Wn= t k
 rY } z | j t j k rG ‚  n  WYd d } ~ Xn Xd S)zƒ
        Handle deletion race condition present in Django prior to 1.4
        https://code.djangoproject.com/ticket/16108
        N)r
   r	   r   ÚOSErrorÚerrnoÚENOENT)r   r   Úe)r   r   r   r   /   s
    zCompressorFileStorage.delete)
Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   r   r!   r   r   r   )r   r   r	      s   r	   c               C   s   t  d ƒ ƒ  S)Nz(compressor.storage.CompressorFileStorage)r   r   r   r   r   Ú<lambda><   s    r*   c                   s(   e  Z d  Z d Z ‡  f d d †  Z ‡  S)ÚGzipCompressorFileStoragezs
    The standard compressor file system storage that gzips storage files
    additionally to the usual files.
    c                sÏ   t  t |  ƒ j | | ƒ } |  j | ƒ } d | } t | d ƒ } t | d ƒ } z) t j d | ƒ } | j | j ƒ  ƒ Wd  | j	 ƒ  | j	 ƒ  t
 j
 ƒ  } t j | | | f ƒ t j | | | f ƒ X| S)Nz%s.gzÚrbÚwbÚfileobj)r
   r+   Úsaver   ÚopenÚgzipÚGzipFileÚwriteÚreadÚcloseÚtimer   Úutime)r   ÚfilenameÚcontentÚ	orig_pathZcompressed_pathZf_inZf_outZstamp)r   r   r   r/   D   s    


zGzipCompressorFileStorage.save)r&   r'   r(   r)   r/   r   r   )r   r   r+   ?   s   r+   c               @   s   e  Z d  Z d d „  Z d S)ÚDefaultStoragec             C   s   t  t j ƒ ƒ  |  _ d  S)N)r   r   ZCOMPRESS_STORAGEZ_wrapped)r   r   r   r   Ú_setup]   s    zDefaultStorage._setupN)r&   r'   r(   r<   r   r   r   r   r;   \   s   r;   )Ú
__future__r   r#   r1   r   r   r6   Zdjango.core.files.storager   r   Zdjango.utils.functionalr   r   Zcompressor.confr   r	   Zcompressor_file_storager+   r;   Zdefault_storager   r   r   r   Ú<module>   s   -