
    Ked                        d Z ddlZddlZddlZddlZddlZddlZddl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ZddlmZ ddlmZmZmZ ddlmZ ddlmZ dd	lmZmZmZmZmZ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* ddl+m,Z,m-Z- ddl.m/Z/m0Z0m1Z1 	 ddl2m3Z3 n# e4$ r	 ddl&m5Z3 Y nw xY wdZ6 G d de-e0          Z7 G d de7e,e/          Z8dS )z?A contents manager that uses the local file system for storage.    N)datetime)Path)run_sync)existsis_file_hidden	is_hidden)
send2trash)web)BoolInt
TraitErrorUnicodedefaultvalidate)_tzAuthenticatedFileHandler)_i18n)to_api_path   )AsyncFileCheckpointsFileCheckpoints)AsyncFileManagerMixinFileManagerMixin)AsyncContentsManagerContentsManagercopy_pat)samefile)samefile_simplec                   4    e Zd ZdZ ed          Z eddd          Z ed          d             Z	 e
d          d	             Z ed
          d             Z e
d
           fd            Z ed          d             Z eddd          Z eddd          Z ed          d             Z ed          d             Zd Zd Zd Zd Zd Zd Zd.dZd/dZd.dZd0d Zd1d"Zd1d#Zd$ Z d% Z!d& Z"d2d'Z#d2 fd(	Z$d) Z%d* Z&d3d,Z'd- Z( xZ)S )4FileContentsManagerzA file contents manager.T)config  z&The max folder size that can be copied)r"   helproot_dirc                 N    | j         st          j                    S | j         j        S N)parentosgetcwdr%   selfs    Llib/python3.11/site-packages/jupyter_server/services/contents/filemanager.py_default_root_dirz%FileContentsManager._default_root_dir1   s#    { 	9;;{##    c                     |d         }t           j                            |          st           j                            |          }t           j                            |          st          d|z            |S )Nvaluez%r is not a directory)r)   pathisabsabspathisdirr   )r,   proposalr1   s      r-   _validate_root_dirz&FileContentsManager._validate_root_dir7   sd    !w}}U## 	+GOOE**Ew}}U## 	>4u<===r/   preferred_dirc                 n   | j         sdS 	 | j         j        }|| j         j        k    rd }|yt          j        dt
          d           	 t          |          }|                    | j                                                  S # t          $ r t          d|z            d w xY wn# t          $ r Y nw xY wdS )N zqServerApp.preferred_dir config is deprecated in jupyter-server 2.0. Use FileContentsManager.preferred_dir instead   )
stacklevelz%%s is outside root contents directory)r(   r8   r%   warningswarnFutureWarningr   relative_toas_posix
ValueErrorr   AttributeError)r,   r1   r2   s      r-   _default_preferred_dirz*FileContentsManager._default_preferred_dirA   s    { 	2	`K-E,,,   H!    
`;;D++DM::CCEEE! ` ` `$%Lu%TUU[__` !  	 	 	D	 rs   B% :B B!%
B21B2c                     t          |d         | j                  |d<   t                                          |          S )Nr1   )r   r%   super_validate_preferred_dir)r,   r6   	__class__s     r-   rG   z+FileContentsManager._validate_preferred_dirY   s8     ((94=IIww..x888r/   checkpoints_classc                     t           S r'   )r   r+   s    r-   _checkpoints_class_defaultz.FileContentsManager._checkpoints_class_default_   s    r/   zIf True (default), deleting files will send them to the
        platform's trash/recycle bin, where they can be recovered. If False,
        deleting files really deletes them.Fa  If True, deleting a non-empty directory will always be allowed.
        WARNING this may result in files being permanently removed; e.g. on Windows,
        if the data size is too big for the trash/recycle bin the directory will be permanently
        deleted. If False (default), the non-empty directory will be sent to the trash only
        if safe. And if ``delete_to_trash`` is True, the directory won't be deleted.files_handler_classc                     t           S r'   r   r+   s    r-   _files_handler_class_defaultz0FileContentsManager._files_handler_class_defaultu   s    ''r/   files_handler_paramsc                     d| j         iS )Nr2   )r%   r+   s    r-   _files_handler_params_defaultz1FileContentsManager._files_handler_params_defaulty   s    &&r/   c                     |                     d          }|                     |          }t          || j                  S )aT  Does the API style path correspond to a hidden directory or file?

        Parameters
        ----------
        path : str
            The path to check. This is an API path (`/` separated,
            relative to root_dir).

        Returns
        -------
        hidden : bool
            Whether the path exists and is hidden.
        /r2   strip_get_os_pathr   r%   r,   r2   os_paths      r-   r   zFileContentsManager.is_hidden}   s;     zz####..$-000r/   c                     |                     d          }|                     |          }	 t          j        |t          j                  S # t
          $ r | j                            d|           Y dS w xY w)aX  Does the API style path correspond to a writable directory or file?

        Parameters
        ----------
        path : str
            The path to check. This is an API path (`/` separated,
            relative to root_dir).

        Returns
        -------
        hidden : bool
            Whether the path exists and is writable.
        rS   rT   z'Failed to check write permissions on %sF)rV   rW   r)   accessW_OKOSErrorlogerrorrX   s      r-   is_writablezFileContentsManager.is_writable   s{     zz####..	9Wbg... 	 	 	HNNDgNNN55	s   A %A54A5c                     |                     d          }|                     |          }t          j                            |          S )aC  Returns True if the file exists, else returns False.

        API-style wrapper for os.path.isfile

        Parameters
        ----------
        path : str
            The relative path to the file (with '/' as separator)

        Returns
        -------
        exists : bool
            Whether the file exists.
        rS   rV   rW   r)   r2   isfilerX   s      r-   file_existszFileContentsManager.file_exists   s:     zz###D))w~~g&&&r/   c                     |                     d          }|                     |          }t          j                            |          S )av  Does the API-style path refer to an extant directory?

        API-style wrapper for os.path.isdir

        Parameters
        ----------
        path : str
            The path to check. This is an API path (`/` separated,
            relative to root_dir).

        Returns
        -------
        exists : bool
            Whether the path is indeed a directory.
        rS   rT   rV   rW   r)   r2   r5   rX   s      r-   
