
    Mh%                        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Zddlm	Z	 ddl
mZmZmZ ddlmZmZ ddlmZ d	d
gZe	ddd            Z G d d          Z G d d
          ZdS )a  
patch_stdout
============

This implements a context manager that ensures that print statements within
it won't destroy the user interface. The context manager will replace
`sys.stdout` by something that draws the output above the current prompt,
rather than overwriting the UI.

Usage::

    with patch_stdout(application):
        ...
        application.run()
        ...

Multiple applications can run in the body of the context manager, one after the
other.
    )annotationsN)contextmanager)	GeneratorTextIOcast   )get_app_sessionrun_in_terminal)Outputpatch_stdoutStdoutProxyFrawboolreturnGenerator[None, None, None]c              #    K   t          |           5 }t          j        }t          j        }t	          t
          |          t          _        t	          t
          |          t          _        	 dV  |t          _        |t          _        n# |t          _        |t          _        w xY w	 ddd           dS # 1 swxY w Y   dS )a  
    Replace `sys.stdout` by an :class:`_StdoutProxy` instance.

    Writing to this proxy will make sure that the text appears above the
    prompt, and that it doesn't destroy the output from the renderer.  If no
    application is curring, the behavior should be identical to writing to
    `sys.stdout` directly.

    Warning: If a new event loop is installed using `asyncio.set_event_loop()`,
        then make sure that the context manager is applied after the event loop
        is changed. Printing to stdout will be scheduled in the event loop
        that's active when the context manager is created.

    :param raw: (`bool`) When True, vt100 terminal escape sequences are not
                removed/escaped.
    )r   N)r   sysstdoutstderrr   r   )r   proxyoriginal_stdoutoriginal_stderrs       [/var/www/html/test/jupyter/venv/lib/python3.11/site-packages/prompt_toolkit/patch_stdout.pyr   r   (   s      $ 
			 )** &%((
&%((
	)EEE(CJ(CJJ )CJ(CJ((((J) ) ) ) ) ) ) ) ) ) ) ) ) ) ) ) ) )s*   AB3+B/B3B""B33B7:B7c                      e Zd ZdZdS )_Donez-Sentinel value for stopping the stdout proxy.N)__name__
__module____qualname____doc__     r   r   r   I   s        3333r!   r   c                      e Zd ZdZ	 	 d&d'd
Zd(dZd)dZd*dZd+dZd*dZ	d,dZ
d-dZd.dZd*dZd/dZd*dZed0d             Zd1d!Zd2d"Zed3d#            Zed3d$            Zd%S )4r   a  
    File-like object, which prints everything written to it, output above the
    current application/prompt. This class is compatible with other file
    objects and can be used as a drop-in replacement for `sys.stdout` or can
    for instance be passed to `logging.StreamHandler`.

    The current application, above which we print, is determined by looking
    what application currently runs in the `AppSession` that is active during
    the creation of this instance.

    This class can be used as a context manager.

    In order to avoid having to repaint the prompt continuously for every
    little write, a short delay of `sleep_between_writes` seconds will be added
    between writes in order to bundle many smaller writes in a short timespan.
    皙?Fsleep_between_writesfloatr   r   r   Nonec                   || _         || _        t          j                    | _        g | _        t                      | _        | j        j        | _	        t          j                    | _        |                                 | _        d| _        d S NF)r$   r   	threadingRLock_lock_bufferr	   app_sessionoutput_outputqueueQueue_flush_queue_start_write_thread_flush_threadclosed)selfr$   r   s      r   __init__zStdoutProxy.__init___   sw    
 %9!_&&
"$ +,,  $/6 7<kmm!5577r!   c                    | S Nr    r6   s    r   	__enter__zStdoutProxy.__enter__z   s    r!   argsobjectc                .    |                                   d S r9   )close)r6   r<   s     r   __exit__zStdoutProxy.__exit__}   s    

r!   c                    | j         sH| j                            t                                 | j                                         d| _         dS dS )z
        Stop `StdoutProxy` proxy.

        This will terminate the write thread, make sure everything is flushed
        and wait for the write thread to finish.
        TN)r5   r2   putr   r4   joinr:   s    r   r?   zStdoutProxy.close   sS     { 	!!%''***##%%%DKKK	 	r!   threading.Threadc                f    t          j        | j        dd          }|                                 |S )Nzpatch-stdout-flush-threadT)targetnamedaemon)r)   Thread_write_threadstart)r6   threads     r   r3   zStdoutProxy._start_write_thread   s9    !%,
 
 

 	r!   c                   d}|s| j                                         }t          |t                    rd S |s6g }|                    |           	 	 | j                                         }t          |t                    rd}n+|                    |           n# t          j        $ r Y nw xY w^|                                 }| 	                    |d
                    |                     |t          j        | j                   |d S d S )NFT )r2   get
