ó
šßÈ[c           @` s•  d  d l  m Z m Z m Z m Z d  d l Z d  d l Z d  d l Z d  d l Z d  d l	 Z	 d  d l
 Z
 d d l m Z m Z m Z m Z d d l m Z d d l m Z d d l m Z m Z m Z d	 Z y d  d l Z Wn e k
 r÷ e Z n Xe Z y2 e j d5 k rd  d l Z n d  d
 l  m Z Wn e k
 rIe Z! n Xe Z! y d  d l" Z" Wn e k
 rye Z# n Xe Z# e d d ƒ d „  ƒ Z$ e d d ƒ d „  ƒ Z% e
 j& j' e# oÏe j( j) d ƒ d d ƒd „  ƒ Z* e d d ƒ d „  ƒ Z+ e d d ƒ d „  ƒ Z, e d d ƒ d „  ƒ Z- e d d ƒ d „  ƒ Z. e d d ƒ d „  ƒ Z/ e
 j& j0 d d d d d g ƒ d „  ƒ Z1 e
 j2 d d d  g ƒ d! „  ƒ Z3 d" „  Z4 d# „  Z5 d$ „  Z6 e e7 ƒ d% „  ƒ Z8 d& „  Z9 d' „  Z: e d d ƒ d( „  ƒ Z; e
 j& j0 d d) d* d+ d, g ƒ d- „  ƒ Z< d. „  Z= e d d ƒ d/ „  ƒ Z> d0 „  Z? e d d ƒ d1 „  ƒ Z@ d2 „  ZA e
 j& j' d3 ƒ d4 „  ƒ ZB d S(6   i    (   t   absolute_importt   divisiont   print_functiont   unicode_literalsNi   (   t   _get_download_cache_locst   CacheMissingWarningt   get_pkg_data_filenamet   get_readable_fileobji   (   t   six(   t   urllib(   t   remote_datat   raisest   catch_warningsu   http://www.astropy.org(   t   lzmat   sourceu   astropyc          C` s8   d d l  m }  |  t ƒ } t j j | ƒ s4 t ‚ d  S(   Ni   (   t   download_file(   t   dataR   t   TESTURLt   ost   patht   isfilet   AssertionError(   R   t   fnout(    (    s<   lib/python2.7/site-packages/astropy/utils/tests/test_data.pyt   test_download_nocache5   s    c          C` s¿   d d l  m }  m } |  j } |  j } d } y  | | | | g d t ƒ} Wn3 t j j k
 rƒ | | | | g d t ƒ} n Xt	 g  | D] } t
 j j | ƒ ^ qŽ ƒ s» t | ƒ ‚ d  S(   Ni   (   t   conft   download_files_in_parallelu   intersphinx/READMEt   cache(   R   R   R   t   dataurlt   dataurl_mirrort   TrueR	   t   errort   URLErrort   allR   R   R   R   (   R   R   t   main_urlt
   mirror_urlt   filelocR   t   f(    (    s<   lib/python2.7/site-packages/astropy/utils/tests/test_data.pyt   test_download_parallel=   s    		u   wint   reasonu)   URI incorrect for Windows without pathlibc          C` s\  d d l  m }  m } m } m } |  t j j d d ƒ ƒ } |  t j j d d ƒ ƒ } t r‹ t	 j
 | ƒ j ƒ  } t	 j
 | ƒ j ƒ  } n d | } d | } t | d ƒ } t | d ƒ }	 | | d t ƒ| |	 d t ƒt ƒ  \ }
 } | | t ƒ  } | | =Wd  QX| ƒ  } | | d t ƒ| |	 d t ƒk rR|	 | k rR| | k sXt ‚ d  S(	   Ni   (   t   _find_pkg_data_pathR   t   get_cached_urlst   _open_shelveu   datau   dataurlu   dataurl_mirroru   file://u   /index.htmlR   (   R   R'   R   R(   R)   R   R   t   joint   HAS_PATHLIBt   pathlibt   Patht   as_urit   strR   R   R   (   R'   R   R(   R)   t	   main_patht   mirror_pathR!   R"   t	   main_filet   mirror_filet   dldirt   urlmapfnt   url2hasht   c_urls(    (    s<   lib/python2.7/site-packages/astropy/utils/tests/test_data.pyt   test_download_mirror_cacheN   s&    "

	c          C` s>   d d l  m }  |  t d t ƒ} t j j | ƒ s: t ‚ d  S(   Ni   (   R   t   show_progress(   R   R   R   t   FalseR   R   R   R   (   R   R   (    (    s<   lib/python2.7/site-packages/astropy/utils/tests/test_data.pyt   test_download_noprogressr   s    c          C` sW  d d l  m }  m } t ƒ  d } |  t d t ƒ} t j j | ƒ sM t	 ‚ t j j
 | ƒ se t	 ‚ | t ƒ t j j | ƒ sˆ t	 ‚ |  t d t ƒ} t j j
 | ƒ s² t	 ‚ | ƒ  t j j | ƒ sÒ t	 ‚ t j j | ƒ së t	 ‚ |  t d t ƒ} t j j
 | ƒ st	 ‚ | d ƒ t j j | d ƒ } t j j | ƒ sSt	 d ƒ ‚ d  S(   Ni   (   R   t   clear_download_cachei    R   u+   http://this_was_never_downloaded_before.comu   locku    Cache dir lock was not released!(   R   R   R<   R   R   R   R   R   t   isdirR   R   t   existsR*   (   R   R<   t   download_dirR   t   lockdir(    (    s<   lib/python2.7/site-packages/astropy/utils/tests/test_data.pyt   test_download_cachez   s"    

c          C` sF   t  t d t d d ƒ( }  |  j ƒ  j d ƒ d k s< t ‚ Wd  QXd  S(   NR   t   encodingu   utf-8u   Astropyiÿÿÿÿ(   R   R   R:   t   readt   findR   (   t   page(    (    s<   lib/python2.7/site-packages/astropy/utils/tests/test_data.pyt   test_url_nocache›   s    c          C` sà   d d l  m }  t t d d d t ƒ } t j | j ƒ  ƒ } Wd  QXd | j ƒ  } t	 | ƒ } t
 j j | ƒ sz t ‚ |  | d ƒ t
 j j | ƒ s¡ t ‚ t
 j j t ƒ  d d	 ƒ } t
 j j | ƒ sÜ t d
 ƒ ‚ d  S(   Ni   (   R<   RB   u   binaryR   u   hash/i   i    u   locku    Cache dir lock was not released!(   R   R<   R   R   R   t   hashlibt   md5RC   t	   hexdigestR   R   R   R   R   R*   R   R=   (   R<   RE   t   hasht   hashstrR   R@   (    (    s<   lib/python2.7/site-packages/astropy/utils/tests/test_data.pyt   test_find_by_hash¡   s    c           C` s*   t  j t j j ƒ  t d ƒ Wd  QXd  S(   Nu@   kjfrhgjklahgiulrhgiuraehgiurhgiuhreglhurieghruelighiuerahiulruli(   t   pytestR   R	   R   R   R   (    (    (    s<   lib/python2.7/site-packages/astropy/utils/tests/test_data.pyt   test_find_invalid´   s    u   filenameu	   local.datu   local.dat.gzu   local.dat.bz2u   local.dat.xzc      	   C` só   d d l  m } t r# d |  k s6 t r d |  k r t j t ƒ ; } | t j j	 d |  ƒ d d ƒ } | j
 ƒ  Wd  QXWd  QXd t | ƒ k sï t ‚ nR | t j j	 d |  ƒ d d ƒ/ } | j
 ƒ  | j ƒ  j ƒ  d	 k sé t ‚ Wd  QXd  S(
   Ni   (   t   get_pkg_data_fileobju   bz2u   xzu   dataRB   u   binaryu    format files are not supportedt   CONTENT(   R   RO   t   HAS_BZ2t   HAS_XZRM   R   t
   ValueErrorR   R   R*   t   readlineR/   R   RC   t   rstrip(   t   filenameRO   t   eR$   (    (    s<   lib/python2.7/site-packages/astropy/utils/tests/test_data.pyt   test_local_data_obj½   s    &$$
t   paramsu   invalid.dat.bz2u   invalid.dat.gzc         C` st   d } d } | j  |  j ƒ } | j } | j d ƒ r? | } n | j d ƒ rW | } n d } | j | d d ƒ| S(   Nt
   BZhinvalids
   ‹invalidu   .bz2u   .gzu   invalidt   modeu   wb(   R*   t   paramt   strpatht   endswitht   write(   t   requestt   tmpdirt
   bz_contentt
   gz_contentt   datafileRV   t   contents(    (    s<   lib/python2.7/site-packages/astropy/utils/tests/test_data.pyt   bad_compressedÎ   s    			c      	   C` sÐ   |  j  d ƒ } |  j  d ƒ } t r+ | s8 t r | r t j t ƒ , } t |  d d ƒ } | j ƒ  Wd  QXWd  QXd t | ƒ k sÌ t	 ‚ n< t |  d d ƒ( } | j ƒ  j
 ƒ  j  d ƒ sÆ t	 ‚ Wd  QXd  S(   Nu   .bz2u   .xzRB   u   binaryu    format files are not supportedt   invalid(   R^   RQ   RR   RM   R   RS   R   RC   R/   R   RU   (   Rf   t   is_bz2t   is_xzRW   R$   (    (    s<   lib/python2.7/site-packages/astropy/utils/tests/test_data.pyt   test_local_data_obj_invalidå   s    	c          C` s7   t  d ƒ }  t j j |  ƒ r- |  j d ƒ s3 t ‚ d  S(   Nu   data/local.datu	   local.dat(   R   R   R   R   R^   R   (   R   (    (    s<   lib/python2.7/site-packages/astropy/utils/tests/test_data.pyt   test_local_data_nameû   s    c          C` s   t  j j t  j j t ƒ d ƒ }  t j j d |  ƒ zC d d l } | j ƒ  } | t  j j |  d d d ƒ k sv t	 ‚ Wd t j j
 d ƒ Xd S(   uì   Regression test for issue #1256

    Tests that `get_pkg_data_filename` works in a third-party package that
    doesn't make any relative imports from the module it's used from.

    Uses a test package under ``data/test_package``.
    u   datai    Nu   test_packageu   foo.txt(   R   R   R*   t   dirnamet   __file__t   syst   insertt   test_packaget   get_data_filenameR   t   pop(   t   data_dirRp   RV   (    (    s<   lib/python2.7/site-packages/astropy/utils/tests/test_data.pyt"   test_data_name_third_party_package  s    
!c           C` s   t  d ƒ d  S(   Nu   ../../../data/README.rst(   R   (    (    (    s<   lib/python2.7/site-packages/astropy/utils/tests/test_data.pyt   test_local_data_nonlocalfail  s    c         C` s‘   d d l  m } d } |  j d ƒ j } t j | d ƒ  } | j | ƒ | j ƒ  Wd  QX| | ƒ } t j	 | ƒ j
 ƒ  } | | k s t ‚ d  S(   Ni   (   t   compute_hasht$   1234567890abcdefghijklmnopqrstuvwxyzu   tmp.datu   wb(   R   Rv   R*   R]   t   iot   openR_   t   flushRG   RH   RI   R   (   Ra   Rv   t   randsRV   t   ntft   chhasht   shash(    (    s<   lib/python2.7/site-packages/astropy/utils/tests/test_data.pyt   test_compute_hash$  s    c          C` sY   d d l  m }  m } |  d ƒ  } | j ƒ  } Wd  QX| d ƒ } | | k sU t ‚ d  S(   Ni   (   RO   t   get_pkg_data_contentsu   data/local.dat(   R   RO   R€   RC   R   (   RO   R€   R$   t	   contents1t	   contents2(    (    s<   lib/python2.7/site-packages/astropy/utils/tests/test_data.pyt   test_get_pkg_data_contents5  s
    c         C` sS  d d l  m } d d l m } t j j t ƒ  d d ƒ } t | j	 _
 |  j t d ƒ d ƒ |  j t d ƒ ƒ |  j t d	 ƒ d
 ƒ |  j t d	 ƒ ƒ |  j | j d d ƒ |  j | j d d ƒ d „  } |  j | d | ƒ t j t ƒ  | j ƒ  Wd QXt t ƒ  } | j t d t ƒ} Wd QXt j j | ƒ sDt ‚ t | ƒ d k s\t ‚ | j d ƒ } | j d ƒ } | j t k st ‚ d | j j  d k s«t ‚ | j t k sÀt ‚ d | j j  d k sÜt ‚ | | j j  d k søt ‚ t t ƒ  } | j! t ƒ Wd QXt j j | ƒ s2t ‚ | j" ƒ  t j j | ƒ sUt ‚ t | ƒ d k smt ‚ | j ƒ  }	 |	 j | j k s‘t ‚ d t |	 j ƒ k s¬t ‚ t t ƒ  } | j t d t# ƒ}
 Wd QXt$ |
 d ƒ 1 } | j% ƒ  j& d ƒ j' d ƒ d k st ‚ Wd QXt | ƒ d k s0t ‚ t j j( | ƒ sOt d ƒ ‚ d S(   uj   
    Tests to make sure the default behavior when the cache directory can't
    be located is correct
    i   (   R   i   (   t   pathsi    u   locku   XDG_CONFIG_HOMEu   foou   XDG_CACHE_HOMEu   baru
   _temp_pathc         S` s
   t  ‚ d  S(   N(   t   OSError(   t   dirnmt   linkto(    (    s<   lib/python2.7/site-packages/astropy/utils/tests/test_data.pyt   osraiser[  s    u   _find_or_create_astropy_dirNR   i   u'   Remote data cache could not be accessedu%   File downloaded to temporary locationu+   Not clearing data cache - cache inacessableu   rbu   utf-8u   Astropyiÿÿÿÿu    Cache dir lock was not released!()   t    R   t   configR„   R   R   R*   R   R   R   t"   delete_temporary_downloads_at_exitt   setenvR/   t   delenvt   setattrt   set_temp_configt   Nonet   set_temp_cacheRM   R   R…   t   get_cache_dirR   R   R   R   R   R   t   lenRr   t   categoryt   messaget   argsR<   t	   _deltempsR:   Ry   RC   t   decodeRD   R=   (   t   monkeypatchR   R„   R@   Rˆ   t   wR   t   w1t   w2t   w3t	   fnnocacheRE   (    (    s<   lib/python2.7/site-packages/astropy/utils/tests/test_data.pyt   test_data_noastropy_fallback@  sP    	
0u   unicode.txtu   unicode.txt.gzu   unicode.txt.bz2u   unicode.txt.xzc         C` s  |  d k r& t  r& t j d d ƒ n  |  d k rL t rL t j d d ƒ n  d d l m } | t j j d |  ƒ d	 d
 ƒ} t	 | t
 j ƒ s• t ‚ | j ƒ  d } | d k s· t ‚ | t j j d |  ƒ d	 d ƒ} t	 | t ƒ sí t ‚ | j ƒ  d } | d d k st ‚ d  S(   Nu   unicode.txt.bz2R&   u   no bz2 supportu   unicode.txt.xzu   no lzma supporti   (   R€   u   dataRB   u   utf-8i   u!   ×”××¡×˜×¨×•× ×•×ž×™ ×¤×™×™×ª×•×Ÿu   binarys"   ÿ×”××¡×˜×¨×•× ×•×ž×™ ×¤×™×™×ª×•×Ÿ(   RQ   RM   t   xfailRR   R   R€   R   R   R*   t
   isinstanceR   t	   text_typeR   t
   splitlinest   bytes(   RV   R€   Re   t   x(    (    s<   lib/python2.7/site-packages/astropy/utils/tests/test_data.pyt   test_read_unicode’  s    !!c          C` sœ   d d  l  }  d } |  j | ƒ } t | t ƒ s6 t ‚ d d d „  ƒ  Y} | | ƒ } t | d d ƒ/ } | j ƒ  | j ƒ  j ƒ  d k s’ t ‚ Wd  QXd  S(	   Ni    s€   H4sICIxwG1AAA2xvY2FsLmRhdAALycgsVkjLzElVANKlxakpCpl5CiUZqQolqcUl8Tn5yYk58SmJJYnxWmCRzLx0hbTSvOSSzPy8Yi5nf78QV78QLgAlLytnRQAAAA==t
   FakeStreamc           B` s#   e  Z d  Z d „  Z d d „ Z RS(   u?   
        A fake stream that has `read`, but no `seek`.
        c         S` s   | |  _  d  S(   N(   R   (   t   selfR   (    (    s<   lib/python2.7/site-packages/astropy/utils/tests/test_data.pyt   __init__¶  s    c         S` sB   | d  k r! |  j } d |  _ n |  j |  } |  j | |  _ | S(   NR‰   (   R   R   (   R¨   t   nbytest   result(    (    s<   lib/python2.7/site-packages/astropy/utils/tests/test_data.pyRC   ¹  s    	N(   t   __name__t
   __module__t   __doc__R©   R   RC   (    (    (    s<   lib/python2.7/site-packages/astropy/utils/tests/test_data.pyR§   ±  s   	RB   u   binaryRP   (    (	   t   base64t	   b64decodeR¡   R¤   R   R   RT   RC   RU   (   R¯   t   gzipped_dataR§   t   streamR$   (    (    s<   lib/python2.7/site-packages/astropy/utils/tests/test_data.pyt   test_compressed_stream©  s    
c          C` s:   d d l  m }  t j t j j ƒ  |  d ƒ Wd QXd S(   u’   
    checks that download_file gives a URLError and not an AttributeError,
    as its code pathway involves some fiddling with the exception.
    i   (   R   u&   http://www.astropy.org/nonexistentfileN(   R   R   RM   R   R	   R   R   (   R   (    (    s<   lib/python2.7/site-packages/astropy/utils/tests/test_data.pyt   test_invalid_location_downloadÈ  s    c          C` s4   d d l  m }  t j t ƒ  |  d ƒ Wd QXd S(   uM   
    checks that download_file gives an IOError if the socket is blocked
    i   (   R   u"   http://astropy.org/nonexistentfileN(   R   R   RM   R   t   IOError(   R   (    (    s<   lib/python2.7/site-packages/astropy/utils/tests/test_data.pyt(   test_invalid_location_download_noconnectÔ  s    c          C` sU   d d l  m }  m } | d ƒ s) t ‚ |  t d t d t ƒ| t ƒ sQ t ‚ d  S(   Ni   (   R   t   is_url_in_cacheu"   http://astropy.org/nonexistentfileR   R9   (   R   R   R·   R   R   R   R:   (   R   R·   (    (    s<   lib/python2.7/site-packages/astropy/utils/tests/test_data.pyt   test_is_url_in_cacheß  s    c         C` s…   t  t j j d d ƒ ƒ } d t j j | ƒ } | j t d t	 |  ƒ ƒ t
 | ƒ  Wd QX|  j ƒ  } t | ƒ d k s t ‚ d S(   uA   checks that get_readable_fileobj leaves no temporary files behindu   datau	   local.datu   file://u   tempdirNi    (   R   R   R   R*   R	   R`   t   pathname2urlRŽ   t   tempfileR/   R   t   listdirR“   R   (   Ra   R™   t   local_filenamet   urlt   tempdir_listing(    (    s<   lib/python2.7/site-packages/astropy/utils/tests/test_data.pyt3   test_get_readable_fileobj_cleans_up_temporary_filesé  s    u   not HAS_PATHLIBc          C` s[   t  j t t j j d d ƒ ƒ ƒ }  t |  ƒ % } | j ƒ  j ƒ  d k sQ t	 ‚ Wd  QXd  S(   Nu   datau	   local.datuD   This file is used in the test_local_data_* testing functions
CONTENT(
   R,   R-   R   R   R   R*   R   RC   RU   R   (   t   fpathR$   (    (    s<   lib/python2.7/site-packages/astropy/utils/tests/test_data.pyt&   test_path_objects_get_readable_fileobjþ  s    $(   i   i   i    (C   t
   __future__R    R   R   R   RG   Rx   R   Rn   Rº   RM   R   R   R   R   R   t   externR   t   extern.six.movesR	   t   tests.helperR
   R   R   R   t   bz2t   ImportErrorR:   RQ   R   t   version_infoR   t	   backportsRR   R,   R+   R   R%   t   markt   skipift   platformt
   startswithR8   R;   RA   RF   RL   RN   t   parametrizeRX   t   fixtureRf   Rj   Rk   Rt   t   RuntimeErrorRu   R   Rƒ   RŸ   R¦   R³   R´   R¶   R¸   R¿   RÁ   (    (    (    s<   lib/python2.7/site-packages/astropy/utils/tests/test_data.pyt   <module>   sn   ""


"#!	!					R		
	