
    ^h10                         S r SSKrSSKrSSKrSSKJr  SSKJr  SSKJ	r	  SSK
Jr  SSKJr  SS	KJrJrJrJr  SS
KJrJr   " S S\\5      r " S S\\\5      r " S S\\5      r " S S\\5      rg)z)
File-based Checkpoints implementations.
    N)run_sync)ensure_dir_exists	HTTPError)Unicode)_tz   )AsyncCheckpointsAsyncGenericCheckpointsMixinCheckpointsGenericCheckpointsMixin)AsyncFileManagerMixinFileManagerMixinc                   j    \ rS rSrSr\" SSSS9r\" SS9rS rS	 r	S
 r
S rS rS rS rS rS rSrg)FileCheckpoints   z
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                 z    U R                   (       d  [        R                  " 5       $ U R                   R                  $ )N)parentosgetcwdroot_dir)selfs    j/home/james-whalen/.local/lib/python3.13/site-packages/jupyter_server/services/contents/filecheckpoints.py_root_dir_default!FileCheckpoints._root_dir_default/   s%    {{99;{{###    c                     SnUR                  U5      nU R                  X25      nU R                  XE5        U R                  X55      $ )Create a checkpoint.
checkpoint_get_os_pathcheckpoint_path_copycheckpoint_modelr   contents_mgrpathcheckpoint_idsrc_path	dest_paths         r   create_checkpoint!FileCheckpoints.create_checkpoint5   sD    $,,T2((=	

8'$$]>>r   c                 j    U R                  X#5      nUR                  U5      nU R                  XE5        gzRestore a checkpoint.Nr$   r#   r%   r   r(   r*   r)   r+   r,   s         r   restore_checkpoint"FileCheckpoints.restore_checkpoint=   s.    ''< --d3	

8'r   c                 L   U R                  X5      nU R                  X5      n[        R                  R                  U5      (       aM  U R                  R                  SUU5        U R                  5          [        R                  " XE5        SSS5        gg! , (       d  f       g= f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_checkpoint!FileCheckpoints.rename_checkpointD   s~    **=C**=C77>>+&&HHNN.
 !!#K5 $# ' $#s   4B
B#c                 j   UR                  S5      nU R                  X5      n[        R                  R	                  U5      (       d  U R                  X!5        U R                  R                  SU5        U R                  5          [        R                  " U5        SSS5        g! , (       d  f       g= fzdelete a file's checkpoint/zunlinking %sN)
stripr$   r   r)   r7   no_such_checkpointr8   r9   r:   unlinkr   r*   r)   cp_paths       r   delete_checkpoint!FileCheckpoints.delete_checkpointQ   sv    zz#&&};ww~~g&&##D8~w/IIg  s   B$$
B2c                     UR                  S5      nSnU R                  X!5      n[        R                  R	                  U5      (       d  / $ U R                  X#5      /$ )nlist the checkpoints for a given file

This contents manager currently only supports one checkpoint per file.
rF   r!   rG   r$   r   r)   r7   r&   r   r)   r*   os_paths       r   list_checkpoints FileCheckpoints.list_checkpoints\   sR    
 zz#$&&};ww~~g&&I))-ABBr   c                 (   UR                  S5      nSU-   R                  SS5      u  p4UR                  S5      n[        R                  R	                  U5      u  pVU SU U 3nU R                  US9n[        R                  R                  XR                  5      n	[        R                  " [        R                  R                  U	5      [        R                  5      (       d[  [        R                  R                  XR                  S9n
[        R                  R                  [        R                  " 5       SU
5      n	U R                  5          [!        U	5        SSS5        [        R                  R                  X5      nU$ ! , (       d  f       N/= f)zfind the path to a checkpointrF   r	   -)r)   )startjupyter_checkpointsN)rG   rsplitr   r)   splitextr#   joincheckpoint_diraccessdirnameW_OKrelpathr   tempfile
gettempdirr:   r   )r   r*   r)   r   namebasenameextfilenamerR   cp_dirrelrK   s               r   r$   FileCheckpoints.checkpoint_pathj   s   zz#d
**32c"((.Zqu5###0g':':;yy0"''::''//'/?CWW\\("5"5"79NPSTFf%  '',,v0  s   F
Fc                 |    [         R                  " U5      n[        R                  " UR                  5      nUUS.nU$ ).construct the info dict for a given checkpointidlast_modified)r   stattzutcfromtimestampst_mtimer   r*   rR   statsrn   infos         r   r&    FileCheckpoints.checkpoint_model|   s8     ++ENN;*
 r   c                 &    [        SSU SU 35      e)Ni  zCheckpoint does not exist: @r   )r   r)   r*   s      r   rH   "FileCheckpoints.no_such_checkpoint   s    :4&-QRRr    N)__name__
