o
    iE                     @   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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d
dZG dd deZdd Z G dd ded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)PicklingError   )numpy_pickle)concurrency_safe_rename)memstr_to_bytesmkdirp
rm_subdirs)format_timeCacheItemInfozpath size last_accessc                   @   s   e Zd ZdZdS )CacheWarningz:Warning to capture dump failures except for PicklingError.N)__name__
__module____qualname____doc__ r   r   e/var/www/html/eduruby.in/lip-sync/lip-sync-env/lib/python3.10/site-packages/joblib/_store_backends.pyr      s    r   c                 C   s>   t t }| dt j dt  d| }|| | |S )z>Writes an object into a unique file in a concurrency-safe way..-)id	threadingcurrent_threaduuiduuid4hexosgetpid)Zobject_to_writefilename
write_func	thread_idtemporary_filenamer   r   r   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 )StoreBackendBasez^Helper 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_item5       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_existsH   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_itemY   r)   zStoreBackendBase._move_itemc                 C   r$   )zCreates 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_locationg   r)   z StoreBackendBase.create_locationc                 C   r$   )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*   r   r   r   clear_locationr   r)   zStoreBackendBase.clear_locationc                 C   r$   )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%   r   r   r   	get_items}   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 dictionary of named parameters used to configure the
            store backend.
        Nr   )r%   r+   verbosebackend_optionsr   r   r   	configure   r)   zStoreBackendBase.configure)r   r   r   r   r+   r   r(   r,   r/   r0   r1   r3   dictr6   r   r   r   r   r#   /   s"    







	r#   )	metaclassc                   @   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dZd)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   sL  t jj| jg|R  }|dkr_|durdtt | nd}t j|d }|durEd|v rEddd	 |d  D }|d
|7 }d||}	|dk rWt	d|	 nt	d|	| t
| dsfdn| j}
t j|d}| |s{td| |
du r| |d}t|}W d   |S 1 sw   Y  |S tj||
d}|S )z:Load an item from the store given its id as a list of str.r   Nz{: <16} r   Z
input_argsz, c                 s   s    | ]}d j | V  qdS )z{}={}N)format.0itemr   r   r   	<genexpr>   s    

z.StoreBackendMixin.load_item.<locals>.<genexpr>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)rA   )r   pathjoinr+   r;   r   timebasenameitemsprinthasattrrA   r,   KeyErrorr(   r   load)r%   call_idr4   	timestampmetadata	full_pathZ	ts_string	signaturekwargsmsgrA   r   r&   r>   r   r   r   	load_item   sB   



zStoreBackendMixin.load_itemc              
      s   z6t jj jg|R  } |s | t j|d}|dkr'td|   fdd} ||| W dS  tyS } zt	
d| dt W Y d}~dS d}~ww )	z;Dump an item in the store at the id given as a list of str.rB   r@   zPersisting in %sc                    s     |d9}ztj| | jd W n ty. } ztd| dt W Y d }~nd }~ww W d    d S W d    d S 1 sBw   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(   r   dumprV   r   warningswarnFutureWarning)to_writedest_filenamer&   er2   r   r   r      s    "z/StoreBackendMixin.dump_item.<locals>.write_funcz`Unable to cache to disk. Possibly a race condition in the creation of the directory. Exception: r   N)r   rD   rE   r+   r,   r0   rI   _concurrency_safe_write	ExceptionrX   rY   r   )r%   rM   r>   r4   	item_pathr   r   r]   r   r2   r   	dump_item   s$   

zStoreBackendMixin.dump_itemc                 C   2   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   rD   rE   r+   r,   r1   )r%   rM   r`   r   r   r   
clear_item      
zStoreBackendMixin.clear_itemc                 C   s.   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.rB   )r   rD   rE   r+   r,   )r%   rM   r`   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   rD   rE   r+   r%   rM   r   r   r   get_item_info      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.jsonrC   utf-8N)	r   rD   rE   r+   r(   jsonloadsreaddecode)r%   rM   r`   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.rl   c                    sH     |d}|t| d W d    d S 1 sw   Y  d S )NrU   rm   )r(   writern   dumpsencode)r[   r\   r&   r2   r   r   r   
  s   "z4StoreBackendMixin.store_metadata.<locals>.write_funcN)r   rD   rE   r+   r0   r^   )r%   rM   rO   r`   r   r   r   r2   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   rD   rE   r+   Zobject_existsr%   rM   	func_pathr   r   r   contains_path  s   
zStoreBackendMixin.contains_pathc                 C   rb   )z0Clear all items with a common path in the store.Nrc   rw   r   r   r   
clear_path  re   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.pyrU   rm   )	r   rD   rE   r+   r,   r0   r(   rs   ru   )r%   rM   	func_coderx   r   r&   r   r   r   store_cached_func_code  s   

