
    WhE                     D   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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j8                  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$y)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y)CacheWarningz:Warning to capture dump failures except for PicklingError.N)__name__
__module____qualname____doc__     P/var/www/html/jupyter_env/lib/python3.12/site-packages/joblib/_store_backends.pyr   r      s    Dr   r   c                     t        t        j                               }| dt        j                         j
                   dt        j                          d| } || |       |S )z>Writes an object into a unique file in a concurrency-safe way..-)id	threadingcurrent_threaduuiduuid4hexosgetpid)object_to_writefilename
write_func	thread_idtemporary_filenames        r   concurrency_safe_writer'       sX     9++-.I$:Qtzz|'7'7&8"))+a	{S 2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y)StoreBackendBasez^Helper Abstract Base Class which defines all methods that
    a StorageBackend must implement.Nc                      y)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_item5       r   c                      y)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_existsH   r/   r   c                      y)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_itemY   r/   r   c                      y)zCreates a location on the store.

        Parameters
        ----------
        location: string
            The location in the store. On a filesystem, this corresponds to a
            directory.
        Nr   r1   s     r   create_locationz StoreBackendBase.create_locationg   r/   r   c                      y)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   r1   s     r   clear_locationzStoreBackendBase.clear_locationr   r/   r   c                      y)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_items}   r/   r   r   c                      y)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+   r2   verbosebackend_optionss       r   	configurezStoreBackendBase.configure   r/   r   )r   r   r   r   r2   r   r.   r3   r7   r9   r;   r>   dictrB   r   r   r   r)   r)   /   s    ( H $            *+TV  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y)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.
    Nc                    t        j                  j                  | j                  g| }|dkD  r|/dj	                  t        t        j                         |z
              nd}t         j                  j                  |d         }|Ad|v r=dj                  d |d   j                         D              }|d	j	                  |      z  }d
j	                  ||      }	|dk  rt        dj	                  |	             nt        dj	                  |	|             t        | d      sdn| j                  }
t         j                  j                  |d      }| j                  |      st        d|z        |
2| j                  |d      5 }t        j                   |      }ddd       |S t        j                   ||
      }|S # 1 sw Y   S xY w)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                   |   yw)z{}={}N)format.0items     r   	<genexpr>z.StoreBackendMixin.load_item.<locals>.<genexpr>   s       #.2NGNND)#s   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)rQ   )r    pathjoinr2   rK   r   timebasenameitemsprinthasattrrQ   r3   KeyErrorr.   r   load)r+   call_idr@   	timestampmetadata	full_path	ts_string	signaturekwargsmsgrQ   r#   r,   rN   s                 r   	load_itemzStoreBackendMixin.load_item   s   GGLL99	Q; (   TYY[9-D!EF 
 ((4I#(@ #6>|6L6R6R6T#  V]]622	*11)YGC|hooc*+n++C;< 'k :D	77<<	<8  *46>?  40 ,A#((+,   $$XCD	, s   F;;Gc                     	 t        j                  j                   j                  g| } j	                  |      s j                  |       t         j                  j                  |d      }|dkD  rt        d|z          fd} j                  |||       y# t        $ r(}t        j                  d| dt               Y d}~yd}~ww xY w)z;Dump an item in the store at the id given as a list of str.rR   rP   zPersisting in %sc                    j                  |d      5 }	 t        j                  | |j                         d d d        y # t        $ r(}t        j                  d| dt               Y d }~5d }~ww xY w# 1 sw Y   y xY w)Nwb)compresszjUnable to cache to disk: failed to pickle output. In version 1.5 this will raise an exception. Exception: r   )r.   r   dumpri   r   warningswarnFutureWarning)to_writedest_filenamer,   er+   s       r   r$   z/StoreBackendMixin.dump_item.<locals>.write_func   s    __]D9 
Q	$))(AN
 
 )  556Cq: *	 
 