dir_existszFileContentsManager.dir_exists   s=      zz####..w}}W%%%r/   c                 v    |                     d          }|                     |          }t          |          S )a@  Returns True if the path exists, else returns False.

        API-style wrapper for os.path.exists

        Parameters
        ----------
        path : str
            The API path to the file (with '/' as separator)

        Returns
        -------
        exists : bool
            Whether the target exists.
        rS   rT   )rV   rW   r   rX   s      r-   r   zFileContentsManager.exists   s5     zz####..gr/   c           	         |                      |          }t          j        |          }d|z  }| j        sEt	          || j                  r0| j                            d|           t          j	        d|          	 |j
        }n3# t          t          f$ r | j                            d           d}Y nw xY w	 t          j        |j                  }nW# t          t          f$ rC | j                            d|j        |           t#          dddd	d	t          j        
          }Y nw xY w	 t          j        |j                  }nW# t          t          f$ rC | j                            d|j        |           t#          dddd	d	t          j        
          }Y nw xY wi }|                    dd          d         |d<   ||d<   ||d<   ||d<   d|d<   d|d<   d|d<   ||d<   |                     |          |d<   |S )z)Build the common base of a contents model$file or directory does not exist: %r<Refusing to serve hidden file or directory %r, via 404 Error  zUnable to get size.NzInvalid mtime %s for %si  r   r   )tzinfozInvalid ctime %s for %srS   namer2   last_modifiedcreatedcontentformatmimetypesizewritable)rW   r)   lstatallow_hiddenr   r%   r^   infor
   	HTTPErrorst_sizerB   r]   warningtzutcfromtimestampst_mtimer   UTCst_ctimersplitr`   )	r,   r2   rY   ry   four_o_fourru   rp   rq   models	            r-   _base_modelzFileContentsManager._base_model   s?   ##D))x  <tC  	2Yw%F%F 	2HMMXZabbb-[111	<DDG$ 	 	 	H2333DDD		F/>>MMG$ 	F 	F 	F
 H6wOOO$T1aAbfEEEMMM	F	@)$-88GGG$ 	@ 	@ 	@H6wOOOtQ1a???GGG	@
 C++B/ff!.o"iih jf ,,T22js8   <B -B43B48C AD&%D&*E AFFc                    |                      |          }d|z  }t          j                            |          st	          j        d|          | j        sEt          || j                  r0| j	        
                    d|           t	          j        d|          |                     |          }d|d<   d|d<   |r7g x|d<   }|                      |          }t          j        |          D ] }	 t          j                            ||          }n3# t          $ r&}	| j	                            d	||	           Y d}	~	Pd}	~	ww xY w	 t          j        |          }
ny# t"          $ rl}	|	j        t$          j        k    r| j	                            d
|           n1|	j        t$          j        k    r| j	                            d||	           Y d}	~	d}	~	ww xY wt+          j        |
j                  sOt+          j        |
j                  s6t+          j        |
j                  s| j	                            d|           O	 |                     |          rG| j        st9          ||
          s/|                    |                     | d| d                     # t"          $ rG}	|	j        t$          j        t$          j        fvr| j	                            d|d           Y d}	~	d}	~	ww xY wd|d<   |S zpBuild a model for a directory

        if content is requested, will include a listing of the directory
        zdirectory does not exist: %rrl   z4Refusing to serve hidden directory %r, via 404 Error	directorytypeNru   rr   z"failed to decode filename '%s': %rz%s doesn't existzError stat-ing %s: %rz%s not a regular file)stat_resrS   F)r2   rr   z+Unknown error checking if file %r is hiddenTexc_infojsonrs   ) rW   r)   r2   r5   r
   rz   rx   r   r%   r^   ry   r   listdirjoinUnicodeDecodeErrorr|   rw   r]   errnoENOENTEACCESstatS_ISLNKst_modeS_ISREGS_ISDIRdebugshould_listr   appendgetELOOP)r,   r2   rr   rY   r   r   contentsos_dirro   ests              r-   
_dir_modelzFileContentsManager._dir_model  s1   
 ##D))4t;w}}W%% 	2-[111" 	2y$-'H'H 	2HMMPRYZZZ-[111  &&#ff *	%*,,E)x&&t,,F
6** % % gll6488GG)   H$$%I4QRSSSHHHH'**BB   w%,..(();WEEEEEL00(()@'1MMMHHHH RZ00 L44 !L44
 HNN#:GDDD''-- X)X1?RT1U1U1UX !6F6F6F6FPU(V(VWWW   wu{EL&AAA((I#%) )    %E(OsJ   1 D
ED==EE
G%A"GG=AJ
K,%<K''K,Nc                 2   |                      |          }d|d<   |                     |          }t          j        |          d         |d<   |rH|                     ||          \  }}|d         ddd|         }||d<   |                    ||	           |S 
a@  Build a model for a file

        if content is requested, include the file contents.

        format:
          If 'text', the contents will be decoded as UTF-8.
          If 'base64', the raw bytes contents will be encoded as base64.
          If not specified, try to decode as UTF-8, and fall back to base64
        filer   r   rt   Nz
text/plainzapplication/octet-stream)textbase64rr   rs   r   rW   	mimetypes
guess_type
_read_fileupdater,   r2   rr   rs   r   rY   default_mimes          r-   _file_modelzFileContentsManager._file_modelO  s       &&f##D))%099!<j 	"oogv>>OGVZ ((8      %1j!LL    
 r/   c                    |                      |          }d|d<   |                     |          }|rPi }|                     |d|          }|                     ||           ||d<   d|d<   |                     ||           |S )Build a notebook model

        if content is requested, the notebook content will be populated
        as a JSON structure (not double-serialized)
        notebookr      
as_versioncapture_validation_errorrr   r   rs   r   rW   _read_notebookmark_trusted_cellsvalidate_notebook_modelr,   r2   rr   r   rY   validation_errornbs          r-   _notebook_modelz#FileContentsManager._notebook_modelo  s       &&"f##D)) 	B%'$$A@P %  B ##B---!E)$E(O((0@AAAr/   c                 
   |                     d          }|                     |          }d|z  }|                     |          st          j        d|          | j        sEt          || j                  r0| j        	                    d|           t          j        d|          t          j                            |          r8|dvrt          j        d| d| d	          |                     ||
          }nm|dk    s|-|                    d          r|                     ||
          }n8|dk    rt          j        dd|z  d	          |                     |||          }|                     d|d           |S )  Takes a path for an entity and returns its model

        Parameters
        ----------
        path : str
            the API path that describes the relative path for the target
        content : bool
            Whether to include the contents in the reply
        type : str, optional
            The requested type - 'file', 'notebook', or 'directory'.
            Will raise HTTPError 400 if the content doesn't match.
        format : str, optional
            The requested format for file contents. 'text' or 'base64'.
            Ignored if this returns a notebook or directory model.

        Returns
        -------
        model : dict
            the contents model. If content=True, returns the contents
            of the file or directory as well.
        rS   rj   rl   rk   Nr      is a directory, not a bad typereasonrr   r   N.ipynbr   %s is not a directoryr   r   actionr2   data)rV   rW   r   r
   rz   rx   r   r%   r^   ry   r)   r2   r5   r   endswithr   r   emit)r,   r2   rr   r   rs   rY   r   r   s           r-   r   zFileContentsManager.get  s   , zz###D))<tC{{4   	2-[111  	2Yw%F%F 	2HMMXZabbb-[1117==!! 	K...m::D::%   
 OOD'O::EEZDLT]]85L5LL((w(??EE{""mC)@4)GPZ[[[[$$T76$JJE		%66	777r/   r:   c                    | j         s-t          || j                  rt          j        dd|z            t
          j                            |          sC|                                 5  t          j	        |           ddd           dS # 1 swxY w Y   dS t
          j        
                    |          st          j        dd|z            | j                            d|           dS )create a directoryr   zCannot create directory %rNNot a directory: %sDirectory %r already exists)rx   r   r%   r
   rz   r)   r2   r   perm_to_403mkdirr5   r^   r   r,   rY   r   r2   s       r-   _save_directoryz#FileContentsManager._save_directory  s#     	MYw%F%F 	M-%AG%KLLLw~~g&& 	C!!## " "!!!" " " " " " " " " " " " " " " " " "w'' 	C-%:g%FGGGHNN8'BBBBBs   (B

BBc                 x   |                     d          }|                     ||           d|vrt          j        dd          d|vr!|d         dk    rt          j        dd          |                     |          }| j        s-t          || j                  rt          j        dd	|          | j        	                    d
|           i }	 |d         dk    rxt          j        |d                   }|                     ||           |                     |||           | j                            |          s|                     |           n|d         dk    r1|                     ||d         |                    d                     nB|d         dk    r|                     |||           nt          j        dd|d         z            n[# t          j        $ r  t(          $ r?}| j                            d||d           t          j        dd| d|           |d}~ww xY wd}|d         dk    r-|                     ||           |                    dd          }|                     |d          }|r||d<   |                     ||           |                     d|d           |S )9Save the file model and return the model with no content.rS   r   r2   r   r   No file type providedrr   r   No file content providedz Cannot create file or directory 	Saving %sr   r   r   rs   Unhandled contents type: %sError while saving file: %s %sTr   r#   $Unexpected error while saving file:  Nr   messageFr   r   rY   saver   r   )rV   run_pre_save_hooksr
   rz   rW   rx   r   r%   r^   r   nbformat	from_dictcheck_and_sign_save_notebookcheckpointslist_checkpointscreate_checkpoint
_save_filer   r   	Exceptionr_   r   run_post_save_hooksr   r,   r   r2   rY   r   r   r   validation_messages           r-   r   zFileContentsManager.save  s   zz#e$777-%<===E!!eFm{&B&B-%?@@@##D))  	UYw%F%F 	U-%S%S%STTT{G,,,!#	_V}
**'i(899##B---##GRJZ#[[['88>> 1**4000v&((y)9599X;N;NOOOOv+--$$WeT::::mC)Fv)VWWW} 	 	 	 	_ 	_ 	_HNN;T1tNTTT-%VD%V%VST%V%VWW]^^	_ "=J&&((AQ(RRR!&9d!;!;u-- 	21E)  ug >>>		&$77	888s   DG H./:H))H.c                     |                     d          }                     |          }t          j        } j        s-t          | j                  rt          j        dd|          d|z  } 	                    |          st          j        d|           fd} j
        r j        s3t          j        dk    r# ||          rt          j        dd|z                                 |          st          j        d	d
|z            d j                            d|           	 t#          |           n+# t$          $ r}t          j        dd|z            |d}~ww xY wdS t          j                            |          r j        s# ||          rt          j        dd|z             j                            d|                                            5  t-          j        |           ddd           dS # 1 swxY w Y   dS  j                            d|                                            5   ||           ddd           dS # 1 swxY w Y   dS )Delete file at path.rS   r    Cannot delete file or directory rj   rl   c                     t           j                            |           r=t          j        dd           }t          t          j        |                     |hz
  rdS dS Ncheckpoint_dirTF)r)   r2   r5   getattrr   setr   )rY   cp_dirr,   s     r-   is_non_empty_dirz9FileContentsManager.delete_file.<locals>.is_non_empty_dir  s[    w}}W%%   !!13CTJJrz'**++vh6  45r/   win32Directory %s not empty  Permission denied: %sNSending %s to trashzsend2trash failed: %sRemoving directory %sUnlinking file %s)rV   rW   r)   unlinkrx   r   r%   r
   rz   r   delete_to_trashalways_delete_dirsysplatformr`   r^   r   r	   r]   r2   r5   r   shutilrmtree)r,   r2   rY   rmr   r   r   s   `      r-   delete_filezFileContentsManager.delete_file  s)   zz###D))Y  	UYw%F%F 	U-%S%S%STTT<tC{{4   	2-[111	 	 	 	 	  	) Mclg.E.EJZJZ[bJcJc.E mC)AG)KLLL##D)) SmC)@4)GHHdRHNN0':::M7#### M M MmC)@1)DEE1LMF7==!! 
	) M.>.>w.G.G MmC)AG)KLLLHNN2G<<<!!## ' 'g&&&' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' HNN.888!!##  7                 s<   +D; ;
E#EE# HH	H>IIIc                    |                     d          }|                     d          }||k    rdS |                     |          }|                     |          }| j        sBt          || j                  st          || j                  rt          j        dd|          t          j        	                    |          r(t          ||          st          j        dd|z            	 |                                 5  t          j        ||           ddd           dS # 1 swxY w Y   dS # t
          j        $ r  t          $ r!}t          j        dd| d	|           |d}~ww xY w
zRename a file.rS   Nr   z Cannot rename file or directory i  zFile already exists: %sr#   zUnknown error renaming file: r   )rV   rW   rx   r   r%   r
   rz   r)   r2   r   r   r   r  mover   r,   old_pathnew_pathnew_os_pathold_os_pathr   s         r-   rename_filezFileContentsManager.rename_file)  s   >>#&&>>#&&xF''11''11  	Yk4=11	Y5>{DM5Z5Z	Y -%W%W%WXXX 7>>+&& 	Kx[/Q/Q 	K-%>%IJJJ	\!!## 6 6K5556 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6} 	 	 	 	\ 	\ 	\-%SX%S%SPQ%S%STTZ[[	\s<   .D2 D%D2 %D))D2 ,D)-D2 2E,E''E,c                 0    t          d          | j        z  S )z+Get the information string for the manager.z*Serving notebooks from local directory: %s)r   r%   r+   s    r-   info_stringzFileContentsManager.info_stringE  s    ABBT]RRr/   c                 x    |                      |          r|S d|v r|                    dd          d         nd}|S )zIReturn the initial API path of  a kernel associated with a given notebookrS   r   r   r:   rg   r   r,   r2   r   
parent_dirs       r-   get_kernel_pathz#FileContentsManager.get_kernel_pathI  sF    ??4   	K/2d{{T[[a((++
r/   c                    t          |          }|                    d          }||                    d          }d|v r|                    dd          \  }}nd}|}|                     |          }|d         dk    r|                     |           n#t                                          ||          S |du}t          j        d|          }	|s|}| 	                    |          r9t          j        d|          }
t                      
                    |
|d	
          }	| d|	 }|                     |||	|          S aA  
        Copy an existing file or directory and return its new model.
        If to_path not specified, it will be the parent directory of from_path.
        If copying a file and to_path is a directory, filename/directoryname will increment `from_path-Copy#.ext`.
        Considering multi-part extensions, the Copy# part will be placed before the first dot for all the extensions except `ipynb`.
        For easier manual searching in case of notebooks, the Copy# part will be placed before the last dot.
        from_path must be a full path to a file or directory.
        rS   Nr   r:   r   r   )	from_pathto_path.z-Copy)insert)r!  to_path_originalto_namer"  )strrV   r   r   check_folder_sizerF   copyr   subrg   increment_filename	_copy_dirr,   r!  r"  r%  r2   from_dir	from_namer   is_destination_specifiedr&  ro   rH   s              r-   r)  zFileContentsManager.copyP  s^    w<<s##mmC((G$;;"&++c1"5"5HiiHI=K''""4(((( 77<<)W<EEE#*$#6 ,sI..' 	G??7## 	P<Y//Dgg00ww0OOG((w((~~-	  
 
 	
