
     hb8                        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mZ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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   )concurrency_safe_rename)mkdirpmemstr_to_bytes
rm_subdirs)numpy_pickleCacheItemInfozpath size last_accessc                     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/var/www/html/Sam_Eipo/venv/lib/python3.11/site-packages/joblib/_store_backends.pyconcurrency_safe_writer      sT    9+--..I.55)RY[[* *J 2333    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 )StoreBackendBasezaHelper 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
        N )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_exists:   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_itemK   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_locationY   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_locationd   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_itemso   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.configurey   r#   r   )__name__
__module____qualname____doc__r&   r   r"   r'   r+   r-   r/   r2   dictr6   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 Z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                 X   t          j        j        | j        g|R  }|dk    rL|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 )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)rA   )r   pathjoinr&   printr   hasattrrA   r'   KeyErrorr"   r
   load)	r   rD   r4   msg	full_pathrA   r   r    items	            r   	load_itemzStoreBackendMixin.load_item   sx    GL6666	Q;;||hooc**++++n++C;;<<<!({!;!; )TT. 	 7<<	<88  ** 	K ?AIJ K K K 400 ,A#(++, , , , , , , , , , , , , , ,  $XCCCDs   'DDDc                 N    	 t          j        j         j        g|R  }                     |          s                     |           t           j                            |d          }|dk    rt          d|z              fd}                     |||           dS #  Y dS xY w)zLDump an item in the store at the path given as a list of
           strings.rB   r@   zPersisting in %sc                                          |d          5 }t          j        | |j                   d d d            d S # 1 swxY w Y   d S )Nwb)compress)r"   r
   dumprQ   to_writedest_filenamer    r   s      r   r   z/StoreBackendMixin.dump_item.<locals>.write_func   s    __]D99 >Q %h/3}> > > >> > > > > > > > > > > > > > > > > >s   AA	AN)r   rD   rE   r&   r'   r-   rF   _concurrency_safe_write)r   rD   rL   r4   	item_pathr   r   s   `      r   	dump_itemzStoreBackendMixin.dump_item   s    	@T]:T:::I$$Y// 0$$Y///w||I|<<H||(94555> > > > >
 ((xDDDDD	@???s   BB B$c                     t          j        j        | j        g|R  }|                     |          r|                     |           dS dS )z7Clear the item at the path, given as a list of strings.Nr   rD   rE   r&   r'   r/   )r   rD   rW   s      r   
clear_itemzStoreBackendMixin.clear_item   W    GL6666	Y'' 	+	*****	+ 	+r   c                     t          j        j        | j        g|R  }t           j                            |d          }|                     |          S )zLCheck if there is an item at the path, given as a list of
           stringsrB   )r   rD   rE   r&   r'   )r   rD   rW   r   s       r   contains_itemzStoreBackendMixin.contains_item   sH     GL6666	7<<	<88  ***r   c                 >    dt          j        j        | j        g|R  iS )zReturn information about item.r&   r   rD   rE   r&   r   rD   s     r   get_item_infozStoreBackendMixin.get_item_info   s0    BGL 0*.0 0 0 1 	1r   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.jsonrC   utf-8N)	r   rD   rE   r&   r"   jsonloadsreaddecode)r   rD   rW   r   r    s        r   get_metadatazStoreBackendMixin.get_metadata   s    	T]:T:::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.rd   c                                          |d          5 }|                    t          j        |                               d                     d d d            d S # 1 swxY w Y   d S )NrP   re   )r"   writerf   dumpsencoderS   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   rD   rE   r&   r-   rV   )r   rD   metadatarW   r   r   s   `     r   store_metadataz StoreBackendMixin.store_metadata   s    	T]:T:::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   rD   rE   r&   object_existsr   rD   	func_paths      r   contains_pathzStoreBackendMixin.contains_path   s1    GL6666	!!),,,r   c                     t          j        j        | j        g|R  }|                     |          r|                     |           dS dS )z0Clear all items with a common path in the store.NrZ   rt   s      r   
