
    Mh                       d Z ddlm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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l0m1Z1m2Z2 ddl3m4Z4m5Z5m6Z6 	 ddl7m8Z8 n# e9$ r	 ddl+m:Z8 Y nw xY wdZ; G d de2e5          Z< G d de<e1e4          Z=dS )z?A contents manager that uses the local file system for storage.    )annotationsN)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d0dZd1d Zd2d"Zd2d#Zd$ Z d% Z!d& Z"d3d'Z#d3 fd(	Z$d) Z%d* Z&d4d,Z'd- Z( xZ)S )5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    l/var/www/html/test/jupyter/venv/lib/python3.11/site-packages/jupyter_server/services/contents/filemanager.py_default_root_dirz%FileContentsManager._default_root_dir6   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-   proposalr2   s      r.   _validate_root_dirz&FileContentsManager._validate_root_dir<   sd    !w}}U## 	+GOOE**Ew}}U## 	>4u<===r0   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)   r9   r&   warningswarnFutureWarningr   relative_toas_posix
ValueErrorr   AttributeError)r-   r2   r3   s      r.   _default_preferred_dirz*FileContentsManager._default_preferred_dirF   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 )Nr2   )r   r&   super_validate_preferred_dir)r-   r7   	__class__s     r.   rH   z+FileContentsManager._validate_preferred_dir^   s8     ((94=IIww..x888r0   checkpoints_classc                    t           S r(   )r   r,   s    r.   _checkpoints_class_defaultz.FileContentsManager._checkpoints_class_defaultd   s    r0   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_defaultz   s    ''r0   files_handler_paramsc                    d| j         iS )Nr3   )r&   r,   s    r.   _files_handler_params_defaultz1FileContentsManager._files_handler_params_default~   s    &&r0   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.
        /r3   strip_get_os_pathr	   r&   r-   r3   os_paths      r.   r	   zFileContentsManager.is_hidden   s;     zz####..$-000r0   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.
        rT   rU   z'Failed to check write permissions on %sF)rW   rX   r*   accessW_OKOSErrorlogerrorrY   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.
        rT   rW   rX   r*   r3   isfilerY   s      r.   file_existszFileContentsManager.file_exists   s:     zz###D))w~~g&&&r0   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.
        rT   rU   rW   rX   r*   r3   r6   rY   s      r.   
dir_existszFileContentsManager.dir_exists   s=      zz####..w}}W%%%r0   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.
        rT   rU   )rW   rX   r   rY   s      r.   r   zFileContentsManager.exists   s5     zz####..gr0   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<   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 %srT   namer3   last_modifiedcreatedcontentformatmimetypesizewritablehashhash_algorithm)rX   r*   lstatallow_hiddenr	   r&   r_   infor   	HTTPErrorst_sizerC   r^   warningtzutcfromtimestampst_mtimer   UTCst_ctimersplitra   )	r-   r3   rZ   r|   four_o_fourrv   rq   rr   models	            r.   _base_modelzFileContentsManager._base_model   sS   ##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f"&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: %rrm   z4Refusing to serve hidden directory %r, via 404 Error	directorytypeNrv   rs   z"failed to decode filename '%s': %rz%s doesn't existzError stat-ing %s: %rz%s not a regular file)stat_resrT   F)r3   rs   z+Unknown error checking if file %r is hiddenTexc_infojsonrt   ) rX   r*   r3   r6   r   r}   r{   r	   r&   r_   r|   r   listdirjoinUnicodeDecodeErrorr   rz   r^   errnoENOENTEACCESstatS_ISLNKst_modeS_ISREGS_ISDIRdebugshould_listr   appendgetELOOP)r-   r3   rs   rZ   r   r   contentsos_dirrp   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                   |                      |          }d|d<   |                     |          }t          j        |          d         |d<   d}|rK|                     ||d          \  }}}|d         dd	d
|         }||d<   |                    ||           |r;||                     |d          \  }}	 |j        di |                     |           |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

        if require_hash is true, the model will include 'hash'
        filer   r   ru   NT)rawz