r/   c           	         	 |                      |                    d                    }|                      |                    d                     d| }t          j        ||           |                     |d          }nL# t
          $ r?}| j                            d|            t          j	        dd| d| d          |d	}~ww xY w|S )
`
        handles copying directories
        returns the model for the copied directory
        rS   Fr   OSError in _copy_dir: r   Can't copy 'z' into Folder ''N
rW   rV   r  copytreer   r]   r^   r_   r
   rz   	r,   r!  r%  r&  r"  os_from_path
os_to_pathr   errs	            r-   r,  zFileContentsManager._copy_dir|  s    

	,,Y__S-A-ABBL --.>.D.DS.I.IJJVVWVVJOL*555HHWeH44EE 	 	 	HNN9C99:::-CyCCCCC  	 s   BB 
C:CCc                 @   | j         dz  dz  }t          |                     |                     |                              }t	          j                    dk    r|dz  n|}||k    r7t          j        dd| j          d| d|                     |           d          dS )	
        limit the size of folders being copied to be no more than the
        trait max_copy_folder_size_mb to prevent a timeout error
           Darwinr   4
                    Can't copy folders larger than MB,
                    "" is 
                N	max_copy_folder_size_mbint_get_dir_sizerW   r
  systemr
   rz   _human_readable_sizer,   r2   limit_bytesru   s       r-   r(  z%FileContentsManager.check_folder_size  s    
 2T9D@4%%d&7&7&=&=>>??&o//8;;td{{+-484P  !%!:!:4!@!@     r/   r#  c                    	 t          j                    dk    r2t          j        dd|gdd          j                                        }n2t          j        ddd|gdd          j                                        }| j                            d|            |d	                             d
          }n,# t          $ r | j        
                    d|           Y dS w xY w|S zM
        calls the command line program du to get the directory size
        r?  duz-skT)capture_outputcheckz-sz--block-size=1zcurrent status of du command r   zutf-8z^Not able to get the size of the %s directory. Copying might be slow if the directory is large!0r
  rH  
subprocessrunstdoutsplitr^   ry   decoder   r|   r,   r2   resultru   s       r-   rG  z!FileContentsManager._get_dir_size  s	   	  H,,#5$'D    $4!148UY    HMMB&BBCCC!9##G,,DD 	 	 	Hp   33	 s   B3B6 6%CCc                     |dk    rdS g d}|r$t          t          j        |          dz            nd}|d|dz  z  z  dd||          S z@
        returns folder size in a human readable format
        r   z0 Bytes)BytesKBMBGBTBPB
   r   z.4gr   rF  mathlog2r,   ru   unitsorders       r-   rI  z(FileContentsManager._human_readable_size  sj     1999777-18DIdOOb()))q!
+,AAA5<AAAr/   TTNTNNr:   r'   r#  )*__name__
__module____qualname____doc__r   r%   r   rE  r   r.   r   r7   rD   rG   rK   r   r  r  rN   rQ   r   r`   rd   rg   r   r   r   r   r   r   r   r   r  r  r  r  r)  r,  r(  rG  rI  __classcell__rH   s   @r-   r!   r!   *   s6       ""wd###H!c#d9abbbWZ$ $ $
 Xj   W_  . Xo9 9 9 9 9
 W !!  "! d/  O X   W"##( ( $#( W#$$' ' %$'1 1 1$  ,' ' '&& & &(  &. . .`> > > >@   @   ,0 0 0 0d
C 
C 
C 
C2 2 2 2h0 0 0d\ \ \8S S S   *
 *
 *
 *
 *
 *
X  &  &   2
B 
B 
B 
B 
B 
B 
Br/   r!   c            
            e Zd ZdZ ed          d             Zd dZd!dZd dZd"d	Z	d#dZ
d#dZd Zd Zd Zd Zd Zd$dZd$ fd	Zdededededef
dZdeddfdZd%dedefdZdedefdZ xZS )&AsyncFileContentsManagerzAn async file contents manager.rI   c                     t           S r'   )r   r+   s    r-   rK   z3AsyncFileContentsManager._checkpoints_class_default  s    ##r/   Tc                 0  K   |                      |          }d|z  }t          j                            |          st	          j        d|          | j        sEt          || j                  r0| j	        
                    d|           t	          j        d|          |                     |          }d|d<   d|d<   |rWg x|d<   }|                      |          }t          t          j        |           d{V }|D ]}		 t          j                            ||	          }n3# t          $ r&}
