
    ^h                        S SK r S SKJr  S SKJrJrJr  S SKrS SK	J
r
  S SKJrJr  S SKJr  S SKJr  S SKJr   " S	 S
5      r " S S5      r " S S5      r " S S\5      r " S S\5      r " S S5      r " S S\R4                  5      rg)    N)StringIO)AnyOptionalTextIO)DisplayHook)CapturingDisplayPublisherDisplayPublisher)InteractiveShell)
CapturedIO)shellc                   ,    \ rS rSrS rS rS rS rSrg)Tee   c                     Xl         X l        g Nout1out2)selfr   r   s      Y/home/james-whalen/.local/lib/python3.13/site-packages/ipyflow/tracing/output_recorder.py__init__Tee.__init__       		    c                 N    US;   a
  [        5       e[        U R                  U5      $ )Nr   )AttributeErrorgetattrr   r   items     r   __getattr__Tee.__getattr__   %    ## ""tyy$''r   c                 p    U R                   R                  U5        U R                  R                  U5        g r   )r   writer   )r   datas     r   r$   	Tee.write   s"    				r   c                 l    U R                   R                  5         U R                  R                  5         g r   )r   flushr   r   s    r   r(   	Tee.flush   s    				r   r   N)	__name__
__module____qualname____firstlineno__r   r    r$   r(   __static_attributes__ r   r   r   r      s    (r   r   c                   J    \ rS rSrS\S\SS4S jrS\S\4S jrSSS	 jjr	S
r
g)TeeDisplayHook"   disp1disp2returnNc                     Xl         X l        g r   r4   r5   )r   r4   r5   s      r   r   TeeDisplayHook.__init__#   s    

r   r   c                 N    US;   a
  [        5       e[        U R                  U5      $ )Nr8   )r   r   r4   r   s     r   r    TeeDisplayHook.__getattr__'   s%    %% ""tzz4((r   c                 @    U R                  US9  U R                  US9  g )N)resultr8   )r   r=   s     r   __call__TeeDisplayHook.__call__-   s    

&
!

&
!r   r8   r   r6   N)r+   r,   r-   r.   r   r   strr   r    r>   r/   r0   r   r   r2   r2   "   s9    k + $ ) ) )" "r   r2   c                   Z    \ rS rSrS\S\SS4S jrS\S\4S jrSS	 jr	SS
 jr
