o
    i!                     @   s  d 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ddlmZ d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mZ d
dlmZ ddl m!Z! dZ"dZ#G dd de$Z%G dd deZ&G dd de&Z'edddZ(dS )zBase class for notebook tests.    )hexlify)contextmanagerN)ThreadEvent)TestCase)patch)IOLoop)Config   )NotebookAppurlencode_unix_socket)url_path_join)TemporaryDirectory   g?c                   @   s   e Zd ZdS )TimeoutErrorN)__name__
__module____qualname__ r   r   X/var/www/edux/Edux_v2/venv/lib/python3.10/site-packages/notebook/tests/launchnotebook.pyr      s    r   c                   @   s   e Zd ZdZdZdZdZedd Zedd Z	ed	d
 Z
edd Zedd Zedd Zedd Zedd Zedd Zedd Zedd ZdS )NotebookTestBasezA 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  Nz/a%40b/c                 C   s   |   d }tttt D ]9}z	| | W  dS  ty' } z|d}~w tyG } z| j	 s8t
d|tt W Y d}~qd}~ww td)zWait for the server to be alivezapi/contentsNz#The notebook server failed to startz.The notebook server didn't start up correctly.)base_urlrangeintMAX_WAITTIMEPOLL_INTERVAL	fetch_urlModuleNotFoundError	Exceptionnotebook_threadis_aliveRuntimeErrortimesleepr   )clsurl_errorer   r   r   wait_until_alive/   s   	

z!NotebookTestBase.wait_until_alivec                 C   s$   | j jtd | j  rtddS )z7Wait for the server process to terminate after shutdown)timeoutzUndead notebook serverN)r   joinr   r    r   r$   r   r   r   wait_until_deadB   s   
z NotebookTestBase.wait_until_deadc                 C   s   i }| j rd| j  |d< |S )Nztoken Authorization)token)r$   headersr   r   r   auth_headersI   s   zNotebookTestBase.auth_headersc                 C   s
   t | S N)requestsget)r%   r   r   r   r   P   s   
zNotebookTestBase.fetch_urlc                 K   s<   | di }||   tj|t|  |fi |}|S )zQSend a request to my server

        with authentication and everything.
        r0   )
setdefaultupdater1   r3   requestr   r   )r$   verbpathkwargsr0   responser   r   r   r7   T   s   zNotebookTestBase.requestc                 C   s.   | j tjtjt| j dd| j| j| j	dS )Nz.ipython1)HOME
PYTHONPATH
IPYTHONDIRJUPYTER_NO_CONFIGJUPYTER_CONFIG_DIRJUPYTER_DATA_DIRJUPYTER_RUNTIME_DIR)
home_dirospathsepr+   sysr9   pjoin
config_dirdata_dirruntime_dirr,   r   r   r   get_patch_enva   s   
zNotebookTestBase.get_patch_envc                 C   s   g S r2   r   r,   r   r   r   get_argvm   s   zNotebookTestBase.get_argvc                 C      t | jdS )N)port)dictrO   r,   r   r   r   get_bind_argsq      zNotebookTestBase.get_bind_argsc                    s  t   _ fdd}|d _|d } _|d } _|d } _|d _td 	  _
 j
  tjtj|d	d
g|dd
gd _ j   jpSt dj_ttdd _t  fdd}t|d _d j_ j       d S )Nc               
      s^   t jj jjg| R  }zt | W |S  ty. } z|jtjkr# W Y d }~|S d }~ww r2   )	rE   r9   r+   tmp_dirnamemakedirsOSErrorerrnoEEXIST)partsr9   r(   r,   r   r   tmpx   s   
