o
    l¨ÊhÂ8  ã                   @   sî   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mZ ddlmZ ddlmZmZmZ ddlmZ e d	d
¡Zdd„ ZG dd„ deeƒƒZG dd„ deƒZG dd„ deeƒZdS )z.Storage providers backends for Memory caching.é    N)ÚABCMetaÚabstractmethodé   )Úwith_metaclassÚ_basestring)Úconcurrency_safe_rename)ÚmkdirpÚmemstr_to_bytesÚ
rm_subdirs)Únumpy_pickleÚCacheItemInfozpath size last_accessc                 C   s,   t t ¡ ƒ}d ||t ¡ ¡}|| |ƒ |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_filename© r   úH/var/www/html/env/lib/python3.10/site-packages/joblib/_store_backends.pyÚconcurrency_safe_write   s   
ÿ
r   c                   @   sp   e Zd ZdZdZedd„ ƒZedd„ ƒZedd„ ƒZed	d
„ ƒZ	edd„ ƒZ
edd„ ƒZedeƒ fdd„ƒZdS )ÚStoreBackendBasezaHelper Abstract Base Class which defines all methods that
       a StorageBackend must implement.Nc                 C   ó   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Úmoder   r   r   Ú
_open_item(   ó    zStoreBackendBase._open_itemc                 C   r   )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   Úlocationr   r   r   Ú_item_exists;   r!   zStoreBackendBase._item_existsc                 C   r   )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Údstr   r   r   Ú
_move_itemL   r!   zStoreBackendBase._move_itemc                 C   r   )zÎCreates a location on the store.

        Parameters
        ----------
        location: string
            The location in the store. On a filesystem, this corresponds to a
            directory.
        Nr   r"   r   r   r   Úcreate_locationZ   r!   z StoreBackendBase.create_locationc                 C   r   )zèClears 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"   r   r   r   Úclear_locatione   r!   zStoreBackendBase.clear_locationc                 C   r   )z»Returns 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   r   r   r   Ú	get_itemsp   r!   zStoreBackendBase.get_itemsr   c                 C   r   )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 dictionnary of named paremeters used to configure the
            store backend.
        Nr   )r   r#   ÚverboseÚbackend_optionsr   r   r   Ú	configurez   r!   zStoreBackendBase.configure)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r#   r   r    r$   r'   r(   r)   r+   Údictr.   r   r   r   r   r   "   s"    







	r   c                   @   sž   e Zd ZdZd&dd„Zd'dd„Zdd	„ Zd
d„ Zdd„ Zdd„ Z	dd„ Z
dd„ Zdd„ Zd(dd„Zdd„ Zdd„ Zdd„ Zdd„ Zd d!„ Zd"d#„ Zd$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           	      C   sÔ   t jj| jg|¢R Ž }|dkr#|dk rtd |¡ƒ ntd ||¡ƒ t| dƒs*dn| j}t j |d¡}|  |¡s?t	d| ƒ‚|du ra|  
|d	¡�}t |¡}W d  ƒ |S 1 sZw   Y  |S tj||d
�}|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)r6   )r   ÚpathÚjoinr#   Úprintr   Úhasattrr6   r$   ÚKeyErrorr    r   Úload)	r   r9   r,   ÚmsgÚ	full_pathr6   r   r   Úitemr   r   r   Ú	load_item•   s,   ÿ
ÿ
ÿüÿzStoreBackendMixin.load_itemc                    sz   z6t jjˆ jg|¢R Ž }ˆ  |¡sˆ  |¡ t j |d¡}|dkr'td| ƒ ‡ fdd„}ˆ  |||¡ W dS    Y dS )zLDump an item in the store at the path given as a list of
           strings.r7   r5   zPersisting in %sc                    sD   ˆ   |d¡�}tj| |ˆ jd� W d   ƒ d S 1 sw   Y  d S )NÚwb)Úcompress)r    r   ÚdumprD   ©Úto_writeÚdest_filenamer   r*   r   r   r   »   s
   ÿ"ÿz/StoreBackendMixin.dump_item.<locals>.write_funcN)r   r9   r:   r#   r$   r(   r;   Ú_concurrency_safe_write)r   r9   rA   r,   Ú	item_pathr   r   r   r*   r   Ú	dump_item°   s   

