ó
ú2¢\c           @  s’  d  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	 Z	 d d l
 m Z m Z d d l Z d d l m Z e j j Z y d d l m Z Wn! e k
 rÏ d d l m Z n Xd d l Z d d	 l m Z d d l Z d d l Z d d
 l m Z d d l m  Z  d d l! m" Z" d d l# m$ Z$ d Z% d Z& d e' f d „  ƒ  YZ( d e f d „  ƒ  YZ) e d d „ ƒ Z+ d S(   s   Base class for notebook tests.iÿÿÿÿ(   t   print_function(   t   hexlify(   t   contextmanagerN(   t   Threadt   Event(   t   TestCase(   t   patch(   t   IOLoop(   t   Configi   (   t   NotebookApp(   t   url_path_join(   t   TemporaryDirectoryi   gš™™™™™¹?t   TimeoutErrorc           B  s   e  Z RS(    (   t   __name__t
   __module__(    (    (    s<   lib/python2.7/site-packages/notebook/tests/launchnotebook.pyR   $   s   t   NotebookTestBasec           B  s‰   e  Z d  Z d Z d
 Z d Z e d „  ƒ Z e d „  ƒ Z	 e d „  ƒ Z
 e d „  ƒ Z e d „  ƒ Z e d „  ƒ Z e d	 „  ƒ Z RS(   s§   A base class for tests that need a running notebook.

    This create some empty config and runtime directories
    and then starts the notebook server with them.
    i50  s   /a%40b/c         C  s—   |  j  ƒ  d } xt t t t t ƒ ƒ D]\ } y t j | ƒ Wn> t k
 r~ } |  j j	 ƒ  sn t
 d ƒ ‚ n  t j t ƒ q' Xd Sq' Wt d ƒ ‚ d S(   s   Wait for the server to be alives   api/contentss#   The notebook server failed to startNs.   The notebook server didn't start up correctly.(   t   base_urlt   ranget   intt   MAX_WAITTIMEt   POLL_INTERVALt   requestst   gett	   Exceptiont   notebook_threadt   is_alivet   RuntimeErrort   timet   sleepR   (   t   clst   urlt   _t   e(    (    s<   lib/python2.7/site-packages/notebook/tests/launchnotebook.pyt   wait_until_alive4   s    c         C  s5   |  j  j d t ƒ |  j  j ƒ  r1 t d ƒ ‚ n  d S(   s7   Wait for the server process to terminate after shutdownt   timeouts   Undead notebook serverN(   R   t   joinR   R   R   (   R   (    (    s<   lib/python2.7/site-packages/notebook/tests/launchnotebook.pyt   wait_until_deadD   s    c         C  s'   i  } |  j  r# d |  j  | d <n  | S(   Ns   token %st   Authorization(   t   token(   R   t   headers(    (    s<   lib/python2.7/site-packages/notebook/tests/launchnotebook.pyt   auth_headersK   s    	c         K  sM   | j  d i  ƒ } | j |  j ƒ  ƒ t j | t |  j ƒ  | ƒ |  } | S(   sY   Send a request to my server
        
        with authentication and everything.
        R'   (   t
   setdefaultt   updateR(   R   t   requestR
   R   (   R   t   verbt   patht   kwargsR'   t   response(    (    s<   lib/python2.7/site-packages/notebook/tests/launchnotebook.pyR+   R   s    		c           só  t  ƒ  ˆ  _ ‡  f d †  } | d ƒ ˆ  _ | d ƒ } ˆ  _ | d ƒ } ˆ  _ | d ƒ } ˆ  _ | d ƒ ˆ  _ t j d i ˆ  j d 6t	 j
 j t j ƒ d	 6t ˆ  j d
 ƒ d 6d d 6| d 6| d 6| d 6ƒ ˆ  _ ˆ  j j ƒ  t j t j d | d d ƒ g d | d d d ƒ g d | d d ƒ g d | d d d ƒ g ƒˆ  _ ˆ  j j ƒ  ˆ  j pbt ƒ  ‰ d ˆ j _ t t	 j d ƒ ƒ j d ƒ ˆ  _ t ƒ  ‰ ‡  ‡ ‡ f d †  } t d | ƒ ˆ  _ t  ˆ  j _! ˆ  j j ƒ  ˆ j" ƒ  ˆ  j# ƒ  d  S(   Nc            s^   t  j j ˆ  j j |  Œ } y t  j | ƒ Wn+ t k
 rY } | j t j k rZ ‚  qZ n X| S(   N(	   t   osR-   R#   t   tmp_dirt   namet   makedirst   OSErrort   errnot   EEXIST(   t   partsR-   R    (   R   (    s<   lib/python2.7/site-packages/notebook/tests/launchnotebook.pyt   tmpb   s    
t   homet   datat   configt   runtimet	   notebookss
   os.environt   HOMEt
   PYTHONPATHs   .ipythont
   IPYTHONDIRt   1t   JUPYTER_NO_CONFIGt   JUPYTER_CONFIG_DIRt   JUPYTER_DATA_DIRt   JUPYTER_RUNTIME_DIRt   SYSTEM_JUPYTER_PATHt   sharet   jupytert   ENV_JUPYTER_PATHt   envt   SYSTEM_CONFIG_PATHt   etct   ENV_CONFIG_PATHs   :memory:i   t   asciic            s-  d t  j k r1 d d  l }  |  j |  j ƒ  ƒ n  t d ˆ  j d d d t d ˆ  j d ˆ  j	 d	 ˆ  j
 d
 ˆ  j d ˆ  j d ˆ d t d ˆ  j ƒ } ˆ  _ d „  | _ t | j _ g  | j _ | j d g  ƒ t | j _ g  | j _ t j ƒ  } | j ˆ j ƒ z | j ƒ  Wd  ˆ j ƒ  | j j ƒ  Xd  S(   Nt   asyncioiÿÿÿÿt   portt   port_retriesi    t   open_browsert
   config_dirt   data_dirt   runtime_dirt   notebook_dirR   R;   t
   allow_rootR&   c           S  s   d  S(   N(   t   None(    (    (    s<   lib/python2.7/site-packages/notebook/tests/launchnotebook.pyt   <lambda>›   t    t   argv(   t   syst   modulesRO   t   set_event_loopt   new_event_loopR	   RP   t   FalseRS   RT   RU   RV   t
   url_prefixt   TrueR&   t   notebookt   init_signalt   logt	   propagatet   handlerst
   initializeR   t   currentt   add_callbackt   sett   startt   session_managert   close(   RO   t   appt   loop(   R   R;   t   started(    s<   lib/python2.7/site-packages/notebook/tests/launchnotebook.pyt   start_thread‰   s6    						
t   target($   R   R1   t   home_dirRT   RS   RU   RV   R   t   dictR0   t   pathsepR#   R\   R-   t   pjoint	   env_patchRl   t   multiplet   jupyter_coret   pathst
   path_patchR;   R   t   NotebookNotaryt   db_fileR   t   urandomt   decodeR&   R   R   R   Rb   t   daemont   waitR!   (   R   R8   RT   RS   RU   Rr   (    (   R   R;   Rq   s<   lib/python2.7/site-packages/notebook/tests/launchnotebook.pyt   setup_class_   sB    	
	!	"
c         C  s•   |  j  j ƒ  |  j ƒ  |  j j ƒ  |  j j ƒ  |  j j ƒ  d „  } t d | ƒ } t | _	 | j
 ƒ  | j d ƒ | j ƒ  r‘ t d ƒ ‚ n  d  S(   Nc           S  s   t  j j ƒ  j ƒ  d  S(   N(   t   zmqt   Contextt   instancet   term(    (    (    s<   lib/python2.7/site-packages/notebook/tests/launchnotebook.pyt   cleanup_zmq¹   s    Rs   i   sF   Failed to teardown zmq Context, open sockets likely left lying around.(   Rc   t   stopR$   Rx   R|   R1   t   cleanupR   Rb   R   Rl   R#   R   R   (   R   Rˆ   t   t(    (    s<   lib/python2.7/site-packages/notebook/tests/launchnotebook.pyt   teardown_class±   s    
		
c         C  s   d |  j  |  j f S(   Ns   http://localhost:%i%s(   RP   Ra   (   R   (    (    s<   lib/python2.7/site-packages/notebook/tests/launchnotebook.pyR   Ã   s    N(   R   R   t   __doc__RP   RX   R;   Ra   t   classmethodR!   R$   R(   R+   Rƒ   RŒ   R   (    (    (    s<   lib/python2.7/site-packages/notebook/tests/launchnotebook.pyR   '   s   Rc         c  s   y	 d  VWnk t  j k
 rv } | j j } | |  k sL t d |  | f ƒ ‚ | r‰ | t | ƒ k ss t | ƒ ‚ q‰ n Xt s‰ t d ƒ ‚ d  S(   Ns   Expected status %d, got %ds   Expected HTTP error status(   R   t	   HTTPErrorR/   t   status_codet   AssertionErrort   strR`   (   t   statust   msgR    t   real_status(    (    s<   lib/python2.7/site-packages/notebook/tests/launchnotebook.pyt   assert_http_errorÈ   s    	%(,   R   t
   __future__R    t   binasciiR   t
   contextlibR   R5   R0   R\   t	   threadingR   R   R   t   unittestR   R-   R#   Rw   t   unittest.mockR   t   ImportErrort   mockR   t   tornado.ioloopR   R„   t   jupyter_core.pathsRz   t   traitlets.configR   t   notebookappR	   t   utilsR
   t   ipython_genutils.tempdirR   R   R   R   R   R   RX   R–   (    (    (    s<   lib/python2.7/site-packages/notebook/tests/launchnotebook.pyt   <module>   s8   ¡