s.   A5"A	A2
A-(A5-A22A55A>z`Unable to cache to disk. Possibly a race condition in the creation of the directory. Exception: r   N)r    rT   rU   r2   r3   r9   rY   _concurrency_safe_write	Exceptionrk   rl   r   )r+   r]   rN   r@   	item_pathr#   r$   rp   s   `       r   	dump_itemzStoreBackendMixin.dump_item   s    	T]]=W=I$$Y/$$Y/ww||I|<H|(945 ((xD 	MM99:1> 	s   BB 	C$CCc                     t        j                  j                  | j                  g| }| j	                  |      r| j                  |       yy)z1Clear the item at the id, given as a list of str.Nr    rT   rU   r2   r3   r;   )r+   r]   rs   s      r   
clear_itemzStoreBackendMixin.clear_item   >    GGLL99	Y'	* (r   c                     t        j                  j                  | j                  g| }t         j                  j                  |d      }| j	                  |      S )z<Check if there is an item at the id, given as a list of str.rR   )r    rT   rU   r2   r3   )r+   r]   rs   r#   s       r   contains_itemzStoreBackendMixin.contains_item   sB    GGLL99	77<<	<8  **r   c                 Z    dt        j                  j                  | j                  g| iS )zReturn information about item.r2   r    rT   rU   r2   r+   r]   s     r   get_item_infozStoreBackendMixin.get_item_info   #    BGGLLAABBr   c                 ^   	 t        j                  j                  | j                  g| }t         j                  j                  |d      }| j	                  |d      5 }t        j                  |j                         j                  d            cddd       S # 1 sw Y   yxY w#  i cY S xY w)z"Return actual metadata of an item.metadata.jsonrS   utf-8N)	r    rT   rU   r2   r.   jsonloadsreaddecode)r+   r]   rs   r#   r,   s        r   get_metadatazStoreBackendMixin.get_metadata   s    	T]]=W=Iww||I?H40 <Azz!&&(//'":;< < <	Is*   AB& 2B	B& B#B& #B& &B,c                      	 t        j                  j                   j                  g| } j	                  |       t         j                  j                  |d      } fd} j                  |||       y#  Y yxY w)z Store metadata of a computation.r   c                     j                  |d      5 }|j                  t        j                  |       j	                  d             d d d        y # 1 sw Y   y xY w)Nrh   r   )r.   writer   dumpsencode)rn   ro   r,   r+   s      r   r$   z4StoreBackendMixin.store_metadata.<locals>.write_func
  sL    __]D9 BQGGDJJx077@AB B Bs   4AAN)r    rT   rU   r2   r9   rq   )r+   r]   r_   rs   r#   r$   s   `     r   store_metadataz StoreBackendMixin.store_metadata  si    	T]]=W=I  +ww||I?HB ((8ZH	s   A3A7 7A;c                 x    t        j                  j                  | j                  g| }| j	                  |      S )z,Check cached function is available in store.)r    rT   rU   r2   object_existsr+   r]   	func_paths      r   contains_pathzStoreBackendMixin.contains_path  s.    GGLL99	!!),,r   c                     t        j                  j                  | j                  g| }| j	                  |      r| j                  |       yy)z0Clear all items with a common path in the store.Nrv   r   s      r   
clear_pathzStoreBackendMixin.clear_path  rx   r   c                 p   t        j                  j                  | j                  g| }| j	                  |      s| j                  |       |\t         j                  j                  |d      }| j                  |d      5 }|j                  |j                  d             ddd       yy# 1 sw Y   yxY w)&Store the code of the cached function.Nfunc_code.pyrh   r   )	r    rT   rU   r2   r3   r9   r.   r   r   )r+   r]   	func_coder   r#   r,   s         r   store_cached_func_codez(StoreBackendMixin.store_cached_func_code  s    GGLL99	  +  + ww||I~>H40 3A	((123 3 !3 3s   !B,,B5c                     t        j                  j                  | j                  g|d }	 | j	                  |d      5 }|j                         j                  d      cddd       S # 1 sw Y   yxY w#   xY w)r   r   rS   r   N)r    rT   rU   r2   r.   r   r   )r+   r]   r#   r,   s       r   get_cached_func_codez&StoreBackendMixin.get_cached_func_code(  sg    77<<HHH	40 0Avvxw/0 0 0	s)   A5  A)	A5 )A2.A5 2A5 5A8c                 Z    dt        j                  j                  | j                  g| iS )z?Return information related to the cached function if it exists.r2   r|   r}   s     r   get_cached_func_infoz&StoreBackendMixin.get_cached_func_info1  r   r   c                 :    | j                  | j                         y)zClear the whole store content.N)r;   r2   r=   s    r   clearzStoreBackendMixin.clear5  s    DMM*r   c                     | j                  |||      }|D ]G  }| j                  dkD  rt        dj                  |             	 | j	                  |j
                         I y# t        $ r Y Vw xY w)zX
        Remove the store's oldest files to enforce item, byte, and age limits.
        rP   zDeleting item {0}N)_get_items_to_deleter@   rY   rK   r;   rT   OSError)r+   bytes_limititems_limit	age_limititems_to_deleterN   s         r   enforce_store_limitsz&StoreBackendMixin.enforce_store_limits9  st     33KiX# 
	D||b )0067##DII.	
	
  
 s   A!!	A-,A-c                 ~   t        |t              rt        |      }| j                         }|sg S t	        d |D              }|||z
  }nd}|t        |      |z
  }nd}|Rt        d |D              }|j                         dk  rt        d      t        j                  j                         |z
  }	nd}	|dk  r|dk  r	|	|	kD  rg S |j                  t        j                  d             g }
d}d}|D ]E  }||k\  r||k\  r|	|	|j                  k  r |
S |
j                  |       ||j                   z  }|dz  }G |
S )	zW
        Get items to delete to keep the store under size, file, & age limits.
        c              3   4   K   | ]  }|j                     y wN)sizerL   s     r   rO   z9StoreBackendMixin._get_items_to_delete.<locals>.<genexpr>V  s     /499/   Nr   c              3   4   K   | ]  }|j                     y wr   )last_accessrL   s     r   rO   z9StoreBackendMixin._get_items_to_delete.<locals>.<genexpr>c  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   rX   r   to_delete_sizeto_delete_items
