a
    ø£Beå.  ã                   @   sÆ   d Z ddlZddlZddlmZ ddl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mZ dd
lmZmZ G dd„ deeƒZG dd„ deeeƒZG dd„ deeƒZG dd„ deeƒZdS )z)
File-based Checkpoints implementations.
é    N)Úrun_sync)Úensure_dir_exists©Ú	HTTPError)ÚUnicode)Ú_tzé   )ÚAsyncCheckpointsÚAsyncGenericCheckpointsMixinÚCheckpointsÚGenericCheckpointsMixin)ÚAsyncFileManagerMixinÚFileManagerMixinc                   @   sp   e Zd ZdZedddd�Ze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„ Zdd„ ZdS )ÚFileCheckpointszá
    A Checkpoints that caches checkpoints for files in adjacent
    directories.

    Only works with FileContentsManager.  Use GenericFileCheckpoints if
    you want file-based checkpoints with another ContentsManager.
    z.ipynb_checkpointsTz©The directory name in which to keep file checkpoints

        This is a path relative to the file's own directory.

        By default, it is .ipynb_checkpoints
        )ÚconfigÚhelp)r   c                 C   s   | j st ¡ S | j jS )N)ÚparentÚosÚgetcwdÚroot_dir)Úself© r   ús/home/yuhw/wc/wire-cell-python/wcpy/lib/python3.9/site-packages/jupyter_server/services/contents/filecheckpoints.pyÚ_root_dir_default-   s    z!FileCheckpoints._root_dir_defaultc                 C   s2   d}|  |¡}|  ||¡}|  ||¡ |  ||¡S )úCreate a checkpoint.Ú
checkpoint©Ú_get_os_pathÚcheckpoint_pathÚ_copyÚcheckpoint_model©r   Úcontents_mgrÚpathÚcheckpoint_idÚsrc_pathÚ	dest_pathr   r   r   Úcreate_checkpoint3   s
    
z!FileCheckpoints.create_checkpointc                 C   s&   |   ||¡}| |¡}|  ||¡ dS ©zRestore a checkpoint.N©r   r   r   ©r   r"   r$   r#   r%   r&   r   r   r   Úrestore_checkpoint;   s    
z"FileCheckpoints.restore_checkpointc                 C   sl   |   ||¡}|   ||¡}tj |¡rh| j d||¡ |  ¡ � t ||¡ W d  ƒ n1 s^0    Y  dS ©z.Rename a checkpoint from old_path to new_path.zRenaming checkpoint %s -> %sN)	r   r   r#   ÚisfileÚlogÚdebugÚperm_to_403ÚshutilÚmove©r   r$   Úold_pathÚnew_pathZold_cp_pathZnew_cp_pathr   r   r   Úrename_checkpointB   s    ý
z!FileCheckpoints.rename_checkpointc                 C   sr   |  d¡}|  ||¡}tj |¡s.|  ||¡ | j d|¡ |  ¡ � t 	|¡ W d  ƒ n1 sd0    Y  dS ©zdelete a file's checkpointú/zunlinking %sN)
Ústripr   r   r#   r-   Úno_such_checkpointr.   r/   r0   Úunlink©r   r$   r#   Úcp_pathr   r   r   Údelete_checkpointO   s    

z!FileCheckpoints.delete_checkpointc                 C   s<   |  d¡}d}|  ||¡}tj |¡s*g S |  ||¡gS dS ©z~list the checkpoints for a given file

        This contents manager currently only supports one checkpoint per file.
        r8   r   N©r9   r   r   r#   r-   r    ©r   r#   r$   Úos_pathr   r   r   Úlist_checkpointsZ   s    
z FileCheckpoints.list_checkpointsc                 C   s¨   |  d¡}d|  dd¡\}}|  d¡}tj |¡\}}|› d|› |› �}| j|d�}tj || j¡}	|  ¡ � t	|	ƒ W d  ƒ n1 sŒ0    Y  tj |	|¡}
|
S )zfind the path to a checkpointr8   r   ú-)r#   N)
r9   Úrsplitr   r#   Úsplitextr   ÚjoinÚcheckpoint_dirr0   r   )r   r$   r#   r   ÚnameÚbasenameÚextÚfilenamerB   Zcp_dirr=   r   r   r   r   h   s    


&zFileCheckpoints.checkpoint_pathc                 C   s$   t  |¡}t |j¡}||dœ}|S )ú.construct the info dict for a given checkpoint©ÚidÚlast_modified)r   ÚstatÚtzÚutcfromtimestampÚst_mtime©r   r$   rB   ÚstatsrP   Úinfor   r   r   r    v   s    
þz FileCheckpoints.checkpoint_modelc                 C   s   t dd|› d|› �ƒ‚d S )Ni”  zCheckpoint does not exist: ú@r   )r   r#   r$   r   r   r   r:   �   s    z"FileCheckpoints.no_such_checkpointN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   rH   r   r   r'   r+   r6   r>   rC   r   r    r:   r   r   r   r   r      s    ý
r   c                   @   s<   e Zd Zdd„ Zdd„ Zdd„ Zdd„ Zd	d
„ Zdd„ ZdS )ÚAsyncFileCheckpointsc                 Ã   s>   d}|  |¡}|  ||¡}|  ||¡I dH  |  ||¡I dH S )r   r   Nr   r!   r   r   r   r'   †   s
    
