U
    ¸mœdK)  ã                   @   sX  d dl Z d dlZd dlmZmZmZ d dlmZ d dlm	  m
Z d dlmZmZmZ d dlmZ d dlmZ d dlmZ d4dd„Zdad	d
„ Zdd„ ZdZdZdZdZddddgZdZ dd„ Z!dd„ Z"dd„ Z#dd„ Z$dd„ Z%d d!„ Z&d"d#„ Z'd$d%„ Z(G d&d'„ d'ƒZ)G d(d)„ d)ƒZ*G d*d+„ d+ƒZ+G d,d-„ d-ƒZ,G d.d/„ d/ƒZ-G d0d1„ d1ƒZ.d2d3„ Z/dS )5é    N)ÚmkdtempÚmkstempÚNamedTemporaryFile)Úrmtree)Úassert_Úassert_equalÚassert_raises)Úurlparse)ÚURLErrorc                 C   s$   | t ƒ krtdd�}|S tdƒ‚dS )z$Stub to replace urlopen for testing.Zurltmp_)ÚprefixzName or service not knownN)Úvalid_httpurlr   r
   )ÚurlÚdataÚtmpfile© r   úY/home/sam/Atlas/atlas_env/lib/python3.8/site-packages/numpy/lib/tests/test__datasource.pyÚurlopen_stub   s    

r   c                   C   s   t jatt _d S ©N)Úurllib_requestÚurlopenÚold_urlopenr   r   r   r   r   Úsetup_module   s    r   c                   C   s
   t t_d S r   )r   r   r   r   r   r   r   Úteardown_module!   s    r   zhttp://www.google.com/z
index.htmlzhttp://fake.abc.web/site/zfake.txtz/etc/shadowz../../shadowz..\system.datzc:\windows\system.dats   three is the magic numberc                 C   s"   t dd| dd�\}}t |¡ |S )Nú.txtÚdstmp_T)Úsuffixr   ÚdirÚtext)r   ÚosÚclose©ZfiledirÚfdÚpathr   r   r   Úvalid_textfile2   s    
r#   c                 C   s*   t dd| d�\}}t |¡ t |¡ |S )Nr   r   )r   r   r   )r   r   r   Úremover    r   r   r   Úinvalid_textfile9   s    

