
    ^he                       S r SSKJr  SSKrSSKrSSKrSSKrSSKrSSKrSSK	r	SSK
r
SSKrSSKrSSKrSSKrSSKJr  SSKJr  SSKrSSKJr  SSKJrJrJr  SSKJr  SS	KJr  SS
KJrJrJ r J!r!J"r"J#r#  SSK$J%r&  SSK'J(r(  SSK)J*r*  SSK+J,r,  SSK-J.r.J/r/  SSK0J1r1J2r2  SSK3J4r4J5r5J6r6   SSK7J8r8  Sr; " S S\2\55      r< " S S\<\1\45      r=g! \9 a	    SSK+J:r8   N*f = f)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                    ^  \ rS rSrSr\" SS9r\" SSSS9r\	" S5      S	 5       r
\" S5      S
 5       r\	" S5      S 5       r\" S5      U 4S j5       r\	" S5      S 5       r\" SSSS9r\" SSSS9r\	" S5      S 5       r\	" S5      S 5       rS rS rS rS rS rS rS-S jrS.S jrS/S jrS0S  jrS1S! jrS1S" jr S# r!S$ r"S% r#S2S& jr$S2U 4S' jjr%S( r&S) r'S3S* jr(S+ r)S,r*U =r+$ )4FileContentsManager/   zA file contents manager.T)config  z&The max folder size that can be copied)r$   helproot_dirc                z    U R                   (       d  [        R                  " 5       $ U R                   R                  $ N)parentosgetcwdr'   selfs    f/home/james-whalen/.local/lib/python3.13/site-packages/jupyter_server/services/contents/filemanager.py_default_root_dir%FileContentsManager._default_root_dir6   s%    {{99;{{###    c                    US   n[         R                  R                  U5      (       d  [         R                  R                  U5      n[         R                  R	                  U5      (       d  [        SU-  5      eU$ )Nvaluez%r is not a directory)r+   pathisabsabspathisdirr   )r.   proposalr4   s      r/   _validate_root_dir&FileContentsManager._validate_root_dir<   sZ    !ww}}U##GGOOE*Eww}}U##4u<==r2   preferred_dirc                   U R                   (       d  g U R                   R                  nXR                   R                  :X  a  S nUbO  [        R                  " S[
        SS9   [        U5      nUR                  U R                  5      R                  5       $  g! [         a    [        SU-  5      S ef = f! [         a     gf = f)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*   r<   r'   warningswarnFutureWarningr   relative_toas_posix
ValueErrorr   AttributeError)r.   r4   r5   s      r/   _default_preferred_dir*FileContentsManager._default_preferred_dirF   s    {{	`KK--E,,,   H! 
`;D++DMM:CCEE !  " `$%Lu%TU[__`  	 	s   1B6 #3B B36
CCc                Z   > [        US   U R                  5      US'   [        TU ]  U5      $ )Nr4   )r   r'   super_validate_preferred_dir)r.   r9   	__class__s     r/   rL   +FileContentsManager._validate_preferred_dir^   s/     ((94==Iw.x88r2   checkpoints_classc                    [         $ r)   )r   r-   s    r/   _checkpoints_class_default.FileContentsManager._checkpoints_class_defaultd   s    r2   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                    [         $ r)   r   r-   s    r/   _files_handler_class_default0FileContentsManager._files_handler_class_defaultz   s    ''r2   files_handler_paramsc                    SU R                   0$ )Nr5   )r'   r-   s    r/   _files_handler_params_default1FileContentsManager._files_handler_params_default~   s    &&r2   c                l    UR                  S5      nU R                  US9n[        X R                  5      $ )a  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.
/r5   strip_get_os_pathr	   r'   r.   r5   os_paths      r/   r	   FileContentsManager.is_hidden   s3     zz####.--00r2   c                    UR                  S5      nU R                  US9n [        R                  " U[        R                  5      $ ! [
         a    U R                  R                  SU5         gf = f)a  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.
r\   r]   z'Failed to check write permissions on %sF)r_   r`   r+   accessW_OKOSErrorlogerrorra   s      r/   is_writableFileContentsManager.is_writable   sc     zz####.	99Wbgg.. 	HHNNDgN	s   $A &A0/A0c                    UR                  S5      nU R                  U5      n[        R                  R	                  U5      $ )zReturns 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.
r\   r_   r`   r+   r5   isfilera   s      r/   file_existsFileContentsManager.file_exists   s4     zz###D)ww~~g&&r2   c                    UR                  S5      nU R                  US9n[        R                  R	                  U5      $ )a  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.
r\   r]   r_   r`   r+   r5   r8   ra   s      r/   
dir_existsFileContentsManager.dir_exists   s7      zz####.ww}}W%%r2   c                X    UR                  S5      nU R                  US9n[        U5      $ )zReturns 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.
r\   r]   )r_   r`   r   ra   s      r/   r   FileContentsManager.exists   s.     zz####.gr2   c           
     4   U R                  U5      n[        R                  " U5      nSU-  nU R                  (       dM  [	        X R
                  5      (       a3  U R                  R                  SU5        [        R                  " SU5      e UR                  n [        R                  " UR                   5      n [        R                  " UR&                  5      n0 nUR)                  SS5      S   US'   XS'   XhS'   XxS'   SUS'   SUS'   SUS'   XXS'   U R+                  U5      US'   SUS'   SUS'   U$ ! [        [        4 a     U R                  R                  S5        Sn Nf = f! [        [        4 aG    U R                  R                  SUR                   U5        [#        SSSS	S	[        R$                  S
9n GNf = f! [        [        4 aG    U R                  R                  SUR&                  U5        [#        SSSS	S	[        R$                  S
9n GN<f = f)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 %sr\   namer5   last_modifiedcreatedcontentformatmimetypesizewritablehashhash_algorithm)r`   r+   lstatallow_hiddenr	   r'   rh   infor   	HTTPErrorst_sizerF   rg   warningtzutcfromtimestampst_mtimer   UTCst_ctimersplitrj   )	r.   r5   rb   r   four_o_fourr   r~   r   models	            r/   _base_modelFileContentsManager._base_model   s   ##D)xx <tC  Yw%F%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   D3  E& : G  3-E#"E#&AF=<F= AHHc                   U R                  U5      nSU-  n[        R                  R                  U5      (       d  [        R
                  " SU5      eU R                  (       dM  [        X0R                  5      (       a3  U R                  R                  SU5        [        R
                  " SU5      eU R                  U5      nSUS'   SUS'   U(       GaQ  / =US'   nUn[        R                  " U5       GH(  n [        R                  R                  Xx5      n [        R                   " U5      n
[*        R,                  " U
R.                  5      (       dh  [*        R0                  " U
R.                  5      (       dC  [*        R2                  " U
R.                  5      (       d  U R                  R5                  SU5        M   U R7                  U5      (       aI  U R                  (       d  [9        X:S9(       d'  UR;                  U R=                  U SU 3SS95        GM%  GM(  GM+     SUS'   U$ ! [         a(  n	U R                  R                  S	X5         Sn	A	GMa  Sn	A	ff = f! ["         a  n	U	R$                  [$        R&                  :X  a  U R                  R                  S
U5        O:U	R$                  [$        R(                  :w  a  U R                  R                  SX95         Sn	A	GM  Sn	A	ff = f! ["         a\  n	U	R$                  [$        R>                  [$        R(                  4;  a"  U R                  R                  SUSS9   Sn	A	GMQ   Sn	A	GMX  Sn	A	ff = fz`Build a model for a directory

if content is requested, will include a listing of the directory
zdirectory does not exist: %rrz   z4Refusing to serve hidden directory %r, via 404 Error	directorytypeNr   r   z"failed to decode filename '%s': %rz%s doesn't existzError stat-ing %s: %rz%s not a regular file)stat_resr\   F)r5   r   z+Unknown error checking if file %r is hiddenTexc_infojsonr   ) r`   r+   r5   r8   r   r   r   r	   r'   rh   r   r   listdirjoinUnicodeDecodeErrorr   r   rg   errnoENOENTEACCESstatS_ISLNKst_modeS_ISREGS_ISDIRdebugshould_listr   appendgetELOOP)r.   r5   r   rb   r   r   contentsos_dirr}   ests              r/   
_dir_modelFileContentsManager._dir_model  s   
 ##D)4t;ww}}W%%--[11""y--'H'HHHMMPRYZ--[11  &#ff*,,E)xF

6* ggll68G
'*B RZZ00 LL44 LL44HHNN#:GD''--))1U av6FPU(VW 2V .5 +N %E(OM * HH$$%I4S  ww%,,.(();WEELL0(()@'M*  wwu{{ELL&AA((I#%) )   BsK   7H&I;AK)&
I0II
K&%A5K!!K&)
M3A	M

Mc                   U R                  U5      nSUS'   U R                  U5      n[        R                  " U5      S   US'   SnU(       a5  U R	                  XcSS9u  p#nUS   c  SS	S
.U   nXS'   UR                  UUS9  U(       a8  Uc  U R	                  US5      u  pyUR
                  " S0 U R                  U5      D6  U$ aH  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   r   NT)rawz
text/plainzapplication/octet-stream)textbase64)r   r   byte r   r`   	mimetypes
guess_type
_read_fileupdate	_get_hash
r.   r5   r   r   require_hashr   rb   bytes_contentdefault_mime_s
             r/   _file_modelFileContentsManager._file_modelV  s       &f##D)%009!<j-1__WRV_-W*G]Z ((8    %1j!LL  
 $#'??7F#C LL94>>-89r2   c                f   U R                  U5      nSUS'   U R                  U5      nSnU(       aA  0 nU R                  USUSS9u  pU R                  X5        XS'   SUS	'   U R	                  XG5        U(       a8  Uc  U R                  US
5      u  piUR                  " S0 U R                  U5      D6  U$ )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   r   r   r   r   r   r   r`   _read_notebookmark_trusted_cellsvalidate_notebook_modelr   r   r   
r.   r5   r   r   r   rb   r   validation_errornbr   s
             r/   _notebook_model#FileContentsManager._notebook_model~  s       &"f##D)13 $ 3 3A@PVZ !4 !B ##B-!)$E(O((A$#'??7F#C LL94>>-89r2   c                   UR                  S5      nU R                  U5      nSU-  nU R                  U5      (       d  [        R                  " SU5      eU R
                  (       dM  [        X`R                  5      (       a3  U R                  R                  SU5        [        R                  " SU5      e[        R                  R                  U5      (       a1  US;  a  [        R                  " SU SU 3SS	9eU R                  XS
9nO_US:X  d  Uc'  UR                  S5      (       a  U R                  XUS9nO/US:X  a  [        R                  " SSU-  SS	9eU R!                  XXES9nU R#                  SUS.S9  U$ )  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.
r\   rx   rz   ry   Nr      is a directory, not a bad typereasonr   r   .ipynbr   r   r   %s is not a directoryr   r   r   r   actionr5   data)r_   r`   r   r   r   r   r	   r'   rh   r   r+   r5   r8   r   endswithr   r   emit)	r.   r5   r   r   r   r   rb   r   r   s	            r/   r   FileContentsManager.get  s`   0 zz###D)<tC{{4  --[11  Yw%F%FHHMMXZab--[1177==!!..mmf3D6:% 
 OODO:EZDLT]]85L5L((\(ZE{"mmC)@4)GPZ[[$$f % E 			%6	7r2   c                   U R                   (       d4  [        XR                  5      (       a  [        R                  " SSU-  5      e[
        R                  R                  U5      (       d0  U R                  5          [
        R                  " U5        SSS5        g[
        R                  R                  U5      (       d  [        R                  " SSU-  5      eU R                  R                  SU5        g! , (       d  f       g= f)create a directoryr   zCannot create directory %rNNot a directory: %sDirectory %r already exists)r   r	   r'   r   r   r+   r5   r   perm_to_403mkdirr8   rh   r   r.   rb   r   r5   s       r/   _save_directory#FileContentsManager._save_directory  s      Yw%F%F--%AG%KLLww~~g&&!!#! $#w''--%:g%FGGHHNN8'B $#s   :C55
Dc                   UR                  S5      nU R                  XS9  SU;  a  [        R                  " SS5      eSU;  a   US   S:w  a  [        R                  " SS5      eU R	                  U5      nU R
                  (       d5  [        X0R                  5      (       a  [        R                  " SS	U< 35      eU R                  R                  S
U5        0 n US   S:X  al  [        R                  " US   5      nU R                  XR5        U R                  X5US9  U R                  R                  U5      (       d  U R!                  U5        OgUS   S:X  a%  U R#                  X1S   UR%                  S5      5        O9US   S:X  a  U R'                  X1U5        O[        R                  " SSUS   -  5      eSnUS   S:X  a!  U R-                  XS9  UR%                  SS5      nU R%                  USS9nU(       a  XqS'   U R/                  XS9  U R1                  SUS.S9  U$ ! [        R                   a    e [(         a>  nU R                  R+                  SX&SS9  [        R                  " SSU SU 35      UeSnAff = f)9Save the file model and return the model with no content.r\   r   r5   r   r   No file type providedr   r   No file content providedz Cannot create file or directory 	Saving %sr   r   r   r   Unhandled contents type: %sError while saving file: %s %sTr   r%   $Unexpected error while saving file:  Nr   messageFr   r   rb   saver   r   )r_   run_pre_save_hooksr   r   r`   r   r	   r'   rh   r   nbformat	from_dictcheck_and_sign_save_notebookcheckpointslist_checkpointscreate_checkpoint
_save_filer   r   	Exceptionri   r   run_post_save_hooksr   r.   r   r5   rb   r   r   r   validation_messages           r/   r  FileContentsManager.save  s`   zz#e7--%<==E!eFm{&B--%?@@##D)  Yw%F%F--'G{%STT{G,-/	_V}
*''i(89##B-##GJZ#[''88>>**40v&(y)9599X;NOv+-$$WT:mmC)Fv)VWW "=J&(((R!&9d!;u-1)  u >		&$7	8# }} 	 	_HHNN;TtNT--'KD6QRSTRU%VW]^^	_s+   A4H  -H  =H  H   I<>9I77I<c                >  ^  UR                  S5      nT R                  U5      n[        R                  nT R                  (       d6  [        UT R                  5      (       a  [        R                  " SSU< 35      eSU-  nT R                  U5      (       d  [        R                  " SU5      eU 4S jnT R                  (       a  T R                  (       d;  [        R                  S:X  a'  U" U5      (       a  [        R                  " SSU-  5      eT R                  U5      (       d  [        R                  " S	S
U-  5      SeT R                  R!                  SU5         [#        U5        g[        R&                  R)                  U5      (       a  T R                  (       d'  U" U5      (       a  [        R                  " SSU-  5      eT R                  R!                  SU5        T R+                  5          [,        R.                  " U5        SSS5        gT R                  R!                  SU5        T R+                  5          U" U5        SSS5        g! [$         a   n[        R                  " SSU-  5      UeSnAff = f! , (       d  f       g= f! , (       d  f       g= f)Delete file at path.r\   r    Cannot delete file or directory rx   rz   c                   > [         R                  R                  U 5      (       a@  [        TR                  SS 5      n[        [         R                  " U 5      5      U1-
  (       a  ggNcheckpoint_dirTF)r+   r5   r8   getattrr
  setr   )rb   cp_dirr.   s     r/   is_non_empty_dir9FileContentsManager.delete_file.<locals>.is_non_empty_dir  sK    ww}}W%% !!1!13CTJrzz'*+vh6r2   win32Directory %s not empty  Permission denied: %sNSending %s to trashsend2trash failed: %sRemoving directory %sUnlinking file %s)r_   r`   r+   unlinkr   r	   r'   r   r   r   delete_to_trashalways_delete_dirsysplatformrj   rh   r   r
   rg   r5   r8   r   shutilrmtree)r.   r5   rb   rmr   r  r   s   `      r/   delete_fileFileContentsManager.delete_file  s   zz###D)YY  Yw%F%F--'G{%STT<tC{{4  --[11	 ))cllg.EJZ[bJcJc mmC)AG)KLL##D))mmC)@4)GHdRHHNN0':M7# 77==!!)).>w.G.GmmC)AG)KLLHHNN2G<!!#g& $# HHNN.8!!#7 $#  MmmC)@1)DE1LM $# $#s0   I 2I=>	J
I:I55I:=
J
Jc                F   UR                  S5      nUR                  S5      nX!:X  a  gU R                  U5      nU R                  U5      nU R                  (       dO  [        X@R                  5      (       d  [        X0R                  5      (       a  [
        R                  " SSU< 35      e[        R                  R                  U5      (       a*  [        XC5      (       d  [
        R                  " SSU-  5      e U R                  5          [        R                  " XC5        SSS5        g! , (       d  f       g= f! [
        R                   a    e [         a    [
        R                  " SSU 35      Se[         a#  n[
        R                  " S	S
U SU 35      UeSnAff = fzRename a file.r\   Nr   z Cannot rename file or directory i  zFile already exists: %srz   z"File or directory does not exist: r%   zUnknown error renaming file: r   )r_   r`   r   r	   r'   r   r   r+   r5   r   r   r   r+  moveFileNotFoundErrorr  r.   old_pathnew_pathnew_os_pathold_os_pathr   s         r/   rename_fileFileContentsManager.rename_fileD  sN   >>#&>>#&''1''1  k==11Y{MM5Z5Z--'G%WXX 77>>+&&x/Q/Q--%>%IJJ	\!!#K5 $##}} 	  	`--'I(%TU[__ 	\--'DXJaPQs%STZ[[	\s7   :D; 
D*!D; *
D84D; 8D; ;AF =FF c                2    [        S5      U R                  -  $ )z+Get the information string for the manager.z*Serving notebooks from local directory: %s)r   r'   r-   s    r/   info_stringFileContentsManager.info_stringb  s    ABT]]RRr2   c                t    U R                  U5      (       a  U$ SU;   a  UR                  SS5      S   nU$ SnU$ )zIReturn the initial API path of  a kernel associated with a given notebookr\   r   r   r>   rs   r   r.   r5   r   
parent_dirs       r/   get_kernel_path#FileContentsManager.get_kernel_pathf  sF    ??4  K/2d{T[[a(+
 AC
r2   c                  > [        U5      nUR                  S5      nUb  UR                  S5      nSU;   a  UR                  SS5      u  pVOSnUnU R                  U5      nUS   S:X  a  U R	                  U5        O[
        TU ]  XS9$ USLn[        R                  " SU5      n	U(       d  UnU R                  U5      (       a%  [        R                  " SU5      n
[
        TU ])  XS	S
9n	U SU	 3nU R                  UUU	US9$ a	  
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.
r\   Nr   r>   r   r   )	from_pathto_path.z-Copy)insert)rF  to_path_originalto_namerG  )strr_   r   r   check_folder_sizerK   copyr   subrs   increment_filename	_copy_dirr.   rF  rG  rJ  r5   from_dir	from_namer   is_destination_specifiedrK  r}   rM   s              r/   rN  FileContentsManager.copym  s    w<s#mmC(G$;"&++c1"5HiHI=K'""4( 7<)<EE#*$#6 ,,sI.'G??7##<<Y/Dg0w0OGIQwi(~~-	  
 	
r2   c           	     |    U R                  UR                  S5      5      nU R                  UR                  S5      5       SU 3n[        R                  " XV5        U R	                  USS9nU$ ! [
         aB  nU R                  R                  SU 35        [        R                  " SSU SU S35      UeS	nAff = f)
H
handles copying directories
returns the model for the copied directory
r\   Fr   OSError in _copy_dir: r   Can't copy 'z' into Folder ''N
r`   r_   r+  copytreer   rg   rh   ri   r   r   	r.   rF  rJ  rK  rG  os_from_path
os_to_pathr   errs	            r/   rQ  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+A/ /
B;9=B66B;c                8   U R                   S-  S-  n[        U R                  U R                  U5      5      5      n[        R
                  " 5       S:X  a  US-  OUnX2:  a:  [        R                  " SSU R                    SU SU R                  U5       S35      eg)	x
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_sizer`   r*  systemr   r   _human_readable_sizer.   r5   limit_bytesr   s       r/   rM  %FileContentsManager.check_folder_size  s    
 22T9D@4%%d&7&7&=>?&oo/8;td{--4484P4P3Q RV5!:!:4!@ A B  r2   c                    [         R                  " 5       S:X  a2  [        R                  " SSU/SSS9R                  R                  5       nO2[        R                  " SSSU/SSS9R                  R                  5       nU R                  R                  SU 35        US	   R                  S
5      nU$ ! [         a    U R                  R                  SU5         gf = f)=
calls the command line program du to get the directory size
rf  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*  ro  
subprocessrunstdoutsplitrh   r   decoder  r   )r.   r5   resultr   s       r/   rn  !FileContentsManager._get_dir_size  s    	 H,#5$'#' &	  $4!148#' &	  HHMM9&BC!9##G,D   	HHp 	s   B/B3 3&CCc                    US:X  a  g/ SQnU(       a"  [        [        R                  " U5      S-  5      OSnUSUS-  -  -  S SX#    3$ z0
returns folder size in a human readable format
r   z0 Bytes)BytesKBMBGBTBPB
   r   z.4gr   rm  mathlog2r.   r   unitsorders       r/   rp  (FileContentsManager._human_readable_size  sR     197-1DIIdOb()q!
+,S15<.AAr2   r   TTNFTFTNNFr>   r)   rH  ),__name__
__module____qualname____firstlineno____doc__r   r'   r   rl  r   r0   r   r:   rH   rL   rQ   r   r'  r(  rU   rY   r	   rj   ro   rs   r   r   r   r   r   r   r   r  r.  r9  r<  rB  rN  rQ  rM  rn  rp  __static_attributes____classcell__rM   s   @r/   r"   r"   /   sz   "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 
Br2   r"   c                     ^  \ rS rSrSr\" S5      S 5       rSS jrSS jrSS jr	SS jr
SS	 jrSS
 jrS rS rS rS rS rSS jrSU 4S jjr          SS jrSS jrSS S jjrS!S jrSrU =r$ )"AsyncFileContentsManageri  zAn async file contents manager.rO   c                    [         $ r)   )r   r-   s    r/   rQ   3AsyncFileContentsManager._checkpoints_class_default  s    ##r2   c                   #    U R                  U5      nSU-  n[        R                  R                  U5      (       d  [        R
                  " SU5      eU R                  (       dM  [        X0R                  5      (       a3  U R                  R                  SU5        [        R
                  " SU5      eU R                  U5      nSUS'   SUS'   U(       Gas  / =US'   nUn[        [        R                  U5      I Sh  vN nU GH<  n	 [        R                  R                  Xy5      n [        [        R"                  U5      I Sh  vN n[,        R.                  " UR0                  5      (       dh  [,        R2                  " UR0                  5      (       dC  [,        R4                  " UR0                  5      (       d  U R                  R7                  SU5        M   U R9                  U	5      (       aQ  U R                  (       d  [;        X;S9(       d/  UR=                  U R?                  U SU	 3SS9I Sh  vN 5        GM9  GM<  GM?     SUS'   U$  GNO! [         a(  n
U R                  R!                  S	X5         Sn
A
GMx  Sn
A
ff = f GN>! [$         a  n
U
R&                  [&        R(                  :X  a  U R                  R!                  S
U5        O:U
R&                  [&        R*                  :w  a  U R                  R!                  SX:5         Sn
A
GM	  Sn
A
ff = f N! [$         a\  n
U
R&                  [&        R@                  [&        R*                  4;  a"  U R                  R!                  SUSS9   Sn
A
GMm   Sn
A
GMt  Sn
A
ff = f7fr   )!r`   r+   r5   r8   r   r   r   r	   r'   rh   r   r   r   r   r   r   r   r   rg   r   r   r   r   r   r   r   r   r   r   r   r   r   r   )r.   r5   r   rb   r   r   r   r   dir_contentsr}   r   r   s               r/   r   #AsyncFileContentsManager._dir_model  s    
 ##D)4t;ww}}W%%--[11""y--'H'HHHMMPRYZ--[11  &#ff*,,E)xF!)"**f!==L$ ggll68G
''::B RZZ00 LL44 LL44HHNN#:GD''--))1U dhhtfAdV<LV[h.\(\] 2V .5 %N %E(OU > * HH$$%I4S
 ; ww%,,.(();WEELL0(()@'M( )] wwu{{ELL&AA((I#%) )   Bs   C9M>;I
<
M>I'JJJ	BM>AL/L
0L8M>
I?I:3M>:I??M>J
LA5LM>LM>L
M;A	M6(M>6M;;M>c                  #    U R                  U5      nSUS'   U R                  U5      n[        R                  " U5      S   US'   SnU(       a=  U R	                  XcSS9I Sh  vN u  p#nUS   c  SS	S
.U   nXS'   UR                  UUS9  U(       a@  Uc  U R	                  US5      I Sh  vN u  pyUR
                  " S0 U R                  U5      D6  U$  Nr N+7fr   r   r   s
             r/   r   $AsyncFileContentsManager._file_model1  s        &f##D)%009!<j37??7X\?3]-]*G]Z ((8    %1j!LL  
 $)-&)I#I LL94>>-89% .^ $Js%   A C"C#AC+C,*CCc                  #    U R                  U5      nSUS'   U R                  U5      nSnU(       aI  0 nU R                  USUSS9I Sh  vN u  pU R                  X5        XS'   SUS	'   U R	                  XG5        U(       a@  Uc  U R                  US
5      I Sh  vN u  piUR                  " S0 U R                  U5      D6  U$  Nz N+7f)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   r   r   r   r   r   r   r   s
             r/   r   (AsyncFileContentsManager._notebook_modelY  s        &"f##D)13&*&9&9A@PVZ ': ' !B ##B-!)$E(O((A$)-&)I#I LL;DNN=9;! $Js%   AC	
CAC	C*C	C	c                z  #    UR                  S5      nU R                  U5      (       d  [        R                  " SSU-  5      eU R	                  U5      n[
        R                  R                  U5      (       a9  US;  a  [        R                  " SU SU 3SS9eU R                  XS	9I S
h  vN nOoUS:X  d  Uc/  UR                  S5      (       a  U R                  XUS9I S
h  vN nO7US:X  a  [        R                  " SSU-  SS9eU R                  XXES9I S
h  vN nU R                  SUS.S9  U$  N NR N7f)r   r\   rz   zNo such file or directory: %sr   r   r   r   r   r   Nr   r   r   r   r   r   r   r   r   )r_   r   r   r   r`   r+   r5   r8   r   r   r   r   r   )r.   r5   r   r   r   r   rb   r   s           r/   r   AsyncFileContentsManager.getu  s=    0 zz#{{4  --%Dt%KLL##D)77==!!..mmf3D6:% 
 //$/@@EZDLT]]85L5L..tS_.``E{"mmC)@4)GPZ[[**f +  E 			%6	7 A`s6   B*D;,D5-7D;$D7%7D;D9D;7D;9D;c                0  #    U R                   (       d4  [        XR                  5      (       a  [        R                  " SSU-  5      e[
        R                  R                  U5      (       d<  U R                  5          [        [
        R                  U5      I Sh  vN   SSS5        g[
        R                  R                  U5      (       d  [        R                  " SSU-  5      eU R                  R                  SU5        g Nh! , (       d  f       g= f7f)r   r   z!Cannot create hidden directory %rNr   r   )r   r	   r'   r   r   r+   r5   r   r   r   r   r8   rh   r   r   s       r/   r   (AsyncFileContentsManager._save_directory  s       Yw%F%F--%H7%RSSww~~g&&!!#rxx111 $#w''--%:g%FGGHHNN8'B	 2 $#s1   A:D<DDDA$DD
DDc                  #    UR                  S5      nU R                  XS9  SU;  a  [        R                  " SS5      eSU;  a   US   S:w  a  [        R                  " SS5      eU R	                  U5      nU R
                  R                  S	U5        0 n US   S
:X  a  [        R                  " US   5      nU R                  XR5        U R                  X5US9I Sh  vN   U R                  R                  U5      I Sh  vN (       d  U R                  U5      I Sh  vN   OwUS   S:X  a-  U R                  X1S   UR                  S5      5      I Sh  vN   OAUS   S:X  a  U R!                  X1U5      I Sh  vN   O[        R                  " SSUS   -  5      eSnUS   S
:X  a!  U R'                  XS9  UR                  SS5      nU R                  USS9I Sh  vN nU(       a  XqS'   U R)                  XS9  U R+                  SUS.S9  U$  GN0 GN N N N! [        R                   a    e ["         a>  nU R
                  R%                  SX&SS9  [        R                  " SSU SU 35      UeSnAff = f N7f)r   r\   r   r   r   r   r   r   r   r   r   r   Nr   r   r   r   Tr   r%   r   r   r  r  Fr   r  r  r   r   )r_   r  r   r   r`   rh   r   r  r  r  r	  r
  r  r  r  r   r   r  ri   r   r  r   r  s           r/   r  AsyncFileContentsManager.save  sk    zz#e7--%<==E!eFm{&B--%?@@##D){G,-/	_V}
*''i(89##B-))'P`)aaa"..??EEE00666v&(oogY/?8ATUUUv+-**74@@@mmC)Fv)VWW "=J&(((R!&9d!;hhtUh331)  u >		&$7	89 bE6 V@ }} 	 	_HHNN;TtNT--'KD6QRSTRU%VW]^^	_ 4s   BI9AH H"H ?H H HH "I9#0H HH I9H 7H8H <I9=H ?I9I72I9H H H H H I469I//I44I9c                  ^ #    UR                  S5      nT R                  U5      n[        R                  nT R                  (       d6  [        UT R                  5      (       a  [        R                  " SSU< 35      e[        R                  R                  U5      (       d  [        R                  " SSU-  5      eU 4S jnT R                  (       a  T R                  (       dC  [        R                  S:X  a/  U" U5      I Sh  vN (       a  [        R                  " SS	U-  5      eT R                  U5      (       d  [        R                  " S
SU-  5      SeT R                   R#                  SU5         [%        U5        g[        R                  R)                  U5      (       a  T R                  (       d/  U" U5      I Sh  vN (       a  [        R                  " SS	U-  5      eT R                   R#                  SU5        T R+                  5          [-        [.        R0                  U5      I Sh  vN   SSS5        gT R                   R#                  SU5        T R+                  5          [-        X25      I Sh  vN   SSS5        g GN! [&         a   n[        R                  " SSU-  5      UeSnAff = f N N! , (       d  f       g= f NR! , (       d  f       g= f7f)r  r\   r   r  rz   z$File or directory does not exist: %sc                   >#    [         R                  R                  U 5      (       aM  [        TR                  SS 5      n[        [        [         R                  U 5      I S h  vN 5      nX!1-
  (       a  gg N7fr  )r+   r5   r8   r  r
  r  r   r   )rb   r  r  r.   s      r/   r  >AsyncFileContentsManager.delete_file.<locals>.is_non_empty_dir  s\     ww}}W%% !!1!13CTJ""**g)F#FG(*	 $Gs   AA8!A6"A8r  Nr  r   r!  r"  r#  r$  r%  )r_   r`   r+   r&  r   r	   r'   r   r   r5   r   r'  r(  r)  r*  rj   rh   r   r
   rg   r8   r   r   r+  r,  )r.   r5   rb   r-  r  r   s   `     r/   r.  $AsyncFileContentsManager.delete_file  s*    zz###D)YY  Yw%F%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<!!#v}}g666 $# HHNN.8!!#r+++ $#1 4  MmmC)@1)DE1LM /N 7 $#
 , $#s   C?KJA0K4J ?AK J5AKJ9.J7/J935K(K7K
8K<
K
J2J--J22K7J99
KK
K
KKc                j  #    UR                  S5      nUR                  S5      nX!:X  a  gU R                  U5      nU R                  U5      nU R                  (       dO  [        X@R                  5      (       d  [        X0R                  5      (       a  [
        R                  " SSU< 35      e[        R                  R                  U5      (       a*  [        XC5      (       d  [
        R                  " SSU-  5      e U R                  5          [        [        R                  XC5      I Sh  vN   SSS5        g N! , (       d  f       g= f! [
        R                   a    e [         a    [
        R                  " SSU 35      Se[          a#  n[
        R                  " S	S
U SU 35      UeSnAff = f7fr1  )r_   r`   r   r	   r'   r   r   r+   r5   r   r   r   r   r+  r2  r3  r  r4  s         r/   r9  $AsyncFileContentsManager.rename_file  s`    >>#&>>#&''1''1  k==11Y{MM5Z5Z--'G%WXX 77>>+&&x/Q/Q--%>%IJJ	\!!#v{{KEEE $#E $#}} 	  	`--'I(%TU[__ 	\--'DXJaPQs%STZ[[	\sa   C9F3<E D:*D8+D:/E 7F38D::
EE F3E AF0F++F00F3c                   #    UR                  S5      nU R                  US9n[        R                  R	                  U5      $ 7f)z(Does a directory exist at the given pathr\   r]   rr   ra   s      r/   rs   #AsyncFileContentsManager.dir_exists7  s8     zz####.ww}}W%%s   A Ac                   #    UR                  S5      nU R                  U5      n[        R                  R	                  U5      $ 7f)z#Does a file exist at the given pathr\   rm   ra   s      r/   ro   $AsyncFileContentsManager.file_exists=  s5     zz###D)ww~~g&&s   AAc                t   #    UR                  S5      nU R                  US9n[        X R                  5      $ 7f)z"Is path a hidden directory or filer\   r]   r^   ra   s      r/   r	   "AsyncFileContentsManager.is_hiddenC  s4     zz####.--00s   68c                   #    U R                  U5      I Sh  vN (       a  U$ SU;   a  UR                  SS5      S   nU$ SnU$  N,7f)zHReturn the initial API path of a kernel associated with a given notebookNr\   r   r   r>   r?  r@  s       r/   rB  (AsyncFileContentsManager.get_kernel_pathI  sQ     &&&K/2d{T[[a(+
 AC
 's   AA-Ac                  >#    [        U5      nUR                  S5      nUb  UR                  S5      nSU;   a  UR                  SS5      u  pVOSnUnU R                  U5      I Sh  vN nUS   S:X  a  U R	                  U5      I Sh  vN   O[
        R                  " XUS9I Sh  vN $ USLn[        R                  " SU5      n	U(       d  UnU R                  U5      I Sh  vN (       a-  [        R                  " SU5      n
[        TU ]-  XS	S
9I Sh  vN n	U SU	 3nU R                  UUU	US9I Sh  vN $  N N N N] N- N7frE  )rL  r_   r   r   rM  r   rN  r   rO  rs   rK   rP  rQ  rR  s              r/   rN  AsyncFileContentsManager.copyP  sS     w<s#mmC(G$;"&++c1"5HiHIhhtn$=K'((... .224V]^^^#*$#6 ,,sI.'G)))<<Y/D!G6tW6UUGIQwi(^^-	 $ 
 
 	
# % / _ *U
sl   A$E'D<(!E	D>
E'E (<E$E%1EE E7E8E>E EEEEc           	       #     U R                  UR                  S5      5      nU R                  UR                  S5      5       SU 3n[        R                  " XV5        U R	                  USS9I Sh  vN nU$  N! [
         aB  nU R                  R                  SU 35        [        R                  " SSU SU S	35      UeSnAff = f7f)
rX  r\   Fr   NrY  r   rZ  z' into read-only Folder 'r[  r\  r^  s	            r/   rQ  "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
A.A; 2A93A; 7C
9A; ;
C=CCC
c                h  #    U R                   S-  S-  n[        U R                  U R                  U5      5      I Sh  vN 5      n[        R
                  " 5       S:X  a  US-  OUnX2:  aB  [        R                  " SSU R                    SU SU R                  U5      I Sh  vN  S35      eg Np N7f)	rd  re  Nrf  r   rg  rh  ri  rj  rk  rq  s       r/   rM  *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"   ;B2B.A#B2!B0"B20B2c                T  #     [         R                  " 5       S:X  a  SU/nOSSU/n[        R                  " S/UQ7[        R                  R
                  [        R                  R
                  S.6I Sh  vN nUR                  5       I Sh  vN u  pEUR                  5       I Sh  vN nU R                  R                  SU 35        US	:X  d   eUR                  S
5      R                  5       S	   nU$  N Nj NR! [         a    U R                  R                  SU5         gf = f7f)ru  rf  rw  rz  r{  rv  )r  stderrNr|  r   r}  r~  r  )r*  ro  asynciocreate_subprocess_execr  PIPEcommunicatewaitrh   r   r  r  r  r   )r.   r5   argsprocr  r   r  r   s           r/   rn  &AsyncFileContentsManager._get_dir_size  s    	 H,t}.5 77$+$6$6$;$;GDVDVD[D[ D #..00IF99;&FHHMM9&BCQ;;==)//1!4D  1&  	HHp 	s`   D(A1C< 5C66C< C8C< 'C:(AC< 4D(6C< 8C< :C< <&D%"D($D%%D(c                   #    US:X  a  g/ SQnU(       a"  [        [        R                  " U5      S-  5      OSnUSUS-  -  -  S SX#    3$ 7fr  r  r  s       r/   rp  -AsyncFileContentsManager._human_readable_size  sU      197-1DIIdOb()q!
+,S15<.AAs   A
Ar   r  r  r  r  r  r)   )
rF  rL  rJ  rL  rK  rL  rG  rL  returnzdict[str, t.Any])r5   rL  r  Noner  )r5   rL  r  rL  )r   rm  r  rL  )r  r  r  r  r  r   rQ   r   r   r   r   r   r  r.  r9  rs   ro   r	   rB  rN  rQ  rM  rn  rp  r  r  r  s   @r/   r  r    s    ) !$ "$?B&P8/b
C/b4,l\<&'1*
X03>ALO	*&6
B 
Br2   r  )>r  
__future__r   r  r   r  r   r+   r*  r+  r   r  r)  typingtrA   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   r2   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