z&AsyncFileCheckpoints.create_checkpointc                 Ã   s,   |   ||¡}| |¡}|  ||¡I dH  dS r(   r)   r*   r   r   r   r+   Ž   s    
z'AsyncFileCheckpoints.restore_checkpointc                 Ã   s,   t tj|ƒI dH }t |j¡}||dœ}|S )rM   NrN   )r   r   rQ   rR   rS   rT   rU   r   r   r   r    ”   s    þz%AsyncFileCheckpoints.checkpoint_modelc                 Ã   st   |   ||¡}|   ||¡}tj |¡rp| j d||¡ |  ¡ �$ ttj	||ƒI dH  W d  ƒ n1 sf0    Y  dS r,   )
r   r   r#   r-   r.   r/   r0   r   r1   r2   r3   r   r   r   r6   Ÿ   s    ý
z&AsyncFileCheckpoints.rename_checkpointc                 Ã   sz   |  d¡}|  ||¡}tj |¡s.|  ||¡ | j d|¡ |  ¡ �" t	tj
|ƒI dH  W d  ƒ n1 sl0    Y  dS r7   )r9   r   r   r#   r-   r:   r.   r/   r0   r   r;   r<   r   r   r   r>   ¬   s    

z&AsyncFileCheckpoints.delete_checkpointc                 Ã   sB   |  d¡}d}|  ||¡}tj |¡s*g S |  ||¡I dH gS dS r?   r@   rA   r   r   r   rC   ·   s    
z%AsyncFileCheckpoints.list_checkpointsN)	rY   rZ   r[   r'   r+   r    r6   r>   rC   r   r   r   r   r]   …   s   r]   c                   @   s0   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	„ Zd
S )ÚGenericFileCheckpointszZ
    Local filesystem Checkpoints that works with any conforming
    ContentsManager.
    c                 C   sl   |  d¡}d}|  ||¡}| j d|¡ |  ¡ �  | j|||d� W d  ƒ n1 sV0    Y  |  ||¡S ©z7Create a checkpoint from the current content of a file.r8   r   úcreating checkpoint for %s©ÚformatN©r9   r   r.   r/   r0   Z
_save_filer    ©r   Úcontentrb   r#   r$   Úos_checkpoint_pathr   r   r   Úcreate_file_checkpointË   s    

.z-GenericFileCheckpoints.create_file_checkpointc                 C   sh   |  d¡}d}|  ||¡}| j d|¡ |  ¡ � |  ||¡ W d  ƒ n1 sR0    Y  |  ||¡S ©z;Create a checkpoint from the current content of a notebook.r8   r   r`   N©r9   r   r.   r/   r0   Z_save_notebookr    ©r   Únbr#   r$   rf   r   r   r   Úcreate_notebook_checkpointØ   s    

*z1GenericFileCheckpoints.create_notebook_checkpointc                 C   sR   |  d¡}| j d||¡ |  ||¡}tj |¡s>|  ||¡ d| j|dd�dœS )ú Get a checkpoint for a notebook.r8   úrestoring %s from checkpoint %sÚnotebooké   ©Z
as_version©Útypere   ©	r9   r.   rW   r   r   r#   r-   r:   Z_read_notebook©r   r$   r#   rf   r   r   r   Úget_notebook_checkpointå   s    
þþz.GenericFileCheckpoints.get_notebook_checkpointc                 C   s\   |  d¡}| j d||¡ |  ||¡}tj |¡s>|  ||¡ | j|dd�\}}d||dœS ©zGet a checkpoint for a file.r8   rn   Nra   Úfile)rs   re   rb   ©	r9   r.   rW   r   r   r#   r-   r:   Z
_read_file©r   r$   r#   rf   re   rb   r   r   r   Úget_file_checkpointö   s    
ýz*GenericFileCheckpoints.get_file_checkpointN©rY   rZ   r[   r\   rg   rl   rv   r{   r   r   r   r   r^   Å   s
   r^   c                   @   s0   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	„ Zd
S )ÚAsyncGenericFileCheckpointszg
    Asynchronous Local filesystem Checkpoints that works with any conforming
    ContentsManager.
    c                 Ã   sx   |  d¡}d}|  ||¡}| j d|¡ |  ¡ �& | j|||d�I dH  W d  ƒ n1 s\0    Y  |  ||¡I dH S r_   rc   rd   r   r   r   rg     s    

4z2AsyncGenericFileCheckpoints.create_file_checkpointc                 Ã   st   |  d¡}d}|  ||¡}| j d|¡ |  ¡ �" |  ||¡I dH  W d  ƒ n1 sX0    Y  |  ||¡I dH S rh   ri   rj   r   r   r   rl     s    

0z6AsyncGenericFileCheckpoints.create_notebook_checkpointc                 Ã   sX   |  d¡}| j d||¡ |  ||¡}tj |¡s>|  ||¡ d| j|dd�I dH dœS )rm   r8   rn   ro   rp   rq   Nrr   rt   ru   r   r   r   rv   '  s    
þ
þz3AsyncGenericFileCheckpoints.get_notebook_checkpointc                 Ã   sb   |  d¡}| j d||¡ |  ||¡}tj |¡s>|  ||¡ | j|dd�I dH \}}d||dœS rw   ry   rz   r   r   r   r{   8  s    
ýz/AsyncGenericFileCheckpoints.get_file_checkpointNr|   r   r   r   r   r}     s
   r}   )r\   r   r1   Zanyio.to_threadr   Zjupyter_core.utilsr   Ztornado.webr   Z	traitletsr   Zjupyter_serverr   rR   Zcheckpointsr	   r
   r   r   Zfileior   r   r   r]   r^   r}   r   r   r   r   Ú<module>   s   n@B