
    lKj                       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;d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                    t        | dd      }t        |t        t        f      r|dk\  rt	        j
                  |      r|S | j                  S )a^  Get best-effort file creation timestamp from stat result.

    Uses st_birthtime (actual creation time) when available (macOS, BSD,
    and Linux with kernel 4.11+ via statx on supported filesystems).
    On Windows, st_ctime is the creation time.
    On Linux/other, falls back to st_ctime (note: this is inode change time,
    not creation time, so operations like chmod may update 'created').

    Falls back to st_ctime if st_birthtime is unavailable, non-numeric,
    negative, or non-finite. Returns st_ctime as final fallback, which
    is validated in _base_model() during datetime conversion.
    st_birthtimeNr   )getattr
isinstanceintfloatmathisfinitest_ctime)info	birthtimes     }/Users/ahmed/devFolder/Ultron/claude-voice/.venv/lib/python3.12/site-packages/jupyter_server/services/contents/filemanager.py_get_created_timestampr-   /   sF     nd3I
 )c5\*yA~$--PYBZ ==    c                      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,dZd Zd Zd-dZd Zd.dZd/dZd0dZd1d Zd2d!Zd2d"Z d# Z!d$ Z"d% Z#d3d&Z$d3 fd'	Z%d( Z&d) Z'd4d*Z(d+ Z) xZ*S )5FileContentsManagerzA file contents manager.T)config  z&The max folder size that can be copied)r1   helproot_dirc                n    | j                   st        j                         S | j                   j                  S N)parentosgetcwdr4   selfs    r,   _default_root_dirz%FileContentsManager._default_root_dirO   s%    {{99;{{###r.   c                    |d   }t         j                  j                  |      st         j                  j                  |      }t         j                  j	                  |      st        d|z        |S )Nvaluez%r is not a directory)r8   pathisabsabspathisdirr   )r;   proposalr>   s      r,   _validate_root_dirz&FileContentsManager._validate_root_dirU   sT    !ww}}U#GGOOE*Eww}}U#4u<==r.   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)r7   rE   r4   warningswarnFutureWarningr   relative_toas_posix
ValueErrorr   AttributeError)r;   r>   r?   s      r,   _default_preferred_dirz*FileContentsManager._default_preferred_dir_   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 )Nr>   )r   r4   super_validate_preferred_dir)r;   rC   	__class__s     r,   rT   z+FileContentsManager._validate_preferred_dirw   s/     ((94==Iw.x88r.   checkpoints_classc                    t         S r6   )r   r:   s    r,   _checkpoints_class_defaultz.FileContentsManager._checkpoints_class_default}   s    r.   zIf True (default), deleting files will send them to the
        platform's trash/recycle bin, where they can be recovered. If False,
        deleting files really deletes them.Fa  If True, deleting a non-empty directory will always be allowed.
        WARNING this may result in files being permanently removed; e.g. on Windows,
        if the data size is too big for the trash/recycle bin the directory will be permanently
        deleted. If False (default), the non-empty directory will be sent to the trash only
        if safe. And if ``delete_to_trash`` is True, the directory won't be deleted.files_handler_classc                    t         S r6   r   r:   s    r,   _files_handler_class_defaultz0FileContentsManager._files_handler_class_default   s    ''r.   files_handler_paramsc                    d| j                   iS )Nr?   )r4   r:   s    r,   _files_handler_params_defaultz1FileContentsManager._files_handler_params_default   s    &&r.   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.
        /r?   strip_get_os_pathr	   r4   r;   r?   os_paths      r,   r	   zFileContentsManager.is_hidden   s5     zz####.$--00r.   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.
        r`   ra   z'Failed to check write permissions on %sF)rc   rd   r8   accessW_OKOSErrorlogerrorre   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.
        r`   rc   rd   r8   r?   isfilere   s      r,   file_existszFileContentsManager.file_exists   s4     zz###D)ww~~g&&r.   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.
        r`   ra   rc   rd   r8   r?   rB   re   s      r,   
dir_existszFileContentsManager.dir_exists   s7      zz####.ww}}W%%r.   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.
        r`   ra   )rc   rd   r   re   s      r,   r   zFileContentsManager.exists   s.     zz####.gr.   c                    t        || j                        }	 | j                  |      }t        |      r|S y# t        j                  $ r Y yw xY w)z'Resolve path relative to root resource.ra   N)r   r4   rd   r   	HTTPErrorr   )r;   r?   relative_pathrf   s       r,   resolve_pathz FileContentsManager.resolve_path   sV     $D$--8	'']';G '?  	 }} 		s   8 AAc           	     *   | j                  |      }t        j                  |      }d|z  }| j                  sHt	        || j
                        r2| j                  j                  d|       t        j                  d|      	 |j                  }	 t        j                  |j                         }t)        |      }	 t        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"        t        f$ rJ | j                  j                  d|j                   |       t%        dddd	d	t        j&                  
      }Y w xY w# t        t"        t        f$ r@ | j                  j                  d||       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 creation time %s for %sr`   namer?   last_modifiedcreatedcontentformatmimetypesizewritablehashhash_algorithm)rd   r8   lstatallow_hiddenr	   r4   rk   r*   r   rw   st_sizerO   rj   warningtzutcfromtimestampst_mtimeOverflowErrorr   UTCr-   rsplitrm   )
