)L i$dZddlmZddlZddlZddlZddlmZmZddl m Z ddl m Z m Z mZmZgdZGdd e ZGd d eZGd deZGddeZeeeddfZGddeZy)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)ABCMetaabstractmethod)get_running_loop)AsyncGeneratorIterableSequenceUnion)HistoryThreadedHistory DummyHistory FileHistoryInMemoryHistorycTeZdZdZd dZd dZd dZd dZed dZ ed dZ y)r zg Base ``History`` class. This also includes abstract methods for loading/storing history. c d|_g|_y)NF)_loaded_loaded_stringsselfs \/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/prompt_toolkit/history.py__init__zHistory.__init__$s +-cK|js%t|j|_d|_|jD]}| yw)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)rlistload_history_stringsr)ritems rloadz History.load0sF||#'(A(A(C#DD DL(( DJ sA A c&|jdddS)zq Get the strings from the history that are loaded so far. (In order. Oldest item first.) N)rrs r get_stringszHistory.get_stringsBs ##DbD))rc^|jjd||j|y)zAdd string to the history.rN)rinsert store_stringrstrings r append_stringzHistory.append_stringIs& ##Av. &!rc# Kyw)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.) Nrs rrzHistory.load_history_stringsRs scy)z9 Store the string in persistent storage. Nr(r$s rr#zHistory.store_string^srNreturnNoner+zAsyncGenerator[str, None])r+z list[str]r%strr+r,r+z Iterable[str]) __name__ __module__ __qualname____doc__rrr r&rrr#r(rrr r sC -$*"    rr ) metaclasscTeZdZdZd fd Zd dZd dZd dZd dZd dZ ddZ xZ 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. c~t|||_d|_t j |_g|_yN)superrhistory _load_thread threadingLock_lock_string_load_events)rr: __class__s rrzThreadedHistory.__init__os7  59^^% ;= rcKjs@tjjd_jj t }tj jjjd |jdfdd{}|s!dfd }|jd|d{\}}t|z |D]}| |rnc jjy7g7D#jjwxYww)zn Like `History.load(), but call `self.load_history_strings()` in a background thread. T)targetdaemonrNc(jdS)Ng?)timeout)wait)eventsrz&ThreadedHistory.load..s%**S*"9rcj5jd}j}jddd||fS#1swYfSxYwr8)r>rrclear) new_itemsdonerG items_yieldedrs r in_executorz)ThreadedHistory.load..in_executorsV&$($8$8$H #|| &%d?* &%d?*s ,AA)r+ztuple[list[str], bool]) r;r<Thread_in_load_threadstartrEventsetr?appendrun_in_executorlenremove) rloop got_timeoutrNrKrLrrGrMs ` @@rrzThreadedHistory.load~s7   ) 0 0++!D     # # % !!    ''.  3%)$8$89% #+)-(<( g|_|jjD]S}|j5|jj |ddd|j D]}|j U |j5d|_ddd|j D]}|j y#1swYkxYw#1swY7xYw#|j5d|_dddn #1swYnxYw|j D]}|j wxYw)NT)rr:rr>rTr?rSr)rrrGs rrPzThreadedHistory._in_load_threads $&D  99; ZZ6((//56"55 EIIK   $#  $11   66  $ $ $#  $ $ $11   sF0CB8+CC8C =CC  DC.% D.C7 3)Dc|j5|jjd|ddd|j|y#1swYxYw)Nr)r>rr"r#r$s rr&zThreadedHistory.append_stringsB ZZ 3  ' '6 2 3 &! 3 3s AA c6|jjSr8)r:rrs rrz$ThreadedHistory.load_history_stringss||0022rc:|jj|yr8)r:r#r$s rr#zThreadedHistory.store_strings !!&)rc"d|jdS)NzThreadedHistory())r:rs r__repr__zThreadedHistory.__repr__s!$,,!133r)r:r r+r,r-r*r.r0)r+r/) r1r2r3r4rrrPr&rr#r` __classcell__r@s@rr r es, =73r&"3*4rr c6eZdZdZddfd ZddZddZxZS) rz :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. cVt||g|_yt||_yr8)r9r_storager)rhistory_stringsr@s rrzInMemoryHistory.__init__s'   "DM 1DMrc#DK|jdddEd{y7w)Nr)rers rrz$InMemoryHistory.load_history_stringss==2&&&s   c:|jj|yr8)rerTr$s rr#zInMemoryHistory.store_strings V$rr8)rfzSequence[str] | Noner+r,r0r.r1r2r3r4rrr#rarbs@rrrs2'%rrc(eZdZdZddZddZddZy)r zB :class:`.History` object that doesn't remember anything. cgSr8r(rs rrz!DummyHistory.load_history_stringss rcyr8r(r$s rr#zDummyHistory.store_strings rcyr8r(r$s rr&zDummyHistory.append_strings rNr0r.)r1r2r3r4rr#r&r(rrr r s  rr zos.PathLike[str]zos.PathLike[bytes]c4eZdZdZdfd ZddZddZxZS)rzD :class:`.History` class that stores all strings in a file. c0||_t| yr8)filenamer9r)rrpr@s rrzFileHistory.__init__ s   rcggdfd }tjj|jrzt |jd5}|D]D}|j dd}|j drj|dd<|gF|dddtStS#1swYtSxYw) NcVr&djdd}j|yy)Nr)joinrT)r%linesstringss raddz-FileHistory.load_history_strings..adds+,v& rrbutf-8replace)errors+r*) ospathexistsrpopendecode startswithrTreversed)rrwf line_byteslinerurvs @@rrz FileHistory.load_history_stringss ' 77>>$-- (dmmT* a"##J%,,WY,GDs+ T!"X. "#   x     s AB<<Cct|jd5dfd }|dtjjd|j dD]}|d|d dddy#1swYyxYw)NabcFj|jdy)Nry)writeencode)trs rrz'FileHistory.store_string..write-s)*rz #  r|)rr/r+r,)rrpdatetimenowsplit)rr%rrrs @rr#zFileHistory.store_string)sx $-- & $! + D**..014 5 T* $$rl# $  $ $ $s AA33A<)rp_StrOrBytesPathr+r,r0r.rirbs@rrrs!6 $rr)r4 __future__rrr~r<abcrrasynciortypingrrr r __all__r r rr r/bytesrrr(rrrs# '$<< E E Ps4gs4l%g%.  7  U$68LLM-$'-$r