
    Ib                         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e	e      Z G d dee      Zy)z)
File-based Checkpoints implementations.
    N	HTTPError   )CheckpointsGenericCheckpointsMixin)FileManagerMixin)ensure_dir_exists)getcwd)Unicode)_tzc                   n    e Zd ZdZ eddd      Z ed      Zd Zd Zd	 Z	d
 Z
d Zd Zd Zd Zd Zy)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                 b    	 | j                   j                  S # t        $ r t               cY S w xY w)N)parentroot_dirAttributeErrorr
   )selfs    L/usr/lib/python3/dist-packages/notebook/services/contents/filecheckpoints.py_root_dir_defaultz!FileCheckpoints._root_dir_default,   s-    	;;''' 	8O	s    ..c                     d}|j                  |      }| j                  ||      }| j                  ||       | j                  ||      S )zCreate a checkpoint.
checkpoint)_get_os_pathcheckpoint_path_copycheckpoint_model)r   contents_mgrpathcheckpoint_idsrc_path	dest_paths         r   create_checkpointz!FileCheckpoints.create_checkpoint3   sJ    $,,T2((=	

8Y'$$]I>>    c                 n    | j                  ||      }|j                  |      }| j                  ||       y)zRestore a checkpoint.N)r   r   r   )r   r   r    r   r!   r"   s         r   restore_checkpointz"FileCheckpoints.restore_checkpoint;   s2    ''t< --d3	

8Y'r$   c                 <   | j                  ||      }| j                  ||      }t        j                  j                  |      rM| j                  j                  d||       | j                         5  t        j                  ||       ddd       yy# 1 sw Y   yxY w)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_paths         r   rename_checkpointz!FileCheckpoints.rename_checkpointB   s    **=(C**=(C77>>+&HHNN.
 !!# 6K56 6 '6 6s   1BBc                 X   |j                  d      }| j                  ||      }t        j                  j	                  |      s| j                  ||       | j                  j                  d|       | j                         5  t        j                  |       ddd       y# 1 sw Y   yxY w)zdelete a file's checkpoint/zunlinking %sN)
stripr   r(   r   r)   no_such_checkpointr*   r+   r,   unlink)r   r    r   cp_paths       r   delete_checkpointz!FileCheckpoints.delete_checkpointO   s    zz#&&}d;ww~~g&##D-8~w/ 	IIg	 	 	s   B  B)c                     |j                  d      }d}| j                  ||      }t        j                  j	                  |      sg S | j                  ||      gS )z~list the checkpoints for a given file

        This contents manager currently only supports one checkpoint per file.
        r5   r   )r6   r   r(   r   r)   r   )r   r   r    os_paths       r   list_checkpointsz FileCheckpoints.list_checkpointsZ   sS    
 zz#$&&}d;ww~~g&I))-ABBr$   c                    |j                  d      }d|z   j                  dd      \  }}|j                  d      }t        j                  j	                  |      \  }}| d| | }| j                  |      }t        j                  j                  || j                        }	| j                         5  t        |	       ddd       t        j                  j                  |	|      }
|
S # 1 sw Y   +xY w)zfind the path to a checkpointr5   r   -)r   N)
r6   rsplitr(   r   splitextr   joincheckpoint_dirr,   r	   )r   r    r   r   namebasenameextfilenamer<   cp_dirr9   s              r   r   zFileCheckpoints.checkpoint_pathh   s    zz#d
**32c"((.#Zqu5###0gt':':; 	&f%	&'',,vx0	& 	&s   2C((C1c                     t        j                  |      }t        j                  |j                        }t        ||      }|S )z.construct the info dict for a given checkpoint)idlast_modified)r(   stattzutcfromtimestampst_mtimedict)r   r    r<   statsrK   infos         r   r   z FileCheckpoints.checkpoint_modelv   s;     ++ENN;'
 r$   c                 &    t        dd| d|       )Ni  zCheckpoint does not exist: @r   )r   r   r    s      r   r7   z"FileCheckpoints.no_such_checkpoint   s"    )$q@
 	
r$   N)__name__
__module____qualname____doc__r   rC   r   r   r#   r&   r3   r:   r=   r   r   r7    r$   r   r   r      sX     	N d#H?(6	C
r$   r   c                   (    e Zd ZdZd Zd Zd Zd Zy)GenericFileCheckpointszZ
    Local filesystem Checkpoints that works with any conforming
    ContentsManager.
    c                    |j                  d      }d}| j                  ||      }| j                  j                  d|       | j	                         5  | j                  |||       ddd       | j                  ||      S # 1 sw Y   xY w)z7Create a checkpoint from the current content of a file.r5   r   creating checkpoint for %sformatN)r6   r   r*   r+   r,   
_save_filer   )r   contentr_   r   r    os_checkpoint_paths         r   create_file_checkpointz-GenericFileCheckpoints.create_file_checkpoint   s    zz#$!11-F3T: 	HOO.OG	H $$]4FGG		H 	Hs   BB
c                    |j                  d      }d}| j                  ||      }| j                  j                  d|       | j	                         5  | j                  ||       ddd       | j                  ||      S # 1 sw Y   xY w)z;Create a checkpoint from the current content of a notebook.r5   r   r]   N)r6   r   r*   r+   r,   _save_notebookr   )r   nbr   r    rb   s        r   create_notebook_checkpointz1GenericFileCheckpoints.create_notebook_checkpoint   s    zz#$!11-F3T: 	8 2B7	8 $$]4FGG		8 	8s   A??Bc                    |j                  d      }| j                  j                  d||       | j                  ||      }t        j
                  j                  |      s| j                  ||       d| j                  |d      dS )z Get a checkpoint for a notebook.r5   restoring %s from checkpoint %snotebook   )
as_version)typera   )	r6   r*   rR   r   r(   r   r)   r7   _read_notebook)r   r    r   rb   s       r   get_notebook_checkpointz.GenericFileCheckpoints.get_notebook_checkpoint   s    zz#7}M!11-Fww~~01##D-8 **" + 
 	
r$   c                    |j                  d      }| j                  j                  d||       | j                  ||      }t        j
                  j                  |      s| j                  ||       | j                  |d      \  }}d||dS )zGet a checkpoint for a file.r5   ri   Nr^   file)rm   ra   r_   )	r6   r*   rR   r   r(   r   r)   r7   
_read_file)r   r    r   rb   ra   r_   s         r   get_file_checkpointz*GenericFileCheckpoints.get_file_checkpoint   s    zz#7}M!11-Fww~~01##D-8//*<T/J
 	
r$   N)rU   rV   rW   rX   rc   rg   ro   rs   rY   r$   r   r[   r[      s    HH
"
r$   r[   )rX   r(   r-   tornado.webr   checkpointsr   r   fileior   jupyter_core.utilsr	   ipython_genutils.py3compatr
   	traitletsr   rj   r   rM   r   r[   rY   r$   r   <module>rz      sL    
  ! % 0 -  o
& o
d>
4o >
r$   