a
    m b®/  ã                   @   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 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 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é   )ÚAsyncCheckpoints)ÚAsyncGenericCheckpointsMixin)ÚCheckpoints)ÚGenericCheckpointsMixin)ÚAsyncFileManagerMixin)ÚFileManagerMixin)Ú_tzc                   @   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*   z
| j jW S  ty$   t ¡  Y S 0 d S )N)ÚparentÚroot_dirÚAttributeErrorÚosÚgetcwd)Úself© r   úOlib/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_checkpoint2   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%   Zold_pathÚnew_pathZold_cp_pathZnew_cp_pathr   r   r   Úrename_checkpointA   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/   r0   r1   Úunlink©r   r%   r$   Úcp_pathr   r   r   Údelete_checkpointN   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_checkpointsY   s    
z FileCheckpoints.list_checkpointsc                 C   s¦   |  d¡}d|  dd¡\}}|  d¡}tj |¡\}}d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   z{name}-{checkpoint_id}{ext})Únamer%   Úext)r$   N)r9   Úrsplitr   r$   ÚsplitextÚformatr   ÚjoinÚcheckpoint_dirr1   r   )r   r%   r$   r   rD   ÚbasenamerE   ÚfilenamerB   Zcp_dirr=   r   r   r   r   g   s    

ý
&zFileCheckpoints.checkpoint_pathc                 C   s&   t  |¡}t |j¡}t||d}|S )ú.construct the info dict for a given checkpoint©ÚidÚlast_modified)r   ÚstatÚtzÚutcfromtimestampÚst_mtimeÚdict©r   r%   rB   ZstatsrP   Úinfor   r   r   r!   y   s    
þz FileCheckpoints.checkpoint_modelc                 C   s   t dd||f ƒ‚d S )Ni”  z Checkpoint does not exist: %s@%sr   )r   r$   r%   r   r   r   r:   „   s    z"FileCheckpoints.no_such_checkpointN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   rJ   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¡}t||d}|S )rM   NrN   )r   r   rQ   rR   rS   rT   rU   rV   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/   r0   r1   r   r2   r3   r4   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/   r0   r1   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)	rX   rY   rZ   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©rH   N©r9   r   r/   r0   r1   Z
_save_filer!   ©r   ÚcontentrH   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/   r0   r1   Z_save_notebookr!   ©r   Únbr$   r%   rd   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©Útyperc   ©	r9   r/   rW   r   r   r$   r.   r:   Z_read_notebook©r   r%   r$   rd   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   rl   Nr`   Úfile)rq   rc   rH   ©	r9   r/   rW   r   r   r$   r.   r:   Z
_read_file©r   r%   r$   rd   rc   rH   r   r   r   Úget_file_checkpointù   s    
ýz*GenericFileCheckpoints.get_file_checkpointN©rX   rY   rZ   r[   re   rj   rt   ry   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^   ra   rb   r   r   r   re     s    

4z2AsyncGenericFileCheckpoints.create_file_checkpointc                 Ã   st   |  d¡}d}|  ||¡}| j d|¡ |  ¡ " |  ||¡I dH  W d  ƒ n1 sX0    Y  |  ||¡I dH S rf   rg   rh   r   r   r   rj     s    

0z6AsyncGenericFileCheckpoints.create_notebook_checkpointc                 Ã   sX   |  d¡}| j d||¡ |  ||¡}tj |¡s>|  ||¡ d| j|ddI dH dœS )rk   r8   rl   rm   rn   ro   Nrp   rr   rs   r   r   r   rt   *  s    
þ
þz3AsyncGenericFileCheckpoints.get_notebook_checkpointc                 Ã   sb   |  d¡}| j d||¡ |  ||¡}tj |¡s>|  ||¡ | j|ddI dH \}}d||dœS ru   rw   rx   r   r   r   ry   ;  s    
ýz/AsyncGenericFileCheckpoints.get_file_checkpointNrz   r   r   r   r   r{   
  s
   r{   )r[   r   r2   Zanyio.to_threadr   Zjupyter_core.utilsr   Ztornado.webr   Z	traitletsr   Zcheckpointsr   r	   r
   r   Zfileior   r   Zjupyter_serverr   rR   r   r\   r]   r{   r   r   r   r   Ú<module>   s"   s@B