__module____qualname____firstlineno____doc__r   r\   r   r   r-   r3   rB   rL   rS   r$   r&   rH   __static_attributes__rz   r   r   r   r      sY     	N d#H$?(6	C$Sr   r   c                   8    \ rS rSrS rS rS rS rS rS r	Sr
g	)
AsyncFileCheckpoints   c                    #    SnUR                  U5      nU R                  X25      nU R                  XE5      I Sh  vN   U R                  X55      I Sh  vN $  N N7f)r    r!   Nr"   r'   s         r   r-   &AsyncFileCheckpoints.create_checkpoint   sX     $,,T2((=	jj---**=DDD 	.Ds!   9AAAAAAc                    #    U R                  X#5      nUR                  U5      nU R                  XE5      I Sh  vN   g N7fr0   r1   r2   s         r   r3   'AsyncFileCheckpoints.restore_checkpoint   s7     ''< --d3	jj---s   7A?Ac                    #    [        [        R                  U5      I Sh  vN n[        R                  " UR
                  5      nUUS.nU$  N+7f)rk   Nrl   )r   r   ro   rp   rq   rr   rs   s         r   r&   %AsyncFileCheckpoints.checkpoint_model   sE     rww00++ENN;*
  1s   AA,Ac                 p  #    U R                  X5      nU R                  X5      n[        R                  R                  U5      (       aY  U R                  R                  SUU5        U R                  5          [        [        R                  XE5      I Sh  vN   SSS5        gg N! , (       d  f       g= f7fr6   )
r$   r   r)   r7   r8   r9   r:   r   r;   r<   r=   s         r   rB   &AsyncFileCheckpoints.rename_checkpoint   s     **=C**=C77>>+&&HHNN.
 !!#v{{KEEE $# ' F $#s0   A4B66B%B#B%
B6#B%%
B3/B6c                   #    UR                  S5      nU R                  X5      n[        R                  R	                  U5      (       d  U R                  X!5        U R                  R                  SU5        U R                  5          [        [        R                  U5      I Sh  vN   SSS5        g N! , (       d  f       g= f7frE   )rG   r$   r   r)   r7   rH   r8   r9   r:   r   rI   rJ   s       r   rL   &AsyncFileCheckpoints.delete_checkpoint   s     zz#&&};ww~~g&&##D8~w/299g...  .  s0   BCB4$B2%B4)	C2B44
C>Cc                    #    UR                  S5      nSnU R                  X!5      n[        R                  R	                  U5      (       d  / $ U R                  X#5      I Sh  vN /$  N7f)rO   rF   r!   NrP   rQ   s       r   rS   %AsyncFileCheckpoints.list_checkpoints   s[     
 zz#$&&};ww~~g&&I//GGHHGs   AA)!A'"A)rz   N)r{   r|   r}   r~   r-   r3   r&   rB   rL   rS   r   rz   r   r   r   r      s#    E.F	/Ir   r   c                   0    \ rS rSrSrS rS rS rS rSr	g)	GenericFileCheckpoints   zN
