ó
:ęjfc           @   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 y d  d l m	 Z	 Wn e
 k
 r d  d l	 Z	 n Xd  d l m Z d   Z d   Z d d	  Z d
   Z d   Z d   Z d   Z d   Z d   Z d S(   i’’’’N(   t   six(   t
   LazyObject(   t   timezone(   t   Image(   t   settingsc         C   s   t  |  t j  s d S|  j   } t t |   } g  | D] } | | ^ q; } t g  | D]( } | d k r[ | t j | d  ^ q[  S(   sG   
    Calculate the entropy of an image. Used for "smart cropping".
    i    i   (   t
   isinstanceR   t	   histogramt   floatt   sumt   matht   log(   t   imt   histt	   hist_sizet   ht   p(    (    s3   /var/www/svtgbi.org/svtgbi/easy_thumbnails/utils.pyt   image_entropy   s    c         C   sl   |  j  d  } | d k r. t |  i  i  g   S|  |  |  | d } } t | i  i  | g  } t | |  S(   s0   
    Dynamically import a module or object.
    t   .i’’’’i   (   t   rfindt
   __import__t   getattr(   t   import_stringt   lastdott   module_namet   attrt   parent_module(    (    s3   /var/www/svtgbi.org/svtgbi/easy_thumbnails/utils.pyt   dynamic_import   s    c         C   s“   |  d k rD g  t t j  t t j  D] } t |  ^ q) }  n  t d d d g  } xN |  D]F } t j r t	 j
 |  d n t	 j |  d } | j | d  q` Wt |  S(   sm   
    Return a list of unique valid options for a list of image processors
    (and/or source generators)
    t   sizet   qualityt   subsamplingi    i   N(   t   Nonet   tupleR   t   THUMBNAIL_PROCESSORSt   THUMBNAIL_SOURCE_GENERATORSR   t   setR    t   PY3t   inspectt   getfullargspect
   getargspect   updatet   list(   t
   processorsR   t   valid_optionst	   processort   args(    (    s3   /var/www/svtgbi.org/svtgbi/easy_thumbnails/utils.pyt   valid_processor_options,   s    )/c         C   s*   y |  j  d  Wn t k
 r% t SXt S(   s2   
    Check to see if a file storage is local.
    t   test(   t   patht   NotImplementedErrort   Falset   True(   t   storage(    (    s3   /var/www/svtgbi.org/svtgbi/easy_thumbnails/utils.pyt   is_storage_local>   s
    c         C   s   t  |  t  r7 |  j d k r+ |  j   n  |  j }  n  t  |  t j  sk |  j } d | j | j	 f }  n  t
 j |  j d   j   S(   s   
    Return a hex string hash for a storage object (or string containing
    'full.path.ClassName' referring to a storage object).
    s   %s.%st   utf8N(   R   R   t   _wrappedR   t   _setupR    t   string_typest	   __class__t
   __module__t   __name__t   hashlibt   md5t   encodet	   hexdigest(   R3   t   storage_cls(    (    s3   /var/www/svtgbi.org/svtgbi/easy_thumbnails/utils.pyt   get_storage_hashI   s    	c         C   sA   t  |  t j  s t S|  j d k p@ |  j d k o@ d |  j k S(   s2   
    Check to see if an image is transparent.
    t   RGBAt   LAt   Pt   transparency(   RB   RC   (   R   R   R1   t   modet   info(   t   image(    (    s3   /var/www/svtgbi.org/svtgbi/easy_thumbnails/utils.pyt   is_transparentY   s    c         C   s2   t  |  t j  s t Sd |  j k p1 d |  j k S(   s2   
    Check to see if an image is progressive.
    t   progressivet   progression(   R   R   R1   RG   (   RH   (    (    s3   /var/www/svtgbi.org/svtgbi/easy_thumbnails/utils.pyt   is_progressivee   s    c         C   sE  y |  j    } Wn t k
 r) d	 } n X| rA| j d  } | d k r` |  j t j  }  qA| d k r |  j t j  }  qA| d k r¢ |  j t j  }  qA| d k rĻ |  j t j	  j t j  }  qA| d k rš |  j t j	  }  qA| d k r|  j t j
  j t j  }  qA| d k rA|  j t j
  }  qAn  |  S(
   sS   
    Rotate and/or flip an image to respect the image's EXIF orientation data.
    i  i   i   i   i   i   i   i   N(   t   _getexift	   ExceptionR   t   gett	   transposeR   t   FLIP_LEFT_RIGHTt
   ROTATE_180t   FLIP_TOP_BOTTOMt
   ROTATE_270t	   ROTATE_90(   R   t   exift   orientation(    (    s3   /var/www/svtgbi.org/svtgbi/easy_thumbnails/utils.pyt   exif_orientationo   s*    
!!c         C   sŖ   y: y |  j  |  } Wn  t k
 r8 |  j |  } n XWn# t k
 rN d St k
 r_ d SX| r¦ t j |  r¦ t t	 d t
  r¦ t j   } t j | |  Sn  | S(   s_   
    Get modified time from storage, ensuring the result is a timezone-aware
    datetime.
    i    t   USE_TZN(   t   get_modified_timet   AttributeErrort   modified_timet   OSErrorR0   R   R   t   is_naiveR   R   R1   t   get_default_timezonet
   make_aware(   R3   t   nameR\   t   default_timezone(    (    s3   /var/www/svtgbi.org/svtgbi/easy_thumbnails/utils.pyRZ      s    (   R<   R$   R	   t   django.utilsR    t   django.utils.functionalR   R   t   PILR   t   ImportErrort   easy_thumbnails.confR   R   R   R   R-   R4   RA   RI   RL   RX   RZ   (    (    (    s3   /var/www/svtgbi.org/svtgbi/easy_thumbnails/utils.pyt   <module>   s&   						
	