U
    /e)                     @   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   >/tmp/pip-unpacked-wheel-g43y689f/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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 kr| | | _ | j S N)Z_shell_channelshell_channel_classr   r   r   r   shell_channelG   s    
z#InProcessKernelClient.shell_channelc                 C   s   | j d kr| | | _ | j S r   )Z_iopub_channeliopub_channel_classr   r   r   r   iopub_channelM   s    
z#InProcessKernelClient.iopub_channelc                 C   s   | j d kr| | | _ | j S r   )Z_stdin_channelstdin_channel_classr   r   r   r   stdin_channelS   s    
z#InProcessKernelClient.stdin_channelc                 C   s   | j d kr| | | _ | j S r   )Z_control_channelcontrol_channel_classr   r   r   r   control_channelY   s    
z%InProcessKernelClient.control_channelc                 C   s   | j d kr| | | _ | j S r   )Z_hb_channelhb_channel_classr   r   r   r   
hb_channel_   s    
z InProcessKernelClient.hb_channelFNc                 C   sH   |dk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   executeh   s    
zInProcessKernelClient.executec                 C   s@   |dk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   completey   s    
zInProcessKernelClient.completer   c                 C   sB   |dk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k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k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krd}t||j}| j|| | }tdk	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asyncioZget_event_loopZrun_until_completerecvr    Zcall_handlers_later)	r   r2   r   streamZ	msg_partsrO   Z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"   rS   rT   r   r   r   get_iopub_msg   s    z#InProcessKernelClient.get_iopub_msgc                 C   s   | j ||S )zGet a stdin message.)r$   rS   rT   r   r   r   get_stdin_msg   s    z#InProcessKernelClient.get_stdin_msgc                 C   s   | j ||S )zGet a control message.)r&   rS   rT   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   rW   rX   rY   rZ   __classcell__r   r   r   r   r      sN   






	       

	






r   )r^   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    @