
    Mh$                       d Z ddlmZ ddlZddlZddlZddlmZmZ ddl	m
Z
 ddlmZmZmZmZ g dZ G d d	e
          Z G d de          Z G d de          Z G d de          Zeeeddf         Z G d de          ZdS )aW  
Implementations for the history of a `Buffer`.

NOTE: There is no `DynamicHistory`:
      This doesn't work well, because the `Buffer` needs to be able to attach
      an event handler to the event when a history entry is loaded. This
      loading can be done asynchronously and making the history swappable would
      probably break this.
    )annotationsN)ABCMetaabstractmethod)get_running_loop)AsyncGeneratorIterableSequenceUnion)HistoryThreadedHistoryDummyHistoryFileHistoryInMemoryHistoryc                  b    e Zd ZdZddZddZddZddZedd            Z	edd            Z
dS )r   zg
    Base ``History`` class.

    This also includes abstract methods for loading/storing history.
    returnNonec                "    d| _         g | _        d S )NF)_loaded_loaded_stringsselfs    V/var/www/html/test/jupyter/venv/lib/python3.11/site-packages/prompt_toolkit/history.py__init__zHistory.__init__$   s     +-    AsyncGenerator[str, None]c                  K   | j         s-t          |                                           | _        d| _         | j        D ]}|W V  dS )a  
        Load the history and yield all the entries in reverse order (latest,
        most recent history entry first).

        This method can be called multiple times from the `Buffer` to
        repopulate the history when prompting for a new input. So we are
        responsible here for both caching, and making sure that strings that
        were were appended to the history will be incorporated next time this
        method is called.
        TN)r   listload_history_stringsr   )r   items     r   loadzHistory.load0   s^       | 	 #'(A(A(C(C#D#DD DL( 	 	DJJJJJ	 	r   	list[str]c                "    | j         ddd         S )zq
        Get the strings from the history that are loaded so far.
        (In order. Oldest item first.)
        N)r   r   s    r   get_stringszHistory.get_stringsB   s    
 #DDbD))r   stringstrc                f    | j                             d|           |                     |           dS )zAdd string to the history.r   N)r   insertstore_stringr   r%   s     r   append_stringzHistory.append_stringI   s5    ##Av...&!!!!!r   Iterable[str]c              #  
   K   dS )z
        This should be a generator that yields `str` instances.

        It should yield the most recent items first, because they are the most
        important. (The history can already be used, even when it's only
        partially loaded.)
        FN r   s    r   r   zHistory.load_history_stringsR   s      	 	r   c                    dS )z9
        Store the string in persistent storage.
        Nr.   r*   s     r   r)   zHistory.store_string^   s      r   Nr   r   r   r   )r   r!   r%   r&   r   r   r   r,   )__name__
__module____qualname____doc__r   r    r$   r+   r   r   r)   r.   r   r   r   r      s         - - - -   $* * * *" " " " 	 	 	 ^	    ^  r   r   )	metaclassc                  T     e Zd ZdZd fdZddZdd	ZddZddZddZ	ddZ
 xZS )r   a$  
    Wrapper around `History` implementations that run the `load()` generator in
    a thread.

    Use this to increase the start-up time of prompt_toolkit applications.
    History entries are available as soon as they are loaded. We don't have to
    wait for everything to be loaded.
    historyr   r   r   c                    t                                                       || _        d | _        t	          j                    | _        g | _        d S N)superr   r:   _load_thread	threadingLock_lock_string_load_events)r   r:   	__class__s     r   r   zThreadedHistory.__init__o   sI    59 ^%%
 ;=   r   r   c               |   K    j         s9t          j         j        d           _          j                                          t                      }t          j                                                      j        	                               d	 	 |
                    dfd           d{V }|s#d	 fd}|
                    d|           d{V \  }}t          |          z  |D ]}|W V  |rni	  j                                       dS #  j                                       w xY w)
