
    "h>                     (   d Z ddlm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mZmZ ddlmZ  e
j0                  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.    )PicklingErrorN)ABCMetaabstractmethod   )concurrency_safe_rename)mkdirpmemstr_to_bytes
rm_subdirs)numpy_pickle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__     S/var/www/html/sandstorm/venv/lib/python3.12/site-packages/joblib/_store_backends.pyr   r      s    Dr   r   c                     t        t        j                               }dj                  ||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"      sD    9++-.I.55)RYY[* 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)StoreBackendBasezaHelper 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_item-       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_exists@   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_itemQ   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   r,   s     r   create_locationz StoreBackendBase.create_location_   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   r,   s     r   clear_locationzStoreBackendBase.clear_locationj   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_itemsu   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&   r-   verbosebackend_optionss       r   	configurezStoreBackendBase.configure   r*   r   )r   r   r   r   r-   r   r)   r.   r2   r4   r6   r9   dictr=   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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)zKLoad an item from the store given its path as a list of
           strings.r   
   z{0}...z{0} from {1}	mmap_modeN
output.pklzANon-existing item (may have been cleared).
File %s does not existrb)rD   )r   pathjoinr-   printr   hasattrrD   r.   KeyErrorr)   r   load)	r&   rG   r;   msg	full_pathrD   r   r'   items	            r   	load_itemzStoreBackendMixin.load_item   s    GGLL66	Q;|hooc*+n++C;<!({!;T.. 	 77<<	<8  * ?AIJ K K 40 ,A#((+,   $$XCD	, s   DD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)zLDump an item in the store at the path given as a list of
           strings.rE   rC   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   dumprT   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: rU   N)r   rG   rH   r-   r.   r4   rI   _concurrency_safe_write	ExceptionrW   rX   r   )r&   rG   rO   r;   	item_pathr   r   r\   s   `       r   	dump_itemzStoreBackendMixin.dump_item   s    	T]]:T: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)z7Clear the item at the path, given as a list of strings.Nr   rG   rH   r-   r.   r6   )r&   rG   r_   s      r   
clear_itemzStoreBackendMixin.clear_item   >    GGLL66	Y'	* (r   c                     t        j                  j                  | j                  g| }t         j                  j                  |d      }| j	                  |      S )zLCheck if there is an item at the path, given as a list of
           stringsrE   )r   rG   rH   r-   r.   )r&   rG   r_   r   s       r   contains_itemzStoreBackendMixin.contains_item   sD     GGLL66	77<<	<8  **r   c                 Z    dt        j                  j                  | j                  g| iS )zReturn information about item.r-   r   rG   rH   r-   r&   rG   s     r   get_item_infozStoreBackendMixin.get_item_info   s,    BGGLL 0*.0 1 	1r   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.jsonrF   utf-8N)	r   rG   rH   r-   r)   jsonloadsreaddecode)r&   rG   r_   r   r'   s        r   get_metadatazStoreBackendMixin.get_metadata   s    	T]]:T: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.rl   c                     j                  |d      5 }|j                  t        j                  |       j	                  d             d d d        y # 1 sw Y   y xY w)NrS   rm   )r)   writern   dumpsencode)rZ   r[   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   rG   rH   r-   r4   r]   )r&   rG   metadatar_   r   r   s   `     r   store_metadataz StoreBackendMixin.store_metadata   si    	T]]:T: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   rG   rH   r-   object_existsr&   rG   	func_paths      r   contains_pathzStoreBackendMixin.contains_path  s.    GGLL66	!!),,r   c                     t        j                  j                  | j                  g| }| j	                  |      r| j                  |       yy)z0Clear all items with a common path in the store.Nrb   r|   s      r   
