a
    ÷Df}                     @   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y   d dlmZ d
d	 ZY n0 G dd deZdS )    )TestCase)utils)convert)readsN)copy)Emptyc                 C   s
   t | tS N)
isinstancestrs r   c/nfs/NAS7/SABIOD/METHODE/ermites/ermites_venv/lib/python3.9/site-packages/numba/testing/notebook.pyisstr   s    r   c                 C   s
   t | tS r   )r	   
basestringr   r   r   r   r      s    c                   @   sd   e Zd ZdZg 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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pyin)Zexecution_count	tracebackZprompt_numbersource   c           	      C   s   t |$}tt| | j}W d    n1 s20    Y  t \}}tdd |jD D ]\}}| 	| 
|||| q\d S )Nc                 S   s   g | ]}|j d kr|qS )code)Z	cell_type).0cr   r   r   
<listcomp>-       z/NotebookTest._test_notebook.<locals>.<listcomp>)openr   r   readNBFORMAT_VERSIONr   Zstart_new_kernel	enumeratecells_test_notebook_cellsanitize_cell)	selfZnotebooktestfnb_kernelir   r   r   r   _test_notebook(   s
    
2zNotebookTest._test_notebookc                    s8  t |dr|j}n|j}|j}|| g }d }d}	d}
d} |rz|jddd}W n tyn   Y q<Y n0 |d  jvr<|d dkrd	|d
 d  d |d
 d  }d}	|
dkr|}
t	|}|t	|j
k r|j
| pg } ||}|| 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   r   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|v rB||d |d< d|v 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rG   rO   r   r   r   i   r   zCNotebookTest.scrub_outputs.<locals>._scrubLines.<locals>.<listcomp>)r   rN   rO   )linesrQ   r   r   _scrubLinese   s    z/NotebookTest.scrub_outputs.<locals>._scrubLinestextdataN)r   r	   dictitems)r"   r>   outputoutrS   mimerU   r   rQ   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"   rB   rX   r   r   r   r!   ~   s    
zNotebookTest.sanitize_cellc                 C   s   ddddi}d|d i}| |d  | |}| D ],\}}||v r:|| |v r:|||  ||< q:d|v rd|vr|d |d< |d= |S )	zF
        transform a message into something like the notebook
        output_typeZexecute_resultr0   )ZpyoutZpyerrr/   r1   rU   rT   )updater_   rW   )r"   rC   rE   Z	SWAP_KEYSrX   r^   Zswapsr   r   r   r?      s     
zNotebookTest.transform_messagec                 C   s*   |du 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_stateZidler   )r"   rC   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   rN   rK   r   queuer   r   ImportErrorQueuer   r   r   r   r   <module>   s   