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G dd	 d	eZG d
d deZdS )z#
Classes for managing Checkpoints.
    )	HTTPError)LoggingConfigurablec                   @   H   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 zCreate a checkpoint.NotImplementedErrorselfcontents_mgrpath r   g/var/www/edux/Edux_v2/venv/lib/python3.10/site-packages/jupyter_server/services/contents/checkpoints.pycreate_checkpoint      zCheckpoints.create_checkpointc                 C   r   zRestore a checkpointr   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   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   delete_checkpoint#   r   zCheckpoints.delete_checkpointc                 C   r   z-Return a list of checkpoints for a given filer   r   r   r   r   r   list_checkpoints'   r   zCheckpoints.list_checkpointsc                 C   s&   |  |D ]}| |d || qdS )0Rename 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 )*Delete all checkpoints for the given path.r"   Nr    r   r   r   
checkpointr   r   r   delete_all_checkpoints0   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                   @   @   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   sZ   |j |dd}|d }|dkr| |d |S |dkr&| |d |d |S tdd	t 
NTcontenttypenotebookr5   fileformat  Unexpected type %s)getcreate_notebook_checkpointcreate_file_checkpointr   r6   r   r   r   modeltype_r   r   r   r   L   s   z)GenericCheckpointsMixin.create_checkpointc                 C   s\   |j |ddd }|dkr| ||}n|dkr| ||}ntdd| ||| dS )	Restore a checkpoint.Fr4   r6   r7   r8   r:   r;   Nr<   get_notebook_checkpointget_file_checkpointr   save)r   r   r   r   rA   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   r5   r9   r   r   r   r   r>   i      z.GenericCheckpointsMixin.create_file_checkpointc                 C   r   rG   r   r   nbr   r   r   r   r=   p   rI   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   rE   w   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   rD      s   
z/GenericCheckpointsMixin.get_notebook_checkpointN
r-   r.   r/   r0   r   r   r>   r=   rE   rD   r   r   r   r   r2   6   s    r2   c                   @   r   )AsyncCheckpointszS
    Base class for managing checkpoints for a ContentsManager asynchronously.
    c                       t r   r   r
   r   r   r   r         z"AsyncCheckpoints.create_checkpointc                    rP   r   r   r   r   r   r   r      rQ   z#AsyncCheckpoints.restore_checkpointc                    rP   r   r   r   r   r   r   r      rQ   z"AsyncCheckpoints.rename_checkpointc                    rP   r   r   r   r   r   r   r      rQ   z"AsyncCheckpoints.delete_checkpointc                    rP   r   r   r   r   r   r   r       rQ   z!AsyncCheckpoints.list_checkpointsc                    s4   |  |I dH D ]}| |d ||I dH  q	dS )r!   Nr"   r#   r$   r   r   r   r&      s   z'AsyncCheckpoints.rename_all_checkpointsc                    s2   |  |I dH D ]}| |d |I dH  q	dS )r'   Nr"   r(   r)   r   r   r   r+      s   z'AsyncCheckpoints.delete_all_checkpointsNr,   r   r   r   r   rO      s    rO   c                   @   r1   )AsyncGenericCheckpointsMixinzp
    Helper for creating Asynchronous Checkpoints subclasses that can be used with any
    ContentsManager.
    c                    sn   |j |ddI d H }|d }|dkr| |d |I d H S |dkr0| |d |d |I d H S tdd	| r3   )r<   r=   r>   r   r?   r   r   r   r      s   

z.AsyncGenericCheckpointsMixin.create_checkpointc                    sz   |j |ddI dH }|d }|dkr| ||I dH }n|dkr+| ||I dH }ntdd| |||I dH  dS )	rB   Fr4   Nr6   r7   r8   r:   r;   rC   )r   r   r   r   content_modelrA   r@   r   r   r   r      s   z/AsyncGenericCheckpointsMixin.restore_checkpointc                    rP   rG   r   rH   r   r   r   r>         z3AsyncGenericCheckpointsMixin.create_file_checkpointc                    rP   rG   r   rJ   r   r   r   r=      rT   z7AsyncGenericCheckpointsMixin.create_notebook_checkpointc                    rP   rL   r   r   r   r   r   rE      s   z0AsyncGenericCheckpointsMixin.get_file_checkpointc                    rP   rM   r   r   r   r   r   rD      s   
z4AsyncGenericCheckpointsMixin.get_notebook_checkpointNrN   r   r   r   r   rR      s    rR   N)	r0   tornado.webr   traitlets.config.configurabler   r   r2   rO   rR   r   r   r   r   <module>   s    ,[$