Local filesystem Checkpoints that works with any conforming
ContentsManager.
c                    UR                  S5      nSnU R                  XC5      nU R                  R                  SU5        U R	                  5          U R                  XQUS9  SSS5        U R                  XE5      $ ! , (       d  f       N= fz7Create a checkpoint from the current content of a file.rF   r!   creating checkpoint for %sformatNrG   r$   r8   r9   r:   
_save_filer&   r   contentr   r)   r*   os_checkpoint_paths         r   create_file_checkpoint-GenericFileCheckpoints.create_file_checkpoint   sv    zz#$!11-F3T:OO.OG   $$]GG	  s   A;;
B	c                    UR                  S5      nSnU R                  X25      nU R                  R                  SU5        U R	                  5          U R                  XA5        SSS5        U R                  X45      $ ! , (       d  f       N= fz;Create a checkpoint from the current content of a notebook.rF   r!   r   NrG   r$   r8   r9   r:   _save_notebookr&   r   nbr)   r*   r   s        r   create_notebook_checkpoint1GenericFileCheckpoints.create_notebook_checkpoint   st    zz#$!11-F3T: 27   $$]GG	  s   A<<
B
c                    UR                  S5      nU R                  R                  SX!5        U R                  X5      n[        R
                  R                  U5      (       d  U R                  X!5        SU R                  USS9S.$ ) Get a checkpoint for a notebook.rF   restoring %s from checkpoint %snotebook   
as_versiontyper   	rG   r8   ru   r$   r   r)   r7   rH   _read_notebookr   r*   r)   r   s       r   get_notebook_checkpoint.GenericFileCheckpoints.get_notebook_checkpoint   s}    zz#7M!11-Fww~~011##D8 **" + 
 	
r   c                    UR                  S5      nU R                  R                  SX!5        U R                  X5      n[        R
                  R                  U5      (       d  U R                  X!5        U R                  USS9u  pESUUS.$ zGet a checkpoint for a file.rF   r   Nr   file)r   r   r   	rG   r8   ru   r$   r   r)   r7   rH   
_read_filer   r*   r)   r   r   r   s         r   get_file_checkpoint*GenericFileCheckpoints.get_file_checkpoint   s}    zz#7M!11-Fww~~011##D8//*<T/J
 	
r   rz   N
r{   r|   r}   r~   r   r   r   r   r   r   rz   r   r   r   r      s    
HH
"
r   r   c                   0    \ rS rSrSrS rS rS rS rSr	g)	AsyncGenericFileCheckpointsi  z[
Asynchronous Local filesystem Checkpoints that works with any conforming
ContentsManager.
c                 H  #    UR                  S5      nSnU R                  XC5      nU R                  R                  SU5        U R	                  5          U R                  XQUS9I Sh  vN   SSS5        U R                  XE5      I Sh  vN $  N%! , (       d  f       N)= f N7fr   r   r   s         r   r   2AsyncGenericFileCheckpoints.create_file_checkpoint  s     zz#$!11-F3T://"4f/MMM   **=MMM N   Ns<   AB"B'B(B,B"B 	B"B
BB"c                 J  #    UR                  S5      nSnU R                  X25      nU R                  R                  SU5        U R	                  5          U R                  XA5      I Sh  vN   SSS5        U R                  X45      I Sh  vN $  N%! , (       d  f       N)= f N7fr   r   r   s        r   r   6AsyncGenericFileCheckpoints.create_notebook_checkpoint   s     zz#$!11-F3T:%%&8===   **=MMM >   Ns<   AB#B(B)B-B#	B!
B#B
BB#c                 *  #    UR                  S5      nU R                  R                  SX!5        U R                  X5      n[        R
                  R                  U5      (       d  U R                  X!5        SU R                  USS9I Sh  vN S.$  N7f)r   rF   r   r   r   r   Nr   r   r   s       r   r   3AsyncGenericFileCheckpoints.get_notebook_checkpoint-  s     zz#7M!11-Fww~~011##D8 !00" 1  
 	
s   BB
BBc                 4  #    UR                  S5      nU R                  R                  SX!5        U R                  X5      n[        R
                  R                  U5      (       d  U R                  X!5        U R                  USS9I Sh  vN u  pESUUS.$  N7fr   r   r   s         r   r   /AsyncGenericFileCheckpoints.get_file_checkpoint>  s     zz#7M!11-Fww~~011##D8 $0B4 PP
 	
 Qs   BB	B
Brz   Nr   rz   r   r   r   r     s    
NN
"
r   r   )r   r   r;   ra   anyio.to_threadr   jupyter_core.utilsr   tornado.webr   	traitletsr   jupyter_serverr   rp   checkpointsr
   r   r   r   fileior   r   r   r   r   r   rz   r   r   <module>r      s|    
   $ 0 !  $  <oS& oSd=I?,ACS =I@?
4o ?
D?
">@T ?
r   