a
    Db                     @   s@   d Z ddlmZ ddlmZ G dd deZG dd deZdS )	z#
Classes for managing Checkpoints.
    )	HTTPError)LoggingConfigurablec                   @   sH   e Zd Z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 )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                 C   s   t ddS )zCreate a checkpoint.!must be implemented in a subclassNNotImplementedError)selfcontents_mgrpath r   Elib/python3.9/site-packages/notebook/services/contents/checkpoints.pycreate_checkpoint   s    zCheckpoints.create_checkpointc                 C   s   t ddS )zRestore a checkpointr   Nr   )r   r	   checkpoint_idr
   r   r   r   restore_checkpoint   s    zCheckpoints.restore_checkpointc                 C   s   t ddS )z5Rename a single checkpoint from old_path to new_path.r   Nr   )r   r   old_pathnew_pathr   r   r   rename_checkpoint!   s    zCheckpoints.rename_checkpointc                 C   s   t ddS )zdelete a checkpoint for a filer   Nr   r   r   r
   r   r   r   delete_checkpoint%   s    zCheckpoints.delete_checkpointc                 C   s   t ddS )z-Return a list of checkpoints for a given filer   Nr   )r   r
   r   r   r   list_checkpoints)   s    zCheckpoints.list_checkpointsc                 C   s&   |  |D ]}| |d || q
dS )z0Rename all checkpoints for old_path to new_path.idN)r   r   )r   r   r   Zcpr   r   r   rename_all_checkpoints-   s    z"Checkpoints.rename_all_checkpointsc                 C   s$   |  |D ]}| |d | q
dS )z*Delete all checkpoints for the given path.r   N)r   r   )r   r
   Z
checkpointr   r   r   delete_all_checkpoints2   s    z"Checkpoints.delete_all_checkpointsN)__name__
__module____qualname____doc__r   r   r   r   r   r   r   r   r   r   r   r      s   r   c                   @   s@   e Zd ZdZdd Zdd Zdd Zdd	 Zd
d Zdd Z	dS )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                 C   s^   |j |dd}|d }|dkr.| |d |S |dkrL| |d |d |S tdd	| d S )
NTcontenttypenotebookr   fileformat  Unexpected type %s)getcreate_notebook_checkpointcreate_file_checkpointr   )r   r	   r
   modelr    r   r   r   r   N   s    z)GenericCheckpointsMixin.create_checkpointc                 C   s\   |j |ddd }|dkr(| ||}n$|dkr>| ||}ntdd| ||| dS )	zRestore a checkpoint.Fr   r    r!   r"   r$   r%   N)r&   get_notebook_checkpointget_file_checkpointr   Zsave)r   r	   r   r
   r    r)   r   r   r   r   _   s    z*GenericCheckpointsMixin.restore_checkpointc                 C   s   t ddS zwCreate a checkpoint of the current state of a file

        Returns a checkpoint model for the new checkpoint.
        r   Nr   )r   r   r#   r
   r   r   r   r(   k   s    z.GenericCheckpointsMixin.create_file_checkpointc                 C   s   t ddS r,   r   )r   Znbr
   r   r   r   r'   r   s    z2GenericCheckpointsMixin.create_notebook_checkpointc                 C   s   t ddS )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   Nr   r   r   r   r   r+   y   s    
z+GenericCheckpointsMixin.get_file_checkpointc                 C   s   t ddS )zGet the content of a checkpoint for a notebook.

        Returns a dict of the form:
        {
            'type': 'notebook',
            'content': <output of nbformat.read>,
        }
        r   Nr   r   r   r   r   r*      s    	z/GenericCheckpointsMixin.get_notebook_checkpointN)
r   r   r   r   r   r   r(   r'   r+   r*   r   r   r   r   r   8   s   r   N)r   Ztornado.webr   Ztraitlets.config.configurabler   r   objectr   r   r   r   r   <module>   s   +