U
    /e'                     @  s   d Z ddlmZ ddlZeeZddlZddlZddl	Z	ddl
Z
ddlmZmZmZmZ ddlmZmZmZ ddlZddlZddlmZ ddlmZ dZd	Zejd
dddddddZdS )z4 Define Pytest plugins for Jupyter Notebook tests.

    )annotationsN)dirnameexistsjoinpardir)IOAnyCallable)ConnectionError)write)zbokeh._testing.plugins.log_file)jupyter_notebooksession)Zscopezpytest.FixtureRequestzIO[str]str)requestlog_filereturnc              	     s  ddl m} | }d}t|d}t|s4t| t|d dt rft }| W 5 Q R X t d}|	| W 5 Q R X dd	 fd
d}| 
| | jjj}tj }	d|	d< ttttt}
ddg}dd| d|
 g}d| ztj|| |	||dW n8 tk
rJ   t	dd||   td Y nX dd	fdd}| 
| d&dddddd d!dd	fd"d#}| st	d$d||   td jdk	rt	d%j  td S dS )'a+  
    Starts a jupyter notebook server at the beginning of a session, and
    closes at the end of a session.

    Adds custom.js that runs all the cells on notebook opening. Cleans out
    this custom.js at the end of the test run.

    Returns the url that the jupyter notebook is running at.

    r   )pathsz
require(["base/js/namespace", "base/js/events"], function (IPython, events) {
    events.on("kernel_ready.Kernel", function () {
        IPython.notebook.execute_all_cells();
    });
});
customz	custom.jsNwNoner   c               	     s4   d k	rnd} t  d}||  W 5 Q R X d S )N r   )openr   )textf)customjsold_customjs K/tmp/pip-unpacked-wheel-f5fndrjf/bokeh/_testing/plugins/jupyter_notebook.pyclean_up_customjsf   s    z+jupyter_notebook.<locals>.clean_up_customjsserverZBOKEH_RESOURCESZjupyterZnotebookz--no-browserz--port=z--notebook-dir=zhttp://localhost:)envstdoutstderrzFailed to run:     c                     s   t d    d S )Nz"Shutting down jupyter-notebook ...)r   killr   )procr   r   stop_jupyter_notebook   s    z/jupyter_notebook.<locals>.stop_jupyter_notebook      @{Gz?zCallable[[], Any]floatbool)functimeoutintervalr   c                 S  s6   t   }|  rdS t   | |kr&dS t | qd S NTF)timesleep)r-   r.   r/   startr   r   r   
wait_until   s    z$jupyter_notebook.<locals>.wait_untilc                    s   dd fdd} | S )Nr   r   c                     s6   j d k	rdS zt W S  tk
r0   Y dS X d S r0   )
returncoderequestsgetr
   r   )jupter_notebook_urlr'   r   r   helper   s    
zCjupyter_notebook.<locals>.wait_for_jupyter_notebook.<locals>.helperr   )r9   )r8   r'   r4   r   r   wait_for_jupyter_notebook   s    z3jupyter_notebook.<locals>.wait_for_jupyter_notebookzTimeout when running: z"Jupyter notebook exited with code )r)   r*   )Zjupyter_corer   Zjupyter_config_dirr   r   osmakedirsr   readr   Zaddfinalizerconfigoptionnotebook_portenvironcopyr   __file__r   
subprocessPopenOSErrorsysexitr5   )r   r   r   Z
config_dirbodyr   r   r   r@   r!   Znotebook_dircmdargvr(   r:   r   )r   r8   r   r'   r4   r   r   <   sL    











r   )__doc__
__future__r   logging	getLogger__name__logr;   rD   rG   r1   os.pathr   r   r   r   typingr   r   r	   Zpytestr6   Zrequests.exceptionsr
   Zbokeh.util.terminalr   Zpytest_plugins__all__Zfixturer   r   r   r   r   <module>   s"   

