U
    hâËd}  ã                   @   sª   d dl mZ d dlm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 Z zd dlmZ dd	„ ZW n( ek
r”   d dlmZ d
d	„ ZY nX G dd„ deƒZdS )é    )ÚTestCase)Úutils)Úconvert)ÚreadsN)Úcopy)ÚEmptyc                 C   s
   t | tƒS ©N)Ú
isinstanceÚstr©Ús© r   úO/home/sam/Atlas/atlas_env/lib/python3.8/site-packages/numba/testing/notebook.pyÚisstr   s    r   c                 C   s
   t | tƒS r   )r	   Ú
basestringr   r   r   r   r      s    c                   @   sl   e Zd ZdZddddg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dS )ÚNotebookTestaV  Validate a notebook. All code cells are executed in order. The output is either checked
    for errors (if no reference output is present), or is compared against expected output.


    Useful references:
    http://nbformat.readthedocs.org/en/latest/format_description.html
    http://jupyter-client.readthedocs.org/en/latest/messaging.html
Zexecute_requestZexecute_inputÚstatusZpyinZexecution_countÚ	tracebackZprompt_numberÚsourceé   c           	   	   C   sl   t |ƒ�}tt| ¡ ƒ| jƒ}W 5 Q R X t ¡ \}}tdd„ |jD ƒƒD ]\}}|  	|  
|¡|||¡ qHd S )Nc                 S   s   g | ]}|j d kr|‘qS )Úcode)Z	cell_type)Ú.0Úcr   r   r   Ú
<listcomp>-   s     
 z/NotebookTest._test_notebook.<locals>.<listcomp>)Úopenr   r   ÚreadÚNBFORMAT_VERSIONr   Zstart_new_kernelÚ	enumerateÚcellsÚ_test_notebook_cellÚsanitize_cell)	ÚselfÚnotebookÚtestÚfÚnbÚ_ÚkernelÚir   r   r   r   Ú_test_notebook(   s
    
zNotebookTest._test_notebookc                    s:  t |dƒr|j}n|j}|j}| |¡ g }d }d}	d}
d}ˆ  |¡røz|jddd�}W n tk
rp   Y q<Y nX |d ˆ jkr<|d dkr¼d	|d
 d  d |d
 d  }d}	|
dkr¼|}
t	|ƒ}|t	|j
ƒk rÜ|j
| pÞg }ˆ  ||¡}| |¡ q<|dk�rˆ  |	d|
|f ¡ n‡ fdd„}||ƒ}||j
ƒ}d S )Nr   TéÿÿÿÿÚ é   )ÚblockÚtimeoutÚmsg_typeÚerrorz  ÚcontentZenamez
  ZevalueFZcheck_errorz*Executing cell %d resulted in an error:
%sc                    s   ˆ   tˆ  | ¡ƒ¡S r   )Údump_canonicalÚlistÚscrub_outputs)Úx©r!   r   r   Ú<lambda>S   ó    z2NotebookTest._test_notebook_cell.<locals>.<lambda>)Úhasattrr   ÚinputZiopub_channelÚexecuteÚshould_continueZget_msgr   ÚIGNORE_TYPESÚlenÚoutputsÚtransform_messageÚappendÚ
assertTrue)r!   Úcellr(   r'   r#   r   Ziopubr?   ÚmsgZno_errorZfirst_errorÚ	error_msgÚexpectedÚoÚscrubZscrubbedr   r6   r   r   0   s<    



 
z NotebookTest._test_notebook_cellc                 C   s   t j|ddd�S )Né   T)ÚindentÚ	sort_keys)ÚjsonÚdumps)r!   Úobjr   r   r   r2   Z   s    zNotebookTest.dump_canonicalc                 #   sž   |D ]”}t |ƒ}dD ]|\‰ ‰‡ ‡fdd„}d|krB||d ƒ|d< d|krt|d tƒr€|d  ¡ D ]\}}||ƒ|d |< qdq||d ƒ|d< q|V  qdS )z=
        remove all scrubs from output data and text
        r   c                    s.   t | ƒrt ˆ ˆ| ¡S ‡ ‡fdd„| D ƒS d S )Nc                    s   g | ]}t  ˆ ˆ|¡‘qS r   )ÚreÚsub)r   Úline©rH   rP   r   r   r   i   s     zCNotebookTest.scrub_outputs.<locals>._scrubLines.<locals>.<listcomp>)r   rO   rP   )ÚlinesrR   r   r   Ú_scrubLinese   s    z/NotebookTest.scrub_outputs.<locals>._scrubLinesÚtextÚdataN)r   r	   ÚdictÚitems)r!   r?   ÚoutputÚoutrT   ÚmimerV   r   rR   r   r4   ]   s    zNotebookTest.scrub_outputsc                 C   s   | j D ]}| |d¡ q|S )zE
        remove keys from STRIP_KEYS to ensure comparability
        N)Ú
STRIP_KEYSÚpop)r!   ÚdÚkeyr   r   r   Ú
strip_keysv   s    
zNotebookTest.strip_keysc                 C   s   |j D ]}|  |¡ q|S )z0
        remove non-reproducible things
        )r?   r`   )r!   rC   rY   r   r   r   r    ~   s    
zNotebookTest.sanitize_cellc                 C   sŽ   ddddœi}d|d i}|  |d ¡ |  |¡}| ¡ D ],\}}||kr:|| |kr:|||  ||< q:d|krŠd|krŠ|d |d< |d= |S )	zF
        transform a message into something like the notebook
        Úoutput_typeZexecute_resultr0   )ZpyoutZpyerrr/   r1   rV   rU   )Úupdater`   rX   )r!   rD   rF   Z	SWAP_KEYSrY   r_   Zswapsr   r   r   r@   †   s"    þÿ ÿ
zNotebookTest.transform_messagec                 C   s*   |dkrdS |d dko&|d d dk S )zQ
        determine whether the current message is the last for this cell
        NTr/   r   r1   Zexecution_stateÚidler   )r!   rD   r   r   r   r<   ¡   s
    ÿzNotebookTest.should_continueN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r=   r\   r   r)   r   r2   r4   r`   r    r@   r<   r   r   r   r   r      s   
*r   )Zunittestr   Zipykernel.testsr   Znbformat.converterr   Znbformat.readerr   rO   rL   r   Úqueuer   r   ÚImportErrorÚQueuer   r   r   r   r   Ú<module>   s   