
    Xh10                         d Z ddlZddlZddlZddlmZ ddlmZ ddlm	Z	 ddl
mZ ddlmZ dd	lmZmZmZmZ dd
lmZmZ  G d dee      Z G d deee      Z G d dee      Z G d dee      Zy)z)
File-based Checkpoints implementations.
    N)run_sync)ensure_dir_exists	HTTPError)Unicode)_tz   )AsyncCheckpointsAsyncGenericCheckpointsMixinCheckpointsGenericCheckpointsMixin)AsyncFileManagerMixinFileManagerMixinc                   n    e Zd ZdZ eddd      Z ed      Zd Zd Zd	 Z	d
 Z
d Zd Zd Zd Zd Zy)FileCheckpointsz
    A Checkpoints that caches checkpoints for files in adjacent
    directories.

    Only works with FileContentsManager.  Use GenericFileCheckpoints if
    you want file-based checkpoints with another ContentsManager.
    z.ipynb_checkpointsTzThe directory name in which to keep file checkpoints

        This is a path relative to the file's own directory.

        By default, it is .ipynb_checkpoints
        )confighelp)r   c                 n    | j                   st        j                         S | j                   j                  S )N)parentosgetcwdroot_dir)selfs    d/var/www/html/myenv/lib/python3.12/site-packages/jupyter_server/services/contents/filecheckpoints.py_root_dir_defaultz!FileCheckpoints._root_dir_default/   s%    {{99;{{###    c                     d}|j                  |      }| j                  ||      }| j                  ||       | j                  ||      S )Create a checkpoint.
checkpoint_get_os_pathcheckpoint_path_copycheckpoint_modelr   contents_mgrpathcheckpoint_idsrc_path	dest_paths         r   create_checkpointz!FileCheckpoints.create_checkpoint5   sJ    $,,T2((=	

8Y'$$]I>>r   c                 n    | j                  ||      }|j                  |      }| j                  ||       yzRestore a checkpoint.Nr"   r!   r#   r   r&   r(   r'   r)   r*   s         r   restore_checkpointz"FileCheckpoints.restore_checkpoint=   s2    ''t< --d3	

8Y'r   c                 <   | j                  ||      }| j                  ||      }t        j                  j                  |      rM| j                  j                  d||       | j                         5  t        j                  ||       ddd       yy# 1 sw Y   yxY wz.Rename a checkpoint from old_path to new_path.zRenaming checkpoint %s -> %sN)	r"   r   r'   isfilelogdebugperm_to_403shutilmover   r(   old_pathnew_pathold_cp_pathnew_cp_paths         r   rename_checkpointz!FileCheckpoints.rename_checkpointD   s    **=(C**=(C77>>+&HHNN.
 !!# 6K56 6 '6 6s   1BBc                 X   |j                  d      }| j                  ||      }t        j                  j	                  |      s| j                  ||       | j                  j                  d|       | j                         5  t        j                  |       ddd       y# 1 sw Y   yxY wzdelete a file's checkpoint/zunlinking %sN)
stripr"   r   r'   r3   no_such_checkpointr4   r5   r6   unlinkr   r(   r'   cp_paths       r   delete_checkpointz!FileCheckpoints.delete_checkpointQ   s    zz#&&}d;ww~~g&##D-8~w/ 	IIg	 	 	s   B  B)c                     |j                  d      }d}| j                  ||      }t        j                  j	                  |      sg S | j                  ||      gS )~list the checkpoints for a given file

        This contents manager currently only supports one checkpoint per file.
        rA   r   rB   r"   r   r'   r3   r$   r   r'   r(   os_paths       r   list_checkpointsz FileCheckpoints.list_checkpoints\   sS    
 zz#$&&}d;ww~~g&I))-ABBr   c                 &   |j                  d      }d|z   j                  dd      \  }}|j                  d      }t        j                  j	                  |      \  }}| d| | }| j                  |      }t        j                  j                  || j                        }	t        j                  t        j                  j                  |	      t        j                        s^t        j                  j                  || j                        }
t        j                  j                  t        j                         d|
      }	| j                         5  t!        |	       ddd       t        j                  j                  |	|      }|S # 1 sw Y   +xY w)zfind the path to a checkpointrA   r	   -)r'   )startjupyter_checkpointsN)rB   rsplitr   r'   splitextr!   joincheckpoint_diraccessdirnameW_OKrelpathr   tempfile
gettempdirr6   r   )r   r(   r'   r   namebasenameextfilenamerL   cp_dirrelrF   s               r   r"   zFileCheckpoints.checkpoint_pathj   s,   zz#d
**32c"((.#Zqu5###0gt':':;yy0"'':''//'/?CWW\\("5"5"79NPSTF 	&f%	&'',,vx0	& 	&s   FFc                 x    t        j                  |      }t        j                  |j                        }||d}|S ).construct the info dict for a given checkpointidlast_modified)r   stattzutcfromtimestampst_mtimer   r(   rL   statsrf   infos         r   r$   z FileCheckpoints.checkpoint_model|   s8     ++ENN;*
 r   c                 &    t        dd| d|       )Ni  zCheckpoint does not exist: @r   )r   r'   r(   s      r   rC   z"FileCheckpoints.no_such_checkpoint   s    :4&-QRRr   N)__name__
__module____qualname____doc__r   rU   r   r   r+   r0   r>   rG   rM   r"   r$   rC    r   r   r   r      sY     	N d#H$?(6	C$Sr   r   c                   0    e Zd Zd Zd Zd Zd Zd Zd Zy)AsyncFileCheckpointsc                    K   d}|j                  |      }| j                  ||      }| j                  ||       d{    | j                  ||       d{   S 7 7 w)r   r   Nr    r%   s         r   r+   z&AsyncFileCheckpoints.create_checkpoint   s`     $,,T2((=	jj9---**=)DDD 	.Ds!   ;A AA AA A c                    K   | j                  ||      }|j                  |      }| j                  ||       d{    y7 wr-   r.   r/   s         r   r0   z'AsyncFileCheckpoints.restore_checkpoint   s<     ''t< --d3	jj9---s   9AAAc                    K   t        t        j                  |       d{   }t        j                  |j
                        }||d}|S 7 *w)rc   Nrd   )r   r   rg   rh   ri   rj   rk   s         r   r$   z%AsyncFileCheckpoints.checkpoint_model   sF     rww00++ENN;*
  1s   AA+Ac                 b  K   | j                  ||      }| j                  ||      }t        j                  j                  |      rZ| j                  j                  d||       | j                         5  t        t        j                  ||       d{    ddd       yy7 # 1 sw Y   yxY wwr2   )
r"   r   r'   r3   r4   r5   r6   r   r7   r8   r9   s         r   r>   z&AsyncFileCheckpoints.rename_checkpoint   s     **=(C**=(C77>>+&HHNN.
 !!# Fv{{KEEEF F ' FF Fs0   A1B/3B#B!B#
B/!B##B,(B/c                 ~  K   |j                  d      }| j                  ||      }t        j                  j	                  |      s| j                  ||       | j                  j                  d|       | j                         5  t        t        j                  |       d{    ddd       y7 # 1 sw Y   yxY wwr@   )rB   r"   r   r'   r3   rC   r4   r5   r6   r   rD   rE   s       r   rG   z&AsyncFileCheckpoints.delete_checkpoint   s     zz#&&}d;ww~~g&##D-8~w/ 	/299g...	/ 	/.	/ 	/s0   BB=B1!B/"B1&	B=/B11B:6B=c                    K   |j                  d      }d}| j                  ||      }t        j                  j	                  |      sg S | j                  ||       d{   gS 7 w)rI   rA   r   NrJ   rK   s       r   rM   z%AsyncFileCheckpoints.list_checkpoints   s]     
 zz#$&&}d;ww~~g&I//wGGHHGs   AA&A$A&N)	rp   rq   rr   r+   r0   r$   r>   rG   rM   rt   r   r   rv   rv      s#    E.F	/Ir   rv   c                   (    e Zd ZdZd Zd Zd Zd Zy)GenericFileCheckpointszZ
    Local filesystem Checkpoints that works with any conforming
    ContentsManager.
    c                    |j                  d      }d}| j                  ||      }| j                  j                  d|       | j	                         5  | j                  |||       ddd       | j                  ||      S # 1 sw Y   xY wz7Create a checkpoint from the current content of a file.rA   r   creating checkpoint for %sformatNrB   r"   r4   r5   r6   
