o
    i_/                     @   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_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&   z| j jW S  ty   t  Y S w )N)parentroot_dirAttributeErrorosgetcwd)self r   k/var/www/edux/Edux_v2/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_checkpoint4   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_checkpointC   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/   r0   r1   unlinkr   r%   r$   cp_pathr   r   r   delete_checkpointP   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}| j|d}tj|| j}	| 	  t
|	 W d   n1 sEw   Y  tj|	|}
|
S )zfind the path to a checkpointr;   r   z{name}-{checkpoint_id}{ext})namer%   ext)r$   N)r<   rsplitr   r$   splitextformatr   joincheckpoint_dirr1   r   )r   r%   r$   r   rG   basenamerH   filenamerE   cp_dirr@   r   r   r   r   i   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%   rE   statsrT   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   rM   r   r   r(   r,   r9   rA   rF   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 )rQ   NrR   )r   r   rU   rV   rW   rX   rY   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/   r0   r1   r   r2   r3   r4   r   r   r   r9      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 r:   )r<   r   r   r$   r.   r=   r/   r0   r1   r   r>   r?   r   r   r   rA      s   

"z&AsyncFileCheckpoints.delete_checkpointc                    s@   | d}d}| ||}tj|sg S | ||I dH gS )rB   r;   r   NrC   rD   r   r   r   rF      s   
z%AsyncFileCheckpoints.list_checkpointsN)	r]   r^   r_   r(   r,   r!   r9   rA   rF   r   r   r   r   ra      s    ra   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rK   Nr<   r   r/   r0   r1   
_save_filer!   r   contentrK   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   re   Nr<   r   r/   r0   r1   _save_notebookr!   r   nbr$   r%   rk   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typerj   	r<   r/   r[   r   r   r$   r.   r=   _read_notebookr   r%   r$   rk   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;   rt   Nrf   file)rz   rj   rK   	r<   r/   r[   r   r   r$   r.   r=   
_read_filer   r%   r$   rk   rj   rK   r   r   r   get_file_checkpoint   s   
z*GenericFileCheckpoints.get_file_checkpointNr]   r^   r_   r`   rl   rr   r~   r   r   r   r   r   rc          rc   c                   @   rb   )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 rd   rg   ri   r   r   r   rl     s   

z2AsyncGenericFileCheckpoints.create_file_checkpointc                    sv   | d}d}| ||}| jd| |   | ||I dH  W d   n1 s-w   Y  | ||I dH S rm   rn   rp   r   r   r   rr     s   

z6AsyncGenericFileCheckpoints.create_notebook_checkpointc                    sZ   | d}| jd|| | ||}tj|s | || d| j|ddI dH dS )rs   r;   rt   ru   rv   rw   Nry   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   )r`   r   r2   anyio.to_threadr   jupyter_core.utilsr   tornado.webr   	traitletsr   jupyter_serverr   rV   checkpointsr	   r
   r   r   fileior   r   r   ra   rc   r   r   r   r   r   <module>   s    s@B