
    Xhe                       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 dZ; G d de2e5      Z< G d de<e1e4      Z=y# e9$ r	 ddl+m:Z8 Y )w xY w)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                      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d.dZd/d Zd/d!Zd" Z d# Z!d$ Z"d0d%Z#d0 fd&	Z$d' Z%d( Z&d1d)Z'd* Z( xZ)S )2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    `/var/www/html/myenv/lib/python3.12/site-packages/jupyter_server/services/contents/filemanager.py_default_root_dirz%FileContentsManager._default_root_dir6   s%    {{99;{{###    c                    |d   }t         j                  j                  |      st         j                  j                  |      }t         j                  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<   sT    !ww}}U#GGOOE*Eww}}U#4u<==r0   preferred_dirc                ~   | j                   sy	 | j                   j                  }|| j                   j                  k(  rd }|Qt        j                  dt
        d       	 t        |      }|j                  | j                        j                         S y# t        $ r t        d|z        d w xY w# t        $ r Y yw xY w)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    {{	`KK--E,,,   H! 
`;D++DMM:CCEE  " `$%Lu%TU[__`  	 	s   1B0 3B B-0	B<;B<c                Z    t        |d   | j                        |d<   t        |   |      S )Nr2   )r   r&   super_validate_preferred_dir)r-   r7   	__class__s     r.   rH   z+FileContentsManager._validate_preferred_dir^   s/     ((94==Iw.x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                t    |j                  d      }| j                  |      }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   s5     zz####.$--00r0   c                    |j                  d      }| j                  |      }	 t        j                  |t        j                        S # t
        $ r | j                  j                  d|       Y y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   sc     zz####.	99Wbgg.. 	HHNNDgN	s   #A	 	%A10A1c                    |j                  d      }| j                  |      }t        j                  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   s4     zz###D)ww~~g&&r0   c                    |j                  d      }| j                  |      }t        j                  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   s7      zz####.ww}}W%%r0   c                ^    |j                  d      }| j                  |      }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   s.     zz####.gr0   c           	     (   | j                  |      }t        j                  |      }d|z  }| j                  sHt	        || j
                        r2| j                  j                  d|       t        j                  d|      	 |j                  }	 t        j                  |j                         }	 t        j                  |j&                        }i }|j)                  dd      d   |d<   ||d<   ||d<   ||d<   d|d<   d|d<   d|d<   ||d<   | j+                  |      |d<   d|d<   d|d<   |S # t        t        f$ r  | j                  j                  d       d}Y w xY w# t        t        f$ rJ | j                  j                  d|j                   |       t#        dddd	d	t        j$                  
      }Y w xY w# t        t        f$ rJ | j                  j                  d|j&                  |       t#        dddd	d	t        j$                  
      }Y Cw xY w)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   s   ##D)xx <tC  Yw%FHHMMXZab--[11	<<D
	F//>M	@))$--8G C+B/ff!.o"iih jf ,,T2jf"&E G$ 	HH23D	 G$ 	F
 HH6wO$T1aAbffEM	F G$ 	@HH6wOtQ1a?G	@s8   D* E .F8 *,EEAF54F58AHHc                   | j                  |      }d|z  }t        j                  j                  |      st	        j
                  d|      | j                  sHt        || j                        r2| j                  j                  d|       t	        j
                  d|      | j                  |      }d|d<   d|d<   |r-g x|d<   }|}t        j                  |      D ]  }	 t        j                  j                  ||      }	 t        j                   |      }
t+        j,                  |
j.                        s[t+        j0                  |
j.                        s<t+        j2                  |
j.                        s| j                  j5                  d|       	 | j7                  |      r@| j                  st9        ||
      s'|j;                  | j=                  | d| d             	 d|d<   |S # t        $ r(}	| j                  j                  d	||	       Y d}	~	=d}	~	ww xY w# t"        $ r}	|	j$                  t$        j&                  k(  r| j                  j                  d
|       n:|	j$                  t$        j(                  k7  r| j                  j                  d||	       Y d}	~	d}	~	ww xY w# t"        $ rU}	|	j$                  t$        j>                  t$        j(                  fvr| j                  j                  d|d       Y d}	~	)d}	~	ww xY w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  su   
 ##D)4t;ww}}W%--[11""y$--'HHHMMPRYZ--[11  &#ff*,,E)xF

6* % ggll648G
'*B RZZ0 LL4 LL4HHNN#:GD''-))RT1U av6FPU(VW;%N %E(OM * HH$$%I4QRS  ww%,,.(();WEELL0(()@'1M*  wwu{{ELL&AA((I#%) ) sK   ! G-H!AJ,-	H6HH!	J)*A4J$$J),	L
5A
LL
c                   | j                  |      }d|d<   | j                  |      }t        j                  |      d   |d<   d}|r=| j	                  ||d      \  }}}|d   dd	d
|   }||d<   |j                  ||       |r8|| j	                  |d      \  }}	 |j
                  di | j                  |       |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09!<j-1__WfRV_-W*GV]Z ((8    %1j!LL  
 $#'??7F#C qELL94>>-89r0   c                `   | j                  |      }d|d<   | j                  |      }d}|rHi }| j                  |d|d      \  }}| j                  ||       ||d<   d|d	<   | j	                  ||       |r8|| j                  |d
      \  }}	 |j                  di | j                  |       |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)13 $ 3 3A@PVZ !4 !B ##B-!E)$E(O((0@A$#'??7F#C qELL94>>-89r0   c                   |j                  d      }| j                  |      }d|z  }| j                  |      st        j                  d|      | j
                  sHt        || j                        r2| j                  j                  d|       t        j                  d|      t        j                  j                  |      r5|dvrt        j                  d| d| d	      | j                  ||
      }nb|dk(  s|&|j                  d      r| j                  |||      }n5|dk(  rt        j                  dd|z  d	      | j!                  ||||      }| j#                  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   .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 --[11  Yw%FHHMMXZab--[1177==!..mmf3D6:% 
 OOD'O:EZDLT]]85L((w\(ZE{"mmC)@4)GPZ[[$$gf< % E 			%6	7r0   c                   | j                   s/t        || j                        rt        j                  dd|z        t
        j                  j                  |      s/| j                         5  t        j                  |       ddd       yt
        j                  j                  |      st        j                  dd|z        | j                  j                  d|       y# 1 sw Y   yxY w)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      Yw%F--%AG%KLLww~~g&!!# "!" "w'--%:g%FGGHHNN8'B" "s   +CC(c                   |j                  d      }| j                  ||       d|vrt        j                  dd      d|vr|d   dk7  rt        j                  dd      | j	                  |      }| j
                  s/t        || j                        rt        j                  dd	|      | j                  j                  d