clear_pathzStoreBackendMixin.clear_path   r\   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.pyrP   re   )	r   rD   rE   r&   r'   r-   r"   rm   ro   )r   rD   	func_coderu   r   r    s         r   store_cached_func_codez(StoreBackendMixin.store_cached_func_code   s    GL6666	  ++ 	,  +++ 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                    |dgz  }t          j        j        | j        g|R  }	 |                     |d          5 }|                                                    d          cddd           S # 1 swxY w Y   dS #   xY w)rz   r{   rC   re   N)r   rD   rE   r&   r"   rh   ri   )r   rD   r   r    s       r   get_cached_func_codez&StoreBackendMixin.get_cached_func_code  s    ""7<5555	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.   A; 'A.!A; .A22A; 5A26A; ;A>c                 >    dt          j        j        | j        g|R  iS )z?Return information related to the cached function if it exists.r&   r`   ra   s     r   get_cached_func_infoz&StoreBackendMixin.get_cached_func_info  s#    BGL>>>>??r   c                 :    |                      | j                   dS )zClear the whole store content.N)r/   r&   r1   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 )z9Reduce store size to keep it under the given bytes limit.r@   zDeleting item {0}N)_get_items_to_deleter4   rF   r   r/   rD   OSError)r   bytes_limititems_to_deleterL   s       r   reduce_store_sizez#StoreBackendMixin.reduce_store_size  s    33K@@# 
	 
	D|b  )0066777##DI....   
 
	 
	s   A##
A0/A0c                 r   t          |t                    rt          |          }|                                 }t	          d |D                       }||z
  }|dk     rg S |                    t          j        d                     g }d}|D ])}||k    r n |                    |           ||j	        z  }*|S )z9Get items to delete to keep the store under a size limit.c              3   $   K   | ]}|j         V  d S N)size).0rL   s     r   	<genexpr>z9StoreBackendMixin._get_items_to_delete.<locals>.<genexpr>-  s$      //49//////r   r   last_access)key)

isinstancestrr   r2   sumsortoperator
attrgetterappendr   )r   r   itemsr   to_delete_sizer   size_so_farrL   s           r   r   z&StoreBackendMixin._get_items_to_delete'  s    k3'' 	7)+66K  ///////+AI 	

x*=99
::: 	% 	%D^++""4(((49$KKr   c                 T    t          |||          }|                     ||           dS )z7Writes an object into a file in a concurrency-safe way.N)r   r+   )r   rT   r   r   r   s        r   rV   z)StoreBackendMixin._concurrency_safe_writeC  s6    3H4<jJ J*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__r7   r&   r1   s    r   __repr__zStoreBackendMixin.__repr__I  s.    4;;~. < H H 	Hr   r   N)r   r   )r7   r8   r9   r:   rM   rX   r[   r^   rb   rj   rq   rv   rx   r}   r   r   r   r   r   rV   r   r   r   r   r>   r>      s8           6@ @ @ @(+ + ++ + +1 1 1
    - - -
+ + +	3 	3 	3 	3  @ @ @+ + +     86 6 6H H H H Hr   r>   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   r/   z%FileSystemStoreBackend.clear_locationV  s>    %%x     M($777777r   c                 $    t          |           dS )zCreate object location on storeN)r   r%   s     r   r-   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}rB   c                 P    g | ]"}t           j                            |          #S r   )r   rD   rE   )r   fndirpaths     r   
<listcomp>z4FileSystemStoreBackend.get_items.<locals>.<listcomp>v  s9     &; &; &;*, ')gll7B&?&? &; &; &;r   c              3   T   K   | ]#}t           j                            |          V  $d S r   )r   rD   getsize)r   r   s     r   r   z3FileSystemStoreBackend.get_items.<locals>.<genexpr>x  sD       "< "<&( #%'//""5"5 "< "< "< "< "< "<r   )r   walkr&   rematchrD   basenamerE   getatimer   datetimefromtimestampr   r   r   )
r   r   _	filenamesis_cache_hash_diroutput_filenamer   full_filenamesdirsizer   s
            @r   r2   z FileSystemStoreBackend.get_itemsa  s   %'WT]%;%; 	9 	9!GQ	 ")+)9)9')B)B!D !D ! 9"$',,w"E"E!"$'"2"2?"C"CKK ! ! !!&(g&6&6w&?&?" ! ! ! ! $! '/==kJJ	&; &; &; &;09&; &; &;N! "< "<,:"< "< "< < <GG    H	 ]7G+68 8 9 9 9 sH   :B
C%CC
CCCCC<'D$$
D21D2r   Nc                 F   |i }|| _         t          j                            | j                   st	          | j                    |                    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'
        NrQ   FrA   zSCompressed items cannot be memmapped in a filesystem store. Option will be ignored.   )
stacklevel)r&   r   rD   existsr   getrQ   warningswarnrA   r4   )r   r&   r4   r5   rA   s        r   r6   z FileSystemStoreBackend.configure  s    
 " O !w~~dm,, 	"4=!!! (++J>> $''44	= 	(Y2M F%&( ( ( ( #r   r   )r7   r8   r9   r:   staticmethodopenr"   r   rD   r   r'   r   r+   r/   r-   r2   r6   r   r   r   r   r   O  s        AAd##J<//L566J8 8 8  " " "H     r   r   )r:   r   r   os.pathr   rf   r   r   collectionsr   r   abcr   r   	backportsr   diskr   r   r	    r
   
namedtupler   r   r   objectr>   r   r   r   r   <module>r      s   4 4 				 				               ' ' ' ' ' ' ' ' . . . . . . 5 5 5 5 5 5 5 5 5 5      &&'>@ @  f f f f f f f f fRBH BH BH BH BH BH BH BHJO O O O O-/@ O O O O Or   