a
    ø£BeL  ã                   @  s  d Z ddl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
mZmZ ddlmZmZmZ ddlmZ ddlmZ dd	lmZmZmZ d
ZeeƒZeeƒZe e¡ eeƒZeeƒZe e¡ e e !¡ ƒZ"e e !¡ ƒZ#G dd„ de
eƒZ$e$j% Z&Z'e(dk�re&ƒ  dS )z#A Jupyter console app to run files.é    )ÚannotationsN)Ú
JupyterAppÚbase_aliasesÚ
base_flags)ÚAnyÚDictÚFloat)Úcatch_config_erroré   )Ú__version__)ÚJupyterConsoleAppÚapp_aliasesÚ	app_flagsé
   c                      s®   e Zd ZdZeZdZdZee	ƒZ	ee
ƒZ
eeƒZeeƒZedddd�Zdd	d
dœ‡ fdd„Zedd	d
dœ‡ fdd„ƒZdd
dœdd„Zd
dœdd„Zd
dœ‡ fdd„Z‡  ZS )ÚRunAppz$An Jupyter Console app to run files.zjupyter runzRun Jupyter kernel code.é<   Ta5  Timeout for giving up on a kernel (in seconds).

        On first connect and restart, the console tests whether the
        kernel is running and responsive by sending kernel_info_requests.
        This sets the timeout in seconds for how long the kernel can take
        before being presumed dead.
        )ÚconfigÚhelpNzlist[str] | NoneÚNone)ÚargvÚreturnc                   s,   t ƒ  |¡ |  | j¡ | jdd… | _dS )z!Parse the command line arguments.N)ÚsuperÚparse_command_lineZbuild_kernel_argvÚ
extra_argsÚfilenames_to_run©Úselfr   ©Ú	__class__© úX/home/yuhw/wc/wire-cell-python/wcpy/lib/python3.9/site-packages/jupyter_client/runapp.pyr   ?   s    zRunApp.parse_command_linec                   s>   | j  d¡ tƒ  |¡ t | ¡ t tj| j¡ |  ¡  dS )zInitialize the app.zjupyter run: initialize...N)	ÚlogÚdebugr   Ú
initializer   ÚsignalÚSIGINTÚhandle_sigintÚinit_kernel_infor   r   r   r    r#   E   s
    
zRunApp.initializezt.Any)Úargsr   c                 G  s"   | j r| j  ¡  n| j d¡ dS )zHandle SIGINT.z*Cannot interrupt kernels we didn't start.
N)Zkernel_managerZinterrupt_kernelr!   Úerror)r   r(   r   r   r    r&   N   s    zRunApp.handle_sigint)r   c              
   C  s¢   | j }t ¡ }| jj ¡  | j ¡ }z| jjdd�}W nD tjyz } z*t ¡ | |krfd}t	|ƒ|‚W Y d}~q$d}~0 0 |d  
d¡|kr$|d | _dS q$dS )z4Wait for a kernel to be ready, and store kernel infor
   ©Útimeoutz,Kernel didn't respond to kernel_info_requestNZparent_headerÚmsg_idÚcontent)Úkernel_timeoutÚtimeÚkernel_clientZ
hb_channelZunpauseZkernel_infoZget_shell_msgÚqueueÚEmptyÚRuntimeErrorÚget)r   r+   Zticr,   ÚreplyÚeÚmsgr   r   r    r'   U   s    
 
zRunApp.init_kernel_infoc              	     sæ   | j  d¡ tƒ  ¡  | jr | jD ]z}| j  d|¡ t|ƒ�P}| ¡ }| jj|t	d�}|d d dkrjdnd}|r~t
d	| ƒ‚W d
  ƒ q"1 s’0    Y  q"nBtj ¡ }| jj|t	d�}|d d dkrÎdnd}|râd}t
|ƒ‚d
S )zStart the application.zjupyter run: starting...zjupyter run: executing `%s`r*   r-   ÚstatusÚokr   r
   zjupyter-run error running '%s'Nz!jupyter-run error running 'stdin')r!   r"   r   Ústartr   ÚopenÚreadr0   Zexecute_interactiveÚOUTPUT_TIMEOUTÚ	ExceptionÚsysÚstdin)r   ÚfilenameÚfpÚcoder5   Zreturn_coder7   r   r   r    r:   g   s"    


.
zRunApp.start)N)N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   ÚversionÚnameÚdescriptionr   ÚflagsÚaliasesr   Úfrontend_aliasesÚfrontend_flagsr   r.   r   r	   r#   r&   r'   r:   Ú__classcell__r   r   r   r    r   )   s&   ýr   Ú__main__))rG   Ú
__future__r   r1   r$   r?   r/   ÚtypingÚtZjupyter_core.applicationr   r   r   Z	traitletsr   r   r   Ztraitlets.configr	   Ú r   Z
consoleappr   r   r   r=   ÚdictrK   Zfrontend_flags_dictÚupdaterL   Zfrontend_aliases_dictÚsetÚkeysrM   rN   r   Zlaunch_instanceÚmainZlaunch_new_instancerD   r   r   r   r    Ú<module>   s0   

T