SS jrSrg)TeeDisplayPublisher2   pub1pub2r6   Nc                     Xl         X l        g r   rE   rF   )r   rE   rF   s      r   r   TeeDisplayPublisher.__init__3   r   r   r   c                 N    US;   a
  [        5       e[        U R                  U5      $ )NrH   )r   r   rE   r   s     r   r    TeeDisplayPublisher.__getattr__7   r"   r   c                 t    U R                   R                  " U0 UD6  U R                  R                  " U0 UD6  g r   )rE   publishrF   r   argskwargss      r   rM   TeeDisplayPublisher.publish=   s0    		4*6*		4*6*r   c                 t    U R                   R                  " U0 UD6  U R                  R                  " U0 UD6  g r   )rE   clear_outputrF   rN   s      r   rS    TeeDisplayPublisher.clear_outputA   s0    		//		//r   c                     [        U R                  S5      (       a  U R                  R                  " U0 UD6  [        U R                  S5      (       a  U R                  R                  " U0 UD6  g g )N
set_parent)hasattrrE   rV   rF   rN   s      r   rV   TeeDisplayPublisher.set_parentE   sW    499l++II  $1&1499l++II  $1&1 ,r   rH   r@   )r+   r,   r-   r.   r	   r   rA   r   r    rM   rS   rV   r/   r0   r   r   rC   rC   2   s@    - 5E $ ( ( (+02r   rC   c                       \ rS rSrSS jrSrg)&TeeCompatibleCapturingDisplayPublisherL   c                 8    U R                   R                  5         g r   )outputsclear)r   waits     r   rS   3TeeCompatibleCapturingDisplayPublisher.clear_outputM   s    r   r0   N)F)r+   r,   r-   r.   rS   r/   r0   r   r   rZ   rZ   L   s    r   rZ   c                   L   ^  \ rS rSrSS	U 4S jjjrS
S\SS4U 4S jjjrSrU =r$ )IPyflowCapturedIOQ   Nr6   c                 .   > [         TU ]  XUS9  X@l        g )N)r]   )superr   	_exec_ctr)r   stdoutstderrr]   exec_ctr	__class__s        r   r   IPyflowCapturedIO.__init__R   s    9!r   render_out_exprc                    > [         TU ]  5         U(       d  g [        5       nUR                  R	                  S0 5      R	                  U R
                  5      nUb  UR                  U5        g g )NOut)re   showr   user_nsgetrf   displayhook)r   rl   shell_expr_resultrj   s       r   ro   IPyflowCapturedIO.showV   sW    nn((377G"{+ #r   )rf   )NNr@   )T)	r+   r,   r-   r.   r   boolro   r/   __classcell__rj   s   @r   rb   rb   Q   s%    " ",D ,D , ,r   rb   c                   >    \ rS rSrSrS	S
S jjrS\4S jrS
S jrSr	g)CaptureOutputTee`   a  
Context manager for capturing and replicating stdout/err and rich display publishers.
NB: This is a modified version of IPython.utils.capture.capture_output that doesn't capture
  the displayhook as well (the thing that renders the final expression in a cell). Trying
  to capture both it and the display publisher seems like it can confuse ipywidgets, and
  for this we can always rerender it anyway if necessary using the Out dictionary.
r6   Nc                 n    Xl         X l        X0l        S U l        S U l        S U l        S U l        SU l        g NF)rg   rh   displayr   
sys_stdout
sys_stderrsave_display_pub_in_context)r   rg   rh   r~   s       r   r   CaptureOutputTee.__init__i   s6    15
,0,0<@ r   c                 B   [         R                  U l        [         R                  U l        U R
                  (       a*  [        5       U l        U R                  c  S U l        SU l        S =n=p#U R                  (       a-  [        5       n[        [         R                  U5      [         l        U R                  (       a-  [        5       n[        [         R                  U5      [         l        U R
                  (       am  U R                  b`  U R                  R                  U l        [        5       nUR                  n[        U R                  R                  U5      U R                  l
        SU l        [        UUUU R                  c  S 5      $ U R                  R                   5      $ )NFT)sysrg   r   rh   r   r~   r   r   r   r   display_pubrZ   r]   rC   r   rb   execution_count)r   rg   rh   r]   capture_display_pubs        r   	__enter__CaptureOutputTee.__enter__s   s*   ****<<DJzz!(,%$$(((;;ZFSZZ0CJ;;ZFSZZ0CJ<<DJJ2$(JJ$:$:D!"H"J)11G%8

&&(;&DJJ"   JJ&D	
 	
 -1JJ,F,F	
 	
r   c                 X   U R                   (       d  g SU l         U R                  b  U R                  [        l        S U l        U R                  b  U R                  [        l        S U l        U R                  (       a.  U R                  (       a  U R                  U R                  l	        g g g r}   )
r   r   r   rg   r   rh   r~   r   r   r   )r   exc_type	exc_value	tracebacks       r   __exit__CaptureOutputTee.__exit__   st     ??&CJ"DO??&CJ"DO<<DJJ%)%:%:DJJ" '<r   )r   r~   r   r   rh   rg   r   r   )TTTr@   )
r+   r,   r-   r.   __doc__r   r   r   r   r/   r0   r   r   rz   rz   `   s    !
: 
B;r   rz   c                   z   ^  \ rS rSrSrU 4S jr\R                  " \R                  5      S 5       rSS jr	Sr
U =r$ )OutputRecorder   Fc                    > [         TU ]  " U0 UD6  U R                  5          [        5       U l        S S S 5        S U l        SU l        g ! , (       d  f       N= fr}   )re   r   persistent_fieldsrz   capture_output_teecapture_outputcapturing_output)r   rO   rP   rj   s      r   r   OutputRecorder.__init__   sJ    $)&)##%&6&8D# &" % &%s   A		
Ac                 P    SU l         U R                  R                  5       U l        g )NT)r   r   r   r   )r   ___s      r   init_moduleOutputRecorder.init_module   s!     $"55??Ar   c                 p    U R                   (       d  g SU l         U R                  R                  S S S 5        g r}   )r   r   r   r)   s    r   done_capturing_output$OutputRecorder.done_capturing_output   s.    $$ %((tT:r   )r   r   r   r@   )r+   r,   r-   r.   should_patch_meta_pathr   pycregister_raw_handlerr   r   r/   rw   rx   s   @r   r   r      s=    "& 	coo.B /B; ;r   r   )r   ior   typingr   r   r   pyccolor   IPython.core.displayhookr   IPython.core.displaypubr   r	   IPython.core.interactiveshellr
   IPython.utils.capturer   ipyflow.singletonsr   r   r2   rC   rZ   rb   rz   
BaseTracerr   r0   r   r   <module>r      su    
  ( (  0 O : , $ (" " 2 24-F 
,
 ,?; ?;D;S^^ ;r   