isinstancer   append
get_nowaitr0   Empty_get_app_loop_write_and_flushrC   timesleepr$   )r6   doneitemtextapp_loops        r   rJ   zStdoutProxy._write_thread   sB     	6$((**D$&&   DKK	**,7799D "$.. *#D)))) {   E	* ))++H!!(BGGDMM:::
 #
44555A   	6  	6  	6  	6  	6s   B B+*B+ asyncio.AbstractEventLoop | Nonec                0    | j         j        }|dS |j        S )zj
        Return the event loop for the application currently running in our
        `AppSession`.
        N)r-   apploop)r6   r^   s     r   rT   zStdoutProxy._get_app_loop   s     
 ";4xr!   r_   rZ   strc                l     d fddfd}|              dS |                     |           dS )zx
        Write the given text to stdout and flush.
        If an application is running, use `run_in_terminal`.
        r   r&   c                      j                                           j        r j                                        n j                                         j                                          d S r9   )r/   enable_autowrapr   	write_rawwriteflushr6   rZ   s   r   write_and_flushz5StdoutProxy._write_and_flush.<locals>.write_and_flush   sl     L((***x )&&t,,,,""4(((L     r!   c                 *    t           d           d S )NF)in_executor)r
   )rh   s   r   write_and_flush_in_loopz=StdoutProxy._write_and_flush.<locals>.write_and_flush_in_loop   s     O??????r!   Nr   r&   )call_soon_threadsafe)r6   r_   rZ   rk   rh   s   ` ` @r   rU   zStdoutProxy._write_and_flush   s    	! 	! 	! 	! 	! 	! 	!	@ 	@ 	@ 	@ 	@ 	@
 <O %%&=>>>>>r!   datac                    d|v r^|                     dd          \  }}| j        |dgz   }|g| _        d                    |          }| j                            |           dS | j                            |           dS )a  
        Note: print()-statements cause to multiple write calls.
              (write('line') and write('
')). Of course we don't want to call
              `run_in_terminal` for every individual call, because that's too
              expensive, and as long as the newline hasn't been written, the
              text itself is again overwritten by the rendering of the input
              command line. Therefor, we have a little buffer which holds the
              text until a newline is written to stdout.
        
r   rN   N)rsplitr,   rC   r2   rB   rQ   )r6   rn   beforeafterto_writerZ   s         r   _writezStdoutProxy._write   s     4<< !KKa00MFE|vtn4H!7DL778$$D!!$''''' L%%%%%r!   c                |    d                     | j                  }g | _        | j                            |           d S )NrN   )rC   r,   r2   rB   rg   s     r   _flushzStdoutProxy._flush  s9    wwt|$$d#####r!   intc                    | j         5  |                     |           d d d            n# 1 swxY w Y   t          |          S r9   )r+   ru   len)r6   rn   s     r   re   zStdoutProxy.write  s    Z 	 	KK	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 4yys   *..c                n    | j         5  |                                  ddd           dS # 1 swxY w Y   dS )z(
        Flush buffered output.
        N)r+   rw   r:   s    r   rf   zStdoutProxy.flush  s}     Z 	 	KKMMM	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	s   *..TextIO | Nonec                2    | j         j        pt          j        S r9   )r/   r   r   
__stdout__r:   s    r   r   zStdoutProxy.original_stdout  s    |"4cn4r!   c                4    | j                                         S r9   )r/   filenor:   s    r   r   zStdoutProxy.fileno  s    |""$$$r!   c                J    | j         j        }|dS |                                S r(   )r/   r   isatty)r6   r   s     r   r   zStdoutProxy.isatty  s$    $>5}}r!   c                4    | j                                         S r9   )r/   encodingr:   s    r   r   zStdoutProxy.encoding#  s    |$$&&&r!   c                    dS )Nstrictr    r:   s    r   errorszStdoutProxy.errors'  s    xr!   N)r#   F)r$   r%   r   r   r   r&   )r   r   )r<   r=   r   r&   rl   )r   rD   )r   r\   )r_   r\   rZ   r`   r   r&   )rn   r`   r   r&   )rn   r`   r   rx   )r   r|   )r   rx   )r   r   )r   r`   )r   r   r   r   r7   r;   r@   r?   r3   rJ   rT   rU   ru   rw   re   rf   propertyr   r   r   r   r   r    r!   r   r   r   M   s        & '*    6      
 
 
 
   #6 #6 #6 #6J
 
 
 
"? "? "? "?H& & & &.$ $ $ $
       5 5 5 X5
% % % %    ' ' ' X'    X  r!   )F)r   r   r   r   )r   
__future__r   asyncior0   r   r)   rV   
contextlibr   typingr   r   r   applicationr	   r
   r.   r   __all__r   r   r   r    r!   r   <module>r      sF   ( # " " " " "   



      % % % % % % * * * * * * * * * * 9 9 9 9 9 9 9 9        ) ) ) ) )@4 4 4 4 4 4 4 4\ \ \ \ \ \ \ \ \ \r!   