zn
        Like `History.load(), but call `self.load_history_strings()` in a
        background thread.
        T)targetdaemonr   Nc                 0                          d          S )Ng      ?)timeout)wait)events   r   <lambda>z&ThreadedHistory.load.<locals>.<lambda>   s    %**S*"9"9 r   r   tuple[list[str], bool]c                     j         5  j        d          } j        }                                 d d d            n# 1 swxY w Y   | |fS r<   )rA   r   r   clear)	new_itemsdonerJ   items_yieldedr   s     r   in_executorz)ThreadedHistory.load.<locals>.in_executor   s     & &$($8$H	#|& & & & & & & & & & & & & & & %d?*s   +A  AA)r   rL   )r>   r?   Thread_in_load_threadstartr   EventsetrB   appendrun_in_executorlenremove)	r   loopgot_timeoutrR   rO   rP   r   rJ   rQ   s	   `      @@r   r    zThreadedHistory.load~   s        	& ) 0+! ! !D ##%%%  !! !!		 ''... 	3 %)$8$89999% %       # + + + + + + + + )-(<(<T;(O(O"O"O"O"O"O"O	4Y/%  DJJJJJ ;: $++E22222D$++E2222s   A*D D;c                   	 g | _         | j                                        D ]Y}| j        5  | j                             |           d d d            n# 1 swxY w Y   | j        D ]}|                                 Z	 | j        5  d| _        d d d            n# 1 swxY w Y   | j        D ]}|                                 d S # | j        5  d| _        d d d            n# 1 swxY w Y   | j        D ]}|                                 w xY w)NT)r   r:   r   rA   rX   rB   rW   r   )r   r   rJ   s      r   rT   zThreadedHistory._in_load_thread   s   	 $&D 99;;    Z 6 6(//5556 6 6 6 6 6 6 6 6 6 6 6 6 6 6 "5    EIIKKKK 	   $ $#$ $ $ $ $ $ $ $ $ $ $ $ $ $ $1  		   $ $#$ $ $ $ $ $ $ $ $ $ $ $ $ $ $1  		sd   )C AC A	C A	"C BBBD	CD	C##D	&C#'"D	r%   r&   c                    | j         5  | j                            d|           d d d            n# 1 swxY w Y   |                     |           d S )Nr   )rA   r   r(   r)   r*   s     r   r+   zThreadedHistory.append_string   s    Z 	3 	3 ''6222	3 	3 	3 	3 	3 	3 	3 	3 	3 	3 	3 	3 	3 	3 	3&!!!!!s   044r,   c                4    | j                                         S r<   )r:   r   r   s    r   r   z$ThreadedHistory.load_history_strings   s    |00222r   c                :    | j                             |           d S r<   )r:   r)   r*   s     r   r)   zThreadedHistory.store_string   s    !!&)))))r   c                    d| j         dS )NzThreadedHistory())r:   r   s    r   __repr__zThreadedHistory.__repr__   s    3$,3333r   )r:   r   r   r   r1   r0   r2   r3   )r   r&   )r4   r5   r6   r7   r   r    rT   r+   r   r)   rd   __classcell__rC   s   @r   r   r   e   s         = = = = = =73 73 73 73r   &" " " "3 3 3 3* * * *4 4 4 4 4 4 4 4r   r   c                  6     e Zd ZdZdd fdZdd	ZddZ xZS )r   z
    :class:`.History` class that keeps a list of all strings in memory.

    In order to prepopulate the history, it's possible to call either
    `append_string` for all items or pass a list of strings to `__init__` here.
    Nhistory_stringsSequence[str] | Noner   r   c                    t                                                       |	g | _        d S t          |          | _        d S r<   )r=   r   _storager   )r   rh   rC   s     r   r   zInMemoryHistory.__init__   s=    "DMMM 11DMMMr   r,   c              #  6   K   | j         d d d         E d {V  d S )Nr#   )rk   r   s    r   r   z$InMemoryHistory.load_history_strings   s1      =2&&&&&&&&&&r   r%   r&   c                :    | j                             |           d S r<   )rk   rX   r*   s     r   r)   zInMemoryHistory.store_string   s    V$$$$$r   r<   )rh   ri   r   r   r3   r2   r4   r5   r6   r7   r   r   r)   re   rf   s   @r   r   r      st         2 2 2 2 2 2 2' ' ' '% % % % % % % %r   r   c                  *    e Zd ZdZddZddZdd	Zd
S )r   zB
    :class:`.History` object that doesn't remember anything.
    r   r,   c                    g S r<   r.   r   s    r   r   z!DummyHistory.load_history_strings   s    	r   r%   r&   r   c                    d S r<   r.   r*   s     r   r)   zDummyHistory.store_string   s    r   c                    d S r<   r.   r*   s     r   r+   zDummyHistory.append_string   s    r   Nr3   r2   )r4   r5   r6   r7   r   r)   r+   r.   r   r   r   r      sZ                    r   r   zos.PathLike[str]zos.PathLike[bytes]c                  4     e Zd ZdZd fdZddZddZ xZS )r   zD
    :class:`.History` class that stores all strings in a file.
    filename_StrOrBytesPathr   r   c                V    || _         t                                                       d S r<   )rt   r=   r   )r   rt   rC   s     r   r   zFileHistory.__init__
  s&     r   r,   c                   g g d
fd}t           j                            | j                  rt	          | j        d          5 }|D ]X}|                    dd          }|                    d          r                    |d	d                     L |             g Y |             d d d            n# 1 swxY w Y   t                    S )Nr   r   c                 t    r4d                               d d         }                     |            d S d S )N r#   )joinrX   )r%   linesstringss    r   addz-FileHistory.load_history_strings.<locals>.add  sE     ',v&&&&&	' 'r   rbutf-8replace)errors+   r0   )	ospathexistsrt   opendecode
startswithrX   reversed)r   r}   f
line_bytesliner{   r|   s        @@r   r   z FileHistory.load_history_strings  s4   	' 	' 	' 	' 	' 	' 	' 7>>$-(( 	dmT** 
a"# # #J%,,WY,GGDs++ #T!""X.... "
 
 
 
 
 
 
 
 
 
 
 
 
 
 
    s   A&B99B= B=r%   r&   c                   t          | j        d          5 d
fd} |dt          j                                         d           |                    d          D ]} |d	| d           	 d d d            d S # 1 swxY w Y   d S )Nabtr&   r   r   c                X                         |                     d                     d S )Nr   )writeencode)r   r   s    r   r   z'FileHistory.store_string.<locals>.write-  s'    ))*****r   z
# 
r   )r   r&   r   r   )r   rt   datetimenowsplit)r   r%   r   r   r   s       @r   r)   zFileHistory.store_string)  s    $-&& 	$!+ + + + + + E4*..00444555T** $ $l$lll####$	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$s   AA>>BB)rt   ru   r   r   r3   r2   rn   rf   s   @r   r   r     so              ! ! ! !6	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$r   r   )r7   
__future__r   r   r   r?   abcr   r   asyncior   typingr   r   r	   r
   __all__r   r   r   r   r&   bytesru   r   r.   r   r   <module>r      s    # " " " " "  				     ' ' ' ' ' ' ' ' $ $ $ $ $ $ < < < < < < < < < < < <  E E E E E E E E EPs4 s4 s4 s4 s4g s4 s4 s4l% % % % %g % % %.    7     U$68LLM-$ -$ -$ -$ -$' -$ -$ -$ -$ -$r   