clear_pathzStoreBackendMixin.clear_path  rd   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.pyrS   rm   )	r   rG   rH   r-   r.   r4   r)   ru   rw   )r&   rG   	func_coder}   r   r'   s         r   store_cached_func_codez(StoreBackendMixin.store_cached_func_code  s    GGLL66	  +  + ww||I~>H40 3A	((123 3 !3 3s   !B,,B5c                     |dgz  }t        j                  j                  | j                  g| }	 | j	                  |d      5 }|j                         j                  d      cddd       S # 1 sw Y   yxY w#   xY w)r   r   rF   rm   N)r   rG   rH   r-   r)   rp   rq   )r&   rG   r   r'   s       r   get_cached_func_codez&StoreBackendMixin.get_cached_func_code  so    ""77<<55	40 0Avvxw/0 0 0	s)   A9 A-#	A9 -A62A9 6A9 9A<c                 Z    dt        j                  j                  | j                  g| iS )z?Return information related to the cached function if it exists.r-   rh   ri   s     r   get_cached_func_infoz&StoreBackendMixin.get_cached_func_info!  s#    BGGLL>>??r   c                 :    | j                  | j                         y)zClear the whole store content.N)r6   r-   r8   s    r   clearzStoreBackendMixin.clear%  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.
        rC   zDeleting item {0}N)_get_items_to_deleter;   rI   r   r6   rG   OSError)r&   bytes_limititems_limit	age_limititems_to_deleterO   s         r   enforce_store_limitsz&StoreBackendMixin.enforce_store_limits)  sy     33i
 $ 
	D||b )0067##DII.	
	
  
 s   A!!	A-,A-c                 :   t        |t              rt        |      }| j                         }t	        d |D              }|||z
  }nd}|t        |      |z
  }nd}|4t        d |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)size.0rO   s     r   	<genexpr>z9StoreBackendMixin._get_items_to_delete.<locals>.<genexpr>I  s     /499/   Nr   c              3   4   K   | ]  }|j                     y wr   )last_accessr   s     r   r   z9StoreBackendMixin._get_items_to_delete.<locals>.<genexpr>V  s     @$T--@r   r   )keyr   )
isinstancestrr	   r9   sumlenmindatetimenowsortoperator
attrgetterr   appendr   )r&   r   r   r   itemsr   to_delete_sizeto_delete_items
older_itemdeadliner   size_so_faritems_so_farrO   s                 r   r   z&StoreBackendMixin._get_items_to_delete?  sP    k3')+6K ///"!K/NN"!%j;6OO @%@@J((,,.:HH aOq$8!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"   r2   )r&   rZ   r   r   r!   s        r   r]   z)StoreBackendMixin._concurrency_safe_writew  s$    3H4<jJ*H5r   c                 d    dj                  | j                  j                  | j                        S )z/Printable representation of the store location.z#{class_name}(location="{location}"))
class_namer-   )r   	__class__r   r-   r8   s    r   __repr__zStoreBackendMixin.__repr__}  s/    4;;~~.. < H 	Hr   r   N)r   r   )NN)r   r   r   r   rP   r`   rc   rf   rj   rr   ry   r~   r   r   r   r   r   r   r   r]   r   r   r   r   rA   rA      sl    6@++1
-
+	3@+
 <@. <@6p6Hr   rA   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)r-   r
   shutilrmtreer,   s     r   r6   z%FileSystemStoreBackend.clear_location  s$    %x MM($7r   c                     t        |       y)zCreate object location on storeN)r   r,   s     r   r4   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}rE   c              3   Z   K   | ]#  }t         j                  j                  |       % y wr   )r   rG   getsize)r   fns     r   r   z3FileSystemStoreBackend.get_items.<locals>.<genexpr>  s%      "<&( #%''//""5 "<s   )+)r   walkr-   rematchrG   basenamerH   getatimer   r   fromtimestampr   r   r   )r&   r   dirpath_	filenamesis_cache_hash_diroutput_filenamer   r   full_filenamesdirsizes              r   r9   z FileSystemStoreBackend.get_items  s^   %'WWT]]%; 	9!GQ	 ")+)9)9')B!D !"$'',,w"E!"$''"2"2?"CK '//==kJ	09&;*, ')ggll7B&? &;N &;! "<,:"< <G ]7G+68 97	9< -  !!&(gg&6&6w&?" ! ! $!&;   	sZ    D?E$'E*E$	E(EE	EEEEEE$$	E10E1Nc                 L   |i }|| _         t        j                  j                  | j                         st	        | j                          |j                  dd      | _        |j                  d      }| j                  r|t        j                  dd       || _	        || _
        y)zsConfigure the store backend.

        For this backend, valid store options are 'compress' and 'mmap_mode'
        NrT   FrD   zSCompressed items cannot be memmapped in a filesystem store. Option will be ignored.   )
stacklevel)r-   r   rG   existsr   getrT   rW   rX   rD   r;   )r&   r-   r;   r<   rD   s        r   r=   z FileSystemStoreBackend.configure  s    
 " O !ww~~dmm,4==! (++J> $''4	==Y2MM F%&( #r   r   )r   r   r   r   staticmethodopenr)   r   rG   r   r.   r   r2   r6   r4   r9   r=   r   r   r   r   r     sA    Ad#J/L56J8"Hr   r   )!r   pickler   r   r   os.pathr   rn   r   rW   collectionsr   r   abcr   r   	backportsr   diskr   r	   r
    r   
namedtupler   Warningr   r"   r$   objectrA   r   r   r   r   <module>r      s    4   	 	         ' . 5 5 &&&'>@	7 	
f fRpH pHfO-/@ Or   