
    Mh10                         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dS )z)
File-based Checkpoints implementations.
    N)run_sync)ensure_dir_exists	HTTPError)Unicode)_tz   )AsyncCheckpointsAsyncGenericCheckpointsMixinCheckpointsGenericCheckpointsMixin)AsyncFileManagerMixinFileManagerMixinc                   |    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dS )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    p/var/www/html/test/jupyter/venv/lib/python3.11/site-packages/jupyter_server/services/contents/filecheckpoints.py_root_dir_defaultz!FileCheckpoints._root_dir_default/   s#    { 	9;;{##    c                     d}|                     |          }|                     ||          }|                     ||           |                     ||          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   sY    $,,T22((==	

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

8Y'''''r   c                 \   |                      ||          }|                      ||          }t          j                            |          r`| j                            d||           |                                 5  t          j        ||           ddd           dS # 1 swxY w Y   dS dS 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**=(CC7>>+&& 	6HNN.  
 !!## 6 6K5556 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6	6 	6s   <BB#&B#c                 ~   |                     d          }|                     ||          }t          j                            |          s|                     ||           | j                            d|           |                                 5  t          j	        |           ddd           dS # 1 swxY w Y   dS 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;;w~~g&& 	9##D-888~w/// 	 	Ig	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	s   B22B69B6c                     |                     d          }d}|                     ||          }t          j                            |          sg S |                     ||          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\   sb    
 zz#$&&}d;;w~~g&& 	CI))-AABBr   c                 <   |                     d          }d|z                       dd          \  }}|                     d          }t          j                            |          \  }}| d| | }|                     |          }t          j                            || j                  }	t          j        t          j        	                    |	          t          j
                  sXt          j                            || j                  }
t          j                            t          j                    d|
          }	|                                 5  t!          |	           ddd           n# 1 swxY w Y   t          j                            |	|          }|S )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2c""((..#55555###00gt':;;y00"':: 	U'//'/??CW\\("5"7"79NPSTTF 	& 	&f%%%	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	&',,vx00s   E11E58E5c                 j    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|   s<      +EN;;*
 
 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#    Q4QQ-QQRRRr   N)__name__
__module____qualname____doc__r   rU   r   r   r+   r0   r>   rG   rM   r"   r$   rC    r   r   r   r      s          W	 	 	N wd###H$ $ $? ? ?( ( (6 6 6	 	 	C C C  $  S S S S Sr   r   c                   2    e Zd Zd Zd Zd Zd Zd Zd ZdS )AsyncFileCheckpointsc                    K   d}|                     |          }|                     ||          }|                     ||           d{V  |                     ||           d{V S )r   r   Nr    r%   s         r   r+   z&AsyncFileCheckpoints.create_checkpoint   s      $,,T22((==	jj9---------**=)DDDDDDDDDr   c                    K   |                      ||          }|                    |          }|                     ||           d{V  dS r-   r.   r/   s         r   r0   z'AsyncFileCheckpoints.restore_checkpoint   sZ      ''t<< --d33	jj9-----------r   c                    K   t          t          j        |           d{V }t          j        |j                  }||d}|S )rc   Nrd   )r   r   rg   rh   ri   rj   rk   s         r   r$   z%AsyncFileCheckpoints.checkpoint_model   sT      rw00000000+EN;;*
 
 r   c                 x  K   |                      ||          }|                      ||          }t          j                            |          rl| j                            d||           |                                 5  t          t          j	        ||           d{V  ddd           dS # 1 swxY w Y   dS dS r2   )
r"   r   r'   r3   r4   r5   r6   r   r7   r8   r9   s         r   r>   z&AsyncFileCheckpoints.rename_checkpoint   s&     **=(CC**=(CC7>>+&& 	FHNN.  
 !!## F Fv{KEEEEEEEEEF F F F F F F F F F F F F F F F F F	F 	Fs   >"B--B14B1c                   K   |                     d          }|                     ||          }t          j                            |          s|                     ||           | j                            d|           |                                 5  t          t          j
        |           d{V  ddd           dS # 1 swxY w Y   dS r@   )rB   r"   r   r'   r3   rC   r4   r5   r6   r   rD   rE   s       r   rG   z&AsyncFileCheckpoints.delete_checkpoint   s     zz#&&}d;;w~~g&& 	9##D-888~w/// 	/ 	/29g.........	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/s   !C  CCc                    K   |                     d          }d}|                     ||          }t          j                            |          sg S |                     ||           d{V gS )rI   rA   r   NrJ   rK   s       r   rM   z%AsyncFileCheckpoints.list_checkpoints   sx      
 zz#$&&}d;;w~~g&& 	II//wGGGGGGGGHHr   N)	rp   rq   rr   r+   r0   r$   r>   rG   rM   rt   r   r   rv   rv      sy        E E E. . .  F F F	/ 	/ 	/I I I I Ir   rv   c                   *    e Zd ZdZd Zd Zd Zd ZdS )GenericFileCheckpointszZ
    Local filesystem Checkpoints that works with any conforming
    ContentsManager.
    c                 F   |                     d          }d}|                     ||          }| j                            d|           |                                 5  |                     |||           ddd           n# 1 swxY w Y   |                     ||          S 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F3T::: 	H 	HOO.OGGG	H 	H 	H 	H 	H 	H 	H 	H 	H 	H 	H 	H 	H 	H 	H $$]4FGGGs   BB	Bc                 B   |                     d          }d}|                     ||          }| j                            d|           |                                 5  |                     ||           ddd           n# 1 swxY w Y   |                     ||          S 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F3T::: 	8 	8 2B777	8 	8 	8 	8 	8 	8 	8 	8 	8 	8 	8 	8 	8 	8 	8 $$]4FGGGs   B  BBc                 .   |                     d          }| j                            d||           |                     ||          }t          j                            |          s|                     ||           d|                     |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MM!11-FFw~~011 	9##D-888 **" +  
 
 	
r   c                 :   |                     d          }| j                            d||           |                     ||          }t          j                            |          s|                     ||           |                     |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MM!11-FFw~~011 	9##D-888//*<T/JJ
 
 	
r   Nrp   rq   rr   rs   r   r   r   r   rt   r   r   r~   r~      s`         
H H HH H H
 
 
"
 
 
 
 
r   r~   c                   *    e Zd ZdZd Zd Zd Zd ZdS )AsyncGenericFileCheckpointszg
    Asynchronous Local filesystem Checkpoints that works with any conforming
    ContentsManager.
    c                 b  K   |                     d          }d}|                     ||          }| j                            d|           |                                 5  |                     |||           d{V  ddd           n# 1 swxY w Y   |                     ||           d{V S r   r   r   s         r   r   z2AsyncGenericFileCheckpoints.create_file_checkpoint  s     zz#$!11-FF3T::: 	N 	N//"4gf/MMMMMMMMM	N 	N 	N 	N 	N 	N 	N 	N 	N 	N 	N 	N 	N 	N 	N **=:LMMMMMMMMMs   B

BBc                 ^  K   |                     d          }d}|                     ||          }| j                            d|           |                                 5  |                     ||           d{V  ddd           n# 1 swxY w Y   |                     ||           d{V S r   r   r   s        r   r   z6AsyncGenericFileCheckpoints.create_notebook_checkpoint   s     zz#$!11-FF3T::: 	> 	>%%&8"=========	> 	> 	> 	> 	> 	> 	> 	> 	> 	> 	> 	> 	> 	> 	> **=:LMMMMMMMMMs   BBBc                 >  K   |                     d          }| j                            d||           |                     ||          }t          j                            |          s|                     ||           d|                     |d           d{V dS )r   rA   r   r   r   r   Nr   r   r   s       r   r   z3AsyncGenericFileCheckpoints.get_notebook_checkpoint-  s      zz#7}MMM!11-FFw~~011 	9##D-888 !00" 1        
 
 	
r   c                 J  K   |                     d          }| j                            d||           |                     ||          }t          j                            |          s|                     ||           |                     |d           d{V \  }}d||dS r   r   r   s         r   r   z/AsyncGenericFileCheckpoints.get_file_checkpoint>  s      zz#7}MMM!11-FFw~~011 	9##D-888 $0B4 P PPPPPPP
 
 	
r   Nr   rt   r   r   r   r     s`         
N N NN 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 0 0 0 0 0 ! ! ! ! ! !       $ $ $ $ $ $            < ; ; ; ; ; ; ;oS oS oS oS oS& oS oS oSd=I =I =I =I =I?,ACS =I =I =I@?
 ?
 ?
 ?
 ?
4o ?
 ?
 ?
D?
 ?
 ?
 ?
 ?
">@T ?
 ?
 ?
 ?
 ?
r   