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mZ ddl	m
Z
mZ ddlmZmZmZ ddlmZmZ G dd	„ d	ƒZG d
d„ deƒZdS )z7Replacements for sys.displayhook that publish over ZMQ.é    )ÚannotationsN)ÚDisplayHook)ÚSessionÚextract_header)ÚAnyÚDictÚInstance)Úencode_imagesÚ
json_cleanc                   @  s4   e Zd ZdZdZdd„ Zdd„ Zdd„ Zd	d
„ ZdS )ÚZMQDisplayHookzOA simple displayhook that publishes the object's repr over a ZeroMQ
    socket.s   execute_resultc                 C  s   || _ || _i | _dS )zInitialize the hook.N)ÚsessionÚ
pub_socketÚparent_header)Úselfr   r   © r   úX/home/yuhw/wc/wire-cell-python/wcpy/lib/python3.9/site-packages/ipykernel/displayhook.pyÚ__init__   s    zZMQDisplayHook.__init__c                 C  s   dS )z$This method is replaced in kernelappr   r   ©r   r   r   r   Úget_execution_count   s    z"ZMQDisplayHook.get_execution_countc                 C  s^   |du rdS |t _tj ¡  tj ¡  |  ¡ dt|ƒii dœ}| jj	| j
d|| j| jd� dS )zHandle a hook call.Nz
text/plain)Úexecution_countÚdataÚmetadataÚexecute_result)ÚparentÚident)ÚbuiltinsÚ_ÚsysÚstdoutÚflushÚstderrr   Úreprr   Úsendr   r   Útopic)r   ÚobjÚcontentsr   r   r   Ú__call__"   s    


ýÿzZMQDisplayHook.__call__c                 C  s   t |ƒ| _dS )zSet the parent header.N©r   r   ©r   r   r   r   r   Ú
set_parent3   s    zZMQDisplayHook.set_parentN)	Ú__name__Ú
__module__Ú__qualname__Ú__doc__r#   r   r   r&   r)   r   r   r   r   r      s   r   c                   @  sf   e Zd ZU dZdZeedd�Zedd�Z	e
i ƒZded< dd„ Zd	d
„ Zdd„ Zddd„Zdd„ ZdS )ÚZMQShellDisplayHookz¸A displayhook subclass that publishes data using ZeroMQ. This is intended
    to work with an InteractiveShell instance. It sends a dict of different
    representations of the object.NT)Ú
allow_nonezdict[str, t.Any] | NoneÚmsgc                 C  s   t |ƒ| _dS )z%Set the parent for outbound messages.Nr'   r(   r   r   r   r)   D   s    zZMQShellDisplayHook.set_parentc                 C  s&   | j r"| j jdi i dœ| jd�| _dS )zStart the display hook.r   )r   r   )r   N)r   r0   r   r   r   r   r   Ústart_displayhookH   s    þúz%ZMQShellDisplayHook.start_displayhookc                 C  s   | j r| j| j d d< dS )zWrite the output prompt.Úcontentr   N)r0   Zprompt_countr   r   r   r   Úwrite_output_promptT   s    z'ZMQShellDisplayHook.write_output_promptc                 C  s.   | j r*tt|ƒƒ| j d d< || j d d< dS )z!Write format data to the message.r2   r   r   N)r0   r
   r	   )r   Zformat_dictZmd_dictr   r   r   Úwrite_format_dataY   s    z%ZMQShellDisplayHook.write_format_datac                 C  sP   t j ¡  t j ¡  | jrF| jd d rF| jrF| jj| j| j| jd� d| _dS )z%Finish up all displayhook activities.r2   r   )r   N)	r   r   r   r    r0   r   r"   r   r#   r   r   r   r   Úfinish_displayhook_   s
    

z&ZMQShellDisplayHook.finish_displayhook)N)r*   r+   r,   r-   r#   r   r   r   r   r   r   r   Ú__annotations__r)   r1   r3   r4   r5   r   r   r   r   r.   8   s   


r.   )r-   Ú
__future__r   r   r   ÚtypingÚtZIPython.core.displayhookr   Zjupyter_client.sessionr   r   Z	traitletsr   r   r   Zipykernel.jsonutilr	   r
   r   r.   r   r   r   r   Ú<module>   s   &