"z(StoreBackendMixin.store_cached_func_codec                 C   sf   t jj| jg|dR  }z!| |d}| dW  d   W S 1 s'w   Y  W dS     )r{   r|   rC   rm   N)r   rD   rE   r+   r(   rp   rq   )r%   rM   r   r&   r   r   r   get_cached_func_code(  s   (z&StoreBackendMixin.get_cached_func_codec                 C   rg   )z?Return information related to the cached function if it exists.r+   rh   ri   r   r   r   get_cached_func_info1  rk   z&StoreBackendMixin.get_cached_func_infoc                 C   s   |  | j dS )zClear the whole store content.N)r1   r+   r2   r   r   r   clear5  s   zStoreBackendMixin.clearc              	   C   sV   |  |||}|D ]}| jdkrtd| z| |j W q	 ty(   Y q	w dS )zX
        Remove the store's oldest files to enforce item, byte, and age limits.
        r@   zDeleting item {0}N)_get_items_to_deleter4   rI   r;   r1   rD   OSError)r%   bytes_limititems_limit	age_limititems_to_deleter>   r   r   r   enforce_store_limits9  s   
z&StoreBackendMixin.enforce_store_limitsc                 C   s:  t |tr	t|}|  }|sg S tdd |D }|dur#|| }nd}|dur0t|| }nd}|durQtdd |D }| dk rItdt	j	
 | }	nd}	|dkre|dkre|	du sc||	kreg S |jtdd g }
d}d}|D ]$}||kr||kr|	du s|	|jk r |
S |
| ||j7 }|d	7 }qv|
S )
zW
        Get items to delete to keep the store under size, file, & age limits.
        c                 s       | ]}|j V  qd S N)sizer<   r   r   r   r?   V      z9StoreBackendMixin._get_items_to_delete.<locals>.<genexpr>Nr   c                 s   r   r   )last_accessr<   r   r   r   r?   c  r   z(age_limit has to be a positive timedeltar   )keyr   )
isinstancestrr   r3   sumlenmintotal_seconds
ValueErrordatetimenowsortoperator
attrgetterr   appendr   )r%   r   r   r   rH   r   Zto_delete_sizeZto_delete_itemsZ
older_itemdeadliner   Zsize_so_farZitems_so_farr>   r   r   r   r   K  sH   




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%   r[   r   r   r!   r   r   r   r^     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+   r2   r   r   r   __repr__  s   
zStoreBackendMixin.__repr__)r   NN)r   r   )NN)r   r   r   r   rT   ra   rd   rf   rj   rr   rv   ry   rz   r~   r   r   r   r   r   r^   r   r   r   r   r   r9      s&    
	
'

	

<r9   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   r1     s   
z%FileSystemStoreBackend.clear_locationc                 C   s   t | dS )zCreate object location on storeN)r	   r*   r   r   r   r0     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}rB   c                    s   g | ]	}t j |qS r   )r   rD   rE   r=   fndirpathr   r   
<listcomp>  s    z4FileSystemStoreBackend.get_items.<locals>.<listcomp>c                 s   s    | ]	}t j|V  qd S r   )r   rD   getsizer   r   r   r   r?     s    z3FileSystemStoreBackend.get_items.<locals>.<genexpr>)r   walkr+   rematchrD   rG   rE   getatimer   r   fromtimestampr   r   r   )	r%   rH   _	filenamesZis_cache_hash_dirZoutput_filenamer   Zfull_filenamesdirsizer   r   r   r3     s2   z FileSystemStoreBackend.get_itemsr   Nc                 C   s   |du ri }|| _ tj| j st| j  tj|r)tj|dkr)tj|n|}ttj|dd}|	d |	d W d   n1 sJw   Y  |
dd| _|
d	}| jri|duritjd
dd || _|| _dS )zsConfigure the store backend.

        For this backend, valid store options are 'compress' and 'mmap_mode'
        NZjoblibz
.gitignorewz## Created by joblib automatically.
z*
rV   FrA   zSCompressed items cannot be memmapped in a filesystem store. Option will be ignored.   )
stacklevel)r+   r   rD   existsr	   dirnamerG   openrE   rs   getrV   rX   rY   rA   r4   )r%   r+   r4   r5   Zcache_directoryfilerA   r   r   r   r6     s0   





z FileSystemStoreBackend.configure)r   N)r   r   r   r   staticmethodr   r(   r   rD   r   r,   r   r/   r1   r0   r3   r6   r   r   r   r   r     s     r   )%r   collectionsr   rn   r   r   Zos.pathr   r   r   rF   r   rX   abcr   r   pickler   r:   r   Z	backportsr   Zdiskr   r	   r
   loggerr   
namedtupler   Warningr   r"   r#   objectr9   r   r   r   r   r   <module>   s4    i |