
    Xh!                     v    d Z ddlmZ ddlmZ  G d de      Z G d d      Z G d d	e      Z G d
 de      Zy)z#
Classes for managing Checkpoints.
    )	HTTPError)LoggingConfigurablec                   :    e Zd ZdZd Zd Zd Zd Zd Zd Z	d Z
y	)
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                     t         zCreate a checkpoint.NotImplementedErrorselfcontents_mgrpaths      `/var/www/html/myenv/lib/python3.12/site-packages/jupyter_server/services/contents/checkpoints.pycreate_checkpointzCheckpoints.create_checkpoint       !!    c                     t         zRestore a checkpointr	   r   r   checkpoint_idr   s       r   restore_checkpointzCheckpoints.restore_checkpoint   r   r   c                     t         z5Rename a single checkpoint from old_path to new_path.r	   r   r   old_pathnew_paths       r   rename_checkpointzCheckpoints.rename_checkpoint    r   r   c                     t         zdelete a checkpoint for a filer	   r   r   r   s      r   delete_checkpointzCheckpoints.delete_checkpoint$   r   r   c                     t         z-Return a list of checkpoints for a given filer	   r   r   s     r   list_checkpointszCheckpoints.list_checkpoints(   r   r   c                 \    | j                  |      D ]  }| j                  |d   ||        y)0Rename all checkpoints for old_path to new_path.idNr%   r   r   r   r   cps       r   rename_all_checkpointsz"Checkpoints.rename_all_checkpoints,   s4    ''1 	AB""2d8Xx@	Ar   c                 Z    | j                  |      D ]  }| j                  |d   |        y)*Delete all checkpoints for the given path.r(   Nr%   r!   r   r   
checkpoints      r   delete_all_checkpointsz"Checkpoints.delete_all_checkpoints1   s1    //5 	;J"":d#3T:	;r   N__name__
__module____qualname____doc__r   r   r   r!   r%   r,   r2    r   r   r   r      s+    
"""""A
;r   r   c                   4    e Zd ZdZd Zd Zd Zd Zd Zd Z	y)	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                     |j                  |d      }|d   }|dk(  r| j                  |d   |      S |dk(  r| j                  |d   |d   |      S t        dd	t        z        
NTcontenttypenotebookr>   fileformat  Unexpected type %s)getcreate_notebook_checkpointcreate_file_checkpointr   r?   r   r   r   modeltype_s        r   r   z)GenericCheckpointsMixin.create_checkpointM   s      t 4fJ22i   f_..i h  C!5!<==r   c                     |j                  |d      d   }|dk(  r| j                  ||      }n'|dk(  r| j                  ||      }nt        dd|z        |j	                  ||       y)	Restore a checkpoint.Fr=   r?   r@   rA   rC   rD   NrE   get_notebook_checkpointget_file_checkpointr   save)r   r   r   r   rJ   rI   s         r   r   z*GenericCheckpointsMixin.restore_checkpoint^   sr      u 5f=J00EEf_,,]DAEC!5!=>>%&r   c                     t         zwCreate a checkpoint of the current state of a file

        Returns a checkpoint model for the new checkpoint.
        r	   r   r>   rB   r   s       r   rG   z.GenericCheckpointsMixin.create_file_checkpointj   
    
 "!r   c                     t         rR   r	   r   nbr   s      r   rF   z2GenericCheckpointsMixin.create_notebook_checkpointq   rT   r   c                     t         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    s      r   rO   z+GenericCheckpointsMixin.get_file_checkpointx   s
     "!r   c                     t         zGet the content of a checkpoint for a notebook.

        Returns a dict of the form::

            {
                'type': 'notebook',
                'content': <output of nbformat.read>,
            }
        r	   r    s      r   rN   z/GenericCheckpointsMixin.get_notebook_checkpoint   s
     "!r   N
r4   r5   r6   r7   r   r   rG   rF   rO   rN   r8   r   r   r:   r:   7   s%    *>"	'"""
"r   r:   c                   :    e Zd ZdZd Zd Zd Zd Zd Zd Z	d Z
y	)
AsyncCheckpointszS
    Base class for managing checkpoints for a ContentsManager asynchronously.
    c                    K   t         wr   r	   r   s      r   r   z"AsyncCheckpoints.create_checkpoint        !!   	c                    K   t         wr   r	   r   s       r   r   z#AsyncCheckpoints.restore_checkpoint   r`   ra   c                    K   t         wr   r	   r   s       r   r   z"AsyncCheckpoints.rename_checkpoint   r`   ra   c                    K   t         wr   r	   r    s      r   r!   z"AsyncCheckpoints.delete_checkpoint   r`   ra   c                    K   t         wr#   r	   r$   s     r   r%   z!AsyncCheckpoints.list_checkpoints   r`   ra   c                    K   | j                  |       d{   D ]   }| j                  |d   ||       d{    " y7 (7 	w)r'   Nr(   r)   r*   s       r   r,   z'AsyncCheckpoints.rename_all_checkpoints   sH     --h77 	GB((D8XFFF	G7Fs   AA  AAAAc                    K   | j                  |       d{   D ]  }| j                  |d   |       d{    ! y7 '7 	w)r.   Nr(   r/   r0   s      r   r2   z'AsyncCheckpoints.delete_all_checkpoints   sG      $ 5 5d ;; 	AJ((D)94@@@	A;@s   A?AAAANr3   r8   r   r   r^   r^      s,    """""G
Ar   r^   c                   4    e Zd ZdZd Zd Zd Zd Zd Zd Z	y)	AsyncGenericCheckpointsMixinzp
    Helper for creating Asynchronous Checkpoints subclasses that can be used with any
    ContentsManager.
    c                   K   |j                  |d       d {   }|d   }|dk(  r| j                  |d   |       d {   S |dk(  r!| j                  |d   |d   |       d {   S t        dd	|z        7 `7 ;7 wr<   )rE   rF   rG   r   rH   s        r   r   z.AsyncGenericCheckpointsMixin.create_checkpoint   s     "&&tT&::fJ88i    f_44i h   C!5!=>> ;
s3   B A:&B  A<%B &A>'B <B >B c                 .  K   |j                  |d       d{   }|d   }|dk(  r| j                  ||       d{   }n/|dk(  r| j                  ||       d{   }nt        dd|z        |j	                  ||       d{    y7 s7 Q7 37 w)	rL   Fr=   Nr?   r@   rA   rC   rD   rM   )r   r   r   r   content_modelrJ   rI   s          r   r   z/AsyncGenericCheckpointsMixin.restore_checkpoint   s     *..tU.CCf%J66}dKKEf_22=$GGEC!5!=>>t,,, D LG 	-sC   BB#BBBB)BBBBBBc                    K   t         wrR   r	   rS   s       r   rG   z3AsyncGenericCheckpointsMixin.create_file_checkpoint        
 "!ra   c                    K   t         wrR   r	   rV   s      r   rF   z7AsyncGenericCheckpointsMixin.create_notebook_checkpoint   rn   ra   c                    K   t         wrY   r	   r    s      r   rO   z0AsyncGenericCheckpointsMixin.get_file_checkpoint   s      "!ra   c                    K   t         wr[   r	   r    s      r   rN   z4AsyncGenericCheckpointsMixin.get_notebook_checkpoint   s      "!ra   Nr\   r8   r   r   ri   ri      s%    
?"
-"""
"r   ri   N)	r7   tornado.webr   traitlets.config.configurabler   r   r:   r^   ri   r8   r   r   <module>rt      sK    " =);% );XX" X"v!A{ !AHI"#: I"r   