L iEDdZddlZddlZddlZddlZddlZddlZddlZddlZddl Z ddl Z ddl Z ddl Z ddl mZmZddlmZddlmZddlmZddlmZmZmZdd lmZej8d d ZGd d eZdZ GddeZ!Gdde"Z#Gdde!e#Z$y)z.Storage providers backends for Memory caching.N)ABCMetaabstractmethod) PicklingError) numpy_pickle)concurrency_safe_rename)memstr_to_bytesmkdirp rm_subdirs) format_time CacheItemInfozpath size last_accessceZdZdZy) CacheWarningz:Warning to capture dump failures except for PicklingError.N)__name__ __module__ __qualname____doc__\/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/joblib/_store_backends.pyrrsDrrcttj}|dtjj dt jd|}||||S)z>Writes an object into a unique file in a concurrency-safe way..-)id threadingcurrent_threaduuiduuid4hexosgetpid)object_to_writefilename write_func thread_idtemporary_filenames rconcurrency_safe_writer' sX9++-.I$:Qtzz|'7'7&8"))+a {S 23 rceZdZdZdZedZedZedZedZ edZ edZ ed e fd Z y) StoreBackendBasez^Helper Abstract Base Class which defines all methods that a StorageBackend must implement.Ncy)aOpens an item on the store and return a file-like object. This method is private and only used by the StoreBackendMixin object. Parameters ---------- f: a file-like object The file-like object where an item is stored and retrieved mode: string, optional the mode in which the file-like object is opened allowed valued are 'rb', 'wb' Returns ------- a file-like object Nr)selffmodes r _open_itemzStoreBackendBase._open_item5rcy)aChecks if an item location exists in the store. This method is private and only used by the StoreBackendMixin object. Parameters ---------- location: string The location of an item. On a filesystem, this corresponds to the absolute path, including the filename, of a file. Returns ------- True if the item exists, False otherwise Nrr+locations r _item_existszStoreBackendBase._item_existsHr/rcy)a-Moves an item from src to dst in the store. This method is private and only used by the StoreBackendMixin object. Parameters ---------- src: string The source location of an item dst: string The destination location of an item Nr)r+srcdsts r _move_itemzStoreBackendBase._move_itemYr/rcy)zCreates a location on the store. Parameters ---------- location: string The location in the store. On a filesystem, this corresponds to a directory. Nrr1s rcreate_locationz StoreBackendBase.create_locationgr/rcy)zClears a location on the store. Parameters ---------- location: string The location in the store. On a filesystem, this corresponds to a directory or a filename absolute path Nrr1s rclear_locationzStoreBackendBase.clear_locationrr/rcy)zReturns the whole list of items available in the store. Returns ------- The list of items identified by their ids (e.g filename in a filesystem). Nrr+s r get_itemszStoreBackendBase.get_items}r/rrcy)aConfigures the store. Parameters ---------- location: string The base location used by the store. On a filesystem, this corresponds to a directory. verbose: int The level of verbosity of the store backend_options: dict Contains a dictionary of named parameters used to configure the store backend. Nr)r+r2verbosebackend_optionss r configurezStoreBackendBase.configurer/r)rrrrr2rr.r3r7r9r;r>dictrBrrrr)r)/s(H  $            *+TV    rr)) metaclassceZdZdZddZddZdZdZdZdZ d Z d Z d Z dd Z d ZdZdZddZddZdZdZy)StoreBackendMixinaClass providing all logic for managing the store in a generic way. The StoreBackend subclass has to implement 3 methods: create_location, clear_location and configure. The StoreBackend also has to provide a private _open_item, _item_exists and _move_item methods. The _open_item method has to have the same signature as the builtin open and return a file-like object. Nctjj|jg|}|dkDr|/dj t t j |z nd}tjj|d}|Ad|vr=djd|djD}|d j |z }d j ||} |d krtd j | ntd j | |t|dsdn |j} tjj|d} |j| std| z| 2|j| d5} tj | } ddd| Stj | | } | S#1swY SxYw)z:Load an item from the store given its id as a list of str.rNz{: <16}r input_argsz, c3:K|]}dj|yw)z{}={}N)format.0items r z.StoreBackendMixin.load_item..s #.2NGNND)#sz({})z[Memory]{}: Loading {} z{0}...z {0} from {1} mmap_mode output.pklzANon-existing item (may have been cleared). File %s does not existrb)rQ)r pathjoinr2rKr timebasenameitemsprinthasattrrQr3KeyErrorr.rload)r+call_idr@ timestampmetadata full_path ts_string signaturekwargsmsgrQr#r,rNs r load_itemzStoreBackendMixin.load_itemsGGLL99 Q;(  TYY[9-D!EF  ((4I# (@#6>|6L6R6R6T#V]]622 *11)YGC|hooc*+n++C;< 'k :D 77<< <8  *46>?   40 ,A#((+ ,  $$XCD  , s F;;Gc tjjjg|}j |sj |tjj|d}|dkDrt d|zfd}j|||y#t$r(}tjd|dtYd}~yd}~wwxYw)z;Dump an item in the store at the id given as a list of str.rRrPzPersisting in %scj|d5} tj||jdddy#t$r(}t j d|dtYd}~5d}~wwxYw#1swYyxYw)Nwb)compresszjUnable to cache to disk: failed to pickle output. In version 1.5 this will raise an exception. Exception: r)r.rdumprirwarningswarn FutureWarning)to_write dest_filenamer,er+s rr$z/StoreBackendMixin.dump_item..write_funcs__]D9 Q $))(A N  ) 556Cq:*   s.A5"A A2 A-(A5-A22A55A>z`Unable to cache to disk. Possibly a race condition in the creation of the directory. Exception: rN) r rTrUr2r3r9rY_concurrency_safe_write Exceptionrkrlr)r+r]rNr@ item_pathr#r$rps` r dump_itemzStoreBackendMixin.dump_items  T]]=W=I$$Y/$$Y/ww||I|   sBB C $CC ctjj|jg|}|j |r|j |yy)z1Clear the item at the id, given as a list of str.Nr rTrUr2r3r;)r+r]rss r clear_itemzStoreBackendMixin.clear_item>GGLL99   Y '    * (rctjj|jg|}tjj|d}|j |S)z.write_func sL__]D9BQGGDJJx077@ABBBs 4AAN)r rTrUr2r9rq)r+r]r_rsr#r$s` rstore_metadataz StoreBackendMixin.store_metadatasi  T]]=W=I   +ww||I?H B  ( (8Z H  s A3A77A;cxtjj|jg|}|j |S)z,Check cached function is available in store.)r rTrUr2 object_existsr+r] func_paths r contains_pathzStoreBackendMixin.contains_paths.GGLL99 !!),,rctjj|jg|}|j |r|j |yy)z0Clear all items with a common path in the store.Nrvrs r clear_pathzStoreBackendMixin.clear_pathrxrcptjj|jg|}|j |s|j ||\tjj|d}|j |d5}|j|jddddyy#1swYyxYw)&Store the code of the cached function.N func_code.pyrhr) r rTrUr2r3r9r.rr)r+r] func_coderr#r,s rstore_cached_func_codez(StoreBackendMixin.store_cached_func_codesGGLL99   +   +  ww||I~>H40 3A ((12 3 3 ! 3 3s !B,,B5ctjj|jg|d} |j |d5}|j j dcdddS#1swYyxYw#xYw)rrrSrN)r rTrUr2r.rr)r+r]r#r,s rget_cached_func_codez&StoreBackendMixin.get_cached_func_code(sg77<< HHH 40 0Avvxw/ 0 0 0  s)A5A) A5)A2.A52A55A8cZdtjj|jg|iS)z?Return information related to the cached function if it exists.r2r|r}s rget_cached_func_infoz&StoreBackendMixin.get_cached_func_info1rrc:|j|jy)zClear the whole store content.N)r;r2r=s rclearzStoreBackendMixin.clear5s DMM*rc|j|||}|D]G}|jdkDrtdj| |j |j Iy#t $rYVwxYw)zX Remove the store's oldest files to enforce item, byte, and age limits. rPzDeleting item {0}N)_get_items_to_deleter@rYrKr;rTOSError)r+ bytes_limit items_limit age_limititems_to_deleterNs renforce_store_limitsz&StoreBackendMixin.enforce_store_limits9st33KiX# D||b )0067 ##DII.      sA!! A-,A-c~t|tr t|}|j}|sgSt d|D}|||z }nd}|t ||z }nd}|Rt d|D}|jdkr tdtjj|z } nd} |dkr|dkr | | kDrgS|jtjdg} d} d} |D]E} | |k\r| |k\r| | | jkr| S| j| | | j z } | dz } G| S) zW Get items to delete to keep the store under size, file, & age limits. c34K|]}|jywN)sizerLs rrOz9StoreBackendMixin._get_items_to_delete..Vs/499/Nrc34K|]}|jywr) last_accessrLs rrOz9StoreBackendMixin._get_items_to_delete..cs@$T--@rz(age_limit has to be a positive timedeltar)keyr) isinstancestrr r>sumlenmin total_seconds ValueErrordatetimenowsortoperator attrgetterrappendr)r+rrrrXrto_delete_sizeto_delete_items older_itemdeadliner size_so_far items_so_farrNs rrz&StoreBackendMixin._get_items_to_deleteKsv k3 ')+6K I///  "!K/NN  "!%j;6OO  @%@@J&&(1, !KLL((,,.:HH a 1$!Z(%:I  x**=9 :   D. O3%D4D4D)D   " "4 ( 499 $K A L rcBt|||}|j||y)z7Writes an object into a file in a concurrency-safe way.N)r'r7)r+rnr#r$r&s rrqz)StoreBackendMixin._concurrency_safe_writes 3Hh S *H5rcddj|jj|jS)z/Printable representation of the store location.z#{class_name}(location="{location}")) class_namer2)rK __class__rr2r=s r__repr__zStoreBackendMixin.__repr__s-4;;~~..<  r)rNN)rr)NN)rrrrrertrwrzr~rrrrrrrrrrrqrrrrrFrFs`%N>+ +C - + 3C+$:x6  rrFceZdZdZeeZeejjZ ee Z dZ dZdZddZy)FileSystemStoreBackendz7A StoreBackend used with local or network file systems.ch||jk(r t|ytj|dy)zDelete location on store.T) ignore_errorsN)r2r shutilrmtreer1s rr;z%FileSystemStoreBackend.clear_locations$ t}} $ x MM($ 7rct|y)zCreate object location on storeN)r r1s rr9z&FileSystemStoreBackend.create_locations xrcg}tj|jD]\}}}tjdtj j |}|s=tj j|d} tj j|}tjj|} |Dcgc]"}tj j||$} }td| D} |jt|| ||S#t$r5 tj j|}n#t$rYY3wxYwYwxYwcc}w#t$rYNwxYw)z7Returns the whole list of items available in the store.z [a-f0-9]{32}rRc3ZK|]#}tjj|%ywr)r rTgetsize)rMfns rrOz3FileSystemStoreBackend.get_items..s!O""''//""5!Os)+)r walkr2rematchrTrWrUgetatimerr fromtimestamprrr ) r+rXdirpath_ filenamesis_cache_hash_diroutput_filenamerrfull_filenamesdirsizes rr>z FileSystemStoreBackend.get_itemssC%'WWT]]%; K !GQ "9I9I'9R S  "$'',,w "E!"$''"2"2?"CK'//==kJ JS%TBbggll7B&?%TN%T!!O!OOG ]7G[IJ1 K4 '!!&(gg&6&6w&? "! !$!&U sZD?E$'E*E$ E(EE EEEEEE$$ E10E1Nc|i}||_tjj|jst |jtjj |rAtjj |dk(rtjj |n|}ttjj|dd5}|jd|jdddd|jdd|_ |jd }|jr|tjd d ||_||_y#1swYexYw) zsConfigure the store backend. For this backend, valid store options are 'compress' and 'mmap_mode' Njoblibz .gitignorewz## Created by joblib automatically. z* riFrQzSCompressed items cannot be memmapped in a filesystem store. Option will be ignored.) stacklevel)r2r rTexistsr dirnamerWopenrUrgetrirkrlrQr@)r+r2r@rAcache_directoryfilerQs rrBz FileSystemStoreBackend.configures  " O! ww~~dmm, 4== !wwx(RWW-=-=h-G8-S GGOOH %  "'',, =s C t JJ= > JJu   (++J> $'' 4 ==Y2 MM<  # %  s #EE&)rN)rrrr staticmethodrr.r rTrr3rr7r;r9r>rBrrrrrsAAd#J/L56J8@*rr)%r collectionsrrrr os.pathrrrrVrrkabcrrpicklerrHr backportsrdiskr r r loggerr namedtupler Warningrr'r)objectrFrrrrrs4    ' .55& &&8OP  7 f f Rx x v\-/@\r