o
    Ô­jK)  ã                   @   sT  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 d1dd„Zdad	d
„ Zdd„ ZdZdZdZdZg d¢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 d-d.„ d.ƒZ.d/d0„ Z/dS )2é    N)ÚmkdtempÚmkstempÚNamedTemporaryFile)Úrmtree)Úassert_Úassert_equalÚassert_raises)Úurlparse)ÚURLErrorc                 C   s    | t ƒ krtdd�}|S tdƒ‚)z$Stub to replace urlopen for testing.Úurltmp_)ÚprefixzName or service not known)Úvalid_httpurlr   r
   )ÚurlÚdataÚtmpfile© r   ú]/var/www/html/CropPilot/venv/lib/python3.10/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.txt)z/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©Ú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   ó   t t S r   )Ú	http_pathÚ	http_filer   r   r   r   r   A   ó   r   c                   C   r(   r   )Úhttp_fakepathÚhttp_fakefiler   r   r   r   Úinvalid_httpurlE   r+   r.   c                   C   ó   t S r   )r)   r   r   r   r   Úvalid_baseurlI   ó   r0   c                   C   r/   r   )r,   r   r   r   r   Úinvalid_baseurlM   r1   r2   c                   C   r/   r   )r*   r   r   r   r   Úvalid_httpfileQ   r1   r3   c                   C   r/   r   )r-   r   r   r   r   Úinvalid_httpfileU   r1   r4   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   ó   t ƒ | _t | j¡| _d S r   ©r   ÚtmpdirÚ
datasourceÚ
DataSourceÚds©Úselfr   r   r   Úsetup_methodZ   ó   zTestDataSourceOpen.setup_methodc                 C   ó   t | jƒ | `d S r   ©r   r8   r;   r<   r   r   r   Úteardown_method^   ó   
z"TestDataSourceOpen.teardown_methodc                 C   s"   | j  tƒ ¡}t|ƒ | ¡  d S r   )r;   Úopenr   r   r    )r=   Úfhr   r   r   Útest_ValidHTTPb   s   z!TestDataSourceOpen.test_ValidHTTPc              
   C   s^   t ƒ }tt| jj|ƒ z	| j |¡ W d S  ty. } zt|jd u ƒ W Y d }~d S d }~ww r   )r.   r   ÚOSErrorr;   rD   r   Úerrno)r=   r   Úer   r   r   Útest_InvalidHTTPg   s   €þz#TestDataSourceOpen.test_InvalidHTTPc                 C   s   t t| jjtƒ ƒ d S r   )r   r
   r;   Ú_cacher.   r<   r   r   r   Útest_InvalidHTTPCacheURLErrorp   ó   z0TestDataSourceOpen.test_InvalidHTTPCacheURLErrorc                 C   s*   t | jƒ}| j |¡}t|ƒ | ¡  d S r   )r%   r8   r;   rD   r   r    )r=   Ú
local_filerE   r   r   r   Útest_ValidFiles   s   
z!TestDataSourceOpen.test_ValidFilec                 C   s   t | jƒ}tt| jj|ƒ d S r   )r'   r8   r   rG   r;   rD   )r=   Úinvalid_filer   r   r   Útest_InvalidFiley   s   
z#TestDataSourceOpen.test_InvalidFilec                 C   s€   zdd l }W n ty   t ¡  Y nw tj | jd¡}| |d¡}| 	t
¡ | ¡  | j |¡}| ¡ }| ¡  tt
|ƒ d S )Nr   zfoobar.txt.gzÚw)ÚgzipÚImportErrorÚpytestÚskipr   r$   Újoinr8   rD   ÚwriteÚ
magic_liner    r;   Úreadliner   )r=   rS   ÚfilepathÚfpÚresultr   r   r   Útest_ValidGzipFile}   ó   þ
z%TestDataSourceOpen.test_ValidGzipFilec                 C   s€   zdd l }W n ty   t ¡  Y nw tj | jd¡}| |d¡}| 	t
¡ | ¡  | j |¡}| ¡ }| ¡  tt
|ƒ d S )Nr   zfoobar.txt.bz2rR   )Úbz2rT   rU   rV   r   r$   rW   r8   ÚBZ2FilerX   rY   r    r;   rD   rZ   r   )r=   r`   r[   r\   r]   r   r   r   Útest_ValidBz2File�   r_   z$TestDataSourceOpen.test_ValidBz2FileN)Ú__name__Ú
__module__Ú__qualname__r>   rB   rF   rJ   rL   rO   rQ   r^   rb   r   r   r   r   r5   Y   s    	r5   c                   @   ó<   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   r6   r   r7   r<   r   r   r   r>   Ÿ   r?   z!TestDataSourceExists.setup_methodc                 C   r@   r   rA   r<   r   r   r   rB   £   rC   z$TestDataSourceExists.teardown_methodc                 C   ó   t | j tƒ ¡ƒ d S r   )r   r;   Úexistsr   r<   r   r   r   rF   §   rM   z#TestDataSourceExists.test_ValidHTTPc                 C   s   t | j tƒ ¡dƒ d S ©NF)r   r;   ri   r.   r<   r   r   r   rJ   ª   s   z%TestDataSourceExists.test_InvalidHTTPc                 C   sD   t | jƒ}t| j |¡ƒ tƒ }t |ƒ}t| j |¡ƒ t|ƒ d S r   )r%   r8   r   r;   ri   r   r   )r=   r   Úlocaldirr   r   r   rO   ­   s   
z#TestDataSourceExists.test_ValidFilec                 C   ó    t | jƒ}t| j |¡dƒ d S rj   )r'   r8   r   r;   ri   ©r=   r   r   r   r   rQ   ·   ó   
z%TestDataSourceExists.test_InvalidFileN)	rc   rd   re   r>   rB   rF   rJ   rO   rQ   r   r   r   r   rg   ž   s    
rg   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   r8   r9   r:   r;   r<   r   r   r   r>   ½   s   z"TestDataSourceAbspath.setup_methodc                 C   r@   r   rA   r<   r   r   r   rB   Á   rC   z%TestDataSourceAbspath.teardown_methodc                 C   sN   t tƒ ƒ\}}}}}}tj | j|| tj¡ d¡¡}t|| j	 
tƒ ¡ƒ d S ©Nú/)r	   r   r   r$   rW   r8   ÚstripÚsepr   r;   rp   )r=   ÚschemeÚnetlocÚupathÚpmsÚqryÚfrgÚ
local_pathr   r   r   rF   Å   s
   ÿz$TestDataSourceAbspath.test_ValidHTTPc                 C   sB   t | jƒ}tj |¡d }t|| j |¡ƒ t|| j |¡ƒ d S ©Néÿÿÿÿ)r%   r8   r   r$   Úsplitr   r;   rp   )r=   r   Útmpfilenamer   r   r   rO   Ë   s   