text/plainzapplication/octet-stream)textbase64)rs   rt   byte r   rX   	mimetypes
guess_type
_read_fileupdate	_get_hash
r-   r3   rs   rt   require_hashr   rZ   bytes_contentdefault_mime_s
             r.   _file_modelzFileContentsManager._file_modelV  s      &&f##D))%099!<j 	-1__WfRV_-W-W*GV]Z ((8      %1j!LL    
  	:$#'??7F#C#C qEL994>>-88999r0   c                   |                      |          }d|d<   |                     |          }d}|rTi }|                     |d|d          \  }}|                     ||           ||d<   d|d	<   |                     ||           |r;||                     |d
          \  }}	 |j        di |                     |           |S )zBuild a notebook model

        if content is requested, the notebook content will be populated
        as a JSON structure (not double-serialized)

        if require_hash is true, the model will include 'hash'
        notebookr   N   T
as_versioncapture_validation_errorr   rs   r   rt   r   r   r   rX   _read_notebookmark_trusted_cellsvalidate_notebook_modelr   r   r   
r-   r3   rs   r   r   rZ   r   validation_errornbr   s
             r.   _notebook_modelz#FileContentsManager._notebook_model~  s      &&"f##D)) 	B13 $ 3 3A@PVZ !4 ! !B ##B---!E)$E(O((0@AAA 	:$#'??7F#C#C qEL994>>-88999r0   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	          |                     ||
          }no|dk    s|.|                    d          r|                     |||          }n9|dk    rt          j        dd|z  d	          |                     ||||          }|                     d|d           |S )h  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.
        require_hash: bool, optional
            Whether to include the hash of the file contents.

        Returns
        -------
        model : dict
            the contents model. If content=True, returns the contents
            of the file or directory as well.
        rT   rk   rm   rl   Nr      is a directory, not a bad typereasonrs   r   N.ipynbrs   r   r   %s is not a directoryrs   rt   r   r   actionr3   data)rW   rX   r   r   r}   r{   r	   r&   r_   r|   r*   r3   r6   r   endswithr   r   emit)	r-   r3   rs   r   rt   r   rZ   r   r   s	            r.   r   zFileContentsManager.get  s   0 zz###D))<tC{{4   	2-[111  	2Yw%F%F 	2HMMXZabbb-[1117==!! 	...m::D::%   
 OOD'O::EEZDLT]]85L5LL((w\(ZZEE{""mC)@4)GPZ[[[[$$gf< %  E 			%66	777r0   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)r{   r	   r&   r   r}   r*   r3   r   perm_to_403mkdirr6   r_   r   r-   rZ   r   r3   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.rT   r   r3   r   r   No file type providedrs   r   No file content providedz Cannot create file or directory 	Saving %sr   r   r   rt   Unhandled contents type: %sError while saving file: %s %sTr   r$   $Unexpected error while saving file:  Nr   messageFr   r   rZ   saver   r   )rW   run_pre_save_hooksr   r}   rX   r{   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   r3   rZ   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.rT   r    Cannot delete file or directory rk   rm   c                    t           j                            |           r=t          j        dd           }t          t          j        |                     |hz
  rdS dS Ncheckpoint_dirTF)r*   r3   r6   getattrr   setr   )rZ   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5r0   win32Directory %s not empty  Permission denied: %sNSending %s to trashsend2trash failed: %sRemoving directory %sUnlinking file %s)rW   rX   r*   unlinkr{   r	   r&   r   r}   r   delete_to_trashalways_delete_dirsysplatformra   r_   r   r
   r^   r3   r6   r   shutilrmtree)r-   r3   rZ   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.rT   Nr   z Cannot rename file or directory i  zFile already exists: %sr$   zUnknown error renaming file: r   )rW   rX   r{   r	   r&   r   r}   r*   r3   r   r   r   r  mover   r-   old_pathnew_pathnew_os_pathold_os_pathr   s         r.   rename_filezFileContentsManager.rename_fileD  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_string`  s    ABBT]RRr0   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 notebookrT   r   r   r;   rh   r   r-   r3   r   
parent_dirs       r.   get_kernel_pathz#FileContentsManager.get_kernel_pathd  sF    ??4   	K/2d{{T[[a((++
r0   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.
        rT   Nr   r;   r   r   )	from_pathto_path.z-Copy)insert)r,  to_path_originalto_namer-  )strrW   r   r   check_folder_sizerG   copyr   subrh   increment_filename	_copy_dirr-   r,  r-  r0  r3   from_dir	from_namer   is_destination_specifiedr1  rp   rI   s              r.   r4  zFileContentsManager.copyk  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((~~-	  
 
 	
r0   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
        rT   Fr   OSError in _copy_dir: r   Can't copy 'z' into Folder ''N
rX   rW   r  copytreer   r^   r_   r`   r   r}   	r-   r,  r0  r1  r-  os_from_path
os_to_pathr   errs	            r.   r7  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_sizerX   r  systemr   r}   _human_readable_sizer-   r3   limit_bytesrv   s       r.   r3  z%FileContentsManager.check_folder_size  s    
 2T9D@4%%d&7&7&=&=>>??&o//8;;td{{+-484P  !%!:!:4!@!@     r0   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 )M
        calls the command line program du to get the directory size
        rJ  du-skT)capture_outputcheck-s--block-size=1current status of du command r   utf-8^Not able to get the size of the %s directory. Copying might be slow if the directory is large!0)r  rS  
