o
    i                     @   s>   d Z ddlmZ ddlmZ G dd deZG dd d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      t d)zCreate a checkpoint.!must be implemented in a subclassNotImplementedError)selfcontents_mgrpath r   a/var/www/edux/Edux_v2/venv/lib/python3.10/site-packages/notebook/services/contents/checkpoints.pycreate_checkpoint      zCheckpoints.create_checkpointc                 C   r   )zRestore a checkpointr   r   )r	   r
   checkpoint_idr   r   r   r   restore_checkpoint   r   zCheckpoints.restore_checkpointc                 C   r   )z5Rename a single checkpoint from old_path to new_path.r   r   )r	   r   old_pathnew_pathr   r   r   rename_checkpoint!   r   zCheckpoints.rename_checkpointc                 C   r   )zdelete a checkpoint for a filer   r   r	   r   r   r   r   r   delete_checkpoint%   r   zCheckpoints.delete_checkpointc                 C   r   )z-Return a list of checkpoints for a given filer   r   )r	   r   r   r   r   list_checkpoints)   r   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   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   
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r&| |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$   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   save)r	   r
   r   r   r$   r-   r   r   r   r   _   s   z*GenericCheckpointsMixin.restore_checkpointc                 C   r   zwCreate a checkpoint of the current state of a file

        Returns a checkpoint model for the new checkpoint.
        r   r   )r	   r#   r'   r   r   r   r   r,   k      z.GenericCheckpointsMixin.create_file_checkpointc                 C   r   r1   r   )r	   nbr   r   r   r   r+   r   r2   z2GenericCheckpointsMixin.create_notebook_checkpointc                 C   r   )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   r   r   r   r/   y   s   
z+GenericCheckpointsMixin.get_file_checkpointc                 C   r   )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   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    tornado.webr   traitlets.config.configurabler   r   r!   r   r   r   r   <module>   s
    +