
    .PhC                     R   d Z ddlZddlZddlZddlZddlZddl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mZ ddlmZ ddlmZ ddlmZ ddlmZmZmZ dd	lmZ  ej        d
d          Z G d de          Zd Z G d de          Z  G d de!          Z" G d de e"          Z#dS )z.Storage providers backends for Memory caching.    N)ABCMetaabstractmethod)PicklingError   )numpy_pickle)concurrency_safe_rename)memstr_to_bytesmkdirp
rm_subdirs)format_timeCacheItemInfozpath size last_accessc                       e Zd ZdZdS )CacheWarningz:Warning to capture dump failures except for PicklingError.N)__name__
__module____qualname____doc__     V/var/www/html/test/jupyter/venv/lib/python3.11/site-packages/joblib/_store_backends.pyr   r      s        DDDr   r   c                     t          t          j                              }d                    ||t	          j                              } || |           |S )z>Writes an object into a unique file in a concurrency-safe way.z{}.thread-{}-pid-{})id	threadingcurrent_threadformatosgetpid)object_to_writefilename
write_func	thread_idtemporary_filenames        r   concurrency_safe_writer#      sO    9+--..I.55h	29;;WWJ 2333r   c                       e Zd ZdZdZed             Zed             Zed             Zed             Z	ed             Z
ed             Zed	 e            fd
            ZdS )StoreBackendBasez^Helper Abstract Base Class which defines all methods that
    a StorageBackend must implement.Nc                     dS )a  Opens 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   )selffmodes      r   
_open_itemzStoreBackendBase._open_item.         r   c                     dS )a  Checks 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
        Nr   r'   locations     r   _item_existszStoreBackendBase._item_existsA   r+   r   c                     dS )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'   srcdsts      r   