r;   r?   rf   r*   four_o_fourr   r   raw_createdr   models
             r,   _base_modelzFileContentsManager._base_model
  s   ##D)xx <tC  Yw%FHHMMXZab--[11	<<D
	F//>M -T2	@))+6G C+B/ff!.o"iih jf ,,T2jf"&G G$ 	HH23D	 M73 	F
 HH6wO$T1aAbffEM	F M73 	@HH>WUtQ1a?G	@s8   D+ E 9F> +,EEAF;:F;>A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: %rr}   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)r?   r   z+Unknown error checking if file %r is hiddenTexc_infojsonr   ) rd   r8   r?   rB   r   rw   r   r	   r4   rk   r*   r   listdirjoinUnicodeDecodeErrorr   r   rj   errnoENOENTEACCESstatS_ISLNKst_modeS_ISREGS_ISDIRdebugshould_listr   appendgetELOOP)r;   r?   r   rf   r   r   contentsos_dirr   ests              r,   
_dir_modelzFileContentsManager._dir_model=  sp   
 ##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   r   NT)rawz
text/plainzapplication/octet-stream)textbase64)r   r   byte r   rd   	mimetypes
guess_type
_read_fileupdate	_get_hash
r;   r?   r   r   require_hashr   rf   bytes_contentdefault_mime_s
             r,   _file_modelzFileContentsManager._file_model}  s       &f##D)%009!<j-1__WfRV_-W*GV]Z ((8    %1j!LL  
 $#'??7F#C qELL94>>-89r.   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   r   r   r   r   r   r   rd   _read_notebookmark_trusted_cellsvalidate_notebook_modelr   r   r   
r;   r?   r   r   r   rf   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r.   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.
        r`   r{   r}   r|   Nr      is a directory, not a bad typereasonr   r   .ipynbr   r   r   %s is not a directoryr   r   r   r   actionr?   data)rc   rd   r   r   rw   r   r	   r4   rk   r*   r8   r?   rB   r   endswithr   r   emit)	r;   r?   r   r   r   r   rf   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r.   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	   r4   r   rw   r8   r?   r   perm_to_403mkdirrB   rk   r   r;   rf   r   r?   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.r`   r   r?   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   r2   $Unexpected error while saving file:  Nr   messageFr   r   rf   saver   r   )rc   run_pre_save_hooksr   rw   rd   r   r	   r4   rk   r   nbformat	from_dictcheck_and_sign_save_notebookcheckpointslist_checkpointscreate_checkpoint
