
    Ib                     J    d Z ddlmZ ddlmZ  G d de      Z G d d      Zy)	z#
Classes for managing Checkpoints.
    )	HTTPError)LoggingConfigurablec                   :    e Zd ZdZd Zd Zd Zd Zd Zd Z	d Z
y	)
Checkpointsaq  
    Base class for managing checkpoints for a ContentsManager.

    Subclasses are required to implement:

    create_checkpoint(self, contents_mgr, path)
    restore_checkpoint(self, contents_mgr, checkpoint_id, path)
    rename_checkpoint(self, checkpoint_id, old_path, new_path)
    delete_checkpoint(self, checkpoint_id, path)
    list_checkpoints(self, path)
    c                     t        d      )zCreate a checkpoint.!must be implemented in a subclassNotImplementedError)selfcontents_mgrpaths      H/usr/lib/python3/dist-packages/notebook/services/contents/checkpoints.pycreate_checkpointzCheckpoints.create_checkpoint       !"EFF    c                     t        d      )zRestore a checkpointr   r	   )r   r   checkpoint_idr   s       r   restore_checkpointzCheckpoints.restore_checkpoint   r   r   c                     t        d      )z5Rename a single checkpoint from old_path to new_path.r   r	   )r   r   old_pathnew_paths       r   rename_checkpointzCheckpoints.rename_checkpoint!   r   r   c                     t        d      )zdelete a checkpoint for a filer   r	   r   r   r   s      r   delete_checkpointzCheckpoints.delete_checkpoint%   r   r   c                     t        d      )z-Return a list of checkpoints for a given filer   r	   )r   r   s     r   list_checkpointszCheckpoints.list_checkpoints)   r   r   c                 \    | j                  |      D ]  }| j                  |d   ||        y)z0Rename all checkpoints for old_path to new_path.idN)r   r   )r   r   r   cps       r   rename_all_checkpointsz"Checkpoints.rename_all_checkpoints-   s4    ''1 	AB""2d8Xx@	Ar   c                 Z    | j                  |      D ]  }| j                  |d   |        y)z*Delete all checkpoints for the given path.r   N)r   r   )r   r   
checkpoints      r   delete_all_checkpointsz"Checkpoints.delete_all_checkpoints2   s1    //5 	;J"":d#3T:	;r   N)__name__
__module____qualname____doc__r   r   r   r   r   r!   r$    r   r   r   r      s0    
GGGGGA
;r   r   c                   4    e Zd ZdZd Zd Zd Zd Zd Zd Z	y)	GenericCheckpointsMixina  
    Helper for creating Checkpoints subclasses that can be used with any
    ContentsManager.

    Provides a ContentsManager-agnostic implementation of `create_checkpoint`
    and `restore_checkpoint` in terms of the following operations:

    - create_file_checkpoint(self, content, format, path)
    - create_notebook_checkpoint(self, nb, path)
    - get_file_checkpoint(self, checkpoint_id, path)
    - get_notebook_checkpoint(self, checkpoint_id, path)

    To create a generic CheckpointManager, add this mixin to a class that
    implement the above four methods plus the remaining Checkpoints API
    methods:

    - delete_checkpoint(self, checkpoint_id, path)
    - list_checkpoints(self, path)
    - rename_checkpoint(self, checkpoint_id, old_path, new_path)
    c                     |j                  |d      }|d   }|dk(  r| j                  |d   |      S |dk(  r| j                  |d   |d   |      S t        dd	|       )
NTcontenttypenotebookr.   fileformat  Unexpected type )getcreate_notebook_checkpointcreate_file_checkpointr   )r   r   r   modelr/   s        r   r   z)GenericCheckpointsMixin.create_checkpointN   s      t 4V}:22i   V^..i h  C#3D6!:;;r   c                     |j                  |d      d   }|dk(  r| j                  ||      }n'|dk(  r| j                  ||      }nt        dd|       |j	                  ||       y)	zRestore a checkpoint.Fr-   r/   r0   r1   r3   r4   N)r5   get_notebook_checkpointget_file_checkpointr   save)r   r   r   r   r/   r8   s         r   r   z*GenericCheckpointsMixin.restore_checkpoint_   st    e4V<:00EEV^,,]DAEC#3D6!:;;%&r   c                     t        d      zwCreate a checkpoint of the current state of a file

        Returns a checkpoint model for the new checkpoint.
        r   r	   )r   r.   r2   r   s       r   r7   z.GenericCheckpointsMixin.create_file_checkpointk       
 ""EFFr   c                     t        d      r>   r	   )r   nbr   s      r   r6   z2GenericCheckpointsMixin.create_notebook_checkpointr   r?   r   c                     t        d      )zGet the content of a checkpoint for a non-notebook file.

         Returns a dict of the form:
         {
             'type': 'file',
             'content': <str>,
             'format': {'text','base64'},
         }
        r   r	   r   s      r   r;   z+GenericCheckpointsMixin.get_file_checkpointy   s     ""EFFr   c                     t        d      )zGet the content of a checkpoint for a notebook.

        Returns a dict of the form:
        {
            'type': 'notebook',
            'content': <output of nbformat.read>,
        }
        r   r	   r   s      r   r:   z/GenericCheckpointsMixin.get_notebook_checkpoint   s     ""EFFr   N)
r%   r&   r'   r(   r   r   r7   r6   r;   r:   r)   r   r   r+   r+   8   s)    *<"	'GG
G	Gr   r+   N)r(   tornado.webr   traitlets.config.configurabler   r   r+   r)   r   r   <module>rF      s.    " =(;% (;VVG VGr   