o
    i                     @   s   d Z ddlZddlZddlm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 dd
lmZ G dd de	eZG dd deeZdS )z)
File-based Checkpoints implementations.
    N	HTTPError   )CheckpointsGenericCheckpointsMixin)FileManagerMixin)ensure_dir_exists)getcwd)Unicode)_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_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AttributeErrorr	   )self r   e/var/www/edux/Edux_v2/venv/lib/python3.10/site-packages/notebook/services/contents/filecheckpoints.py_root_dir_default,   s
   

z!FileCheckpoints._root_dir_defaultc                 C   s2   d}| |}| ||}| || | ||S )z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   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   osr   isfilelogdebugperm_to_403shutilmove)r   r   old_pathnew_pathold_cp_pathnew_cp_pathr   r   r   rename_checkpointB   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%   r&   unlink)r   r   r   cp_pathr   r   r   delete_checkpointO   s   

"z!FileCheckpoints.delete_checkpointc                 C   s8   | d}d}| ||}tj|sg S | ||gS )z~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_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 sFw   Y  tj|	|}
|
S )zfind the path to a checkpointr.   r   -)r   N)
r/   rsplitr"   r   splitextr   joincheckpoint_dirr&   r   )r   r   r   r   namebasenameextfilenamer4   cp_dirr2   r   r   r   r   h   s   



zFileCheckpoints.checkpoint_pathc                 C   s&   t |}t|j}t||d}|S )z.construct the info dict for a given checkpoint)idlast_modified)r"   stattzutcfromtimestampst_mtimedict)r   r   r4   statsrA   infor   r   r   r   v   s   
z FileCheckpoints.checkpoint_modelc                 C   s   t dd| d| )Ni  zCheckpoint does not exist: @r   )r   r   r   r   r   r   r0      s   z"FileCheckpoints.no_such_checkpointN)__name__
__module____qualname____doc__r
   r:   r   r   r    r!   r-   r3   r5   r   r   r0   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 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%   r&   
_save_filer   )r   contentrQ   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   rO   N)r/   r   r$   r%   r&   _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 )z Get a checkpoint for a notebook.r.   restoring %s from checkpoint %snotebook   )
as_version)typerS   )	r/   r$   rH   r   r"   r   r#   r0   _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.   rY   NrP   file)r]   rS   rQ   )	r/   r$   rH   r   r"   r   r#   r0   
_read_file)r   r   r   rT   rS   rQ   r   r   r   get_file_checkpoint   s   
z*GenericFileCheckpoints.get_file_checkpointN)rJ   rK   rL   rM   rU   rX   r_   rb   r   r   r   r   rN      s    rN   )rM   r"   r'   tornado.webr   checkpointsr   r   fileior   jupyter_core.utilsr   ipython_genutils.py3compatr	   	traitletsr
   rZ   r   rC   r   rN   r   r   r   r   <module>   s    r