_save_filer   r   	Exceptionrl   r   run_post_save_hooksr   r;   r   r?   rf   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.r`   r    Cannot delete file or directory r{   r}   c                    t         j                  j                  |       r:t        j                  dd       }t        t        j                  |             |hz
  ryyNcheckpoint_dirTF)r8   r?   rB   r#   r  setr   )rf   cp_dirr;   s     r,   is_non_empty_dirz9FileContentsManager.delete_file.<locals>.is_non_empty_dirF  sH    ww}}W% !!1!13CTJrzz'*+vh6r.   win32Directory %s not empty  Permission denied: %sNSending %s to trashsend2trash failed: %sRemoving directory %sUnlinking file %s)rc   rd   r8   unlinkr   r	   r4   r   rw   r   delete_to_trashalways_delete_dirsysplatformrm   rk   r   r
   rj   r?   rB   r   shutilrmtree)r;   r?   rf   rmr   r  r   s   `      r,   delete_filezFileContentsManager.delete_file9  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.r`   Nr   z Cannot rename file or directory i  zFile already exists: %sr}   z"File or directory does not exist: r2   zUnknown error renaming file: r   )rc   rd   r   r	   r4   r   rw   r8   r?   r   r   r   r$  moveFileNotFoundErrorr
  r;   old_pathnew_pathnew_os_pathold_os_pathr   s         r,   rename_filezFileContentsManager.rename_filek  sL   >>#&>>#&x''1''1  k4==1Y{DMM5Z--'G%WXX 77>>+&x[/Q--%>%IJJ	\!!#K5 $##}} 	  	`--'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   r4   r:   s    r,   info_stringzFileContentsManager.info_string  s    ABT]]RRr.   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 notebookr`   r   r   rG   rt   r   r;   r?   r   
parent_dirs       r,   get_kernel_pathz#FileContentsManager.get_kernel_path  sC    ??4 K/2d{T[[a(+
 AC
r.   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.
        r`   Nr   rG   r   r   )	from_pathto_path.z-Copy)insert)r;  to_path_originalto_namer<  )strrc   r   r   check_folder_sizerS   copyr   subrt   increment_filename	_copy_dirr;   r;  r<  r?  r?   from_dir	from_namer   is_destination_specifiedr@  r   rU   s              r,   rC  zFileContentsManager.copy  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(~~-	  
 	
r.   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
        r`   Fr   OSError in _copy_dir: r   Can't copy 'z' into Folder ''N
rd   rc   r$  copytreer   rj   rk   rl   r   rw   	r;   r;  r?  r@  r<  os_from_path
os_to_pathr   errs	            r,   rF  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_mbr%   _get_dir_sizerd   r#  systemr   rw   _human_readable_sizer;   r?   limit_bytesr   s       r,   rB  z%FileContentsManager.check_folder_size  s    
 22T9D@4%%d&7&7&=>?&oo/8;td{+--4484P4P3Q RV5!:!:4!@ A B  r.   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
        rY  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#  ra  
subprocessrunstdoutsplitrk   r*   decoder
  r   )r;   r?   resultr   s       r,   r`  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   r%   r'   log2r;   r   unitsorders       r,   rb  z(FileContentsManager._human_readable_size  sT     197-1DIIdOb()q!
+,S15<.AAr.   )r?   rA  returnzbool | t.Awaitable[bool])r?   rA  r  z
str | NoneTTNFTFTNNFrG   r6   r=  )+__name__
__module____qualname____doc__r   r4   r   r_  r   r<   r   rD   rQ   rT   rX   r   r   r!  r[   r^   r	   rm   rq   rt   r   ry   r   r   r   r   r   r   r   r'  r1  r3  r8  rC  rF  rB  r`  rb  __classcell__rU   s   @r,   r0   r0   H   sy   "d#H!#d9abZ$ $
 j  _ . o9 9
  ! " /O X "#( $( #$' %'1$,'&&(&1f>@&P<4l
