o
    i                     @   s  d Z ddlZddlZddl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dlmZmZ ddlmZmZ ddlmZ ddlmZ dd	lmZ dd
lmZ ddlmZmZ ddlmZ e e!e"Z#e$eZ%dddiidfe%d< dddiidfe%d< dddiidfe%d< e$eZ&de&d< G dd dej'Z(dd Z)dd Z*d d! Z+d"d# Z,d$d% Z-G d&d deZ.d'd( Z/d)d* Z0e1d+krd,d-gZ2e2D ]Z3e3ej4v rd.e._5ej46e3 e.7  qdS dS )/z
This module is meant to run JupyterLab in a headless browser, making sure
the application launches and starts up without errors.
    N)ThreadPoolExecutor)path)aliasesflags)pathname2urlurljoin)IOLoop)StreamClosedError)WebSocketClosedError)Bool   )LabAppget_app_dir)TestEnv
BrowserApp	core_modeTzStart the app in core mode.z	core-modedev_modezStart the app in dev mode.zdev-modewatchzStart the app in watch mode.zBrowserApp.app_dirzapp-dirc                       s4   e Zd ZdZ fddZ fddZdd Z  ZS )LogErrorHandlerz.A handler that exits with 1 on a logged error.c                    s   t  jtjd d| _d S )N)levelF)super__init__loggingERRORerroredself	__class__ S/var/www/edux/Edux_v2/venv/lib/python3.10/site-packages/jupyterlab/browser_check.pyr   *      
zLogErrorHandler.__init__c                    s8   t |dr|jd urt|jd ttfrd S t |S )Nexc_infor   )hasattrr"   
isinstancer	   r
   r   filterr   recordr   r   r    r%   .   s   
zLogErrorHandler.filterc                 C   s   t |jtjd d| _d S )N)fileT)printmsgsysstderrr   r&   r   r   r    emit;   r!   zLogErrorHandler.emit)__name__
__module____qualname____doc__r   r%   r-   __classcell__r   r   r   r    r   '   s
    r   c                 C   s   t  t| | dS )a
  Synchronous entry point to run a test function.
    func is a function that accepts an app url as a parameter and returns a result.
    func can be synchronous or asynchronous.  If it is synchronous, it will be run
    in a thread, so asynchronous is preferred.
    N)r   currentspawn_callbackrun_test_async)appfuncr   r   r    run_test@   s   r8   c              
      s  t  }| j| t }|  | jd t| dr%tdt| j	}n| j
}t|r2||}n| jd t }t }||||}t|g}z|I dH  W n  tys }	 z| jd | jt|	 W Y d}	~	nd}	~	ww | jd d}
|jrd	}
| jd
 n| jd | jd zGz| j  | j  |  W n ty }	 z| jt|	 d	}
W Y d}	~	nd}	~	ww W td t|
 dS W td t|
 dS td t|
 w )zRun a test against the application.
    func is a function that accepts an app url as a parameter and returns a result.
    func can be synchronous or asynchronous.  If it is synchronous, it will be run
    in a thread, so asynchronous is preferred.
    zRunning async testbrowser_open_filezfile:z&Using thread pool executor to run testNz!Caught exception during the test:zTest Completer   r   zExiting with 1 due to errorszExiting normallyzStopping server...   )r   log
addHandlerr   startinfor#   r   r   r9   display_urlinspectiscoroutinefunctionasyncioget_event_loopr   run_in_executorwait	Exceptioncriticalerrorstrr   http_serverstopio_looptimesleepos_exit)r6   r7   handler	env_patchurltestloopexecutortaskeresultr   r   r    r5   I   s^   







r5   c                    sV   t j| i |I dH }| I dH \}}|jdkr'tt| d t|j ||fS )zRun an asynchronous commandNr   z exited with )rB   create_subprocess_execcommunicate
returncodeRuntimeErrorrI   )cmdkwargsprocstdoutr,   r   r   r    run_async_process   s   
rb   c                    s   t t d}t t |ds4t |stt | tg d|dI dH  tg d|dI dH  tt t	dt |d tdd| g|dI dH  dS )	-Run the browser test and return an exit code.browser_testnode_modulesnpminitz-ycwdNrg   installzplaywright@^1.9.2browser-test.jsnode)
ospjoinr   existsrO   makedirsrb   shutilcopyhererS   targetr   r   r    run_browser   s   
rx   c                 C   s~   t t d}t t |ds't| tjg d|d tjg d|d t	t t
dt |d tjdd| g|dS )rc   rd   re   rf   ri   rk   rm   rn   )ro   rp   r   rq   rO   rr   
subprocesscallrs   rt   ru   
check_callrv   r   r   r    run_browser_sync   s   
r|   c                       sT   e Zd ZdZe ZdZddiZdZdZe	Z
eZedZ fdd	Z fd
dZ  ZS )r   zAn app the launches JupyterLab and waits for it to start up, checking for
    JS console errors, JS errors, and Python logged errors.
    Fbase_urlz/foo/z
/lab?resetz	127.0.0.1Tc                    sF   | j di  d| j d d< d| j d d< d| j d d< t   d S )Npage_config_dataTbrowserTestFbuildAvailableexposeAppInBrowser)settings
setdefaultr   initialize_settingsr   r   r   r    r      s
   zBrowserApp.initialize_settingsc                    sB   | j rtndd }tjdkr|tkrt}t| j| t   d S )Nc                 S   s   dS )Nr   r   )rS   r   r   r    <lambda>   s    z0BrowserApp.initialize_handlers.<locals>.<lambda>nt)	test_browserrx   rO   namer|   r8   	serverappr   initialize_handlers)r   r7   r   r   r    r      s
   zBrowserApp.initialize_handlers)r.   r/   r0   r1   r   open_browserserverapp_configdefault_urlip
test_flagsr   test_aliasesr   r   r   r   r   r2   r   r   r   r    r      s    c                   C   s   t tdgS )N)moduler6   )r.   r   r   r   r   r     _jupyter_server_extension_points   s   r   c                   C   s
   ddigS )Nr   zjupyterlab.browser_checkr   r   r   r   r    _jupyter_server_extension_paths   s   
r   __main__z--no-browser-testz--no-chrome-testF)8r1   rB   r@   r   rO   rs   ry   r+   rM   concurrent.futuresr   r   ro   jupyter_server.serverappr   r   jupyter_server.utilsr   r   tornado.ioloopr   tornado.iostreamr	   tornado.websocketr
   	traitletsr   labappr   r   tests.test_appr   abspathdirname__file__ru   dictr   r   Handlerr   r8   r5   rb   rx   r|   r   r   r   r.   skip_optionsoptionargvr   removelaunch_instancer   r   r   r    <module>   sX   	;	

