ó
ç'ÊUc           @   sœ  d  d l  Z  d  d l Z d  d l Z d  d l m Z 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 e Z y d  d	 l m Z Wn e k
 rÛ Z d
 Z n1 Xe d ƒ Z e j e e j j ƒ ƒ sd Z n  d „  Z d e	 j  f d „  ƒ  YZ! d e f d „  ƒ  YZ" d e	 j  f d „  ƒ  YZ# d e	 j  f d „  ƒ  YZ$ d „  Z% e& d k r˜e	 j' ƒ  n  d S(   iÿÿÿÿN(   t   mkdtempt   TemporaryFile(   t   rmtree(   t   unittest(   t   Itemt   Field(   t   Requestt   Response(   t   Settings(   t   ImagesPipeline(   t   ImagesK   Missing Python Imaging Library, install https://pypi.python.org/pypi/Pillowt   jpeg_encodert   jpeg_decoders   Missing JPEG encodersc         C   s)   |  j  j d ƒ } t | ƒ r% | ƒ  S| S(   Nt   response(   t   metat   gett   callable(   t   requestt   infoR   (    (    s>   /home/travis/build/scrapy/scrapy/tests/test_pipeline_images.pyt   _mocked_download_func   s    t   ImagesPipelineTestCasec           B   s;   e  Z e Z d  „  Z d „  Z d „  Z d „  Z d „  Z RS(   c         C   s(   t  ƒ  |  _ t |  j d t ƒ|  _ d  S(   Nt   download_func(   R    t   tempdirR	   R   t   pipeline(   t   self(    (    s>   /home/travis/build/scrapy/scrapy/tests/test_pipeline_images.pyt   setUp"   s    c         C   s   t  |  j ƒ d  S(   N(   R   R   (   R   (    (    s>   /home/travis/build/scrapy/scrapy/tests/test_pipeline_images.pyt   tearDown&   s    c         C   sé   |  j  j } |  j | t d ƒ ƒ d ƒ |  j | t d ƒ ƒ d ƒ |  j | t d ƒ ƒ d ƒ |  j | t d ƒ ƒ d ƒ |  j | t d	 ƒ ƒ d
 ƒ |  j | t d ƒ ƒ d ƒ |  j | t d ƒ d t d ƒ d t ƒ  ƒd ƒ d  S(   Ns!   https://dev.mydeco.com/mydeco.gifs1   full/3fd165099d8e71b8a48b2683946e64dbfad8b52d.jpgsK   http://www.maddiebrown.co.uk///catalogue-items//image_54642_12175_95307.jpgs1   full/0ffcd85d563bca45e2f90becd0ca737bc58a00b2.jpgs9   https://dev.mydeco.com/two/dirs/with%20spaces%2Bsigns.gifs1   full/b250e3a74fff2e4703e310048a5b13eba79379d2.jpgsE   http://www.dfsonline.co.uk/get_prod_image.php?img=status_0907_mdm.jpgs1   full/4507be485f38b0da8a0be9eb2e1dfab8a19223f2.jpgs3   http://www.dorma.co.uk/images/product_details/2532/s1   full/97ee6f8a46cbbb418ea91502fd24176865cf39b2.jpgs2   http://www.dorma.co.uk/images/product_details/2532s1   full/244e0dd7d96a3b7b01f54eded250c9e272577aa1.jpgR   R   (   R   t	   file_patht   assertEqualR   R   t   object(   R   R   (    (    s>   /home/travis/build/scrapy/scrapy/tests/test_pipeline_images.pyt   test_file_path)   s"    	c         C   sÆ   |  j  j } d } |  j | t d ƒ | ƒ d ƒ |  j | t d ƒ | ƒ d ƒ |  j | t d ƒ | ƒ d ƒ |  j | t d ƒ | ƒ d	 ƒ |  j | t d ƒ | d
 t d ƒ d t ƒ  ƒd	 ƒ d  S(   Nt   50s   file:///tmp/foo.jpgs6   thumbs/50/38a86208c36e59d4404db9e37ce04be863ef0335.jpgs   file://foo.pngs6   thumbs/50/e55b765eba0ec7348e50a1df496040449071b96a.jpgs   file:///tmp/foos6   thumbs/50/0329ad83ebb8e93ea7c7906d46e9ed55f7349a50.jpgs   file:///tmp/some.name/foos6   thumbs/50/850233df65a5b83361798f532f1fc549cd13cbe9.jpgR   R   (   R   t
   thumb_pathR   R   R   R   (   R   R    t   name(    (    s>   /home/travis/build/scrapy/scrapy/tests/test_pipeline_images.pyt   test_thumbnail_name<   s    	c         C   s  d } d } t  d d | | ƒ } |  j j | ƒ \ } } |  j | j d ƒ |  j | j ƒ  d | f g ƒ |  j j | d d ƒ\ } } |  j | j d ƒ |  j | j d ƒ d } t  d d | | ƒ } |  j j | ƒ \ } } |  j | j d ƒ |  j | j ƒ  d d f g ƒ d  S(   Nid   i    i   iÿ   t   JPEGt   RGBi'  t   sizei
   i   i2   t   PNGt   RGBAiÍ   iæ   (   id   id   (   i    i   iÿ   (   i
   i   (   i
   i
   (   i    i   iÿ   i2   (   iÍ   iæ   iÿ   (   t   _create_imageR   t   convert_imaget   assertEqualst   modet	   getcolorsR%   (   R   t   SIZEt   COLOURt   imt	   convertedt   _t	   thumbnail(    (    s>   /home/travis/build/scrapy/scrapy/tests/test_pipeline_images.pyt   test_convert_imageL   s    (   t   __name__t
   __module__t   skipR   R   R   R"   R3   (    (    (    s>   /home/travis/build/scrapy/scrapy/tests/test_pipeline_images.pyR      s   				t   DeprecatedImagesPipelinec           B   s#   e  Z d  „  Z d „  Z d „  Z RS(   c         C   s   |  j  | ƒ S(   N(   t	   image_key(   R   t   url(    (    s>   /home/travis/build/scrapy/scrapy/tests/test_pipeline_images.pyt   file_keyc   s    c         C   s   t  j | ƒ j ƒ  } d | S(   Ns   empty/%s.jpg(   t   hashlibt   sha1t	   hexdigest(   R   R9   t
   image_guid(    (    s>   /home/travis/build/scrapy/scrapy/tests/test_pipeline_images.pyR8   f   s    c         C   s#   t  j | ƒ j ƒ  } d | | f S(   Ns   thumbsup/%s/%s.jpg(   R;   R<   R=   (   R   R9   t   thumb_idt
   thumb_guid(    (    s>   /home/travis/build/scrapy/scrapy/tests/test_pipeline_images.pyt	   thumb_keyj   s    (   R4   R5   R:   R8   RA   (    (    (    s>   /home/travis/build/scrapy/scrapy/tests/test_pipeline_images.pyR7   b   s   		t    DeprecatedImagesPipelineTestCasec           B   sP   e  Z d  „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z	 RS(   c         C   s   t  ƒ  |  _ d  S(   N(   R    R   (   R   (    (    s>   /home/travis/build/scrapy/scrapy/tests/test_pipeline_images.pyR   p   s    c         C   s,   | |  j  d t ƒ|  _ |  j j d  ƒ d  S(   NR   (   R   R   R   t   open_spidert   None(   R   t   pipeline_class(    (    s>   /home/travis/build/scrapy/scrapy/tests/test_pipeline_images.pyt   init_pipelines   s    c      	   C   s‹   |  j  t ƒ t j d t ƒ f } t j d ƒ |  j |  j j d ƒ d ƒ |  j t	 | ƒ d ƒ |  j
 d t | d j ƒ k ƒ Wd  QXd  S(   Nt   recordt   alwayss!   https://dev.mydeco.com/mydeco.gifs1   full/3fd165099d8e71b8a48b2683946e64dbfad8b52d.jpgi   s7   image_key(url) and file_key(url) methods are deprecatediÿÿÿÿ(   RF   R	   t   warningst   catch_warningst   Truet   simplefilterR   R   R:   t   lent
   assertTruet   strt   message(   R   t   w(    (    s>   /home/travis/build/scrapy/scrapy/tests/test_pipeline_images.pyt   test_default_file_key_methodw   s    c      	   C   s‹   |  j  t ƒ t j d t ƒ f } t j d ƒ |  j |  j j d ƒ d ƒ |  j t	 | ƒ d ƒ |  j
 d t | d j ƒ k ƒ Wd  QXd  S(   NRG   RH   s!   https://dev.mydeco.com/mydeco.gifs1   full/3fd165099d8e71b8a48b2683946e64dbfad8b52d.jpgi   s7   image_key(url) and file_key(url) methods are deprecatediÿÿÿÿ(   RF   R	   RI   RJ   RK   RL   R   R   R8   RM   RN   RO   RP   (   R   RQ   (    (    s>   /home/travis/build/scrapy/scrapy/tests/test_pipeline_images.pyt   test_default_image_key_method€   s    c      	   C   s‘   |  j  t ƒ t j d t ƒ l } t j d ƒ |  j |  j j t	 d ƒ ƒ d ƒ |  j t
 | ƒ d ƒ |  j d t | d j ƒ k ƒ Wd  QXd  S(   NRG   RH   s!   https://dev.mydeco.com/mydeco.gifs2   empty/3fd165099d8e71b8a48b2683946e64dbfad8b52d.jpgi   s7   image_key(url) and file_key(url) methods are deprecatediÿÿÿÿ(   RF   R7   RI   RJ   RK   RL   R   R   R   R   RM   RN   RO   RP   (   R   RQ   (    (    s>   /home/travis/build/scrapy/scrapy/tests/test_pipeline_images.pyt   test_overridden_file_key_method‰   s    c      	   C   sŽ   |  j  t ƒ t j d t ƒ i } t j d ƒ |  j |  j j d d ƒ d ƒ |  j t	 | ƒ d ƒ |  j
 d t | d j ƒ k ƒ Wd  QXd  S(	   NRG   RH   s   file:///tmp/foo.jpgi2   s6   thumbs/50/38a86208c36e59d4404db9e37ce04be863ef0335.jpgi   s#   thumb_key(url) method is deprecatediÿÿÿÿ(   RF   R	   RI   RJ   RK   RL   R   R   RA   RM   RN   RO   RP   (   R   RQ   (    (    s>   /home/travis/build/scrapy/scrapy/tests/test_pipeline_images.pyt   test_default_thumb_key_method’   s    c      	   C   s”   |  j  t ƒ t j d t ƒ o } t j d ƒ |  j |  j j t	 d ƒ d ƒ d ƒ |  j t
 | ƒ d ƒ |  j d t | d j ƒ k ƒ Wd  QXd  S(	   NRG   RH   s   file:///tmp/foo.jpgi2   s8   thumbsup/50/38a86208c36e59d4404db9e37ce04be863ef0335.jpgi   s#   thumb_key(url) method is deprecatediÿÿÿÿ(   RF   R7   RI   RJ   RK   RL   R   R   R    R   RM   RN   RO   RP   (   R   RQ   (    (    s>   /home/travis/build/scrapy/scrapy/tests/test_pipeline_images.pyt    test_overridden_thumb_key_method›   s    c         C   s   t  |  j ƒ d  S(   N(   R   R   (   R   (    (    s>   /home/travis/build/scrapy/scrapy/tests/test_pipeline_images.pyR   ¤   s    (
   R4   R5   R   RF   RR   RS   RT   RU   RV   R   (    (    (    s>   /home/travis/build/scrapy/scrapy/tests/test_pipeline_images.pyRB   o   s   												t   ImagesPipelineTestCaseFieldsc           B   s   e  Z d  „  Z d „  Z RS(   c         C   sç   d t  f d „  ƒ  Y} xÊ | t f D]¼ } d } | i d d 6| g d 6ƒ } t j t i d d 6ƒ ƒ } t | j | d  ƒ ƒ } |  j | d	 j	 | ƒ t
 i | d
 6f g } | j | | d  ƒ |  j | d | d	 d g ƒ q# Wd  S(   Nt   TestItemc           B   s#   e  Z e ƒ  Z e ƒ  Z e ƒ  Z RS(    (   R4   R5   R   R!   t
   image_urlst   images(    (    (    s>   /home/travis/build/scrapy/scrapy/tests/test_pipeline_images.pyRX   «   s   		s#   http://www.example.com/images/1.jpgt   item1R!   RY   s   s3://example/images/t   IMAGES_STOREi    R9   RZ   i   (   R   t   dictR	   t   from_settingsR   t   listt   get_media_requestsRD   R   R9   RK   t   item_completed(   R   RX   t   clsR9   t   itemR   t   requestst   results(    (    s>   /home/travis/build/scrapy/scrapy/tests/test_pipeline_images.pyt   test_item_fields_defaultª   s    c         C   sõ   d t  f d „  ƒ  Y} xØ | t f D]Ê } d } | i d d 6| g d 6ƒ } t j t i d d 6d d	 6d
 d 6ƒ ƒ } t | j | d  ƒ ƒ } |  j | d j	 | ƒ t
 i | d 6f g } | j | | d  ƒ |  j | d
 | d d g ƒ q# Wd  S(   NRX   c           B   s#   e  Z e ƒ  Z e ƒ  Z e ƒ  Z RS(    (   R4   R5   R   R!   t   imaget   stored_image(    (    (    s>   /home/travis/build/scrapy/scrapy/tests/test_pipeline_images.pyRX   »   s   		s#   http://www.example.com/images/1.jpgR[   R!   Rg   s   s3://example/images/R\   t   IMAGES_URLS_FIELDRh   t   IMAGES_RESULT_FIELDi    R9   i   (   R   R]   R	   R^   R   R_   R`   RD   R   R9   RK   Ra   (   R   RX   Rb   R9   Rc   R   Rd   Re   (    (    s>   /home/travis/build/scrapy/scrapy/tests/test_pipeline_images.pyt"   test_item_fields_override_settingsº   s    (   R4   R5   Rf   Rk   (    (    (    s>   /home/travis/build/scrapy/scrapy/tests/test_pipeline_images.pyRW   ¨   s   	c         O   s?   t  ƒ  } t j | | Ž  j | |  ƒ | j d ƒ t j | ƒ S(   Ni    (   R   R
   t   newt   savet   seekt   open(   t   formatt   at   kwt   buf(    (    s>   /home/travis/build/scrapy/scrapy/tests/test_pipeline_images.pyR(   Ï   s    	t   __main__(   s   jpeg_encoders   jpeg_decoder((   t   osR;   RI   t   tempfileR    R   t   shutilR   t   twisted.trialR   t   scrapy.itemR   R   t   scrapy.httpR   R   t   scrapy.settingsR   t   scrapy.pipelines.imagesR	   t   FalseR6   t   PILR
   t   ImportErrort   et   sett   encoderst   issubsett   coret   __dict__R   t   TestCaseR   R7   RB   RW   R(   R4   t   main(    (    (    s>   /home/travis/build/scrapy/scrapy/tests/test_pipeline_images.pyt   <module>   s2   
		D9'	