_move_itemzStoreBackendBase._move_itemR   r+   r   c                     dS )zCreates a location on the store.

        Parameters
        ----------
        location: string
            The location in the store. On a filesystem, this corresponds to a
            directory.
        Nr   r-   s     r   create_locationz StoreBackendBase.create_location`   r+   r   c                     dS )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
        Nr   r-   s     r   clear_locationzStoreBackendBase.clear_locationk   r+   r   c                     dS )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).
        Nr   r'   s    r   	get_itemszStoreBackendBase.get_itemsv   r+   r   r   c                     dS )a  Configures 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'   r.   verbosebackend_optionss       r   	configurezStoreBackendBase.configure   r+   r   )r   r   r   r   r.   r   r*   r/   r3   r5   r7   r:   dictr>   r   r   r   r%   r%   (   s        ( ( H  ^$   ^    ^   ^   ^   ^ *+TTVV    ^  r   r%   )	metaclassc                       e Zd ZdZddZddZd Zd Zd Zd	 Z	d
 Z
d Zd ZddZd Zd Zd ZddZddZd Zd ZdS )StoreBackendMixina  Class 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.
    r   Nc                    t          j        j        | j        g|R  }|dk    r|6d                    t          t          j                    |z
                      nd}t           j                            |d                   }|Sd|v rOd                    d |d                                         D                       }|d	                    |          z  }d
                    ||          }	|dk     r#t          d                    |	                     n#t          d                    |	|                     t          | d          sdn| j        }
t           j                            |d          }|                     |          st          d|z            |
C|                     |d          5 }t          j        |          }ddd           n# 1 swxY w Y   nt          j        ||
          }|S )z:Load an item from the store given its id as a list of str.r   Nz{: <16} r   
input_argsz, c              3   *   K   | ]} d j         | V  dS )z{}={}N)r   .0items     r   	<genexpr>z.StoreBackendMixin.load_item.<locals>.<genexpr>   s<       # #.2NGND)# # # # # #r   z({})z[Memory]{}: Loading {}
   z{0}...z{0} from {1}	mmap_mode
output.pklzANon-existing item (may have been cleared).
File %s does not existrb)rL   )r   pathjoinr.   r   r   timebasenameitemsprinthasattrrL   r/   KeyErrorr*   r   load)r'   call_idr<   	timestampmetadata	full_path	ts_string	signaturekwargsmsgrL   r   r(   rI   s                 r   	load_itemzStoreBackendMixin.load_item   s:   GL9999	Q;; (   TY[[9-D!E!EFFF 
 ((44I#(@(@ # #6>|6L6R6R6T6T# # #   V]]6222	*11)YGGC||hooc**++++n++C;;<<< 'k : :NDD	7<<	<88  ** 	46>?   400 ,A#(++, , , , , , , , , , , , , , ,  $XCCCDs   2GGGc                     	 t          j        j         j        g|R  }                     |          s                     |           t           j                            |d          }|dk    rt          d|z              fd}                     |||           dS # t          $ r)}t          j
        d| dt                     Y d}~dS d}~ww xY w)z;Dump an item in the store at the id given as a list of str.rM   rK   zPersisting in %sc                                         |d          5 }	 t          j        | |j                   n5# t          $ r(}t          j        d| dt                     Y d }~nd }~ww xY wd d d            d S # 1 swxY w Y   d S )Nwb)compresszjUnable to cache to disk: failed to pickle output. In version 1.5 this will raise an exception. Exception: .)r*   r   dumprd   r   warningswarnFutureWarning)to_writedest_filenamer(   er'   s       r   r    z/StoreBackendMixin.dump_item.<locals>.write_func   s   __]D99 
Q	$)(ANNNNN(    :56: : : *	       
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
s7   A97A9
A)A$A9$A))A99A= A=z`Unable to cache to disk. Possibly a race condition in the creation of the directory. Exception: re   N)r   rO   rP   r.   r/   r5   rT   _concurrency_safe_write	Exceptionrg   rh   r   )r'   rX   rI   r<   	item_pathr   r    rl   s   `       r   	dump_itemzStoreBackendMixin.dump_item   s#   	T]=W===I$$Y// 0$$Y///w||I|<<H||(94555     ((xDDDDD 	 	 	M>9:> > >        	s   BB 
C)CCc                     t          j        j        | j        g|R  }|                     |          r|                     |           dS dS )z1Clear the item at the id, given as a list of str.Nr   rO   rP   r.   r/   r7   )r'   rX   ro   s      r   
clear_itemzStoreBackendMixin.clear_item   W    GL9999	Y'' 	+	*****	+ 	+r   c                     t          j        j        | j        g|R  }t           j                            |d          }|                     |          S )z<Check if there is an item at the id, given as a list of str.rM   )r   rO   rP   r.   r/   )r'   rX   ro   r   s       r   contains_itemzStoreBackendMixin.contains_item   sF    GL9999	7<<	<88  ***r   c                 >    dt          j        j        | j        g|R  iS )zReturn information about item.r.   r   rO   rP   r.   r'   rX   s     r   get_item_infozStoreBackendMixin.get_item_info   #    BGLAAAABBr   c                 `   	 t          j        j        | j        g|R  }t           j                            |d          }|                     |d          5 }t          j        |                                                    d                    cddd           S # 1 swxY w Y   dS #  i cY S xY w)z"Return actual metadata of an item.metadata.jsonrN   utf-8N)	r   rO   rP   r.   r*   jsonloadsreaddecode)r'   rX   ro   r   r(   s        r   get_metadatazStoreBackendMixin.get_metadata   s    	T]=W===Iw||I??H400 <Az!&&((//'":":;;< < < < < < < < < < < < < < < < < <	IIIs0   AB' 9BB' BB' !B"B' 'B-c                      	 t          j        j         j        g|R  }                     |           t           j                            |d          } fd}                     |||           dS #  Y dS xY w)z Store metadata of a computation.r}   c                                          |d          5 }|                    t          j        |                               d                     d d d            d S # 1 swxY w Y   d S )Nrc   r~   )r*   writer   dumpsencode)rj   rk   r(   r'   s      r   r    z4StoreBackendMixin.store_metadata.<locals>.write_func  s    __]D99 BQGGDJx0077@@AAAB B B B B B B B B B B B B B B B B Bs   ;A  A$'A$N)r   rO   rP   r.   r5   rm   )r'   rX   rZ   ro   r   r    s   `     r   store_metadataz StoreBackendMixin.store_metadata   s    	T]=W===I  +++w||I??HB B B B B ((8ZHHHHH	DDs   A-A2 2A7c                 d    t          j        j        | j        g|R  }|                     |          S )z,Check cached function is available in store.)r   rO   rP   r.   object_existsr'   rX   	func_paths      r   contains_pathzStoreBackendMixin.contains_path  s1    GL9999	!!),,,r   c                     t          j        j        | j        g|R  }|                     |          r|                     |           dS dS )z0Clear all items with a common path in the store.Nrr   r   s      r   
clear_pathzStoreBackendMixin.clear_path  rt   r   c                    t          j        j        | j        g|R  }|                     |          s|                     |           |yt           j                            |d          }|                     |d          5 }|                    |                    d                     ddd           dS # 1 swxY w Y   dS dS )&Store the code of the cached function.Nfunc_code.pyrc   r~   )	r   rO   rP   r.   r/   r5   r*   r   r   )r'   rX   	func_coder   r   r(   s         r   store_cached_func_codez(StoreBackendMixin.store_cached_func_code  s    GL9999	  ++ 	,  +++ w||I~>>H400 3A	((112223 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 ! s   ?)B55B9<B9c                     t          j        j        | j        g|dR  }	 |                     |d          5 }|                                                    d          cddd           S # 1 swxY w Y   dS #   xY w)r   r   rN   r~   N)r   rO   rP   r.   r*   r   r   )r'   rX   r   r(   s       r   get_cached_func_codez&StoreBackendMixin.get_cached_func_code!  s    7<HHHHH	400 0Avvxxw//0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0	s.   A7 'A*A7 *A..A7 1A.2A7 7A:c                 >    dt          j        j        | j        g|R  iS )z?Return information related to the cached function if it exists.r.   rx   ry   s     r   get_cached_func_infoz&StoreBackendMixin.get_cached_func_info*  r{   r   c                 :    |                      | j                   dS )zClear the whole store content.N)r7   r.   r9   s    r   clearzStoreBackendMixin.clear.  s    DM*****r   c                     |                      |||          }|D ]Z}| j        dk    r"t          d                    |                     	 |                     |j                   K# t          $ r Y Ww xY wdS )zX
        Remove the store's oldest files to enforce item, byte, and age limits.
        rK   zDeleting item {0}N)_get_items_to_deleter<   rT   r   r7   rO   OSError)r'   bytes_limititems_limit	age_limititems_to_deleterI   s         r   enforce_store_limitsz&StoreBackendMixin.enforce_store_limits2  s     33KiXX# 
	 
	D|b  )0066777##DI....   
 
	 
	s   
