a
    ø£BeÁ   ã                   @   sr   d 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	m
Z
mZ ddlmZmZ G d	d
„ d
eƒZe e¡ dS )z A client for in-process kernels.é    N)ÚKernelClient)ÚKernelClientABC)Úrun_sync)ÚInstanceÚTypeÚdefaulté   )ÚInProcessChannelÚInProcessHBChannelc                       s0  e Zd ZdZeeƒZeeƒZeeƒZeeƒZ	ee
ƒZeddd�Zedƒdd„ ƒZ‡ fdd	„Z‡ fd
d„Zedd„ ƒZedd„ ƒZedd„ ƒZedd„ ƒZedd„ ƒZd6dd„Zd7dd„Zd8dd„Zd9d d!„Zd:d"d#„Zd$d%„ Zd;d&d'„Zd(d)„ Zd*d+„ Z d,d-„ Z!d<d.d/„Z"d=d0d1„Z#d>d2d3„Z$d?d4d5„Z%‡  Z&S )@ÚInProcessKernelClienta  A client for an in-process kernel.

    This class implements the interface of
    `jupyter_client.clientabc.KernelClientABC` and allows
    (asynchronous) frontends to be used seamlessly with an in-process kernel.

    See `jupyter_client.client.KernelClient` for docstrings.
    z,ipykernel.inprocess.ipkernel.InProcessKernelT)Ú
allow_noneZblocking_classc                 C   s   ddl m} |S )Nr   )ÚBlockingInProcessKernelClient)Úblockingr   )Úselfr   © r   ú]/home/yuhw/wc/wire-cell-python/wcpy/lib/python3.9/site-packages/ipykernel/inprocess/client.pyÚ_default_blocking_class6   s    z-InProcessKernelClient._default_blocking_classc                    s   t ƒ  ¡ }| j|d< |S )z'Get the connection info for the client.Úkernel)ÚsuperÚget_connection_infor   )r   Úd©Ú	__class__r   r   r   <   s    

z)InProcessKernelClient.get_connection_infoc                    s"   t ƒ  ¡  | jr| jj | ¡ dS )z!Start the channels on the client.N)r   Ústart_channelsr   Z	frontendsÚappend)r   ÚargsÚkwargsr   r   r   r   B   s    
z$InProcessKernelClient.start_channelsc                 C   s   | j d u r|  | ¡| _ | j S ©N)Z_shell_channelÚshell_channel_class©r   r   r   r   Úshell_channelH   s    
z#InProcessKernelClient.shell_channelc                 C   s   | j d u r|  | ¡| _ | j S r   )Z_iopub_channelÚiopub_channel_classr   r   r   r   Úiopub_channelN   s    
z#InProcessKernelClient.iopub_channelc                 C   s   | j d u r|  | ¡| _ | j S r   )Z_stdin_channelÚstdin_channel_classr   r   r   r   Ústdin_channelT   s    
z#InProcessKernelClient.stdin_channelc                 C   s   | j d u r|  | ¡| _ | j S r   )Z_control_channelÚcontrol_channel_classr   r   r   r   Úcontrol_channelZ   s
    
