U
    ?h]+                     @   s  d Z ddlmZ ddlmZ ddlZddlZddlmZ ddl	m
Z
mZmZ dd	lmZmZmZmZ e Ze Zejjejjd
ddddddgddddddgddd Zejjdd Zejjejjdddgddgdejjd d!d"gfd#d$d%gfd$d&gfd$d'gfd$d%d'gfgd(d)d*d+d,d-gdejjd.e
d/fed0fgd1d2gdd3d4 Zejjejd.e
d/fed0fgd5d6 Zejjejd.e
d/fed0fgd7d8 Zd9d: Zd;d< Zejjejd.e
d/fed0fgd=d> Zejjejd.e
d/fed0fgd?d@ ZdS )Az
Test the processor hooks
    )Path)TemporaryDirectoryN   )Pooch)UnzipUntar
Decompress   )pooch_test_urlpooch_test_registrycheck_tiny_datacapture_logzmethod,ext,name)autoxzN)lzmar   N)r   r   N)bzip2bz2N)gzipgzN)r   r   zdifferent-name.txtr   r   r   r   r   name)Zidsc              
   C   sF  t | |d}t (}t|}|dkr@t|dd|dg }nt|| }t|ttd}t r}|j	d| |d}	|
 }
|
 }t|d	kst|d
  d
 dkst|d dst| |d kstW 5 Q R X |	|kstt|	 t *}|j	d| |d}	|
 dkstW 5 Q R X |	|ks0tt|	 W 5 Q R X dS )z=Check that decompression after download works for all formats)methodr   N.tiny-data.txtdecomppathbase_urlregistryztiny-data.txt.	processorr   r   DownloadingZDecompressing )r   r   r   strjoinr   BASEURLREGISTRYr   fetchgetvalue
splitlineslenAssertionErrorsplit
startswithr   )r   extr   r   local_storer   	true_pathpuplog_filefnameZlogslines r5   M/var/www/html/venv/lib/python3.8/site-packages/pooch/tests/test_processors.pytest_decompress   s,    
r7   c                  C   s  t  } t| }t|ttd}tt.}t	  |j
dtddd W 5 Q R X W 5 Q R X |jjd dsttd|jjd ksttt.}t	  |j
dtd	dd W 5 Q R X W 5 Q R X |jjd d
std|jjd ksttt.}t	  |j
dtddd W 5 Q R X W 5 Q R X |jjd dsFtd|jjd ks\ttt.}t	  |j
dtddd W 5 Q R X W 5 Q R X |jjd dstd|jjd kstW 5 Q R X dS )zGShould fail if method='auto' and no extension is given in the file namer   r   r   )r   r   r   z"Unrecognized file extension '.txt'zpooch.Unzip/UntarZblaz Invalid compression method 'bla'zipz Invalid compression method 'zip'z	store.zipz"Unrecognized file extension '.zip'N)r   r   r   r%   r&   pytestZraises
ValueErrorwarningscatch_warningsr'   r   valueargsr-   r+   )r/   r   r1   	exceptionr5   r5   r6   test_decompress_failsE   s.    

(
(
(
(r@   target_pathZsome_custom_pathZdefault_pathZcustom_pathzarchive,members	tiny-datar   )storeNrC   store/tiny-data.txtstore/subdir/tiny-data.txtzstore/subdirZsingle_fileZarchive_allZarchive_fileZarchive_subdir_fileZarchive_subdirZarchive_multiplezprocessor_class,extensionz.zipz.tar.gzr   r   c              
   C   s  | ||d}|dkr"|| |j  }t }t|}t|||| | j\}}t|ttd}	t 2}
|	j	|| |d}t
||kstt|
| W 5 Q R X |D ]}t| qt 2}
|	j	|| |d}t
||kstt|
g  W 5 Q R X |D ]}t| qW 5 Q R X dS )zGTests the behaviour of processors for unpacking archives (Untar, Unzip))membersextract_dirNr   r   )suffixr   r   "_unpacking_expected_paths_and_logs__name__r   r%   r&   r   r'   setr+   _check_logsr   )processor_class	extensionrA   archiverF   r   r   
true_pathsZexpected_logr1   r2   fnamesr3   r5   r5   r6   test_unpackingd   s0       
rR   c           	   	   C   s   t  }tt|ttd}| dgd}|jd| |d}t|dksHtt|d  | ddgd}|jd| |d}t|d	kstt|d  t|d  |jd| |d}t|dkstt|d  W 5 Q R X d
S )zHTest that multiple subsequent calls to a processor yield correct resultsr   rD   rF   rC   r   r	   r   rE   r   N	r   r   r   r%   r&   r'   r*   r+   r   )	rM   rN   r/   r1   
processor1
filenames1
processor2
filenames2Z
filenames3r5   r5   r6   test_multiple_unpacking   s     rY   c              	   C   sb   t  R}tt|ttd}| dgd}|jd| |d}t|dksHtt|d  W 5 Q R X dS )	zDTest that unpack members can also be specifed both with a leading ./r   z./store/tiny-data.txtrS   rC   r   r	   r   NrT   )rM   rN   r/   r1   rU   rV   r5   r5   r6   $test_unpack_members_with_leading_dot   s    rZ   c                 C   sF   |    }t|t|ks tt||D ]\}}||s*tq*dS )zC
    Assert that the lines in the log match the expected ones.
    N)r(   r)   r*   r+   r8   r-   )r2   Zexpected_linesr4   lineZexpected_liner5   r5   r6   rL      s    rL   c                 C   s   dg}| dkr(t |d h}|d n| dkrv|dkrvt |d d t |d d d h}|| |d  d	 nn| dkr|dk	rg }|D ]L}|t|d
  }t |ds|d }|t | |d| d qt|}||fS )zo
    Generate the appropriate expected paths and log message depending on the
    parameters for the test.
    r    rB   r   zExtracting 'tiny-data.txt'rC   Nsubdirr!   zing contents/zExtracting '')r#   appendr   r,   endswithrK   )rO   rF   r   r   Z	log_linesrP   memberr0   r5   r5   r6   rI      s&    rI   c              	   C   s~   t  n}tt|ttd}| dgd}|jd| |d}t|dksHt|  }|jd| |d}t|dksptW 5 Q R X dS )zx
    Test that calling with valid members then without them works.
    https://github.com/fatiando/pooch/issues/364
    r   rD   rS   rC   r   r	   Nr   r   r   r%   r&   r'   r*   r+   rM   rN   r/   r1   rU   rV   rW   rX   r5   r5   r6   &test_unpacking_members_then_no_members   s    
rd   c              	   C   s~   t  n}tt|ttd}| dgd}|jd| |d}t|dksHt|  }|jd| |d}t|dksptW 5 Q R X dS )zz
    Test that calling with invalid members then without them works.
    https://github.com/fatiando/pooch/issues/364
    r   znot-a-valid-file.csvrS   rC   r   r   Nrb   rc   r5   r5   r6   ,test_unpacking_wrong_members_then_no_members  s    
re   ) __doc__pathlibr   tempfiler   r;   r9   r"   r   Z
processorsr   r   r   utilsr
   r   r   r   r&   r%   marknetworkZparametrizer7   r@   rR   rY   rZ   rL   rI   rd   re   r5   r5   r5   r6   <module>   s   
  
	