zStoreBackendMixin.dump_itemc                 C   ó2   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   r9   r:   r#   r$   r)   )r   r9   rJ   r   r   r   Ú
clear_itemÄ   ó   
ÿzStoreBackendMixin.clear_itemc                 C   s.   t jj| jg|¢R Ž }t j |d¡}|  |¡S )zLCheck if there is an item at the path, given as a list of
           stringsr7   )r   r9   r:   r#   r$   )r   r9   rJ   r   r   r   r   Úcontains_itemÊ   s   
zStoreBackendMixin.contains_itemc                 C   ó   dt jj| jg|¢R Ž iS )zReturn information about item.r#   ©r   r9   r:   r#   ©r   r9   r   r   r   Úget_item_infoÒ   s   
ÿzStoreBackendMixin.get_item_infoc                 C   s|   z6t jj| jg|¢R Ž }t j |d¡}|  |d¡�}t | ¡  d¡¡W  d  ƒ W S 1 s/w   Y  W dS    i  Y S )z"Return actual metadata of an item.úmetadata.jsonr8   úutf-8N)	r   r9   r:   r#   r    ÚjsonÚloadsÚreadÚdecode)r   r9   rJ   r   r   r   r   r   Úget_metadata×   s   (ÿzStoreBackendMixin.get_metadatac                    s\   z't jjˆ jg|¢R Ž }ˆ  |¡ t j |d¡}‡ fdd„}ˆ  |||¡ W dS    Y dS )z Store metadata of a computation.rU   c                    sH   ˆ   |d¡�}| t | ¡ d¡¡ W d   ƒ d S 1 sw   Y  d S )NrC   rV   )r    ÚwriterW   ÚdumpsÚencoderF   r*   r   r   r   è   s   "ÿz4StoreBackendMixin.store_metadata.<locals>.write_funcN)r   r9   r:   r#   r(   rI   )r   r9   ÚmetadatarJ   r   r   r   r*   r   Ústore_metadataá   s   
z StoreBackendMixin.store_metadatac                 C   s    t jj| jg|¢R Ž }|  |¡S )z,Check cached function is available in store.)r   r9   r:   r#   Úobject_exists©r   r9   Ú	func_pathr   r   r   Úcontains_pathð   s   
zStoreBackendMixin.contains_pathc                 C   rL   )z0Clear all items with a common path in the store.NrM   rb   r   r   r   Ú
clear_pathõ   rO   zStoreBackendMixin.clear_pathc                 C   s†   t jj| jg|¢R Ž }|  |¡s|  |¡ |durAt j |d¡}|  |d¡�}| | d¡¡ W d  ƒ dS 1 s:w   Y  dS dS )ú&Store the code of the cached function.Núfunc_code.pyrC   rV   )	r   r9   r:   r#   r$   r(   r    r\   r^   )r   r9   Ú	func_coderc   r   r   r   r   r   Ústore_cached_func_codeû   s   

"ÿþz(StoreBackendMixin.store_cached_func_codec                 C   sl   |dg7 }t jj| jg|¢R Ž }z!|  |d¡�}| ¡  d¡W  d  ƒ W S 1 s*w   Y  W dS    ‚ )rf   rg   r8   rV   N)r   r9   r:   r#   r    rY   rZ   )r   r9   r   r   r   r   r   Úget_cached_func_code  s   
(ÿz&StoreBackendMixin.get_cached_func_codec                 C   rQ   )z?Return information related to the cached function if it exists.r#   rR   rS   r   r   r   Úget_cached_func_info  s   z&StoreBackendMixin.get_cached_func_infoc                 C   s   |   | j¡ dS )zClear the whole store content.N)r)   r#   r*   r   r   r   Úclear  s   zStoreBackendMixin.clearc              
   C   sV   |   |¡}|D ]!}| jdkrtd |¡ƒ z|  |j¡ W q ttfy(   Y qw dS )z9Reduce store size to keep it under the given bytes limit.r5   zDeleting item {0}N)Ú_get_items_to_deleter,   r;   r   r)   r9   ÚOSErrorÚIOError)r   Úbytes_limitÚitems_to_deleterA   r   r   r   Úreduce_store_size  s   