ÿz%InProcessKernelClient.control_channelc                 C   s   | j d u r|  | ¡| _ | j S r   )Z_hb_channelÚhb_channel_classr   r   r   r   Ú
hb_channelb   s    
z InProcessKernelClient.hb_channelFNc                 C   sH   |du r| j }t||||pi |d�}| j d|¡}|  |¡ |d d S )zExecute code on the client.N)ÚcodeÚsilentÚstore_historyÚuser_expressionsÚallow_stdinZexecute_requestÚheaderÚmsg_id)r-   ÚdictÚsessionÚmsgÚ_dispatch_to_kernel)r   r)   r*   r+   r,   r-   Úcontentr2   r   r   r   Úexecutek   s    û
zInProcessKernelClient.executec                 C   s@   |du rt |ƒ}t||d�}| j d|¡}|  |¡ |d d S )zGet code completion.N)r)   Ú
cursor_posZcomplete_requestr.   r/   ©Úlenr0   r1   r2   r3   )r   r)   r6   r4   r2   r   r   r   Úcomplete|   s    
zInProcessKernelClient.completer   c                 C   sB   |du rt |ƒ}t|||d�}| j d|¡}|  |¡ |d d S )zGet code inspection.N)r)   r6   Údetail_levelZinspect_requestr.   r/   r7   )r   r)   r6   r:   r4   r2   r   r   r   Úinspect…   s    ý
zInProcessKernelClient.inspectÚrangec                 K   s:   t f |||dœ|¤Ž}| j d|¡}|  |¡ |d d S )zGet code history.)ÚrawÚoutputÚhist_access_typeZhistory_requestr.   r/   ©r0   r1   r2   r3   )r   r=   r>   r?   Úkwdsr4   r2   r   r   r   Úhistory’   s    
zInProcessKernelClient.historyc                 C   s   d}t |ƒ‚dS )zHandle shutdown.z!Cannot shutdown in-process kernelN)ÚNotImplementedError)r   Zrestartr2   r   r   r   Úshutdown™   s    zInProcessKernelClient.shutdownc                 C   s"   | j  d¡}|  |¡ |d d S )zRequest kernel info.Zkernel_info_requestr.   r/   ©r1   r2   r3   )r   r2   r   r   r   Úkernel_infoŸ   s    
z!InProcessKernelClient.kernel_infoc                 C   s:   |du ri nt |d�}| j d|¡}|  |¡ |d d S )z6Request a dictionary of valid comms and their targets.N)Útarget_nameZcomm_info_requestr.   r/   r@   )r   rG   r4   r2   r   r   r   Ú	comm_info¥   s    
zInProcessKernelClient.comm_infoc                 C   s"   | j du rd}t|ƒ‚|| j _dS )zHandle kernel input.Nz*Cannot send input reply. No kernel exists.)r   ÚRuntimeErrorZraw_input_str)r   Ústringr2   r   r   r   Úinput¬   s    
zInProcessKernelClient.inputc                 C   s(   | j  dd|i¡}|  |¡ |d d S )zHandle an is_complete request.Zis_complete_requestr)   r.   r/   rE   )r   r)   r2   r   r   r   Úis_complete³   s    
z!InProcessKernelClient.is_completec           	      C   sŽ   | j }|du rd}t|ƒ‚|j}| j ||¡ | ¡ }tdurRt|jƒ}||ƒ nt 	¡ }| 
| |¡¡ | jj|dd�\}}| j |¡ dS )z0Send a message to the kernel and handle a reply.Nz&Cannot send request. No kernel exists.F)Úcopy)r   rI   Zshell_streamr1   ÚsendZrecv_multipartr   Údispatch_shellÚasyncioÚget_event_loopÚrun_until_completeÚrecvr    Zcall_handlers_later)	r   r2   r   ÚstreamZ	msg_partsrO   ÚloopZidentsZ	reply_msgr   r   r   r3   ¹   s    

z)InProcessKernelClient._dispatch_to_kernelc                 C   s   | j  ||¡S )zGet a shell message.)r    Úget_msg©r   ÚblockÚtimeoutr   r   r   Úget_shell_msgÌ   s    z#InProcessKernelClient.get_shell_msgc                 C   s   | j  ||¡S )zGet an iopub message.)r"   rV   rW   r   r   r   Úget_iopub_msgÐ   s    z#InProcessKernelClient.get_iopub_msgc                 C   s   | j  ||¡S )zGet a stdin message.)r$   rV   rW   r   r   r   Úget_stdin_msgÔ   s    z#InProcessKernelClient.get_stdin_msgc                 C   s   | j  ||¡S )zGet a control message.)r&   rV   rW   r   r   r   Úget_control_msgØ   s    z%InProcessKernelClient.get_control_msg)FTNN)N)Nr   )TFr<   )F)N)TN)TN)TN)TN)'Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r	   r   r!   r#   r%   r
   r'   r   r   r   r   r   r   Úpropertyr    r"   r$   r&   r(   r5   r9   r;   rB   rD   rF   rH   rK   rL   r3   rZ   r[   r\   r]   Ú__classcell__r   r   r   r   r      sH   






	 ÿ

	






r   )ra   rP   Zjupyter_client.clientr   Zjupyter_client.clientabcr   Zjupyter_core.utilsr   Z	traitletsr   r   r   Zchannelsr	   r
   r   Úregisterr   r   r   r   Ú<module>   s    C