r%   c                   C   s   t t S r   )Ú	http_pathÚ	http_filer   r   r   r   r   A   s    r   c                   C   s   t t S r   )Úhttp_fakepathÚhttp_fakefiler   r   r   r   Úinvalid_httpurlE   s    r*   c                   C   s   t S r   )r&   r   r   r   r   Úvalid_baseurlI   s    r+   c                   C   s   t S r   )r(   r   r   r   r   Úinvalid_baseurlM   s    r,   c                   C   s   t S r   )r'   r   r   r   r   Úvalid_httpfileQ   s    r-   c                   C   s   t S r   )r)   r   r   r   r   Úinvalid_httpfileU   s    r.   c                   @   sT   e Zd Zdd„ Zdd„ Zdd„ Zdd„ Zd	d
„ Zdd„ Zdd„ Z	dd„ Z
dd„ ZdS )ÚTestDataSourceOpenc                 C   s   t ƒ | _t | j¡| _d S r   ©r   ÚtmpdirÚ
datasourceÚ
DataSourceÚds©Úselfr   r   r   Úsetup_methodZ   s    zTestDataSourceOpen.setup_methodc                 C   s   t | jƒ | `d S r   ©r   r1   r4   r5   r   r   r   Úteardown_method^   s    
z"TestDataSourceOpen.teardown_methodc                 C   s"   | j  tƒ ¡}t|ƒ | ¡  d S r   )r4   Úopenr   r   r   )r6   Úfhr   r   r   Útest_ValidHTTPb   s    z!TestDataSourceOpen.test_ValidHTTPc              
   C   s\   t ƒ }tt| jj|ƒ z| j |¡ W n0 tk
rV } zt|jd kƒ W 5 d }~X Y nX d S r   )r*   r   ÚOSErrorr4   r:   r   Úerrno)r6   r   Úer   r   r   Útest_InvalidHTTPg   s    z#TestDataSourceOpen.test_InvalidHTTPc                 C   s   t t| jjtƒ ƒ d S r   )r   r
   r4   Ú_cacher*   r5   r   r   r   Útest_InvalidHTTPCacheURLErrorp   s    z0TestDataSourceOpen.test_InvalidHTTPCacheURLErrorc                 C   s*   t | jƒ}| j |¡}t|ƒ | ¡  d S r   )r#   r1   r4   r:   r   r   )r6   Ú
local_filer;   r   r   r   Útest_ValidFiles   s    
z!TestDataSourceOpen.test_ValidFilec                 C   s   t | jƒ}tt| jj|ƒ d S r   )r%   r1   r   r=   r4   r:   )r6   Zinvalid_filer   r   r   Útest_InvalidFiley   s    
z#TestDataSourceOpen.test_InvalidFilec                 C   s‚   zdd l }W n tk
r(   t ¡  Y nX tj | jd¡}| |d¡}| 	t
¡ | ¡  | j |¡}| ¡ }| ¡  tt
|ƒ d S )Nr   zfoobar.txt.gzÚw)ÚgzipÚImportErrorÚpytestÚskipr   r"   Újoinr1   r:   ÚwriteÚ
magic_liner   r4   Úreadliner   )r6   rG   ÚfilepathÚfpÚresultr   r   r   Útest_ValidGzipFile}   s    
z%TestDataSourceOpen.test_ValidGzipFilec                 C   s‚   zdd l }W n tk
r(   t ¡  Y nX tj | jd¡}| |d¡}| 	t
¡ | ¡  | j |¡}| ¡ }| ¡  tt
|ƒ d S )Nr   zfoobar.txt.bz2rF   )Úbz2rH   rI   rJ   r   r"   rK   r1   ÚBZ2FilerL   rM   r   r4   r:   rN   r   )r6   rS   rO   rP   rQ   r   r   r   Útest_ValidBz2File�   s    
z$TestDataSourceOpen.test_ValidBz2FileN)Ú__name__Ú
__module__Ú__qualname__r7   r9   r<   r@   rB   rD   rE   rR   rU   r   r   r   r   r/   Y   s   	r/   c                   @   s<   e Zd Zdd„ Zdd„ Zdd„ Zdd„ Zd	d
„ Zdd„ ZdS )ÚTestDataSourceExistsc                 C   s   t ƒ | _t | j¡| _d S r   r0   r5   r   r   r   r7   Ÿ   s    z!TestDataSourceExists.setup_methodc                 C   s   t | jƒ | `d S r   r8   r5   r   r   r   r9   £   s    
z$TestDataSourceExists.teardown_methodc                 C   s   t | j tƒ ¡ƒ d S r   )r   r4   Úexistsr   r5   r   r   r   r<   §   s    z#TestDataSourceExists.test_ValidHTTPc                 C   s   t | j tƒ ¡dƒ d S ©NF)r   r4   rZ   r*   r5   r   r   r   r@   ª   s    z%TestDataSourceExists.test_InvalidHTTPc                 C   sD   t | jƒ}t| j |¡ƒ tƒ }t |ƒ}t| j |¡ƒ t|ƒ d S r   )r#   r1   r   r4   rZ   r   r   )r6   r   Zlocaldirr   r   r   rD   ­   s    
z#TestDataSourceExists.test_ValidFilec                 C   s    t | jƒ}t| j |¡dƒ d S r[   )r%   r1   r   r4   rZ   ©r6   r   r   r   r   rE   ·   s    
z%TestDataSourceExists.test_InvalidFileN)	rV   rW   rX   r7   r9   r<   r@   rD   rE   r   r   r   r   rY   ž   s   
rY   c                   @   sL   e Zd Zdd„ Zdd„ Zdd„ Zdd„ Zd	d
„ Zdd„ Zdd„ Z	dd„ Z
dS )ÚTestDataSourceAbspathc                 C   s"   t j tƒ ¡| _t | j¡| _d S r   )r   r"   Úabspathr   r1   r2   r3   r4   r5   r   r   r   r7   ½   s    z"TestDataSourceAbspath.setup_methodc                 C   s   t | jƒ | `d S r   r8   r5   r   r   r   r9   Á   s    
z%TestDataSourceAbspath.teardown_methodc                 C   sN   t tƒ ƒ\}}}}}}tj | j|| tj¡ d¡¡}t|| j	 
tƒ ¡ƒ d S ©Nú/)r	   r   r   r"   rK   r1   ÚstripÚsepr   r4   r^   )r6   ÚschemeÚnetlocÚupathÚpmsÚqryÚfrgÚ
local_pathr   r   r   r<   Å   s
    ÿz$TestDataSourceAbspath.test_ValidHTTPc                 C   sB   t | jƒ}tj |¡d }t|| j |¡ƒ t|| j |¡ƒ d S ©Néÿÿÿÿ)r#   r1   r   r"   Úsplitr   r4   r^   )r6   r   Útmpfilenamer   r   r   rD   Ë   s    