z$TestDataSourceAbspath.test_ValidFilec                 C   sP   t tƒ ƒ\}}}}}}tj | j|| tj¡ d¡¡}t|| j	 
tƒ ¡kƒ d S rq   )r	   r.   r   r$   rW   r8   rs   rt   r   r;   rp   r   )r=   ru   rv   rw   rx   ry   rz   Úinvalidhttpr   r   r   rJ   Ó   s
   ÿz&TestDataSourceAbspath.test_InvalidHTTPc                 C   sP   t | jƒ}t | jƒ}tj |¡d }t|| j |¡kƒ t|| j |¡kƒ d S r|   )r%   r8   r   r$   r~   r   r;   rp   )r=   Úinvalidfiler   r   r   r   r   rQ   Ù   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 ]}t|t
| ƒ ˆ j¡ƒ t||ƒ ˆ j¡ƒ q?d S )Nr}   c                    ó   t j ˆ j | ¡¡S r   )r   r$   rp   r;   ©Úxr<   r   r   Ú<lambda>æ   ó    z7TestDataSourceAbspath.test_sandboxing.<locals>.<lambda>)r%   r8   r   r$   r~   r   r   Ú
startswithr.   Úmalicious_filesr)   )r=   r   r   Útmp_pathÚfnr   r<   r   Útest_sandboxingâ   s   
þz%TestDataSourceAbspath.test_sandboxingc                 C   sJ   t j}zdt _|  ¡  |  ¡  |  ¡  |  ¡  |  ¡  W |t _d S |t _w ©Nú\)r   rt   rF   rO   rJ   rQ   r‹   ©r=   Úorig_os_sepr   r   r   Útest_windows_os_sepð   s   
z)TestDataSourceAbspath.test_windows_os_sepN)rc   rd   re   r>   rB   rF   rO   rJ   rQ   r‹   r�   r   r   r   r   ro   ¼   s    	ro   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$   rp   r   r8   r9   Ú
Repositoryr0   Úreposr<   r   r   r   r>   þ   s   z"TestRepositoryAbspath.setup_methodc                 C   r@   r   ©r   r8   r“   r<   r   r   r   rB     rC   z%TestRepositoryAbspath.teardown_methodc           	      C   sT   t tƒ ƒ\}}}}}}tj | jj|| tj¡ d¡¡}| j 	t
ƒ ¡}t||ƒ d S rq   )r	   r   r   r$   rW   r“   Ú	_destpathrs   rt   rp   r3   r   )	r=   ru   rv   rw   rx   ry   rz   r{   r[   r   r   r   rF     s   ÿz$TestRepositoryAbspath.test_ValidHTTPc                    s\   ‡ fdd„}t |tƒ ƒ ˆ j¡ƒ tD ]}t |t| ƒ ˆ j¡ƒ t ||ƒ ˆ j¡ƒ qd S )Nc                    r‚   r   )r   r$   rp   r“   rƒ   r<   r   r   r…     r†   z7TestRepositoryAbspath.test_sandboxing.<locals>.<lambda>)r   r3   r‡   r8   rˆ   r)   )r=   r‰   rŠ   r   r<   r   r‹     s   þz%TestRepositoryAbspath.test_sandboxingc                 C   s2   t j}zdt _|  ¡  |  ¡  W |t _d S |t _w rŒ   )r   rt   rF   r‹   rŽ   r   r   r   r�     s   
z)TestRepositoryAbspath.test_windows_os_sepN)rc   rd   re   r>   rB   rF   r‹   r�   r   r   r   r   r‘   ý   s    r‘   c                   @   rf   )ÚTestRepositoryExistsc                 C   s   t ƒ | _t tƒ | j¡| _d S r   )r   r8   r9   r’   r0   r“   r<   r   r   r   r>     s   z!TestRepositoryExists.setup_methodc                 C   r@   r   r”   r<   r   r   r   rB   #  rC   z$TestRepositoryExists.teardown_methodc                 C   s   t | jƒ}t| j |¡ƒ d S r   )r%   r8   r   r“   ri   rm   r   r   r   rO   '  s   
z#TestRepositoryExists.test_ValidFilec                 C   rl   rj   )r'   r8   r   r“   ri   rm   r   r   r   rQ   ,  rn   z%TestRepositoryExists.test_InvalidFilec                 C   rh   r   )r   r“   ri   r   r<   r   r   r   Útest_RemoveHTTPFile0  rM   z(TestRepositoryExists.test_RemoveHTTPFilec           
      C   sT   t ƒ }t|ƒ\}}}}}}tj | jj|¡}t |d¡ t|ƒ}	t	| j 