| j	                            d	|	|
           Y d}
~
Pd}
~
ww xY w	 t          t          j        |           d{V }ny# t$          $ rl}
|
j        t&          j        k    r| j	                            d
|           n1|
j        t&          j        k    r| j	                            d||
           Y d}
~
d}
~
ww xY wt-          j        |j                  sOt-          j        |j                  s6t-          j        |j                  s| j	                            d|           [	 |                     |	          rM| j        st;          ||          s5|                    |                     | d|	 d           d{V            # t$          $ rG}
|
j        t&          j         t&          j        fvr| j	                            d|d           Y d}
~
d}
~
ww xY wd|d<   |S r   )!rW   r)   r2   r5   r
   rz   rx   r   r%   r^   ry   r   r   r   r   r   r|   rw   r]   r   r   r   r   r   r   r   r   r   r   r   r   r   r   )r,   r2   rr   rY   r   r   r   r   dir_contentsro   r   r   s               r-   r   z#AsyncFileContentsManager._dir_model  st     
 ##D))4t;w}}W%% 	2-[111" 	2y$-'H'H 	2HMMPRYZZZ-[111  &&#ff +	%*,,E)x&&t,,F!)"*f!=!=======L$ % % gll6488GG)   H$$%I4QRSSSHHHH''::::::::BB   w%,..(();WEEEEEL00(()@'1MMMHHHH RZ00 L44 !L44
 HNN#:GDDD''-- ^)^1?RT1U1U1U^ !dhht<L<Ld<L<LV[h.\.\(\(\(\(\(\(\]]]   wu{EL&AAA((I#%) )    %E(OsJ    D""