subprocessrunstdoutsplitr_   r|   decoder   r   )r-   r3   resultrv   s       r.   rR  z!FileContentsManager._get_dir_size  s   	  H,,#5$'#'   	  $4!148#'   	  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   rQ  mathlog2r-   rv   unitsorders       r.   rT  z(FileContentsManager._human_readable_size  sj     1999777-18DIdOOb()))q!
+,AAA5<AAAr0   TTNFTFTNNFr;   r(   r.  )*__name__
__module____qualname____doc__r   r&   r   rP  r   r/   r   r8   rE   rH   rL   r   r  r  rO   rR   r	   ra   re   rh   r   r   r   r   r   r   r   r   r  r"  r$  r)  r4  r7  r3  rR  rT  __classcell__rI   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d> > > >@& & & &P   <4 4 4 4l
C 
C 
C 
C2 2 2 2h0 0 0d\ \ \8S S S   *
 *
 *
 *
 *
 *
X  &  &   :
B 
B 
B 
B 
B 
B 
Br0   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+dZd,dZd-d.d!Zd/d$Z xZS )0AsyncFileContentsManagerzAn async file contents manager.rJ   c                    t           S r(   )r   r,   s    r.   rL   z3AsyncFileContentsManager._checkpoints_class_default  s    ##r0   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   )!rX   r*   r3   r6   r   r}   r{   r	   r&   r_   r|   r   r   r   r   r   r   rz   r^   r   r   r   r   r   r   r   r   r   r   r   r   r   r   )r-   r3   rs   rZ   r   r   r   r   dir_contentsrp   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NFc                  K   |                      |          }d|d<   |                     |          }t          j        |          d         |d<   d}|rQ|                     ||d           d{V \  }}}|d         dd	d
|         }||d<   |                    ||           |rA||                     |d           d{V \  }}	 |j        di |                     |           |S r   r   r   s
             r.   r   z$AsyncFileContentsManager._file_model/  sF        &&f##D))%099!<j 	37??7FX\?3]3]-]-]-]-]-]-]*GV]Z ((8      %1j!LL    
  	:$)-&)I)I#I#I#I#I#I#I qEL994>>-88999r0   c                  K   |                      |          }d|d<   |                     |          }d}|rZi }|                     |d|d           d{V \  }}|                     ||           ||d<   d|d	<   |                     ||           |rA||                     |d
           d{V \  }}	 |j        di |                     |           |S )zBuild a notebook model

        if content is requested, the notebook content will be populated
        as a JSON structure (not double-serialized)
        r   r   Nr   Tr   rs   r   rt   r   r   r   r   s
             r.   r   z(AsyncFileContentsManager._notebook_modelW  s6        &&"f##D)) 	B13&*&9&9A@PVZ ': ' ' ! ! ! ! ! !B ##B---!E)$E(O((0@AAA 	<$)-&)I)I#I#I#I#I#I#I qEL;;DNN=99;;;r0   c                  K   |                     d          }|                     |          st          j        dd|z            |                     |          }t
          j                            |          r>|dvrt          j        d| d| d          |                     ||	           d
{V }n{|dk    s|4|	                    d          r| 
                    |||           d
{V }n?|dk    rt          j        dd|z  d          |                     ||||           d
{V }|                     d|d           |S )r   rT   rm   zNo such file or directory: %sr   r   r   r   r   r   Nr   r   r   r   r   r   r   r   r   )rW   r   r   r}   rX   r*   r3   r6   r   r   r   r   r   )r-   r3   rs   r   rt   r   rZ   r   s           r.   r   zAsyncFileContentsManager.gets  s     0 zz#{{4   	M-%Dt%KLLL##D))7==!! 	...m::D::%   
 //$/@@@@@@@@EEZDLT]]85L5LL..tWS_.````````EE{""mC)@4)GPZ[[[[**gf< +        E 			%66	777r0   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   )r{   r	   r&   r   r}   r*   r3   r   r   r   r   r6   r_   r   r   s       r.   r   z(AsyncFileContentsManager._save_directory  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   rT   r   r   r   r   rs   r   r   r   r   r   Nr   rt   r   r   Tr   r$   r   r   r   r   Fr   r   r   r   r   )rW   r   r   r}   rX   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   rT   r   r  rm   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*   r3   r6   r  r   r  r   r   )rZ   r  r  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5r0   r	  Nr
  r  r  r  r  r  r  )rW   rX   r*   r  r{   r	   r&   r   r}   r3   r   r  r  r  r  ra   r_   r   r
   r^   r6   r   r   r  r  )r-   r3   rZ   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':::M7#### M M MmC)@1)DEE1LM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  )rW   rX   r{   r	   r&   r   r}   r*   r3   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 pathrT   rU   rg   rY   s      r.   rh   z#AsyncFileContentsManager.dir_exists3  s?      zz####..w}}W%%%r0   c                   K   |                     d          }|                     |          }t          j                            |          S )z#Does a file exist at the given pathrT   rc   rY   s      r.   re   z$AsyncFileContentsManager.file_exists9  s<      zz###D))w~~g&&&r0   c                   K   |                     d          }|                     |          }t          || j                  S )z"Is path a hidden directory or filerT   rU   rV   rY   s      r.   r	   z"AsyncFileContentsManager.is_hidden?  s=      zz####..$-000r0   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 notebookNrT   r   r   r;   r&  r'  s       r.   r)  z(AsyncFileContentsManager.get_kernel_pathE  s\      &&&&&&&& 	K/2d{{T[[a((++
r0   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+  )r2  rW   r   r   r3  r   r4  r   r5  rh   rG   r6  r7  r8  s              r.   r4  zAsyncFileContentsManager.copyL  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((^^-	 $ 
 
 
 
 
 
 
 
 	
r0   r,  r2  r0  r1  r-  returndict[str, t.Any]c           	       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 )
r=  rT   Fr   Nr>  r   r?  z' into read-only Folder 'r@  rA  rC  s	            r.   r7  z"AsyncFileContentsManager._copy_dirx  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r3   Nonec                \  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 )	rH  rI  NrJ  r   rK  rL  rM  rN  rO  rU  s       r.   r3  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     r0   r.  c                .  K   	 t          j                    dk    rd|g}ndd|g}t          j        dg|R t          j        j        t          j        j        d d{V }|                                 d{V \  }}|                                 d{V }| j        	                    d|            |d	k    sJ |
                    d
                                          d	         }n,# t          $ r | j                            d|           Y dS w xY w|S )rX  rJ  rZ  r]  r^  rY  )re  stderrNr_  r   r`  ra  rb  )r  rS  asynciocreate_subprocess_execrc  PIPEcommunicatewaitr_   r|   rg  rf  r   r   )r-   r3   argsprocre  r   rh  rv   s           r.   rR  z&AsyncFileContentsManager._get_dir_size  sr     	  H,,t}.5 7 $+$6$;GDVD[        D #..00000000IFA99;;&&&&&&FHMMB&BBCCCQ;;;;==))//11!4DD 	 	 	Hp   33	 s   C$C) )%DDrv   rQ  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 rj  rr  ru  s       r.   rT  z-AsyncFileContentsManager._human_readable_size  sn       1999777-18DIdOOb()))q!
+,AAA5<AAAr0   rx  ry  rz  r{  r|  r(   )
r,  r2  r0  r2  r1  r2  r-  r2  r  r  )r3   r2  r  r  r}  )r3   r2  r  r2  )rv   rQ  r  r2  )r~  r  r  r  r   rL   r   r   r   r   r   r   r  r"  rh   re   r	   r)  r4  r7  r3  rR  rT  r  r  s   @r.   r  r    s       ))W !!$ $ "!$? ? ? ?B& & & &P   8/ / / /b
C 
C 
C 
C/ / / /b4, 4, 4,l\ \ \8& & &' ' '1 1 1   *
 *
 *
 *
 *
 *
X   *   &    6
B 
B 
B 
B 
B 
B 
B 
Br0   r  )>r  
__future__r   r  r   rs  r   r*   r  r  r   rc  r  typingtr>   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"   r  r   r0   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  u
B u
B u
B u
B u
B*O u
B u
B u
Bp^B ^B ^B ^B ^B24IK_ ^B ^B ^B ^B ^Bs   (B/ /B=<B=