
    kKj                        d Z ddlmZ ddlZddlZddlZddlZddlm	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
      Z G d de      Zy)z7Replacements for sys.displayhook that publish over ZMQ.    )annotationsN)
ContextVar)DisplayHook)Sessionextract_header)AnyInstancedefault)encode_images
json_cleanc                  <    e Zd ZdZdZd Zd Zd Zed        Z	d Z
y)	ZMQDisplayHookzOA simple displayhook that publishes the object's repr over a ZeroMQ
    socket.s   execute_resultc                    || _         || _        t        d      | _        | j                  j	                  i        i | _        y)zInitialize the hook.parent_headerN)session
pub_socketr   _parent_headerset_parent_header_global)selfr   r   s      f/Users/ahmed/devFolder/Ultron/claude-voice/.venv/lib/python3.12/site-packages/ipykernel/displayhook.py__init__zZMQDisplayHook.__init__   s9    $:D_:U#%'"    c                     y)z$This method is replaced in kernelappr    r   s    r   get_execution_countz"ZMQDisplayHook.get_execution_count#   s    r   c                R   |y|t         _        t        j                  j	                          t        j
                  j	                          | j                         dt        |      ii d}| j                  j                  | j                  d|| j                  | j                         y)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contentss      r   __call__zZMQDisplayHook.__call__'   s    ;




#779!49-

 	OO%%** 	 	
r   c                n    	 | j                   j                         S # t        $ r | j                  cY S w xY wNr   getLookupErrorr   r   s    r   r   zZMQDisplayHook.parent_header<   7    	.&&**,, 	.---	.    44c                ^    t        |      }| j                  j                  |       || _        yzSet the parent header.Nr   r   r   r   r   r#   r   s      r   
set_parentzZMQDisplayHook.set_parentC   (    &v..%2"r   N)__name__
__module____qualname____doc__r-   r   r   r0   propertyr   r<   r   r   r   r   r      s6     E(
* . .3r   r   c                       e Zd ZU dZdZ eed      Z ed      Z	de
d<    e       Zde
d<    fd	Z ed
      d        Zed        Zd Zd Zed        Zd Zd Zd ZddZd Z xZS )ZMQShellDisplayHookzA 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ContextVar[dict[str, Any]]r   zdict[str, t.Any] | Nonemsgc                z    t        |   |i | t        d      | _        | j                  j	                  i        y )Nr   )superr   r   r   r   )r   argskwargs	__class__s      r   r   zZMQShellDisplayHook.__init__W   s6    $)&)(9#r   _thread_localc                *    t        j                         S r2   )	threadinglocalr   s    r   _default_thread_localz)ZMQShellDisplayHook._default_thread_local\   s      r   c                |    t        | j                  d      sg | j                  _        | j                  j                  S )Nhooks)hasattrrL   rR   r   s    r   _hookszZMQShellDisplayHook._hooks`   s2    t))73')D$!!'''r   c                :    | j                   j                  |       y)a(  Register a transform hook on the execute_result message.

        Mirrors ``ZMQDisplayPublisher.register_hook``. Each hook receives the
        outbound message dict and must return either a (possibly mutated)
        message dict to continue the chain, or ``None`` to suppress the send.
        N)rT   appendr   hooks     r   register_hookz!ZMQShellDisplayHook.register_hookf   s     	4 r   c                Z    	 | j                   j                  |       y# t        $ r Y yw xY w)z=Remove a previously registered hook. Returns True on success.TF)rT   remove
ValueErrorrW   s     r   unregister_hookz#ZMQShellDisplayHook.unregister_hooko   s.    	KKt$ 		s    	**c                n    	 | j                   j                         S # t        $ r | j                  cY S w xY wr2   r3   r   s    r   r   z!ZMQShellDisplayHook.parent_headerw   r6   r7   c                ^    t        |      }| j                  j                  |       || _        yr9   r:   r;   s      r   r<   zZMQShellDisplayHook.set_parent~   r=   r   c                ~    | j                   r1| j                   j                  di i d| j                        | _        yy)zStart the display hook.r"   )r    r!   )r#   N)r   rF   r   r   s    r   start_displayhookz%ZMQShellDisplayHook.start_displayhook   sB    <<||''  " )) ( DH r   c                V    | j                   r| j                  | j                   d   d<   yy)zWrite the output prompt.contentr   N)rF   prompt_countr   s    r   write_output_promptz'ZMQShellDisplayHook.write_output_prompt   s(    88595F5FDHHY 12 r   c                    | j                   r7t        t        |            | j                   d   d<   || j                   d   d<   yy)z!Write format data to the message.rc   r    r!   N)rF   r   r   )r   format_dictmd_dicts      r   write_format_dataz%ZMQShellDisplayHook.write_format_data   s>    88*4];5O*PDHHY'.5DHHY
+ r   c                   t         j                  j                          t         j                  j                          | j                  r| j                  d   d   rn| j
                  rb| j                  }| j                  D ]  } ||      }|d| _         y | j
                  j                  | j                  || j                         d| _        y)a  Finish up all displayhook activities.

        Runs the registered hook chain before ``session.send``. Each hook
        either returns a message (to continue) or ``None`` (to suppress the
        send). This mirrors the transform pipeline on
        ``ZMQDisplayPublisher.publish`` so a single hook implementation can
        attach to both the ``display_data`` and ``execute_result`` paths.
        rc   r    N)r$   )
r'   r(   r)   r*   rF   r   rT   r,   r   r-   )r   rF   rX   s      r   finish_displayhookz&ZMQShellDisplayHook.finish_displayhook   s     	



88+F3((C3i;#DH	 $
 LLdoos$**Er   r2   )r>   r?   r@   rA   r-   r	   r   r   r   r   __annotations__rL   r   r
   rP   rB   rT   rY   r]   r   r<   ra   re   ri   rk   __classcell__)rK   s   @r   rD   rD   J   s    & Ew40G%J..EM	  $
 _! ! ( (
! . .3
G
6r   rD   )rA   
__future__r   r%   r'   rN   typingtcontextvarsr   IPython.core.displayhookr   jupyter_client.sessionr   r   	traitletsr   r	   r
   ipykernel.jsonutilr   r   r   rD   r   r   r   <module>rv      sD    = #  
   " 0 : , , 833 33ld+ dr   