o
    œh£   ã                   @   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                       s2  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d7d&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   úPc:\Users\shubh\Videos\agent\nova\Lib\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 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   ó   | j d u r|  | ¡| _ | j S ©N)Z_shell_channelÚshell_channel_class©r   r   r   r   Úshell_channelH   ó   
z#InProcessKernelClient.shell_channelc                 C   r   r   )Z_iopub_channelÚiopub_channel_classr    r   r   r   Úiopub_channelN   r"   z#InProcessKernelClient.iopub_channelc                 C   r   r   )Z_stdin_channelÚstdin_channel_classr    r   r   r   Ústdin_channelT   r"   z#InProcessKernelClient.stdin_channelc                 C   r   r   )Z_control_channelÚcontrol_channel_classr    r   r   r   Úcontrol_channelZ   r"   z%InProcessKernelClient.control_channelc                 C   r   r   )Z_hb_channelÚhb_channel_classr    r   r   r   Ú
hb_channel`   r"   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/   Úcontentr4   r   r   r   Úexecutei   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_requestr0   r1   ©Úlenr2   r3   r4   r5   )r   r+   r8   r6   r4   r   r   r   Úcompletez   s   
zInProcessKernelClient.completer   c                 C   sB   |du rt |ƒ}t|||d�}| j d|¡}|  |¡ |d d S )zGet code inspection.N)r+   r8   Údetail_levelZinspect_requestr0   r1   r9   )r   r+   r8   r<   r6   r4   r   r   r   Úinspectƒ   s   ý
zInProcessKernelClient.inspectÚrangec                 K   s:   t d|||dœ|¤Ž}| j d|¡}|  |¡ |d d S )zGet code history.)ÚrawÚoutputÚhist_access_typeZhistory_requestr0   r1   Nr   ©r2   r3   r4   r5   )r   r?   r@   rA   Úkwdsr6   r4   r   r   r   Úhistory�   ó   
zInProcessKernelClient.historyc                 C   s   d}t |ƒ‚)zHandle shutdown.z!Cannot shutdown in-process kernel)ÚNotImplementedError)r   Zrestartr4   r   r   r   Úshutdown—   s   zInProcessKernelClient.shutdownc                 C   s"   | j  d¡}|  |¡ |d d S )zRequest kernel info.Zkernel_info_requestr0   r1   ©r3   r4   r5   )r   r4   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_requestr0   r1   rB   )r   rJ   r6   r4   r   r   r   Ú	comm_info£   rE   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   Ústringr4   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+   r0   r1   rH   )r   r+   r4   r   r   r   Úis_complete±   s   
z!InProcessKernelClient.is_completec           	      C   sŽ   | j }|du rd}t|ƒ‚|j}| j ||¡ | ¡ }tdur)t|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   rL   Zshell_streamr3   ÚsendZrecv_multipartr   Údispatch_shellÚasyncioÚget_event_loopÚrun_until_completeÚrecvr!   Zcall_handlers_later)	r   r4   r   ÚstreamZ	msg_partsrR   ÚloopZidentsZ	reply_msgr   r   r   r5   ·   s   

z)InProcessKernelClient._dispatch_to_kernelc                 C   ó   | j  ||¡S )zGet a shell message.)r!   Úget_msg©r   ÚblockÚtimeoutr   r   r   Úget_shell_msgÊ   ó   z#InProcessKernelClient.get_shell_msgc                 C   rY   )zGet an iopub message.)r$   rZ   r[   r   r   r   Úget_iopub_msgÎ   r_   z#InProcessKernelClient.get_iopub_msgc                 C   rY   )zGet a stdin message.)r&   rZ   r[   r   r   r   Úget_stdin_msgÒ   r_   z#InProcessKernelClient.get_stdin_msgc                 C   rY   )zGet a control message.)r(   rZ   r[   r   r   r   Úget_control_msgÖ   r_   z%InProcessKernelClient.get_control_msg)FTNNr   )Nr   )TFr>   )F)TN)'Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r	   r   r#   r%   r'   r
   r)   r   r   r   r   r   r   Úpropertyr!   r$   r&   r(   r*   r7   r;   r=   rD   rG   rI   rK   rN   rO   r5   r^   r`   ra   rb   Ú__classcell__r   r   r   r   r      sJ    






	
ÿ

	





r   )rf   rS   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     A