ó
P'—^c           @   sÛ   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 d d
 l m Z m Z m Z m Z d e
 e f d „  ƒ  YZ d e f d „  ƒ  YZ d S(   iÿÿÿÿ(   t   copy(   t   settings(   t   File(   t	   ImageFile(   t   SimpleLazyObjecti   (   t
   BaseIKFile(   t   generator_registry(   t   content_requiredt   existence_required(   t
   get_loggert   get_singletont   generatet   get_by_qnamet   ImageCacheFilec           B   sƒ   e  Z d  Z d d d d d „ Z d „  Z d „  Z e d „  ƒ Z e d „  ƒ Z	 e
 d „ Z d „  Z d „  Z d	 „  Z d
 „  Z RS(   s;  
    A file that represents the result of a generator. Creating an instance of
    this class is not enough to trigger the generation of the file. In fact,
    one of the main points of this class is to allow the creation of the file
    to be deferred until the time that the cache file strategy requires it.

    c         C   sû   | |  _  | sQ y | j } WqQ t k
 rM t t j d ƒ } | | ƒ } qQ Xn  | |  _ | p t | d d	 ƒ p t	 t j
 d ƒ } | p« t | d d	 ƒ p« t	 t j d ƒ |  _ | pØ t | d d	 ƒ pØ t	 t j d ƒ |  _ t t |  ƒ j d | ƒ d	 S(
   s«  
        :param generator: The object responsible for generating a new image.
        :param name: The filename
        :param storage: A Django storage object that will be used to save the
            file.
        :param cachefile_backend: The object responsible for managing the
            state of the file.
        :param cachefile_strategy: The object responsible for handling events
            for this file.

        t   namert   cachefile_storages   file storage backendt   cachefile_backends   cache file backendt   cachefile_strategys   cache file strategyt   storageN(   t	   generatort   cachefile_namet   AttributeErrorR   R   t   IMAGEKIT_CACHEFILE_NAMERt   namet   getattrt   NoneR
   t   IMAGEKIT_DEFAULT_FILE_STORAGEt"   IMAGEKIT_DEFAULT_CACHEFILE_BACKENDR   t#   IMAGEKIT_DEFAULT_CACHEFILE_STRATEGYR   t   superR   t   __init__(   t   selfR   R   R   R   R   t   fn(    (    s>   /tmp/pip-unpacked-wheel-XxTEJK/imagekit/cachefiles/__init__.pyR      s(    					c         C   sP   t  |  d d  ƒ d  k rL t j d |  d |  ƒ |  j j |  j d ƒ |  _ n  d  S(   Nt   _filet   sendert   filet   rb(   R   R   R   t   sendR   t   openR   R!   (   R   (    (    s>   /tmp/pip-unpacked-wheel-XxTEJK/imagekit/cachefiles/__init__.pyt   _require_file;   s    c         C   sP   t  |  d d  ƒ d  k r1 t j d |  d |  ƒ n  t  |  j | ƒ } | |  j ƒ S(   NR!   R"   R#   (   R   R   R   R%   R   R   (   R   t   attrR    (    (    s>   /tmp/pip-unpacked-wheel-XxTEJK/imagekit/cachefiles/__init__.pyt   _storage_attrH   s    c         C   s   |  j  d ƒ S(   Nt   path(   R)   (   R   (    (    s>   /tmp/pip-unpacked-wheel-XxTEJK/imagekit/cachefiles/__init__.pyR*   N   s    c         C   s   |  j  d ƒ S(   Nt   url(   R)   (   R   (    (    s>   /tmp/pip-unpacked-wheel-XxTEJK/imagekit/cachefiles/__init__.pyR+   R   s    c         C   s8   | s t  |  d d ƒ d k r4 |  j j |  | ƒ n  d S(   sŠ   
        Generate the file. If ``force`` is ``True``, the file will be generated
        whether the file already exists or not.

        R!   N(   R   R   R   R   (   R   t   force(    (    s>   /tmp/pip-unpacked-wheel-XxTEJK/imagekit/cachefiles/__init__.pyR   V   s    c         C   s‚   t  |  j ƒ } |  j j |  j | ƒ } | j d ƒ t | ƒ |  _ | |  j k r~ t ƒ  j	 d |  j |  j | |  j
 f ƒ n  d  S(   Ni    sN  The storage backend %s did not save the file with the requested name ("%s") and instead used "%s". This may be because a file already existed with the requested name. If so, you may have meant to call generate() instead of generate(force=True), or there may be a race condition in the file backend %s. The saved file will not be used.(   R   R   R   t   saveR   t   seekR   R#   R	   t   warningR   (   R   t   contentt   actual_name(    (    s>   /tmp/pip-unpacked-wheel-XxTEJK/imagekit/cachefiles/__init__.pyt	   _generate_   s    		c         C   s|   |  j  s t St j d |  d |  ƒ y |  j j |  ƒ } Wn& t k
 ra t |  j d t ƒ } n X| rx |  j j	 |  ƒ St
 S(   NR"   R#   t   is_async(   R   t   FalseR   R%   R   t   should_verify_existenceR   R   R   t   existst   True(   R   t   check(    (    s>   /tmp/pip-unpacked-wheel-XxTEJK/imagekit/cachefiles/__init__.pyt   __bool__|   s    	c         C   s#   t  |  j ƒ } | j d d  ƒ | S(   NR!   (   R    t   __dict__t   popR   (   R   t   state(    (    s>   /tmp/pip-unpacked-wheel-XxTEJK/imagekit/cachefiles/__init__.pyt   __getstate__Œ   s    c         C   s
   |  j  ƒ  S(   N(   R9   (   R   (    (    s>   /tmp/pip-unpacked-wheel-XxTEJK/imagekit/cachefiles/__init__.pyt   __nonzero__”   s    N(   t   __name__t
   __module__t   __doc__R   R   R'   R)   t   propertyR*   R+   R4   R   R2   R9   R=   R>   (    (    (    s>   /tmp/pip-unpacked-wheel-XxTEJK/imagekit/cachefiles/__init__.pyR      s   '						t   LazyImageCacheFilec           B   s   e  Z d  „  Z d „  Z RS(   c            s/   ‡  ‡ ‡ f d †  } t  t |  ƒ j | ƒ d  S(   Nc             s   t  j ˆ ˆ  ˆ Ž }  t |  ƒ S(   N(   R   t   getR   (   R   (   t   argst   generator_idt   kwargs(    s>   /tmp/pip-unpacked-wheel-XxTEJK/imagekit/cachefiles/__init__.pyt   setup›   s    (   R   RC   R   (   R   RF   RE   RG   RH   (    (   RE   RF   RG   s>   /tmp/pip-unpacked-wheel-XxTEJK/imagekit/cachefiles/__init__.pyR   š   s    c         C   s    d |  j  j t |  ƒ p d f S(   Ns   <%s: %s>R   (   t	   __class__R?   t   str(   R   (    (    s>   /tmp/pip-unpacked-wheel-XxTEJK/imagekit/cachefiles/__init__.pyt   __repr__    s    (   R?   R@   R   RK   (    (    (    s>   /tmp/pip-unpacked-wheel-XxTEJK/imagekit/cachefiles/__init__.pyRC   ™   s   	N(   R    t   django.confR   t   django.core.filesR   t   django.core.files.imagesR   t   django.utils.functionalR   t   filesR   t   registryR   t   signalsR   R   t   utilsR	   R
   R   R   R   RC   (    (    (    s>   /tmp/pip-unpacked-wheel-XxTEJK/imagekit/cachefiles/__init__.pyt   <module>   s   "