|       i }	 |d   dk(  rkt        j                  |d         }| j                  ||       | j                  |||       | j                  j                  |      sx| j!                  |       nf|d   dk(  r&| j#                  ||d   |j%                  d             n8|d   dk(  r| j'                  |||       nt        j                  dd|d   z        d}|d   dk(  r%| j-                  ||       |j%                  dd      }| j%                  |d      }|r||d<   | j/                  ||       | j1                  d|d       |S # t        j                  $ r  t(        $ rA}| j                  j+                  d||d       t        j                  dd| d|       |d}~ww xY w)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  sn   zz#e$7--%<==E!eFm{&B--%?@@##D)  Yw%F--'G{%STT{G,-/	_V}
*''i(89##B-##GRJZ#[''88>**40v&(y)9599X;NOv+-$$WeT:mmC)Fv)VWW "=J&((AQ(R!&9d!;u-1E)  ug >		&$7	8# }} 	 	_HHNN;T1tNT--'KD6QRSTRU%VW]^^	_s   CH I54<I00I5c                    |j                  d      } j                  |      }t        j                  } j                  s/t        | j                        rt        j                  dd|      d|z  } j                  |      st        j                  d|       fd} j                  r j                  s4t        j                  dk(  r! ||      rt        j                  dd|z         j                  |      st        j                  d	d
|z        d j                  j!                  d|       	 t#        |       yt        j&                  j)                  |      rx j                  s! ||      rt        j                  dd|z         j                  j!                  d|        j+                         5  t-        j.                  |       ddd       y j                  j!                  d|        j+                         5   ||       ddd       y# t$        $ r}t        j                  dd|z        |d}~ww xY w# 1 sw Y   yxY w# 1 sw Y   yxY w)Delete file at path.rT   r    Cannot delete file or directory rk   rm   c                    t         j                  j                  |       r:t        j                  dd       }t        t        j                  |             |hz
  ryyNcheckpoint_dirTF)r*   r3   r6   getattrr   setr   )rZ   cp_dirr-   s     r.   is_non_empty_dirz9FileContentsManager.delete_file.<locals>.is_non_empty_dir  sH    ww}}W% !!1!13CTJrzz'*+vh6r0   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)YY  Yw%F--'G{%STT<tC{{4 --[11	 ))cllg.EJZ[bJc mmC)AG)KLL##D)mmC)@4)GHdRHHNN0':M7# 77==!)).>w.GmmC)AG)KLLHHNN2G<!!# 'g&' ' HHNN.8!!# 7   MmmC)@1)DE1LM' ' s0   4H 8I 	I	H=H88H= I	Ic                    |j                  d      }|j                  d      }||k(  ry| j                  |      }| j                  |      }| j                  sEt        || j                        st        || j                        rt        j                  dd|      t        j                  j                  |      r%t        ||      st        j                  dd|z        	 | j                         5  t        j                  ||       ddd       y# 1 sw Y   yxY w# t
        j                  $ r  t        $ r t        j                  dd|       d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: %srm   z"File or directory does not exist: r$   zUnknown error renaming file: r   )rW   rX   r{   r	   r&   r   r}   r*   r3   r   r   r   r  moveFileNotFoundErrorr   r-   old_pathnew_pathnew_os_pathold_os_pathr   s         r.   rename_filezFileContentsManager.rename_fileD  sU   >>#&>>#&x''1''1  k4==1Y{DMM5Z--'G%WXX 77>>+&x[/Q--%>%IJJ	\!!# 6K56 6 6}} 	  	`--'I(%TU[__ 	\--'DXJaPQs%STZ[[	\s6   !D 1DD DD D >E=E88E=c                2    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_stringb  s    ABT]]RRr0   c                f    | j                  |      r|S d|v r|j                  dd      d   }|S 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_pathf  sC    ??4 K/2d{T[[a(+
 AC
r0   c                   t        |      }|j                  d      }||j                  d      }d|v r|j                  dd      \  }}nd}|}| j                  |      }|d   dk(  r| j	                  |       nt
        |   ||      S |du}t        j                  d|      }	|s|}| j                  |      r(t        j                  d|      }