_save_filer$   r   contentr   r'   r(   os_checkpoint_paths         r   create_file_checkpointz-GenericFileCheckpoints.create_file_checkpoint   s    zz#$!11-F3T: 	HOO.OG	H $$]4FGG		H 	Hs   BB
c                    |j                  d      }d}| j                  ||      }| j                  j                  d|       | j	                         5  | j                  ||       ddd       | j                  ||      S # 1 sw Y   xY wz;Create a checkpoint from the current content of a notebook.rA   r   r   NrB   r"   r4   r5   r6   _save_notebookr$   r   nbr'   r(   r   s        r   create_notebook_checkpointz1GenericFileCheckpoints.create_notebook_checkpoint   s    zz#$!11-F3T: 	8 2B7	8 $$]4FGG		8 	8s   A??Bc                    |j                  d      }| j                  j                  d||       | j                  ||      }t        j
                  j                  |      s| j                  ||       d| j                  |d      dS ) Get a checkpoint for a notebook.rA   restoring %s from checkpoint %snotebook   
as_versiontyper   	rB   r4   rm   r"   r   r'   r3   rC   _read_notebookr   r(   r'   r   s       r   get_notebook_checkpointz.GenericFileCheckpoints.get_notebook_checkpoint   s    zz#7}M!11-Fww~~01##D-8 **" + 
 	
