ó
mÜJ]c           @` sø   d  Z  d d l m Z m Z m Z m Z d d l Z e j e ƒ Z	 d d l
 Z
 d d l m Z m Z m Z m Z d d l Z d d l Z d d l 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 „  ƒ Z d S(   u4    Define Pytest plugins for Jupyter Notebook tests.

i    (   t   absolute_importt   divisiont   print_functiont   unicode_literalsN(   t   joint   existst   dirnamet   pardir(   t   ConnectionError(   t   writeu   bokeh._testing.plugins.log_fileu   jupyter_notebookt   scopeu   sessionc      	   ` sN  d d l  m } | j ƒ  } d } t | d ƒ } t | ƒ sM t j | ƒ n  t | d ƒ ‰  d ‰ t ˆ  ƒ r† t ˆ  d ƒ j	 ƒ  ‰ n  t ˆ  d ƒ  } | j
 | ƒ Wd QX‡  ‡ f d	 †  } |  j | ƒ |  j j j } t j j ƒ  }	 d
 |	 d <t t t ƒ t t ƒ }
 d d g } d d | d |
 g } d | ‰ y) t j | | d |	 d | d | ƒ‰ Wn9 t k
 r›t
 d d j | | ƒ ƒ t j d ƒ n¯ X‡ f d †  } |  j | ƒ d d d „ ‰ ‡ ‡ ‡ f d †  } | ƒ  st
 d d j | | ƒ ƒ t j d ƒ n  ˆ j d k	 rFt
 d t ˆ j ƒ ƒ t j d ƒ n  ˆ Sd S(   u+  
    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.

    i    (   t   pathsu¼   
require(["base/js/namespace", "base/js/events"], function (IPython, events) {
    events.on("kernel_ready.Kernel", function () {
        IPython.notebook.execute_all_cells();
    });
});
u   customu	   custom.jsu   ru   wNc          ` sA   ˆ d  k	 r ˆ n d }  t ˆ  d ƒ  } | j |  ƒ Wd  QXd  S(   Nu    u   w(   t   Nonet   openR	   (   t   textt   f(   t   customjst   old_customjs(    sF   lib/python2.7/site-packages/bokeh/_testing/plugins/jupyter_notebook.pyt   clean_up_customjs`   s    u   serveru   BOKEH_RESOURCESu   jupyteru   notebooku   --no-browseru	   --port=%su   --notebook-dir=%su   http://localhost:%dt   envt   stdoutt   stderru   Failed to run: %su    i   c           ` s   t  d ƒ ˆ  j ƒ  d  S(   Nu"   Shutting down jupyter-notebook ...(   R	   t   kill(    (   t   proc(    sF   lib/python2.7/site-packages/bokeh/_testing/plugins/jupyter_notebook.pyt   stop_jupyter_notebook}   s    
g      @g{®Gáz„?c         S` sQ   t  j  ƒ  } x> t rL |  ƒ  r" t St  j  ƒ  | | k r< t St  j | ƒ q Wd  S(   N(   t   timet   Truet   Falset   sleep(   t   funct   timeoutt   intervalt   start(    (    sF   lib/python2.7/site-packages/bokeh/_testing/plugins/jupyter_notebook.pyt
   wait_untilƒ   s    		c          ` s   ‡  ‡ f d †  }  ˆ |  ƒ S(   Nc           ` s=   ˆ j  d  k	 r t Sy t j ˆ  ƒ SWn t k
 r8 t SXd  S(   N(   t
   returncodeR   R   t   requestst   getR   R   (    (   t   jupter_notebook_urlR   (    sF   lib/python2.7/site-packages/bokeh/_testing/plugins/jupyter_notebook.pyt   helperŽ   s    (    (   R&   (   R%   R   R!   (    sF   lib/python2.7/site-packages/bokeh/_testing/plugins/jupyter_notebook.pyt   wait_for_jupyter_notebook   s    u   Timeout when running: %su"   Jupyter notebook exited with code (   t   jupyter_coreR   t   jupyter_config_dirR   R   t   ost   makedirsR   R   t   readR	   t   addfinalizert   configt   optiont   notebook_portt   environt   copyR   t   __file__R   t
   subprocesst   Popent   OSErrort   syst   exitR"   t   str(   t   requestt   log_fileR   t
   config_dirt   bodyt   customR   R   R0   R   t   notebook_dirt   cmdt   argvR   R'   (    (   R   R%   R   R   R!   sF   lib/python2.7/site-packages/bokeh/_testing/plugins/jupyter_notebook.pyt   jupyter_notebook7   sJ    

)
	(   u   bokeh._testing.plugins.log_file(   u   jupyter_notebook(   t   __doc__t
   __future__R    R   R   R   t   loggingt	   getLoggert   __name__t   logR*   t   os.pathR   R   R   R   R4   R7   R   t   pytestR#   t   requests.exceptionsR   t   bokeh.util.terminalR	   t   pytest_pluginst   __all__t   fixtureRB   (    (    (    sF   lib/python2.7/site-packages/bokeh/_testing/plugins/jupyter_notebook.pyt   <module>	   s   ""