t
        | )  |
|d	
      }	| d|	 }| j                  |||	|      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.  r1  r3   from_dir	from_namer   is_destination_specifiedr2  rp   rI   s              r.   r5  zFileContentsManager.copym  s    w<s#mmC(G$;"&++c1"5HiHI=K'""4( 7<)W<EE#*$#6 ,,sI.'G??7#<<Y/Dg0ww0OGIQwi(~~-	  
 	
r0   c           	     ~   	 | j                  |j                  d            }| j                  |j                  d             d| }t        j                  ||       | j	                  |d      }|S # t
        $ rA}| j                  j                  d|        t        j                  dd| d| d      |d	}~ww xY w)
`
        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-  r1  r2  r.  os_from_path
os_to_pathr   errs	            r.   r8  zFileContentsManager._copy_dir  s    

	,,Y__S-ABL --.>.D.DS.IJK1WIVJOOL*5HHWeH4E   	HHNN3C59:--yk	C 	s   A.A2 2	B<;<B77B<c                2   | j                   dz  dz  }t        | j                  | j                  |                  }t	        j
                         dk(  r|dz  n|}||kD  r9t        j                  dd| j                    d| d| j                  |       d      y)	
        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.   r4  z%FileContentsManager.check_folder_size  s    
 22T9D@4%%d&7&7&=>?&oo/8;td{+--4484P4P3Q RV5!:!:4!@ A B  r0   c                   	 t        j                         dk(  r4t        j                  dd|gdd      j                  j                         }n4t        j                  ddd|gdd      j                  j                         }| j                  j                  d|        |d	   j                  d
      }|S # t        $ r | j                  j                  d|       Y yw xY w)M
        calls the command line program du to get the directory size
        rK  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  rT  
subprocessrunstdoutsplitr_   r|   decoder   r   )r-   r3   resultrv   s       r.   rS  z!FileContentsManager._get_dir_size  s    	 H,#5$'#' &	  $4!148#' &	  HHMM9&BC!9##G,D   	HHp 	s   B1B5 5%CCc                    |dk(  ry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   rR  mathlog2r-   rv   unitsorders       r.   rU  z(FileContentsManager._human_readable_size  sT     197-1DIIdOb()q!
+,S15<.AAr0   TTNFTFTNNFr;   r(   r/  )*__name__
__module____qualname____doc__r   r&   r   rQ  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*  r5  r8  r4  rS  rU  __classcell__rI   s   @r.   r"   r"   /   st   "d#H!#d9abZ$ $
 j  _ . o9 9
  ! " /O X "#( $( #$' %'1$,'&&(&0d>@&P<4l
C2h0d\<S*
X&&:
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 ) AsyncFileContentsManagerzAn async file contents manager.rJ   c                    t         S r(   )r   r,   s    r.   rL   z3AsyncFileContentsManager._checkpoints_class_default  s    ##r0   c                z  K   | j                  |      }d|z  }t        j                  j                  |      st	        j
                  d|      | j                  sHt        || j                        r2| j                  j                  d|       t	        j
                  d|      | j                  |      }d|d<   d|d<   |rQg x|d<   }|}t        t        j                  |       d{   }|D ]  }		 t        j                  j                  ||	      }	 t        t        j"                  |       d{   }t-        j.                  |j0                        s[t-        j2                  |j0                        s<t-        j4                  |j0                        s| j                  j7                  d|       	 | j9                  |	      rH| j                  st;        ||      s/|j=                  | j?                  | d|	 d       d{           d|d<   |S 7 -# t        $ r(}
| j                  j!                  d	|	|
       Y d}
~
Ud}
~
ww xY w7 # t$        $ r}
|
j&                  t&        j(                  k(  r| j                  j!                  d
|       n:|
j&                  t&        j*                  k7  r| j                  j!                  d||
       Y d}
~
d}
~
ww xY w7 # t$        $ rU}
|
j&                  t&        j@                  t&        j*                  fvr| j                  j!                  d|d       Y d}
~
Fd}
~
ww xY ww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  s    
 ##D)4t;ww}}W%--[11""y$--'HHHMMPRYZ--[11  &#ff*,,E)xF!)"**f!==L$ % ggll648G
''::B RZZ0 LL4 LL4HHNN#:GD''-))RT1U dhhtfAdV<LV[h.\(\];%N %E(OU > * HH$$%I4QRS
 ; ww%,,.(();WEELL0(()@'1M( )] wwu{{ELL&AA((I#%) ) s   C$L;&H'
L;2 HI0I
1I5A:L;0AK K
K	L;	II<L;IL;
I	KA4K
L;KL;K	L8#A
L3-L;3L88L;c                  K   | j                  |      }d|d<   | j                  |      }t        j                  |      d   |d<   d}|rE| j	                  ||d       d{   \  }}}|d   dd	d
|   }||d<   |j                  ||       |r@|| j	                  |d       d{   \  }}	 |j
                  di | j                  |       |S 7 q7 ,wr   r   r   s
             r.   r   z$AsyncFileContentsManager._file_model1  s       &f##D)%009!<j37??7FX\?3]-]*GV]Z ((8    %1j!LL  
 $)-&)I#I qELL94>>-89% .^ $Js%   AC C!AC'C(+CCc                  K   | j                  |      }d|d<   | j                  |      }d}|rPi }| j                  |d|d       d{   \  }}| j                  ||       ||d<   d|d	<   | j	                  ||       |r@|| j                  |d
       d{   \  }}	 |j                  di | j                  |       |S 7 y7 ,w)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_modelY  s        &"f##D)13&*&9&9A@PVZ ': ' !B ##B-!E)$E(O((0@A$)-&)I#I qELL;DNN=9;! $Js%   ACC	ACC+CCc                x  K   |j                  d      }| j                  |      st        j                  dd|z        | j	                  |      }t
        j                  j                  |      r=|dvrt        j                  d| d| d      | j                  ||	       d
{   }nr|dk(  s|.|j                  d      r| j                  |||       d
{   }n=|dk(  rt        j                  dd|z  d      | j                  ||||       d
{   }| j                  d|d       |S 7 7 [7 w)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.getu  s?    0 zz#{{4 --%Dt%KLL##D)77==!..mmf3D6:% 
 //$/@@EZDLT]]85L..tWS_.``E{"mmC)@4)GPZ[[**gf< +  E 			%6	7 A`s6   B#D:%D4&4D:D6=D:D8D:6D:8D:c                  K   | j                   s/t        || j                        rt        j                  dd|z        t
        j                  j                  |      s<| j                         5  t        t
        j                  |       d{    ddd       yt
        j                  j                  |      st        j                  dd|z        | j                  j                  d|       y7 b# 1 sw Y   yxY ww)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       Yw%F--%H7%RSSww~~g&!!# 2rxx1112 2w'--%:g%FGGHHNN8'B	 22 2s1   A+C<-C0C.C0AC<.C00C95C<c                  K   |j                  d      }| j                  ||       d|vrt        j                  dd      d|vr|d   dk7  rt        j                  dd      | j	                  |      }| j
                  j                  d	|       i }	 |d   d
