o
    iK!                     @  s   d Z ddlm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 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.    )annotationsN)Any)KernelClient)KernelClientABC)run_sync)InstanceTypedefault   )InProcessChannelInProcessHBChannelc                      s@  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dCdD 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					dEdFd%d&ZdGd'd(ZdHd*d+ZdId-d.ZdCd/d0Zd1d2 ZdGd3d4Zd5d6 Zd7d8 Z d9d: Z!dJd;d<Z"dJd=d>Z#dJd?d@Z$dJdAdBZ%  Z&S )K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_noneblocking_classc                 C  s   ddl m} |S )Nr
   )BlockingInProcessKernelClient)blockingr   )selfr    r   U/var/www/edux/Edux_v2/venv/lib/python3.10/site-packages/ipykernel/inprocess/client.py_default_blocking_class8   s   z-InProcessKernelClient._default_blocking_classFsessionboolreturndict[str, int | str | bytes]c                   s   t  j|d}| j|d< |S )z'Get the connection info for the client.)r   kernel)superget_connection_infor   )r   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   	frontendsappend)r   argskwargsr   r   r   r    D   s   
z$InProcessKernelClient.start_channelsc                 C     | j d u r| | | _ | j S N)_shell_channelshell_channel_classr   r   r   r   shell_channelJ      
z#InProcessKernelClient.shell_channelc                 C  r%   r&   )_iopub_channeliopub_channel_classr)   r   r   r   iopub_channelP   r+   z#InProcessKernelClient.iopub_channelc                 C  r%   r&   )_stdin_channelstdin_channel_classr)   r   r   r   stdin_channelV   r+   z#InProcessKernelClient.stdin_channelc                 C  r%   r&   )_control_channelcontrol_channel_classr)   r   r   r   control_channel\   r+   z%InProcessKernelClient.control_channelc                 C  r%   r&   )_hb_channelhb_channel_classr)   r   r   r   
hb_channelb   r+   z InProcessKernelClient.hb_channelNcodestrsilentstore_historyuser_expressionsdict[str, Any] | Noneallow_stdinbool | Nonestop_on_errorc           
      C  sZ   |du r| j }t||||pi |d}| jd|}| | |d d }	t|	ts+J |	S )zExecute code on the client.N)r8   r:   r;   r<   r>   execute_requestheadermsg_id)r>   dictr   msg_dispatch_to_kernel
isinstancer9   )
r   r8   r:   r;   r<   r>   r@   contentrE   resr   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)r8   
cursor_poscomplete_requestrB   rC   lenrD   r   rE   rF   )r   r8   rK   rH   rE   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)r8   rK   detail_levelinspect_requestrB   rC   rM   )r   r8   rK   rP   rH   rE   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_typehistory_requestrB   rC   Nr   rD   r   rE   rF   )r   rT   rU   rV   kwdsrH   rE   r   r   r   history      
zInProcessKernelClient.historyc                 C  s   d}t |)zHandle shutdown.z!Cannot shutdown in-process kernel)NotImplementedError)r   restartrE   r   r   r   shutdown   s   zInProcessKernelClient.shutdownc                 C  s"   | j d}| | |d d S )zRequest kernel info.kernel_info_requestrB   rC   r   rE   rF   )r   rE   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_namecomm_info_requestrB   rC   rX   )r   rb   rH   rE   r   r   r   	comm_info   r[   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   RuntimeErrorraw_input_str)r   stringrE   r   r   r   input   s   
zInProcessKernelClient.inputc                 C  s(   | j dd|i}| | |d d S )zHandle an is_complete request.is_complete_requestr8   rB   rC   r`   )r   r8   rE   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   re   shell_streamr   sendrecv_multipartr   dispatch_shellasyncioget_event_looprun_until_completerecvr*   call_handlers_later)	r   rE   r   stream	msg_partsro   loop_idents	reply_msgr   r   r   rF      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  rz   )zGet an iopub message.)r.   r{   r|   r   r   r   get_iopub_msg   r   z#InProcessKernelClient.get_iopub_msgc                 C  rz   )zGet a stdin message.)r1   r{   r|   r   r   r   get_stdin_msg   r   z#InProcessKernelClient.get_stdin_msgc                 C  rz   )zGet a control message.)r4   r{   r|   r   r   r   get_control_msg   r   z%InProcessKernelClient.get_control_msg)F)r   r   r   r   )FTNNT)r8   r9   r:   r   r;   r   r<   r=   r>   r?   r@   r   r   r9   r&   )Nr   )TFrS   )TN)'__name__
__module____qualname____doc__r   r   r(   r-   r0   r3   r   r6   r   r   r	   r   r   r    propertyr*   r.   r1   r4   r7   rJ   rO   rR   rZ   r^   ra   rd   rh   rj   rF   r   r   r   r   __classcell__r   r   r   r   r   !   sR    








	





r   )r   
__future__r   rp   typingr   jupyter_client.clientr   jupyter_client.clientabcr   jupyter_core.utilsr   	traitletsr   r   r	   channelsr   r   r   registerr   r   r   r   <module>   s     I