ûûz#StoreBackendMixin.reduce_store_sizec                 C   sŠ   t |tƒr	t|ƒ}|  ¡ }tdd„ |D ƒƒ}|| }|dk r g S |jt d¡d� g }d}|D ]}||kr8 |S | |¡ ||j	7 }q/|S )z9Get items to delete to keep the store under a size limit.c                 s   s   � | ]}|j V  qd S ©N)Úsize)Ú.0rA   r   r   r   Ú	<genexpr>.  s   € z9StoreBackendMixin._get_items_to_delete.<locals>.<genexpr>r   Úlast_access)Úkey)
Ú
isinstancer   r	   r+   ÚsumÚsortÚoperatorÚ
attrgetterÚappendrt   )r   rp   Úitemsrt   Úto_delete_sizerq   Úsize_so_farrA   r   r   r   rm   (  s"   

ýz&StoreBackendMixin._get_items_to_deletec                 C   s   t |||ƒ}|  ||¡ dS )z7Writes an object into a file in a concurrency-safe way.N)r   r'   )r   rG   r   r   r   r   r   r   rI   D  s   ÿz)StoreBackendMixin._concurrency_safe_writec                 C   s   dj | jj| jd�S )z/Printable representation of the store location.z#{class_name}(location="{location}"))Ú
class_namer#   )r   Ú	__class__r/   r#   r*   r   r   r   Ú__repr__J  s   
ÿzStoreBackendMixin.__repr__©r   N)r   rs   )r/   r0   r1   r2   rB   rK   rN   rP   rT   r[   r`   rd   re   ri   rj   rk   rl   rr   rm   rI   r„   r   r   r   r   r4   ‹   s&    
	



r4   c                   @   sN   e Zd ZdZeeƒZeejj	ƒZ
eeƒZdd„ Zdd„ Zdd„ Zdd
d„Zd	S )ÚFileSystemStoreBackendz7A StoreBackend used with local or network file systems.c                 C   s(   || j krt|ƒ dS tj|dd� dS )zDelete location on store.T)Úignore_errorsN)r#   r
   ÚshutilÚrmtreer"   r   r   r   r)   W  s   
z%FileSystemStoreBackend.clear_locationc                 C   s   t |ƒ dS )zCreate object location on storeN)r   r"   r   r   r   r(   ^  s   z&FileSystemStoreBackend.create_locationc           	         sê   g }t  | j¡D ]j\‰ }}t dt j ˆ ¡¡}|rrt j ˆ d¡}zt j |¡}W n t	yD   zt j ˆ ¡}W n
 t	yA   Y Y qw Y nw t
j
 |¡}z‡ fdd„|D ƒ}tdd„ |D ƒƒ}W n	 t	yh   Y qw | tˆ ||ƒ¡ q|S )z7Returns the whole list of items available in the store.z[a-f0-9]{32}r7   c                    s   g | ]	}t j ˆ |¡‘qS r   )r   r9   r:   ©ru   Úfn©Údirpathr   r   Ú
<listcomp>w  s    ÿz4FileSystemStoreBackend.get_items.<locals>.<listcomp>c                 s   s   � | ]	}t j |¡V  qd S rs   )r   r9   ÚgetsizerŠ   r   r   r   rv   y  s   € ÿz3FileSystemStoreBackend.get_items.<locals>.<genexpr>)r   Úwalkr#   ÚreÚmatchr9   Úbasenamer:   Úgetatimern   ÚdatetimeÚfromtimestamprz   r~   r   )	r   r   Ú_Ú	filenamesÚis_cache_hash_dirÚoutput_filenamerw   Úfull_filenamesÚdirsizer   rŒ   r   r+   b  sB   
ÿþÿþ
ÿÿü
ÿ€z FileSystemStoreBackend.get_itemsr   Nc                 C   sn   |du ri }|| _ tj | j ¡st| j ƒ | dd¡| _| d¡}| jr/|dur/tjddd� || _	|| _
dS )zsConfigure the store backend.

        For this backend, valid store options are 'compress' and 'mmap_mode'
        NrD   Fr6   zSCompressed items cannot be memmapped in a filesystem store. Option will be ignored.é   )Ú
stacklevel)r#   r   r9   Úexistsr   ÚgetrD   ÚwarningsÚwarnr6   r,   )r   r#   r,   r-   r6   r   r   r   r.   †  s   

þ
z FileSystemStoreBackend.configurer…   )r/   r0   r1   r2   ÚstaticmethodÚopenr    r   r9   rŸ   r$   r   r'   r)   r(   r+   r.   r   r   r   r   r†   P  s    $r†   ) r2   r‘   r   Úos.pathr•   rW   rˆ   r¡   Úcollectionsr|   r   Úabcr   r   Ú_compatr   r   Ú	backportsr   Údiskr   r	   r
   Ú r   Ú
namedtupler   r   r   Úobjectr4   r†   r   r   r   r   Ú<module>   s0    ÿ
i F