C2h0d\<S*
X&&:
Br.   r0   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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.rV   c                    t         S r6   )r   r:   s    r,   rX   z3AsyncFileContentsManager._checkpoints_class_default  s    ##r.   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   )!rd   r8   r?   rB   r   rw   r   r	   r4   rk   r*   r   r   r   r   r   r   r   rj   r   r   r   r   r   r   r   r   r   r   r   r   r   r   )r;   r?   r   rf   r   r   r   r   dir_contentsr   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_modelZ  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   r   r   r   r   r   r   r   s
             r,   r   z(AsyncFileContentsManager._notebook_model  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   r`   r}   zNo such file or directory: %sr   r   r   r   r   r   Nr   r   r   r   r   r   r   r   r   )rc   r   r   rw   rd   r8   r?   rB   r   r   r   r   r   )r;   r?   r   r   r   r   rf   r   s           r,   r   zAsyncFileContentsManager.get  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	   r4   r   rw   r8   r?   r   r   r   r   rB   rk   r   r   s       r,   r   z(AsyncFileContentsManager._save_directory  s       Yw%F--%H7%RSSww~~g&!!#rxx111 $#w'--%:g%FGGHHNN8'B	 2 $#s1   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   r`   r   r   r   r   r   r   r   r   r   r   Nr   r   r   r   Tr   r2   r   r   r   r   Fr   r   r   r   r   )rc   r  r   rw   rd   rk   r   r  r  r  r  r  r  r  r	  r   r   r
  rl   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  r`   r   r  r}   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  )r8   r?   rB   r#   r  r  r   r   )rf   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  )rc   rd   r8   r  r   r	   r4   r   rw   r?   r   r   r!  r"  r#  rm   rk   r   r
   rj   rB   r   r   r$  r%  )r;   r?   rf   r&  r  r   s   `     r,   r'  z$AsyncFileContentsManager.delete_file  s$    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<!!#v}}g666 $# HHNN.8!!#r7+++ $#1 4  MmmC)@1)DE1LM /N 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)  )rc   rd   r   r	   r4   r   rw   r8   r?   r   r   r   r   r$  r*  r+  r
  r,  s         r,   r1  z$AsyncFileContentsManager.rename_fileB  s`    >>#&>>#&x''1''1  k4==1Y{DMM5Z--'G%WXX 77>>+&x[/Q--%>%IJJ	\!!#v{{KEEE $#E $#}} 	  	`--'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 pathr`   ra   rs   re   s      r,   rt   z#AsyncFileContentsManager.dir_exists`  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 pathr`   ro   re   s      r,   rq   z$AsyncFileContentsManager.file_existsf  s6     zz###D)ww~~g&&s   AAc                |   K   |j                  d      }| j                  |      }t        || j                        S w)z"Is path a hidden directory or filer`   ra   rb   re   s      r,   r	   z"AsyncFileContentsManager.is_hiddenl  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 notebookNr`   r   r   rG   r5  r6  s       r,   r8  z(AsyncFileContentsManager.get_kernel_pathr  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:  )rA  rc   r   r   rB  r   rC  r   rD  rt   rS   rE  rF  rG  s              r,   rC  zAsyncFileContentsManager.copyy  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)
rL  r`   Fr   NrM  r   rN  z' into read-only Folder 'rO  rP  rR  s	            r,   rF  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)	rW  rX  NrY  r   rZ  r[  r\  r]  r^  rc  s       r,   rB  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)rf  rY  rh  rk  rl  rg  )rs  stderrNrm  r   rn  ro  rp  )r#  ra  asynciocreate_subprocess_execrq  PIPEcommunicatewaitrk   r*   ru  rt  r
  r   )r;   r?   argsprocrs  r   rv  r   s           r,   r`  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rx  r  r  s       r,   rb  z-AsyncFileContentsManager._human_readable_size  sX      197-1DIIdOb()q!
+,S15<.AAs   AAr  r  r  r  r  )r?   rA  r  boolr6   )
r;  rA  r?  rA  r@  rA  r<  rA  r  zdict[str, t.Any])r?   rA  r  Noner  )r?   rA  r  rA  )r   r%   r  rA  )r  r  r  r  r   rX   r   r   r   r   r   r   r'  r1  rt   rq   r	   r8  rC  rF  rB  r`  rb  r  r  s   @r,   r  r    s     * !$ "$?B&P8/b
C/b4,l\<&'1*
X03>ALO	*&6
Br.   r  )r*   zos.stat_resultr  r&   )?r  
__future__r   r  r   r'   r   r8   r#  r$  r   rq  r"  typingtrJ   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-   r0   r  r   r.   r,   <module>r     s    E #     	     
      $ @ @ !  G G $ A + , B ; D DA 
  2EB*O EBPbB.0DbBQ  A@As   (C CC