E,EE E77
G-A"G((G-A"J==
L<L		LNc                 B  K   |                      |          }d|d<   |                     |          }t          j        |          d         |d<   |rN|                     ||           d{V \  }}|d         ddd|         }||d<   |                    ||	           |S r   r   r   s          r-   r   z$AsyncFileContentsManager._file_model  s         &&f##D))%099!<j 	$(OOGV$D$DDDDDDDOGVZ ((8      %1j!LL    
 r/   c                   K   |                      |          }d|d<   |                     |          }|rVi }|                     |d|           d{V }|                     ||           ||d<   d|d<   |                     ||           |S )	r   r   r   r   r   Nrr   r   rs   r   r   s          r-   r   z(AsyncFileContentsManager._notebook_model0  s         &&"f##D)) 	B%'**A@P +        B ##B---!E)$E(O((0@AAAr/   c                   K   |                     d          }|                     |          st          j        dd|z            |                     |          }t
          j                            |          r>|dvrt          j        d| d| d          |                     ||	           d
{V }ny|dk    s|3|	                    d          r| 
                    ||	           d
{V }n>|dk    rt          j        dd|z  d          |                     |||           d
{V }|                     d|d           |S )r   rS   rl   zNo such file or directory: %sr   r   r   r   r   r   Nr   r   r   r   r   r   r   r   )rV   r   r
   rz   rW   r)   r2   r5   r   r   r   r   r   )r,   r2   rr   r   rs   rY   r   s          r-   r   zAsyncFileContentsManager.getF  s     , zz#{{4   	M-%Dt%KLLL##D))7==!! 	Q...m::D::%   
 //$/@@@@@@@@EEZDLT]]85L5LL..tW.EEEEEEEEEE{""mC)@4)GPZ[[[[**4*PPPPPPPPE		%66	777r/   r:   c                   K   | j         s-t          || j                  rt          j        dd|z            t
          j                            |          sO|                                 5  t          t
          j
        |           d{V  ddd           dS # 1 swxY w Y   dS t
          j                            |          st          j        dd|z            | j                            d|           dS )r   r   z!Cannot create hidden directory %rNr   r   )rx   r   r%   r
   rz   r)   r2   r   r   r   r   r5   r^   r   r   s       r-   r   z(AsyncFileContentsManager._save_directorys  s;       	TYw%F%F 	T-%H7%RSSSw~~g&& 	C!!## 2 2rx1111111112 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2w'' 	C-%:g%FGGGHNN8'BBBBBs   *!BBBc                 \  K   |                     d          }|                     ||           d|vrt          j        dd          d|vr!|d         dk    rt          j        dd          |                     |          }| j                            d	|           i }	 |d         d
k    rt          j        |d                   }| 	                    ||           | 
                    |||           d{V  | j                            |           d{V s|                     |           d{V  n|d         dk    r7|                     ||d         |                    d                     d{V  nH|d         dk    r|                     |||           d{V  nt          j        dd|d         z            n[# t          j        $ r  t"          $ r?}| j                            d||d           t          j        dd| d|           |d}~ww xY wd}|d         d
k    r-|                     ||           |                    dd          }|                     |d           d{V }|r||d<   |                     ||           |                     d|d           |S )r   rS   r   r   r   r   rr   r   r   r   r   r   Nr   rs   r   r   Tr   r#   r   r   r   r   Fr   r   r   r   r   )rV   r   r
   rz   rW   r^   r   r   r   r   r   r   r   r   r   r   r   r   r_   r   r   r   r   s           r-   r   zAsyncFileContentsManager.save  sA     zz#e$777-%<===E!!eFm{&B&B-%?@@@##D)){G,,,!#	_V}
**'i(899##B---))'2P`)aaaaaaaaa".??EEEEEEEE 700666666666v&((ooguY/?8ATATUUUUUUUUUUv+--**7E4@@@@@@@@@@mC)Fv)VWWW} 	 	 	 	_ 	_ 	_HNN;T1tNTTT-%VD%V%VST%V%VWW]^^	_ "=J&&((AQ(RRR!&9d!;!;hhtUh33333333 	21E)  ug >>>		&$77	888s    D!G H:HHc                 "   K   |                     d          }                     |          }t          j        } j        s-t          | j                  rt          j        dd|          t          j	        
                    |          st          j        dd|z             fd} j        rǉ j        s9t          j        dk    r) ||           d{V rt          j        dd	|z                                 |          st          j        d
d|z            d j                            d|           	 t%          |           n+# t&          $ r}t          j        dd|z            |d}~ww xY wdS t          j	                            |          r j        s) ||           d{V rt          j        dd	|z             j                            d|                                            5  t-          t.          j        |           d{V  ddd           dS # 1 swxY w Y   dS  j                            d|                                            5  t-          ||           d{V  ddd           dS # 1 swxY w Y   dS )r   rS   r   r   rl   z$File or directory does not exist: %sc                    K   t           j                            |           rKt          j        dd           }t          t          t           j        |            d {V           }||hz
  rdS dS r   )r)   r2   r5   r   r   r   r   r   )rY   r   rx  r,   s      r-   r   z>AsyncFileContentsManager.delete_file.<locals>.is_non_empty_dir  sx      w}}W%%   !!13CTJJ""*g)F)F#F#F#F#F#F#FGG6(*  45r/   r   Nr   r  r  r  zsend2trash f`1ailed: %sr  r  )rV   rW   r)   r  rx   r   r%   r
   rz   r2   r   r  r  r	  r
  r`   r^   r   r	   r]   r5   r   r   r  r  )r,   r2   rY   r  r   r   s   `     r-   r  z$AsyncFileContentsManager.delete_file  s{     zz###D))Y  	UYw%F%F 	U-%S%S%STTTw~~g&& 	W-%Kg%UVVV		 		 		 		 		  	*MLG++**733333333 ,
 mC)AG)KLLL##D)) SmC)@4)GHHdRHNN0':::O7#### O O OmC)BQ)FGGQNOF7==!! 
	,) M4D4DW4M4M.M.M.M.M.M.M MmC)AG)KLLLHNN2G<<<!!## 7 7v}g6666666667 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 HNN.888!!## , ,r7+++++++++, , , , , , , , , , , , , , , , , ,s<   ;E 
