a
    ø£BeÂ!  ã                   @   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                   @   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S ©zCreate a checkpoint.N©ÚNotImplementedError©ÚselfÚcontents_mgrÚpath© r   úo/home/yuhw/wc/wire-cell-python/wcpy/lib/python3.9/site-packages/jupyter_server/services/contents/checkpoints.pyÚcreate_checkpoint   s    zCheckpoints.create_checkpointc                 C   s   t ‚dS ©zRestore a checkpointNr   ©r	   r
   Úcheckpoint_idr   r   r   r   Úrestore_checkpoint   s    zCheckpoints.restore_checkpointc                 C   s   t ‚dS ©z5Rename a single checkpoint from old_path to new_path.Nr   ©r	   r   Úold_pathÚnew_pathr   r   r   Úrename_checkpoint   s    zCheckpoints.rename_checkpointc                 C   s   t ‚dS ©zdelete a checkpoint for a fileNr   ©r	   r   r   r   r   r   Údelete_checkpoint#   s    zCheckpoints.delete_checkpointc                 C   s   t ‚dS ©z-Return a list of checkpoints for a given fileNr   ©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   Ú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                   @   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	t ƒ‚d S ©
NT©ÚcontentÚtypeÚnotebookr1   ÚfileÚformatéô  úUnexpected type %s)ÚgetÚcreate_notebook_checkpointÚcreate_file_checkpointr   r2   ©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.Fr0   r2   r3   r4   r6   r7   N©r8   Úget_notebook_checkpointÚget_file_checkpointr   Úsave)r	   r
   r   r   r=   r<   r   r   r   r   ]   s    z*GenericCheckpointsMixin.restore_checkpointc                 C   s   t ‚dS ©zwCreate a checkpoint of the current state of a file

        Returns a checkpoint model for the new checkpoint.
        Nr   ©r	   r1   r5   r   r   r   r   r:   i   s    z.GenericCheckpointsMixin.create_file_checkpointc                 C   s   t ‚dS rC   r   ©r	   Únbr   r   r   r   r9   p   s    z2GenericCheckpointsMixin.create_notebook_checkpointc                 C   s   t ‚dS ©zóGet the content of a checkpoint for a non-notebook file.

        Returns a dict of the form::

            {
                'type': 'file',
                'content': <str>,
                'format': {'text','base64'},
            }
        Nr   r   r   r   r   rA   w   s    z+GenericCheckpointsMixin.get_file_checkpointc                 C   s   t ‚dS ©zÕGet the content of a checkpoint for a notebook.

        Returns a dict of the form::

            {
                'type': 'notebook',
                'content': <output of nbformat.read>,
            }
        Nr   r   r   r   r   r@   „   s    
z/GenericCheckpointsMixin.get_notebook_checkpointN©
r*   r+   r,   r-   r   r   r:   r9   rA   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S r   r   r   r   r   r   r   –   s    z"AsyncCheckpoints.create_checkpointc                 Ã   s   t ‚dS r   r   r   r   r   r   r   š   s    z#AsyncCheckpoints.restore_checkpointc                 Ã   s   t ‚dS r   r   r   r   r   r   r   ž   s    z"AsyncCheckpoints.rename_checkpointc                 Ã   s   t ‚dS r   r   r   r   r   r   r   ¢   s    z"AsyncCheckpoints.delete_checkpointc                 Ã   s   t ‚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   rJ   ‘   s   rJ   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/   )r8   r9   r:   r   r;   r   r   r   r   »   s    þ
ý
z.AsyncGenericCheckpointsMixin.create_checkpointc                 Ã   sx   |j |dd�I dH }|d }|dkr8|  ||¡I dH }n*|dkrT|  ||¡I dH }ntdd| ƒ‚| ||¡I dH  dS )	r>   Fr0   Nr2   r3   r4   r6   r7   r?   )r	   r
   r   r   Zcontent_modelr=   r<   r   r   r   r   Ì   s    z/AsyncGenericCheckpointsMixin.restore_checkpointc                 Ã   s   t ‚dS rC   r   rD   r   r   r   r:   Ù   s    z3AsyncGenericCheckpointsMixin.create_file_checkpointc                 Ã   s   t ‚dS rC   r   rE   r   r   r   r9   à   s    z7AsyncGenericCheckpointsMixin.create_notebook_checkpointc                 Ã   s   t ‚dS rG   r   r   r   r   r   rA   ç   s    z0AsyncGenericCheckpointsMixin.get_file_checkpointc                 Ã   s   t ‚dS rH   r   r   r   r   r   r@   ô   s    
z4AsyncGenericCheckpointsMixin.get_notebook_checkpointNrI   r   r   r   r   rK   µ   s   rK   N)	r-   Ztornado.webr   Ztraitlets.config.configurabler   r   r.   rJ   rK   r   r   r   r   Ú<module>   s   ,[$