r   c                    |j                  d      }| j                  j                  d||       | j                  ||      }t        j
                  j                  |      s| j                  ||       | j                  |d      \  }}d||dS zGet a checkpoint for a file.rA   r   Nr   file)r   r   r   	rB   r4   rm   r"   r   r'   r3   rC   
_read_filer   r(   r'   r   r   r   s         r   get_file_checkpointz*GenericFileCheckpoints.get_file_checkpoint   s    zz#7}M!11-Fww~~01##D-8//*<T/J
 	
r   Nrp   rq   rr   rs   r   r   r   r   rt   r   r   r~   r~      s    
HH
"
r   r~   c                   (    e Zd ZdZd Zd Zd Zd Zy)AsyncGenericFileCheckpointszg
    Asynchronous Local filesystem Checkpoints that works with any conforming
    ContentsManager.
    c                 J  K   |j                  d      }d}| j                  ||      }| j                  j                  d|       | j	                         5  | j                  |||       d{    ddd       | j                  ||       d{   S 7 &# 1 sw Y   %xY w7 wr   r   r   s         r   r   z2AsyncGenericFileCheckpoints.create_file_checkpoint  s     zz#$!11-F3T: 	N//"4gf/MMM	N **=:LMMM N	N 	N Ns<   AB#B,B-B1B#B!B#BBB#c                 F  K   |j                  d      }d}| j                  ||      }| j                  j                  d|       | j	                         5  | j                  ||       d{    ddd       | j                  ||       d{   S 7 &# 1 sw Y   %xY w7 wr   r   r   s        r   r   z6AsyncGenericFileCheckpoints.create_notebook_checkpoint   s     zz#$!11-F3T: 	>%%&8"===	> **=:LMMM >	> 	> Ns<   AB!B*B+B/B!BB!BBB!c                 ,  K   |j                  d      }| j                  j                  d||       | j                  ||      }t        j
                  j                  |      s| j                  ||       d| j                  |d       d{   dS 7 w)r   rA   r   r   r   r   Nr   r   r   s       r   r   z3AsyncGenericFileCheckpoints.get_notebook_checkpoint-  s     zz#7}M!11-Fww~~01##D-8 !00" 1  
 	
s   B	BBBc                 8  K   |j                  d      }| j                  j                  d||       | j                  ||      }t        j
                  j                  |      s| j                  ||       | j                  |d       d{   \  }}d||dS 7 wr   r   r   s         r   r   z/AsyncGenericFileCheckpoints.get_file_checkpoint>  s     zz#7}M!11-Fww~~01##D-8 $0B4 PP
 	
 Qs   BB
BBNr   rt   r   r   r   r     s    
NN
"
r   r   )rs   r   r7   rZ   anyio.to_threadr   jupyter_core.utilsr   tornado.webr   	traitletsr   jupyter_serverr   rh   checkpointsr
   r   r   r   fileior   r   r   rv   r~   r   rt   r   r   <module>r      s|    
   $ 0 !  $  <oS& oSd=I?,ACS =I@?
4o ?
D?
">@T ?
r   