ó
ù2¢\c           @   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 d e	 e f d „  ƒ  YZ d e e f d „  ƒ  YZ d S(   s)   
File-based Checkpoints implementations.
iÿÿÿÿN(   t	   HTTPErrori   (   t   Checkpointst   GenericCheckpointsMixin(   t   FileManagerMixin(   t   ensure_dir_exists(   t   getcwd(   t   Unicode(   t   _tzt   FileCheckpointsc           B   s†   e  Z d  Z e d d e d d ƒZ e d e ƒ Z d „  Z d „  Z d „  Z	 d „  Z
 d	 „  Z d
 „  Z d „  Z d „  Z d „  Z RS(   sá   
    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.
    s   .ipynb_checkpointst   configt   helps©   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
        c         C   s*   y |  j  j SWn t k
 r% t ƒ  SXd  S(   N(   t   parentt   root_dirt   AttributeErrorR   (   t   self(    (    sI   lib/python2.7/site-packages/notebook/services/contents/filecheckpoints.pyt   _root_dir_default,   s    c         C   sG   d } | j  | ƒ } |  j | | ƒ } |  j | | ƒ |  j | | ƒ S(   s   Create a checkpoint.u
   checkpoint(   t   _get_os_patht   checkpoint_patht   _copyt   checkpoint_model(   R   t   contents_mgrt   patht   checkpoint_idt   src_patht	   dest_path(    (    sI   lib/python2.7/site-packages/notebook/services/contents/filecheckpoints.pyt   create_checkpoint3   s
    c         C   s5   |  j  | | ƒ } | j | ƒ } |  j | | ƒ d S(   s   Restore a checkpoint.N(   R   R   R   (   R   R   R   R   R   R   (    (    sI   lib/python2.7/site-packages/notebook/services/contents/filecheckpoints.pyt   restore_checkpoint;   s    c         C   sv   |  j  | | ƒ } |  j  | | ƒ } t j j | ƒ rr |  j j d | | ƒ |  j ƒ   t j | | ƒ Wd QXn  d S(   s.   Rename a checkpoint from old_path to new_path.s   Renaming checkpoint %s -> %sN(	   R   t   osR   t   isfilet   logt   debugt   perm_to_403t   shutilt   move(   R   R   t   old_patht   new_patht   old_cp_patht   new_cp_path(    (    sI   lib/python2.7/site-packages/notebook/services/contents/filecheckpoints.pyt   rename_checkpointB   s    	c         C   s}   | j  d ƒ } |  j | | ƒ } t j j | ƒ sF |  j | | ƒ n  |  j j d | ƒ |  j ƒ   t j	 | ƒ Wd QXd S(   s   delete a file's checkpointt   /s   unlinking %sN(
   t   stripR   R   R   R   t   no_such_checkpointR   R   R   t   unlink(   R   R   R   t   cp_path(    (    sI   lib/python2.7/site-packages/notebook/services/contents/filecheckpoints.pyt   delete_checkpointO   s    c         C   sT   | j  d ƒ } d } |  j | | ƒ } t j j | ƒ s= g  S|  j | | ƒ g Sd S(   s~   list the checkpoints for a given file

        This contents manager currently only supports one checkpoint per file.
        R'   t
   checkpointN(   R(   R   R   R   R   R   (   R   R   R   t   os_path(    (    sI   lib/python2.7/site-packages/notebook/services/contents/filecheckpoints.pyt   list_checkpointsZ   s    c         C   sÐ   | j  d ƒ } d | j d d ƒ \ } } | j  d ƒ } t j j | ƒ \ } } d j d | d | d | ƒ } |  j d | ƒ } t j j | |  j ƒ }	 |  j	 ƒ   t
 |	 ƒ Wd QXt j j |	 | ƒ }
 |
 S(	   s   find the path to a checkpointR'   i   u   {name}-{checkpoint_id}{ext}t   nameR   t   extR   N(   R(   t   rsplitR   R   t   splitextt   formatR   t   joint   checkpoint_dirR   R   (   R   R   R   R   R0   t   basenameR1   t   filenameR.   t   cp_dirR+   (    (    sI   lib/python2.7/site-packages/notebook/services/contents/filecheckpoints.pyR   h   s    		c         C   s:   t  j | ƒ } t j | j ƒ } t d | d | ƒ } | S(   s.   construct the info dict for a given checkpointt   idt   last_modified(   R   t   statt   tzt   utcfromtimestampt   st_mtimet   dict(   R   R   R.   t   statsR;   t   info(    (    sI   lib/python2.7/site-packages/notebook/services/contents/filecheckpoints.pyR   z   s    	c         C   s   t  d d | | f ƒ ‚ d  S(   Ni”  u    Checkpoint does not exist: %s@%s(   R    (   R   R   R   (    (    sI   lib/python2.7/site-packages/notebook/services/contents/filecheckpoints.pyR)   …   s    (   t   __name__t
   __module__t   __doc__R   t   TrueR6   R   R   R   R   R&   R,   R/   R   R   R)   (    (    (    sI   lib/python2.7/site-packages/notebook/services/contents/filecheckpoints.pyR      s   									t   GenericFileCheckpointsc           B   s2   e  Z d  Z d „  Z d „  Z d „  Z d „  Z RS(   sZ   
    Local filesystem Checkpoints that works with any conforming
    ContentsManager.
    c      	   C   ss   | j  d ƒ } d } |  j | | ƒ } |  j j d | ƒ |  j ƒ   |  j | | d | ƒWd QX|  j | | ƒ S(   s7   Create a checkpoint from the current content of a file.R'   u
   checkpoints   creating checkpoint for %sR4   N(   R(   R   R   R   R   t
   _save_fileR   (   R   t   contentR4   R   R   t   os_checkpoint_path(    (    sI   lib/python2.7/site-packages/notebook/services/contents/filecheckpoints.pyt   create_file_checkpoint‘   s    c         C   sm   | j  d ƒ } d } |  j | | ƒ } |  j j d | ƒ |  j ƒ   |  j | | ƒ Wd QX|  j | | ƒ S(   s;   Create a checkpoint from the current content of a notebook.R'   u
   checkpoints   creating checkpoint for %sN(   R(   R   R   R   R   t   _save_notebookR   (   R   t   nbR   R   RJ   (    (    sI   lib/python2.7/site-packages/notebook/services/contents/filecheckpoints.pyt   create_notebook_checkpointž   s    c         C   s}   | j  d ƒ } |  j j d | | ƒ |  j | | ƒ } t j j | ƒ s\ |  j | | ƒ n  i d d 6|  j | d d ƒd 6S(   s    Get a checkpoint for a notebook.R'   s   restoring %s from checkpoint %st   notebookt   typet
   as_versioni   RI   (	   R(   R   RB   R   R   R   R   R)   t   _read_notebook(   R   R   R   RJ   (    (    sI   lib/python2.7/site-packages/notebook/services/contents/filecheckpoints.pyt   get_notebook_checkpoint«   s    c         C   s   | j  d ƒ } |  j j d | | ƒ |  j | | ƒ } t j j | ƒ s\ |  j | | ƒ n  |  j | d d ƒ\ } } i d d 6| d 6| d 6S(   s   Get a checkpoint for a file.R'   s   restoring %s from checkpoint %sR4   t   fileRP   RI   N(
   R(   R   RB   R   R   R   R   R)   t
   _read_filet   None(   R   R   R   RJ   RI   R4   (    (    sI   lib/python2.7/site-packages/notebook/services/contents/filecheckpoints.pyt   get_file_checkpoint¼   s    (   RC   RD   RE   RK   RN   RS   RW   (    (    (    sI   lib/python2.7/site-packages/notebook/services/contents/filecheckpoints.pyRG   Œ   s
   			(   RE   R   R    t   tornado.webR    t   checkpointsR   R   t   fileioR   t   jupyter_core.utilsR   t   ipython_genutils.py3compatR   t	   traitletsR   RO   R   R=   R   RG   (    (    (    sI   lib/python2.7/site-packages/notebook/services/contents/filecheckpoints.pyt   <module>   s   v