o
    ^i10                     @   s   d Z ddl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_checkpointsTzThe 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   k/home/ubuntu/hpml_nyu/venv/lib/python3.10/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_checkpoint5   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   sr   |  ||}|  ||}tj|r7| jd|| |   t|| W d   dS 1 s0w   Y  dS 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_pathold_cp_pathnew_cp_pathr   r   r   rename_checkpointD   s   
"z!FileCheckpoints.rename_checkpointc                 C   st   | d}| ||}tj|s| || | jd| |   t	| W d   dS 1 s3w   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_checkpointQ   s   

"z!FileCheckpoints.delete_checkpointc                 C   s8   | d}d}| ||}tj|sg S | ||gS )~list the checkpoints for a given file

        This contents manager currently only supports one checkpoint per file.
        r:   r   r;   r   r   r#   r-   r    r   r#   r$   os_pathr   r   r   list_checkpoints\   s   
z FileCheckpoints.list_checkpointsc                 C   s   | d}d| dd\}}| d}tj|\}}| d| | }| j|d}tj|| j}	ttj	|	tj
sQtjj|| jd}
tjt d|
}	|   t|	 W d   n1 sdw   Y  tj|	|}|S )zfind the path to a checkpointr:   r   -)r#   )startjupyter_checkpointsN)r;   rsplitr   r#   splitextr   joincheckpoint_diraccessdirnameW_OKrelpathr   tempfile
gettempdirr0   r   )r   r$   r#   r   namebasenameextfilenamerD   cp_dirrelr?   r   r   r   r   j   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$   rD   statsr\   infor   r   r   r    |   s   
z FileCheckpoints.checkpoint_modelc                 C   s   t dd| d| )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   rL   r   r   r'   r+   r8   r@   rE   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 )rY   NrZ   )r   r   r]   r^   r_   r`   ra   r   r   r   r       s   z%AsyncFileCheckpoints.checkpoint_modelc                    s|   |  ||}|  ||}tj|r<| jd|| |   ttj	||I dH  W d   dS 1 s5w   Y  dS dS r,   )
r   r   r#   r-   r.   r/   r0   r   r1   r2   r3   r   r   r   r8      s   
"z&AsyncFileCheckpoints.rename_checkpointc                    s~   | d}| ||}tj|s| || | jd| |   t	tj
|I dH  W d   dS 1 s8w   Y  dS r9   )r;   r   r   r#   r-   r<   r.   r/   r0   r   r=   r>   r   r   r   r@      s   

"z&AsyncFileCheckpoints.delete_checkpointc                    s@   | d}d}| ||}tj|sg S | ||I dH gS )rA   r:   r   NrB   rC   r   r   r   rE      s   
z%AsyncFileCheckpoints.list_checkpointsN)	re   rf   rg   r'   r+   r    r8   r@   rE   r   r   r   r   ri      s    ri   c                   @   0   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 s+w   Y  | ||S z7Create a checkpoint from the current content of a file.r:   r   creating checkpoint for %sformatNr;   r   r.   r/   r0   
_save_filer    r   contentro   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 s)w   Y  | ||S z;Create a checkpoint from the current content of a notebook.r:   r   rm   Nr;   r   r.   r/   r0   _save_notebookr    r   nbr#   r$   rt   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.r:   restoring %s from checkpoint %snotebook   
as_versiontypers   	r;   r.   rc   r   r   r#   r-   r<   _read_notebookr   r$   r#   rt   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.r:   r}   Nrn   file)r   rs   ro   	r;   r.   rc   r   r   r#   r-   r<   
_read_filer   r$   r#   rt   rs   ro   r   r   r   get_file_checkpoint   s   
z*GenericFileCheckpoints.get_file_checkpointNre   rf   rg   rh   ru   r{   r   r   r   r   r   r   rk          rk   c                   @   rj   )AsyncGenericFileCheckpointszg
    Asynchronous Local filesystem Checkpoints that works with any conforming
    ContentsManager.
    c                    sz   | d}d}| ||}| jd| |   | j|||dI dH  W d   n1 s/w   Y  | ||I dH S rl   rp   rr   r   r   r   ru     s   

z2AsyncGenericFileCheckpoints.create_file_checkpointc                    sv   | d}d}| ||}| jd| |   | ||I dH  W d   n1 s-w   Y  | ||I dH S rv   rw   ry   r   r   r   r{      s   

z6AsyncGenericFileCheckpoints.create_notebook_checkpointc                    sZ   | d}| jd|| | ||}tj|s | || d| j|ddI dH dS )r|   r:   r}   r~   r   r   Nr   r   r   r   r   r   r   -  s   

z3AsyncGenericFileCheckpoints.get_notebook_checkpointc                    sd   | d}| jd|| | ||}tj|s | || | j|ddI dH \}}d||dS r   r   r   r   r   r   r   >  s   
z/AsyncGenericFileCheckpoints.get_file_checkpointNr   r   r   r   r   r     r   r   )rh   r   r1   rQ   anyio.to_threadr   jupyter_core.utilsr   tornado.webr   	traitletsr   jupyter_serverr   r^   checkpointsr	   r
   r   r   fileior   r   r   ri   rk   r   r   r   r   r   <module>   s    r@B