E3E..E36!H$$H(+H( JJJc                   K   |                     d          }|                     d          }||k    rdS |                     |          }|                     |          }| j        sBt          || j                  st          || j                  rt          j        dd|          t          j        	                    |          r(t          ||          st          j        dd|z            	 |                                 5  t          t          j        ||           d{V  ddd           dS # 1 swxY w Y   dS # t
          j        $ r  t          $ r!}t          j        dd| d	|           |d}~ww xY wr  )rV   rW   rx   r   r%   r
   rz   r)   r2   r   r   r   r   r  r  r   r  s         r-   r  z$AsyncFileContentsManager.rename_file  s     >>#&&>>#&&xF''11''11  	Yk4=11	Y5>{DM5Z5Z	Y -%W%W%WXXX 7>>+&& 	Kx[/Q/Q 	K-%>%IJJJ	\!!## 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} 	 	 	 	\ 	\ 	\-%SX%S%SPQ%S%STTZ[[	\s<   0E  "D3&E  3D77E  :D7;E   E:E55E:c                    K   |                     d          }|                     |          }t          j                            |          S )z(Does a directory exist at the given pathrS   rT   rf   rX   s      r-   rg   z#AsyncFileContentsManager.dir_exists  s?      zz####..w}}W%%%r/   c                    K   |                     d          }|                     |          }t          j                            |          S )z#Does a file exist at the given pathrS   rb   rX   s      r-   rd   z$AsyncFileContentsManager.file_exists  s<      zz###D))w~~g&&&r/   c                    K   |                     d          }|                     |          }t          || j                  S )z"Is path a hidden directory or filerS   rT   rU   rX   s      r-   r   z"AsyncFileContentsManager.is_hidden  s=      zz####..$-000r/   c                    K   |                      |           d{V r|S d|v r|                    dd          d         nd}|S )zHReturn the initial API path of a kernel associated with a given notebookNrS   r   r   r:   r  r  s       r-   r  z(AsyncFileContentsManager.get_kernel_path  s\      &&&&&&&& 	K/2d{{T[[a((++