k(  rt        j                  |d         }| j                  ||       | j                  |||       d{    | j                  j                  |       d{   s| j                  |       d{    nv|d   dk(  r.| j                  ||d   |j                  d             d{    n@|d   dk(  r| j!                  |||       d{    nt        j                  dd|d   z        d}|d   d
k(  r%| j'                  ||       |j                  dd      }| j                  |d       d{   }|r||d<   | j)                  ||       | j+                  d|d       |S 7 37 7 7 7 # t        j                  $ r  t"        $ rA}| j
                  j%                  d||d       t        j                  dd| d|       |d}~ww xY w7 w)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  s    zz#e$7--%<==E!eFm{&B--%?@@##D){G,-/	_V}
*''i(89##B-))'2P`)aaa"..??EEE00666v&(ooguY/?8ATUUUv+-**7E4@@@mmC)Fv)VWW "=J&((AQ(R!&9d!;hhtUh331E)  ug >		&$7	89 bE6 V@ }} 	 	_HHNN;T1tNT--'KD6QRSTRU%VW]^^	_ 4s   BI=A	H H"H ?H H H5H H#H 2H3!H AI=I;5I=H H H H H I87<I33I88I=c                4   K   |j                  d      } j                  |      }t        j                  } j                  s/t        | j                        rt        j                  dd|      t        j                  j                  |      st        j                  dd|z         fd} j                  r j                  s<t        j                  dk(  r) ||       d{   rt        j                  dd	|z         j                  |      st        j                  d
d|z        d j                   j#                  d|       	 t%        |       yt        j                  j)                  |      r j                  s) ||       d{   rt        j                  dd	|z         j                   j#                  d|        j+                         5  t-        t.        j0                  |       d{    ddd       y j                   j#                  d|        j+                         5  t-        ||       d{    ddd       y7 h# t&        $ r}t        j                  dd|z        |d}~ww xY w7 7 # 1 sw Y   yxY w7 K# 1 sw Y   yxY ww)r   rT   r   r  rm   z$File or directory does not exist: %sc                   K   t         j                  j                  |       rIt        j                  dd       }t        t        t         j                  |        d {         }||hz
  ryy7 w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  s\     ww}}W% !!1!13CTJ""**g)F#FG6(*	 $Gs   AA/A-A/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  s6    zz###D)YY  Yw%F--'G{%STTww~~g&--%Kg%UVV		 **LLG+*7333 mmC)AG)KLL##D)mmC)@4)GHdRHHNN0':M7# 77==!))4DW4M.M.MmmC)AG)KLLHHNN2G<!!# 7v}}g6667 7 HHNN.8!!# ,r7+++, ,1 4  MmmC)@1)DE1LM /N 77 7
 ,, ,s   C!J$I%A$J
I 7JI:A	JI>4I<5I>95J.J>J
?J
J	I7I22I77J<I>>JJ
JJJc                &  K   |j                  d      }|j                  d      }||k(  ry| j                  |      }| j                  |      }| j                  sEt        || j                        st        || j                        rt        j                  dd|      t        j                  j                  |      r%t        ||      st        j                  dd|z        	 | j                         5  t        t        j                  ||       d{    ddd       y7 # 1 sw Y   yxY w# t
        j                  $ r  t        $ r t        j                  dd|       dt         $ r"}t        j                  d	d
| d|       |d}~ww xY wwr  )rW   rX   r{   r	   r&   r   r}   r*   r3   r   r   r   r   r  r  r  r   r  s         r.   r#  z$AsyncFileContentsManager.rename_file  sn    >>#&>>#&x''1''1  k4==1Y{DMM5Z--'G%WXX 77>>+&x[/Q--%>%IJJ	\!!# Fv{{KEEEF FEF F}} 	  	`--'I(%TU[__ 	\--'DXJaPQs%STZ[[	\s`   C F#D. 3D"D D"D. F D""D+'D. *F+D. .>F,F		FFc                   K   |j                  d      }| j                  |      }t        j                  j	                  |      S w)z(Does a directory exist at the given pathrT   rU   rg   rY   s      r.   rh   z#AsyncFileContentsManager.dir_exists7  s9     zz####.ww}}W%%s   AAc                   K   |j                  d      }| j                  |      }t        j                  j	                  |      S w)z#Does a file exist at the given pathrT   rc   rY   s      r.   re   z$AsyncFileContentsManager.file_exists=  s6     zz###D)ww~~g&&s   AAc                |   K   |j                  d      }| j                  |      }t        || j                        S w)z"Is path a hidden directory or filerT   rU   rV   rY   s      r.   r	   z"AsyncFileContentsManager.is_hiddenC  s7     zz####.$--00s   :<c                   K   | j                  |       d{   r|S d|v r|j                  dd      d   }|S d}|S 7 %w)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_pathI  sR     &&&K/2d{T[[a(+
 AC
 's   ?=&?c                  K   t        |      }|j                  d      }||j                  d      }d|v r|j                  dd      \  }}nd}|}| j                  |       d{   }|d   dk(  r| j	                  |       d{    n t        j                  | ||       d{   S |du}t        j                  d|      }	|s|}| j                  |       d{   r0t        j                  d|      }