z)NotebookTestBase.setup_class.<locals>.tmphomedataconfigruntime	notebooksz
os.environsharejupyteretc)SYSTEM_JUPYTER_PATHSYSTEM_CONFIG_PATHz:memory:   asciic                     s   zr   } td	dd j j j j jd jd
|  } _dt	j
v r<|  dd l}||  dd l}|  dd |_d|j_g |j_|j  d d|j_g |j_t }|j |  W   |j  d S   |j  w )
Nr   FT)
port_retriesopen_browserrI   rJ   rK   notebook_dirr   r]   
allow_rootr/   asyncioc                   S   s   d S r2   r   r   r   r   r   <lambda>   s    zDNotebookTestBase.setup_class.<locals>.start_thread.<locals>.<lambda>)argvr   )rQ   r   rI   rJ   rK   ri   
url_prefixr/   notebookrG   modules_init_asyncio_patchrk   set_event_loopnew_event_loopnest_asyncioapplyinit_signallog	propagatehandlers
initializerM   r   currentadd_callbacksetstartsession_managerclose)	bind_argsapprk   rt   loopr$   r]   startedr   r   start_thread   sF   


z2NotebookTestBase.setup_class.<locals>.start_threadtargetT)r   rS   rD   rJ   rI   rK   ri   r   rP   rL   	env_patchr~   multiplejupyter_corepaths
path_patchr]   r	   NotebookNotarydb_filer   rE   urandomdecoder/   r   r   r   daemonwaitr)   )r$   rZ   rJ   rI   rK   r   r   r   r   setup_classu   s2   
	




*
zNotebookTestBase.setup_classc                 C   sn   | j   |   | j  | j  | j  dd }t|d}d|_|	  |
d | r5tdd S )Nc                   S   s   t j   d S r2   )zmqContextinstancetermr   r   r   r   cleanup_zmq   s   z4NotebookTestBase.teardown_class.<locals>.cleanup_zmqr   T   zFFailed to teardown zmq Context, open sockets likely left lying around.)ro   stopr-   r   r   rS   cleanupr   r   r~   r+   r    r!   )r$   r   tr   r   r   teardown_class   s   





zNotebookTestBase.teardown_classc                 C   s   d| j  | j S )Nzhttp://localhost:)rO   rn   r,   r   r   r   r      s   zNotebookTestBase.base_url)r   r   r   __doc__rO   r]   rn   classmethodr)   r-   r1   staticmethodr   r7   rL   rM   rQ   r   r   r   r   r   r   r   r   "   s6    








T
r   c                   @   s@   e Zd Zde  dZedd Zedd Ze	dd Z
d	S )
UNIXSocketNotebookTestBasez/tmp/.notebook.z.sockc                 C   rN   )N)sock)rP   r   r,   r   r   r   rQ      rR   z(UNIXSocketNotebookTestBase.get_bind_argsc                 C   s   t | j | j S r2   )r   r   rn   r,   r   r   r   r      s   z#UNIXSocketNotebookTestBase.base_urlc                 C   sH   t jdkr	dd l}|  t| W  d    S 1 sw   Y  d S )Nntr   )rE   rT   requests_unixsocketmonkeypatchr3   r4   )r%   r   r   r   r   r      s
   

$z$UNIXSocketNotebookTestBase.fetch_urlN)r   r   r   rE   getpidr   r   rQ   r   r   r   r   r   r   r   r      s    

r   c              
   c   s    zd V  W n9 t jy? } z,|jj}|| ks!J d|  d| |r-|t|v s4J |W Y d }~d S W Y d }~d S d }~ww J d)NzExpected status z, got FzExpected HTTP error status)r3   	HTTPErrorr;   status_codestr)statusmsgr(   real_statusr   r   r   assert_http_error   s   

r   r2   ))r   binasciir   
contextlibr   rW   rE   rG   	threadingr   r   r"   unittestr   r9   r+   rH   unittest.mockr   r3   tornado.ioloopr   r   jupyter_core.pathsr   traitlets.configr	   notebookappr   r   utilsr   ipython_genutils.tempdirr   r   r   r   r   r   r   r   r   r   r   r   <module>   s6     @