r/   c                   K   t          |          }|                    d          }||                    d          }d|v r|                    dd          \  }}nd}|}|                     |           d{V }|d         dk    r|                     |           d{V  nt          j        | ||           d{V S |du}t          j        d|          }	|s|}| 	                    |           d{V r?t          j        d|          }
t                                          |
|d	
           d{V }	| d|	 }|                     |||	|           d{V S r   )r'  rV   r   r   r(  r   r)  r   r*  rg   rF   r+  r,  r-  s              r-   r)  zAsyncFileContentsManager.copy  s      w<<s##mmC((G$;;"&++c1"5"5HiiHIhhtnn$$$$$$=K''((.......... .249V]^^^^^^^^^^#*$#6 ,sI..' 	G)))))))) 	V<Y//D!GG66tWW6UUUUUUUUG((w((^^-	 $ 
 
 
 
 
 
 
 
 	
r/   r!  r%  r&  r"  returnc           	        K   	 |                      |                    d                    }|                      |                    d                     d| }t          j        ||           |                     |d           d{V }nL# t
          $ r?}| j                            d|            t          j	        dd| d| d	          |d}~ww xY w|S )
r2  rS   Fr   Nr3  r   r4  z' into read-only Folder 'r5  r6  r8  s	            r-   r,  z"AsyncFileContentsManager._copy_dirG  s
     
	,,Y__S-A-ABBL --.>.D.DS.I.IJJVVWVVJOL*555((7E(::::::::EE 	 	 	HNN9C99:::-MyMM7MMM  	 s   BB 
C:CCr2   c                 \  K   | j         dz  dz  }t          |                     |                     |                     d{V           }t	          j                    dk    r|dz  n|}||k    r=t          j        dd| j          d| d|                     |           d{V  d          dS )	r=  r>  Nr?  r   r@  rA  rB  rC  rD  rJ  s       r-   r(  z*AsyncFileContentsManager.check_folder_size\  s      
 2T9D@++D,=,=d,C,CDDDDDDDDEE&o//8;;td{{+-484P  '+'@'@'F'F!F!F!F!F!F!F     r/   r#  c                   K   	 t          j                    dk    r2t          j        dd|gdd          j                                        }n2t          j        ddd|gdd          j                                        }| j                            d|            |d	                             d
          }n,# t          $ r | j        
                    d|           Y dS w xY w|S rM  rR  rX  s       r-   rG  z&AsyncFileContentsManager._get_dir_sizeo  s     	  H,,#5$'D    $4!148UY    HMMB&BBCCC!9##G,,DD 	 	 	Hp   33	 s   B3B8 8%C! C!ru   c                    K   |dk    rdS g d}|r$t          t          j        |          dz            nd}|d|dz  z  z  dd||          S r[  rc  rf  s       r-   rI  z-AsyncFileContentsManager._human_readable_size  sn       1999777-18DIdOOb()))q!
+,AAA5<AAAr/   ri  rj  rk  rl  r'   rm  )rn  ro  rp  rq  r   rK   r   r   r   r   r   r   r  r  rg   rd   r   r  r)  r'  dictr,  r(  rG  rF  rI  rr  rs  s   @r-   ru  ru    s       ))W !!$ $ "!$? ? ? ?B   @   ,+ + + +Z
C 
C 
C 
C/ / / /b4, 4, 4,l\ \ \8& & &' ' '1 1 1   *
 *
 *
 *
 *
 *
X03>ALO	   *C D    &  c    2
Bs 
Bs 
B 
B 
B 
B 
B 
B 
B 
Br/   ru  )9rq  r   rd  r   r)   r
  r  r   rS  r	  r=   r   pathlibr   r   anyio.to_threadr   jupyter_core.pathsr   r   r   r	   tornador
   	traitletsr   r   r   r   r   r   jupyter_serverr   r}   jupyter_server.base.handlersr   jupyter_server.transutilsr   jupyter_server.utilsr   filecheckpointsr   r   fileior   r   managerr   r   r   os.pathr   ImportErrorr   _script_exporterr!   ru   r/   r-   <module>r     s   E E       				        



               $ $ $ $ $ $ @ @ @ @ @ @ @ @ @ @ ! ! ! ! ! !       G G G G G G G G G G G G G G G G $ $ $ $ $ $ A A A A A A + + + + + + , , , , , , B B B B B B B B ; ; ; ; ; ; ; ; D D D D D D D D D DA        A A A@@@@@@@@A  [
B [
B [
B [
B [
B*O [
B [
B [
B|JB JB JB JB JB24IK_ JB JB JB JB JBs   B! !B/.B/