t        | -  |
|d	
       d{   }	| d|	 }| j                  |||	|       d{   S 7 7 7 7 ^7 07 wr,  )r3  rW   r   r   r4  r   r5  r   r6  rh   rG   r7  r8  r9  s              r.   r5  zAsyncFileContentsManager.copyP  s]     w<s#mmC(G$;"&++c1"5HiHIhhtn$=K'((... .2249V]^^^#*$#6 ,,sI.'G)))<<Y/D!G6tWW6UUGIQwi(^^-	 $ 
 
 	
# % / _ *U
sl   A"E%D7& ED9 E'D;(6ED=/ED?#E2E3E9E;E=E?EEc           	       K   	 | j                  |j                  d            }| j                  |j                  d             d| }t        j                  ||       | j	                  |d       d{   }|S 7 # t
        $ rA}| j                  j                  d|        t        j                  dd| d| d	      |d}~ww xY ww)
r>  rT   Fr   Nr?  r   r@  z' into read-only Folder 'rA  rB  rD  s	            r.   r8  z"AsyncFileContentsManager._copy_dir|  s     
	,,Y__S-ABL --.>.D.DS.IJK1WIVJOOL*5((7E(::E  ; 	HHNN3C59:--yk)B7)1M 	s;   CA1A> 5A<6A> :C<A> >	C<CCCc                b  K   | j                   dz  dz  }t        | j                  | j                  |             d{         }t	        j
                         dk(  r|dz  n|}||kD  rAt        j                  dd| j                    d| d| j                  |       d{    d      y7 m7 w)	rI  rJ  NrK  r   rL  rM  rN  rO  rP  rV  s       r.   r4  z*AsyncFileContentsManager.check_folder_size  s     
 22T9D@++D,=,=d,CDDE&oo/8;td{+--4484P4P3Q RV5t'@'@'F!F G H   E "Gs"   ;B/B+A B/B-B/-B/c                L  K   	 t        j                         dk(  rd|g}ndd|g}t        j                  dg|t        j                  j
                  t        j                  j
                  d d{   }|j                          d{   \  }}|j                          d{   }| j                  j                  d|        |d	k(  sJ |j                  d
      j                         d	   }|S 7 7 j7 Q# t        $ r | j                  j                  d|       Y yw xY ww)rY  rK  r[  r^  r_  rZ  )rf  stderrNr`  r   ra  rb  rc  )r  rT  asynciocreate_subprocess_execrd  PIPEcommunicatewaitr_   r|   rh  rg  r   r   )r-   r3   argsprocrf  r   ri  rv   s           r.   rS  z&AsyncFileContentsManager._get_dir_size  s    	 H,t}.5 77$+$6$6$;$;GDVDVD[D[ D #..00IFA99;&FHHMM9&BCQ;;==)//1!4D  1&  	HHp 	s`   D$A.C9 2C33C9 
C5C9 %C7&AC9 1D$3C9 5C9 7C9 9%D!D$ D!!D$c                   K   |dk(  ryg d}|r!t        t        j                  |      dz        nd}|d|dz  z  z  dd||    S wrk  rs  rv  s       r.   rU  z-AsyncFileContentsManager._human_readable_size  sX      197-1DIIdOb()q!
+,S15<.AAs   AAry  rz  r{  r|  r}  r(   )
r-  r3  r1  r3  r2  r3  r.  r3  returnzdict[str, t.Any])r3   r3  r  Noner~  )r3   r3  r  r3  )rv   rR  r  r3  )r  r  r  r  r   rL   r   r   r   r   r   r   r  r#  rh   re   r	   r*  r5  r8  r4  rS  rU  r  r  s   @r.   r  r    s    ) !$ "$?B&P8/b
C/b4,l\<&'1*
X03>ALO	*&6
Br0   r  )>r  
__future__r   r  r   rt  r   r*   r  r  r   rd  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 #     	     
      $ @ @ !  G G $ A + , B ; D DA 
  w
B*O w
Bt`B24IK_ `BC  A@As   (C
 
CC