|	¡ƒ d S )NiÀ  )r   r	   r   r$   rW   r“   r•   Úmkdirr%   r   ri   )
r=   Ú	localfileru   rv   rw   rx   ry   rz   r{   r   r   r   r   Útest_CachedHTTPFile3  s   z(TestRepositoryExists.test_CachedHTTPFileN)	rc   rd   re   r>   rB   rO   rQ   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   r8   r<   r   r   r   r>   @  s   zTestOpenFunc.setup_methodc                 C   s   t | jƒ d S r   )r   r8   r<   r   r   r   rB   C  s   zTestOpenFunc.teardown_methodc                 C   sH   t | jƒ}tj|| jd�}t|ƒ | ¡  t |¡}t|ƒ | ¡  d S )N)Údestpath)r%   r8   r9   rD   r   r    )r=   rN   r\   r   r   r   Útest_DataSourceOpenF  s   

z TestOpenFunc.test_DataSourceOpenN)rc   rd   re   r>   rB   r�   r   r   r   r   r›   ?  s    r›   c                  C   s   t  ¡ } | `|  ¡  d S r   )r9   r:   Ú
_istmpdestÚ__del__)r;   r   r   r   Útest_del_attr_handlingQ  s   r    r   )0r   rU   Útempfiler   r   r   Úshutilr   Únumpy.lib._datasourceÚlibÚ_datasourcer9   Únumpy.testingr   r   r   Úurllib.requestÚrequestr   Úurllib.parser	   Úurllib.errorr
   r   r   r   r   r)   r*   r,   r-   rˆ   rY   r%   r'   r   r.   r0   r2   r3   r4   r5   rg   ro   r‘   r–   r›   r    r   r   r   r   Ú<module>   sD    
	EA!!