a
    m 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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   Klib/python3.9/site-packages/jupyter_server/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 )0Rename 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 )*Delete all checkpoints for the given path.r    Nr   r   r
   r   Z
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                   @   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notebookr0   fileformat  Unexpected type %sgetcreate_notebook_checkpointcreate_file_checkpointr   r
   r   r   modelr1   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.Fr/   r1   r2   r3   r5   r6   Nr8   get_notebook_checkpointget_file_checkpointr   Zsaver
   r   r   r   r1   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
   r0   r4   r   r   r   r   r:   i   s    z.GenericCheckpointsMixin.create_file_checkpointc                 C   s   t ddS rB   r   r
   nbr   r   r   r   r9   p   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@   w   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:   r9   r@   r?   r   r   r   r   r-   6   s   r-   c                   @   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 )AsyncCheckpointszS
    Base class for managing checkpoints for a ContentsManager asynchronously.
    c                    s   t ddS r   r   r	   r   r   r   r      s    z"AsyncCheckpoints.create_checkpointc                    s   t ddS r   r   r   r   r   r   r      s    z#AsyncCheckpoints.restore_checkpointc                    s   t ddS r   r   r   r   r   r   r      s    z"AsyncCheckpoints.rename_checkpointc                    s   t ddS r   r   r   r   r   r   r      s    z"AsyncCheckpoints.delete_checkpointc                    s   t ddS r   r   r   r   r   r   r      s    z!AsyncCheckpoints.list_checkpointsc                    s2   |  |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                    s0   |  |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   rI      s   rI   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 )AsyncGenericCheckpointsMixinzp
    Helper for creating Asynchronous Checkpoints subclasses that can be used with any
    ContentsManager.
    c                    sp   |j |ddI d H }|d }|dkr:| |d |I d H S |dkr^| |d |d |I d H S tdd	| d S r.   r7   r;   r   r   r   r      s    

z.AsyncGenericCheckpointsMixin.create_checkpointc                    st   |j |ddd I dH }|dkr4| ||I dH }n*|dkrP| ||I dH }ntdd| |||I dH  dS )	r=   Fr/   r1   Nr2   r3   r5   r6   r>   rA   r   r   r   r      s    z/AsyncGenericCheckpointsMixin.restore_checkpointc                    s   t ddS rB   r   rC   r   r   r   r:      s    z3AsyncGenericCheckpointsMixin.create_file_checkpointc                    s   t ddS rB   r   rD   r   r   r   r9      s    z7AsyncGenericCheckpointsMixin.create_notebook_checkpointc                    s   t ddS rF   r   r   r   r   r   r@      s    
z0AsyncGenericCheckpointsMixin.get_file_checkpointc                    s   t ddS rG   r   r   r   r   r   r?      s    	z4AsyncGenericCheckpointsMixin.get_notebook_checkpointNrH   r   r   r   r   rJ      s   rJ   N)
r,   Ztornado.webr   Ztraitlets.config.configurabler   r   objectr-   rI   rJ   r   r   r   r   <module>   s   ,Y$