B
    3¢\"  ã               @   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_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$   y| j jS  tk
r   tƒ S X d S )N)ÚparentÚroot_dirÚAttributeErrorr   )Úself© r   úIlib/python3.7/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   sX   |   ||¡}|   ||¡}tj |¡rT| j d||¡ |  ¡  t ||¡ W dQ R X 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ÚshutilZmove)r   r   Zold_pathÚnew_pathZold_cp_pathZnew_cp_pathr   r   r   Úrename_checkpointB   s    
z!FileCheckpoints.rename_checkpointc          	   C   s^   |  d¡}|  ||¡}tj |¡s.|  ||¡ | j d|¡ |  ¡  t 	|¡ W dQ R X 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   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.
        r)   r   N)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}| j|d}tj || j¡}	|  	¡  t
|	ƒ W dQ R X 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_dirr%   r   )r   r   r   r   r1   Úbasenamer2   Úfilenamer/   Zcp_dirr-   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ÚtzZutcfromtimestampÚst_mtimeÚdict)r   r   r/   Zstatsr;   Úinfor   r   r   r   z   s    
z FileCheckpoints.checkpoint_modelc             C   s   t dd||f ƒ‚d S )Ni”  z Checkpoint does not exist: %s@%s)r   )r   r   r   r   r   r   r+   …   s    z"FileCheckpoints.no_such_checkpointN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r	   r7   r   r   r   r    r(   r.   r0   r   r   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 )ÚGenericFileCheckpointszZ
    Local filesystem Checkpoints that works with any conforming
    ContentsManager.
    c          	   C   sX   |  d¡}d}|  ||¡}| j d|¡ |  ¡  | j|||d W dQ R X |  ||¡S )z7Create a checkpoint from the current content of a file.r)   r   zcreating checkpoint for %s)r5   N)r*   r   r#   r$   r%   Z
_save_filer   )r   Úcontentr5   r   r   Úos_checkpoint_pathr   r   r   Úcreate_file_checkpoint‘   s    

z-GenericFileCheckpoints.create_file_checkpointc          	   C   sT   |  d¡}d}|  ||¡}| j d|¡ |  ¡  |  ||¡ W dQ R X |  ||¡S )z;Create a checkpoint from the current content of a notebook.r)   r   zcreating checkpoint for %sN)r*   r   r#   r$   r%   Z_save_notebookr   )r   Znbr   r   rG   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)   zrestoring %s from checkpoint %sÚnotebooké   )Z
as_version)ÚtyperF   )	r*   r#   r@   r   r!   r   r"   r+   Z_read_notebook)r   r   r   rG   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)   zrestoring %s from checkpoint %sN)r5   Úfile)rL   rF   r5   )	r*   r#   r@   r   r!   r   r"   r+   Z
_read_file)r   r   r   rG   rF   r5   r   r   r   Úget_file_checkpoint¼   s    
z*GenericFileCheckpoints.get_file_checkpointN)rA   rB   rC   rD   rH   rI   rM   rO   r   r   r   r   rE   Œ   s
   rE   )rD   r!   r&   Ztornado.webr   Zcheckpointsr   r   Zfileior   Zjupyter_core.utilsr   Zipython_genutils.py3compatr   Z	traitletsr	   rJ   r
   r=   r   rE   r   r   r   r   Ú<module>   s   v