z$TestDataSourceAbspath.test_ValidFilec                 C   sP   t tƒ ƒ\}}}}}}tj | j|| tj¡ d¡¡}t|| j	 
tƒ ¡kƒ d S r_   )r	   r*   r   r"   rK   r1   ra   rb   r   r4   r^   r   )r6   rc   rd   re   rf   rg   rh   Zinvalidhttpr   r   r   r@   Ó   s
    ÿz&TestDataSourceAbspath.test_InvalidHTTPc                 C   sP   t | jƒ}t | jƒ}tj |¡d }t|| j |¡kƒ t|| j |¡kƒ d S rj   )r#   r1   r   r"   rl   r   r4   r^   )r6   Zinvalidfiler   rm   r   r   r   rE   Ù   s
    

z&TestDataSourceAbspath.test_InvalidFilec                    s´   t ˆ jƒ}tj |¡d }‡ fdd„}t|tƒ ƒ ˆ j¡ƒ t|tƒ ƒ ˆ j¡ƒ t||ƒ ˆ j¡ƒ t||ƒ ˆ j¡ƒ t	D ]0}t|t
| ƒ ˆ j¡ƒ t||ƒ ˆ j¡ƒ q~d S )Nrk   c                    s   t j ˆ j | ¡¡S r   )r   r"   r^   r4   ©Úxr5   r   r   Ú<lambda>æ   ó    z7TestDataSourceAbspath.test_sandboxing.<locals>.<lambda>)r#   r1   r   r"   rl   r   r   Ú
startswithr*   Úmalicious_filesr&   )r6   r   rm   Útmp_pathÚfnr   r5   r   Útest_sandboxingâ   s    
z%TestDataSourceAbspath.test_sandboxingc                 C   sF   t j}z2dt _|  ¡  |  ¡  |  ¡  |  ¡  |  ¡  W 5 |t _X d S ©Nú\)r   rb   r<   rD   r@   rE   rv   ©r6   Zorig_os_sepr   r   r   Útest_windows_os_sepð   s    z)TestDataSourceAbspath.test_windows_os_sepN)rV   rW   rX   r7   r9   r<   rD   r@   rE   rv   rz   r   r   r   r   r]   ¼   s   	r]   c                   @   s4   e Zd Zdd„ Zdd„ Zdd„ Zdd„ Zd	d
„ ZdS )ÚTestRepositoryAbspathc                 C   s&   t j tƒ ¡| _t tƒ | j¡| _d S r   )	r   r"   r^   r   r1   r2   Ú
Repositoryr+   Úreposr5   r   r   r   r7   þ   s    z"TestRepositoryAbspath.setup_methodc                 C   s   t | jƒ | `d S r   ©r   r1   r}   r5   r   r   r   r9     s    
z%TestRepositoryAbspath.teardown_methodc           	      C   sT   t tƒ ƒ\}}}}}}tj | jj|| tj¡ d¡¡}| j 	t
ƒ ¡}t||ƒ d S r_   )r	   r   r   r"   rK   r}   Ú	_destpathra   rb   r^   r-   r   )	r6   rc   rd   re   rf   rg   rh   ri   rO   r   r   r   r<     s    ÿz$TestRepositoryAbspath.test_ValidHTTPc                    s\   ‡ fdd„}t |tƒ ƒ ˆ j¡ƒ tD ]0}t |t| ƒ ˆ j¡ƒ t ||ƒ ˆ j¡ƒ q&d S )Nc                    s   t j ˆ j | ¡¡S r   )r   r"   r^   r}   rn   r5   r   r   rp     rq   z7TestRepositoryAbspath.test_sandboxing.<locals>.<lambda>)r   r-   rr   r1   rs   r&   )r6   rt   ru   r   r5   r   rv     s
    z%TestRepositoryAbspath.test_sandboxingc                 C   s.   t j}zdt _|  ¡  |  ¡  W 5 |t _X d S rw   )r   rb   r<   rv   ry   r   r   r   rz     s    z)TestRepositoryAbspath.test_windows_os_sepN)rV   rW   rX   r7   r9   r<   rv   rz   r   r   r   r   r{   ý   s
   r{   c                   @   s<   e Zd Zdd„ Zdd„ Zdd„ Zdd„ Zd	d
„ Zdd„ ZdS )ÚTestRepositoryExistsc                 C   s   t ƒ | _t tƒ | j¡| _d S r   )r   r1   r2   r|   r+   r}   r5   r   r   r   r7     s    z!TestRepositoryExists.setup_methodc                 C   s   t | jƒ | `d S r   r~   r5   r   r   r   r9   #  s    
z$TestRepositoryExists.teardown_methodc                 C   s   t | jƒ}t| j |¡ƒ d S r   )r#   r1   r   r}   rZ   r\   r   r   r   rD   '  s    
z#TestRepositoryExists.test_ValidFilec                 C   s    t | jƒ}t| j |¡dƒ d S r[   )r%   r1   r   r}   rZ   r\   r   r   r   rE   ,  s    
z%TestRepositoryExists.test_InvalidFilec                 C   s   t | j tƒ ¡ƒ d S r   )r   r}   rZ   r   r5   r   r   r   Útest_RemoveHTTPFile0  s    z(TestRepositoryExists.test_RemoveHTTPFilec           
      C   sT   t ƒ }t|ƒ\}}}}}}tj | jj|¡}t |d¡ t|ƒ}	t	| j 
|	¡ƒ d S )NiÀ  )r   r	   r   r"   rK   r}   r   Úmkdirr#   r   rZ   )
r6   Ú	localfilerc   rd   re   rf   rg   rh   ri   r   r   r   r   Útest_CachedHTTPFile3  s    z(TestRepositoryExists.test_CachedHTTPFileN)	rV   rW   rX   r7   r9   rD   rE   r�   r„   r   r   r   r   r€     s   r€   c                   @   s$   e Zd Zdd„ Zdd„ Zdd„ ZdS )ÚTestOpenFuncc                 C   s   t ƒ | _d S r   )r   r1   r5   r   r   r   r7   @  s    zTestOpenFunc.setup_methodc                 C   s   t | jƒ d S r   )r   r1   r5   r   r   r   r9   C  s    zTestOpenFunc.teardown_methodc                 C   sH   t | jƒ}tj|| jd�}t|ƒ | ¡  t |¡}t|ƒ | ¡  d S )N)Zdestpath)r#   r1   r2   r:   r   r   )r6   rC   rP   r   r   r   Útest_DataSourceOpenF  s    

z TestOpenFunc.test_DataSourceOpenN)rV   rW   rX   r7   r9   r†   r   r   r   r   r…   ?  s   r…   c                  C   s   t  ¡ } | `|  ¡  d S r   )r2   r3   Z
_istmpdestÚ__del__)r4   r   r   r   Útest_del_attr_handlingQ  s    rˆ   )N)0r   rI   Útempfiler   r   r   Úshutilr   Znumpy.lib._datasourceÚlibZ_datasourcer2   Znumpy.testingr   r   r   Úurllib.requestÚrequestr   Úurllib.parser	   Úurllib.errorr
   r   r   r   r   r&   r'   r(   r)   rs   rM   r#   r%   r   r*   r+   r,   r-   r.   r/   rY   r]   r{   r€   r…   rˆ   r   r   r   r   Ú<module>   sH   
	 ÿEA!!