A%%
A21A2c                    t          |t                    rt          |          }|                                 }|sg S t	          d |D                       }|||z
  }nd}|t          |          |z
  }nd}|bt          d |D                       }|                                dk     rt          d          t          j	        
                                |z
  }	nd}	|dk    r|dk    r
|	||	k    rg S |                    t          j        d                     g }
d}d}|D ]A}||k    r||k    r|	|	|j        k     r n%|
                    |           ||j        z  }|dz  }B|
S )	zW
        Get items to delete to keep the store under size, file, & age limits.
        c              3   $   K   | ]}|j         V  d S N)sizerG   s     r   rJ   z9StoreBackendMixin._get_items_to_delete.<locals>.<genexpr>O  s$      //49//////r   Nr   c              3   $   K   | ]}|j         V  d S r   )last_accessrG   s     r   rJ   z9StoreBackendMixin._get_items_to_delete.<locals>.<genexpr>\  s%      @@$T-@@@@@@r   z(age_limit has to be a positive timedeltar   )keyr   )
isinstancestrr	   r:   sumlenmintotal_seconds
ValueErrordatetimenowsortoperator
attrgetterr   appendr   )r'   r   r   r   rS   r   to_delete_sizeto_delete_items
older_itemdeadliner   size_so_faritems_so_farrI   s                 r   r   z&StoreBackendMixin._get_items_to_deleteD  s    k3'' 	7)+66K   	I///////"!K/NNN"!%jj;6OOO @@%@@@@@J&&((1,, !KLLL(,,..:HHH a1$$!Z(%:%:I 	

x*=99
::: 
	 
	D.. O33%D4D)D)D""4(((49$KALLr   c                 T    t          |||          }|                     ||           dS )z7Writes an object into a file in a concurrency-safe way.N)r#   r3   )r'   rj   r   r    r"   s        r   rm   z)StoreBackendMixin._concurrency_safe_write  s/    3Hh