older_itemdeadliner   size_so_faritems_so_farrN   s                 r   r   z&StoreBackendMixin._get_items_to_deleteK  sv    k3')+6K I///"!K/NN"!%j;6OO @%@@J&&(1, !KLL((,,.:HH a1$!Z(%:I 	

x**=9
: 
	D. O3%D4D4D)D 	 ""4(499$KAL
	 r   c                 B    t        |||      }| j                  ||       y)z7Writes an object into a file in a concurrency-safe way.N)r'   r7   )r+   rn   r#   r$   r&   s        r   rq   z)StoreBackendMixin._concurrency_safe_write  s     3Hh
S*H5r   c                 d    dj                  | j                  j                  | j                        S )z/Printable representation of the store location.z#{class_name}(location="{location}"))
class_namer2   )rK   	__class__r   r2   r=   s    r   __repr__zStoreBackendMixin.__repr__  s-    4;;~~.. < 
 	
r   )r   NN)r   r   )NN)r   r   r   r   re   rt   rw   rz   r~   r   r   r   r   r   r   r   r   r   r   rq   r   r   r   r   rF   rF      s`    %N>++C-
+	3C+$:x6

r   rF   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y)FileSystemStoreBackendz7A StoreBackend used with local or network file systems.c                 h    || j                   k(  rt        |       yt        j                  |d       y)zDelete location on store.T)ignore_errorsN)r2   r   shutilrmtreer1   s     r   r;   z%FileSystemStoreBackend.clear_location  s$    t}}$x MM($7r   c                     t        |       y)zCreate object location on storeN)r
   r1   s     r   r9   z&FileSystemStoreBackend.create_location  s
    xr   c                    g }t        j                  | j                        D ]  \  }}}t        j                  dt         j
                  j                  |            }|s=t         j
                  j                  |d      }	 t         j
                  j                  |      }t        j                  j                  |      }	 |D cg c]"  }t         j
                  j                  ||      $ }	}t        d |	D              }
|j                  t        ||
|              |S # t        $ r5 	 t         j
                  j                  |      }n# t        $ r Y Y 3w xY wY w xY wc c}w # t        $ r Y Nw xY w)z7Returns the whole list of items available in the store.z[a-f0-9]{32}rR   c              3   Z   K   | ]#  }t         j                  j                  |       % y wr   )r    rT   getsize)rM   fns     r   rO   z3FileSystemStoreBackend.get_items.<locals>.<genexpr>  s     !O""''//""5!Os   )+)r    walkr2   rematchrT   rW   rU   getatimer   r   fromtimestampr   r   r   )r+   rX   dirpath_	filenamesis_cache_hash_diroutput_filenamer   r   full_filenamesdirsizes              r   r>   z FileSystemStoreBackend.get_items  sC   %'WWT]]%; 	K!GQ	 "9I9I'9R S "$'',,w"E!"$''"2"2?"CK '//==kJJS%TBbggll7B&?%TN%T!!O!OOG ]7G[IJ1	K4 '  !!&(gg&6&6w&?" ! ! $! &U  	sZ    D?E$'E*E$	E(EE	EEEEEE$$	E10E1Nc                    |i }|| _         t        j                  j                  | j                         st	        | j                          t        j                  j                  |      rAt        j                  j                  |      dk(  rt        j                  j                  |      n|}t        t        j                  j                  |d      d      5 }|j                  d       |j                  d       ddd       |j                  dd      | _        |j                  d	      }| j                  r|t        j                  d
d       || _        || _        y# 1 sw Y   exY w)zsConfigure the store backend.

        For this backend, valid store options are 'compress' and 'mmap_mode'
        Njoblibz
.gitignorewz## Created by joblib automatically.
z*
ri   FrQ   zSCompressed items cannot be memmapped in a filesystem store. Option will be ignored.   )
stacklevel)r2   r    rT   existsr
   dirnamerW   openrU   r   getri   rk   rl   rQ   r@   )r+   r2   r@   rA   cache_directoryfilerQ   s          r   rB   z FileSystemStoreBackend.configure  s   
 " O !ww~~dmm,4==! wwx(RWW-=-=h-G8-S GGOOH% 	
 "'',,=sC 	tJJ=>JJu	
 (++J> $''4	==Y2MM< #%	 	s   #EE&)r   N)r   r   r   r   staticmethodr   r.   r    rT   r   r3   r   r7   r;   r9   r>   rB   r   r   r   r   r     sA    Ad#J/L56J8@*r   r   )%r   collectionsr   r   r   r    os.pathr   r   r   rV   r   rk   abcr   r   pickler   rH   r   	backportsr   diskr	   r
   r   loggerr   
namedtupler   Warningr   r'   r)   objectrF   r   r   r   r   <module>r      s    4     	  	      '    . 5 5 &&&8OP	7 	f fRx
 x
v\-/@ \r   