SS*H55555r   c                 N    d                     | j        j        | j                  S )z/Printable representation of the store location.z#{class_name}(location="{location}"))
class_namer.   )r   	__class__r   r.   r9   s    r   __repr__zStoreBackendMixin.__repr__  s+    4;;~. < 
 
 	
r   )r   NN)r   r   )NN)r   r   r   r   r`   rp   rs   rv   rz   r   r   r   r   r   r   r   r   r   r   rm   r   r   r   r   rB   rB      s>        % % % %N   >+ + ++ + +C C C    - - -
+ + +	3 	3 	3 	3  C C C+ + +   $: : : :x6 6 6

 
 
 
 
r   rB   c                       e Zd ZdZ ee          Z eej        j	                  Z
 ee          Zd Zd Zd ZddZdS )	FileSystemStoreBackendz7A StoreBackend used with local or network file systems.c                 j    || j         k    rt          |           dS t          j        |d           dS )zDelete location on store.T)ignore_errorsN)r.   r   shutilrmtreer-   s     r   r7   z%FileSystemStoreBackend.clear_location  s>    t}$$x     M($777777r   c                 $    t          |           dS )zCreate object location on storeN)r
   r-   s     r   r5   z&FileSystemStoreBackend.create_location  s    xr   c                   	 g }t          j        | j                  D ]<\  	}}t          j        dt           j                            	                    }|r t           j                            	d          }	 t           j                            |          }nB# t          $ r5 	 t           j                            	          }n# t          $ r Y Y w xY wY nw xY wt          j
                            |          }	 	fd|D             }t          d |D                       }n# t          $ r Y w xY w|                    t          	||                     >|S )z7Returns the whole list of items available in the store.z[a-f0-9]{32}rM   c                 P    g | ]"}t           j                            |          #S r   )r   rO   rP   )rH   fndirpaths     r   
<listcomp>z4FileSystemStoreBackend.get_items.<locals>.<listcomp>  s)    %T%T%TBbgll7B&?&?%T%T%Tr   c              3   T   K   | ]#}t           j                            |          V  $d S r   )r   rO   getsize)rH   r   s     r   rJ   z3FileSystemStoreBackend.get_items.<locals>.<genexpr>  s0      !O!O""'//""5"5!O!O!O!O!O!Or   )r   walkr.   rematchrO   rR   rP   getatimer   r   fromtimestampr   r   r   )
r'   rS   _	filenamesis_cache_hash_diroutput_filenamer   full_filenamesdirsizer   s
            @r   r:   z FileSystemStoreBackend.get_items  s   %'WT]%;%; 	K 	K!GQ	 "9I9I'9R9R S S  K"$',,w"E"E!"$'"2"2?"C"CKK ! ! !!&(g&6&6w&?&?" ! ! ! ! $! '/==kJJ%T%T%T%T)%T%T%TN!!O!O!O!O!OOOGG    H	 ]7G[IIJJJsH   :B
C%CC
CCCCC<'D$$
D21D2r   Nc                    |i }|| _         t          j                            | j                   st	          | j                    t          j                            |          rBt          j                            |          dk    rt          j                            |          n|}t          t          j                            |d          d          5 }|	                    d           |	                    d           ddd           n# 1 swxY w Y   |
                    dd          | _        |
                    d	          }| j        r|t          j        d
d           || _        || _        dS )zsConfigure the store backend.

        For this backend, valid store options are 'compress' and 'mmap_mode'
        Njoblibz
.gitignorewz## Created by joblib automatically.
z*
rd   FrL   zSCompressed items cannot be memmapped in a filesystem store. Option will be ignored.   )
stacklevel)r.   r   rO   existsr
   dirnamerR   openrP   r   getrd   rg   rh   rL   r<   )r'   r.   r<   r=   cache_directoryfilerL   s          r   r>   z FileSystemStoreBackend.configure  s   
 " O !w~~dm,, 	"4=!!! wx((-/W-=-=h-G-G8-S-S GOOH%%% 	
 "',,==sCC 	tJJ=>>>JJu	 	 	 	 	 	 	 	 	 	 	 	 	 	 	
 (++J>> $''44	= 	Y2M<    #s   +DDD)r   N)r   r   r   r   staticmethodr   r*   r   rO   r   r/   r   r3   r7   r5   r:   r>   r   r   r   r   r     s        AAd##J<//L566J8 8 8    @* * * * * *r   r   )$r   collectionsr   r   r   r   os.pathr   r   r   rQ   rg   abcr   r   pickler   rD   r   	backportsr   diskr	   r
   r   loggerr   
namedtupler   Warningr   r#   r%   objectrB   r   r   r   r   <module>r      s   4 4        				  				        ' ' ' ' ' ' ' '                   . . . . . . 5 5 5 5 5 5 5 5 5 5      &&8OPP	 	 	 	 	7 	 	 	  f f f f f f f f fRx
 x
 x
 x
 x
 x
 x
 x
v\ \ \ \ \-/@ \ \ \ \ \r   