o
    ^ie                     @  s  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 zddl7m8Z8 W n e9y   ddl+m:Z8 Y nw dZ;G dd de2e5Z<G dd de<e1e4Z=dS )z?A contents manager that uses the local file system for storage.    )annotationsN)datetime)Path)run_sync)existsis_file_hidden	is_hidden)
send2trash)web)BoolInt
TraitErrorUnicodedefaultvalidate)_tzAuthenticatedFileHandler)_i18n)to_api_path   )AsyncFileCheckpointsFileCheckpoints)AsyncFileManagerMixinFileManagerMixin)AsyncContentsManagerContentsManagercopy_pat)samefile)samefile_simplec                      sz  e Zd ZdZeddZeddddZeddd	 Z	e
dd
d Zeddd Ze
d fddZeddd ZeddddZeddddZeddd Zeddd Zdd Zdd  Zd!d" Zd#d$ Zd%d& Zd'd( ZdJd)d*ZdKd,d-ZdLd.d/ZdMd0d1ZdNd3d4ZdNd5d6Zd7d8 Z d9d: Z!d;d< Z"dOd=d>Z#dO fd?d@	Z$dAdB Z%dCdD Z&dPdFdGZ'dHdI Z(  Z)S )QFileContentsManagerzA file contents manager.T)config  z&The max folder size that can be copied)r!   helproot_dirc                 C  s   | j st S | j jS N)parentosgetcwdr$   self r+   g/home/ubuntu/hpml_nyu/venv/lib/python3.10/site-packages/jupyter_server/services/contents/filemanager.py_default_root_dir6   s   z%FileContentsManager._default_root_dirc                 C  s<   |d }t j|st j|}t j|std| |S )Nvaluez%r is not a directory)r'   pathisabsabspathisdirr   )r*   proposalr.   r+   r+   r,   _validate_root_dir<   s   z&FileContentsManager._validate_root_dirpreferred_dirc                 C  s   | j sdS z| j j}|| j jkrd }W n
 ty   Y dS w |d urFtjdtdd zt|}|| j	 W S  t
yE   td| d w dS )N zqServerApp.preferred_dir config is deprecated in jupyter-server 2.0. Use FileContentsManager.preferred_dir instead   )
stacklevelz%%s is outside root contents directory)r&   r5   r$   AttributeErrorwarningswarnFutureWarningr   relative_toas_posix
ValueErrorr   )r*   r.   r/   r+   r+   r,   _default_preferred_dirF   s0   z*FileContentsManager._default_preferred_dirc                   s    t |d | j|d< t |S )Nr.   )r   r$   super_validate_preferred_dir)r*   r3   	__class__r+   r,   rB   ^   s   z+FileContentsManager._validate_preferred_dircheckpoints_classc                 C     t S r%   )r   r)   r+   r+   r,   _checkpoints_class_defaultd      z.FileContentsManager._checkpoints_class_defaultzIf 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                 C  rF   r%   r   r)   r+   r+   r,   _files_handler_class_defaultz   rH   z0FileContentsManager._files_handler_class_defaultfiles_handler_paramsc                 C  s
   d| j iS )Nr/   )r$   r)   r+   r+   r,   _files_handler_params_default~   s   
z1FileContentsManager._files_handler_params_defaultc                 C  s"   | d}| j|d}t|| jS )aT  Does the API style path correspond to a hidden directory or file?

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

        Returns
        -------
        hidden : bool
            Whether the path exists and is hidden.
        /r/   strip_get_os_pathr   r$   r*   r/   os_pathr+   r+   r,   r      s   
zFileContentsManager.is_hiddenc                 C  sJ   | d}| j|d}zt|tjW S  ty$   | jd| Y dS 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.
        rM   rN   z'Failed to check write permissions on %sF)rP   rQ   r'   accessW_OKOSErrorlogerrorrR   r+   r+   r,   is_writable   s   
zFileContentsManager.is_writablec                 C  s    | d}| |}tj|S )aC  Returns True if the file exists, else returns False.

        API-style wrapper for os.path.isfile

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

        Returns
        -------
        exists : bool
            Whether the file exists.
        rM   rP   rQ   r'   r/   isfilerR   r+   r+   r,   file_exists   s   

zFileContentsManager.file_existsc                 C  s"   | d}| j|d}tj|S )av  Does the API-style path refer to an extant directory?

        API-style wrapper for os.path.isdir

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

        Returns
        -------
        exists : bool
            Whether the path is indeed a directory.
        rM   rN   rP   rQ   r'   r/   r2   rR   r+   r+   r,   
dir_exists   s   
zFileContentsManager.dir_existsc                 C  s   | d}| j|d}t|S )a@  Returns True if the path exists, else returns False.

        API-style wrapper for os.path.exists

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

        Returns
        -------
        exists : bool
            Whether the target exists.
        rM   rN   )rP   rQ   r   rR   r+   r+   r,   r      s   
zFileContentsManager.existsc           	      C  s  |  |}t|}d| }| js$t|| jr$| jd| t	d|z|j
}W n ttfy<   | jd d}Y nw zt|j}W n ttfyd   | jd|j| tdddd	d	tjd
}Y nw zt|j}W n ttfy   | jd|j| tdddd	d	tjd
}Y nw i }|ddd |d< ||d< ||d< ||d< d|d< d|d< d|d< ||d< | ||d< d|d< d|d< |S )z)Build the common base of a contents model$file or directory does not exist: %r<Refusing to serve hidden file or directory %r, via 404 Error  zUnable to get size.NzInvalid mtime %s for %si  r   r   )tzinfozInvalid ctime %s for %srM   namer/   last_modifiedcreatedcontentformatmimetypesizewritablehashhash_algorithm)rQ   r'   lstatallow_hiddenr   r$   rW   infor
   	HTTPErrorst_sizer?   rV   warningtzutcfromtimestampst_mtimer   UTCst_ctimersplitrY   )	r*   r/   rS   rp   four_o_fourrj   re   rf   modelr+   r+   r,   _base_model   sJ   


zFileContentsManager._base_modelc                 C  s  |  |}d| }tj|std|| js+t|| jr+| j	
d| td|| |}d|d< d|d< |rg  |d< }|}t|D ]}z	tj||}W n tym }	 z| j	d	||	 W Y d}	~	qHd}	~	ww zt|}
W n. ty }	 z"|	jtjkr| j	d
| n|	jtjkr| j	d||	 W Y d}	~	qHd}	~	ww t|
jst|
jst|
js| j	d| qHz| |r| jst||
ds|| j| d| dd W qH ty }	 z|	jtjtjfvr| j	jd|dd W Y d}	~	qHd}	~	ww d|d< |S zpBuild a model for a directory

        if content is requested, will include a listing of the directory
        zdirectory does not exist: %rra   z4Refusing to serve hidden directory %r, via 404 Error	directorytypeNrj   rg   z"failed to decode filename '%s': %rz%s doesn't existzError stat-ing %s: %rz%s not a regular file)stat_resrM   F)r/   rg   z+Unknown error checking if file %r is hiddenTexc_infojsonrh   ) rQ   r'   r/   r2   r
   rq   ro   r   r$   rW   rp   r|   listdirjoinUnicodeDecodeErrorrs   rn   rV   errnoENOENTEACCESstatS_ISLNKst_modeS_ISREGS_ISDIRdebugshould_listr   appendgetELOOP)r*   r/   rg   rS   rz   r{   contentsos_dirrd   estr+   r+   r,   
_dir_model  st   


	



	zFileContentsManager._dir_modelNc           
      C  s   |  |}d|d< | |}t|d |d< d}|r>| j||dd\}}}|d du r7dd	d
| }||d< |j||d |rW|du rL| |d\}}	|jdi | | |S a  Build a model for a file

        if content is requested, include the file contents.

        format:
          If 'text', the contents will be decoded as UTF-8.
          If 'base64', the raw bytes contents will be encoded as base64.
          If not specified, try to decode as UTF-8, and fall back to base64

        if require_hash is true, the model will include 'hash'
        filer   r   ri   NT)rawz
text/plainzapplication/octet-stream)textbase64)rg   rh   byter+   r|   rQ   	mimetypes
guess_type
_read_fileupdate	_get_hash
r*   r/   rg   rh   require_hashr{   rS   bytes_contentdefault_mime_r+   r+   r,   _file_modelV  s.   

zFileContentsManager._file_modelc           
      C  s   |  |}d|d< | |}d}|r3i }| j|d|dd\}}| || ||d< d|d	< | || |rL|du rA| |d
\}}	|jdi | | |S )zBuild a notebook model

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

        if require_hash is true, the model will include 'hash'
        notebookr   N   T
as_versioncapture_validation_errorr   rg   r   rh   r   r+   r|   rQ   _read_notebookmark_trusted_cellsvalidate_notebook_modelr   r   r   
r*   r/   rg   r   r{   rS   r   validation_errornbr   r+   r+   r,   _notebook_model~  s$   


z#FileContentsManager._notebook_modelc           	      C  s  | d}| |}d| }| |std|| js/t|| jr/| j	d| td|t
j|rN|dvrFtjd| d| dd	| j||d
}n-|dks[|du rd|drd| j|||d}n|dkrrtjdd| dd	| j||||d}| jd|dd |S )h  Takes a path for an entity and returns its model

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

        Returns
        -------
        model : dict
            the contents model. If content=True, returns the contents
            of the file or directory as well.
        rM   r_   ra   r`   Nr~      is a directory, not a bad typereasonrg   r   N.ipynbrg   r   r~   %s is not a directoryrg   rh   r   r   actionr/   data)rP   rQ   r   r
   rq   ro   r   r$   rW   rp   r'   r/   r2   r   endswithr   r   emit)	r*   r/   rg   r   rh   r   rS   rz   r{   r+   r+   r,   r     s2   


zFileContentsManager.getr6   c                 C  s   | j st|| jrtdd| tj|s3|   t	| W d   dS 1 s,w   Y  dS tj
|sAtdd| | jd| dS )create a directoryr   zCannot create directory %rNNot a directory: %sDirectory %r already exists)ro   r   r$   r
   rq   r'   r/   r   perm_to_403mkdirr2   rW   r   r*   rS   r{   r/   r+   r+   r,   _save_directory  s   
"z#FileContentsManager._save_directoryc              
   C  s  | d}| j||d d|vrtddd|vr&|d dkr&tdd| |}| js=t|| jr=tdd	|| j	d
| i }zT|d dkrnt
|d }| || | j|||d | j|sm| | n+|d dkr| ||d |d n|d dkr| ||| n
tdd|d  W n+ tjy     ty } z| jjd||dd tdd| d| |d}~ww d}|d dkr| j||d |dd}| j|dd}|r||d< | j||d | jd|dd |S )9Save the file model and return the model with no content.rM   r{   r/   r   r   No file type providedrg   r~   No file content providedz Cannot create file or directory 	Saving %sr   r   r   rh   Unhandled contents type: %sError while saving file: %s %sTr   r"   $Unexpected error while saving file:  Nr   messageFr   r{   rS   saver   r   )rP   run_pre_save_hooksr
   rq   rQ   ro   r   r$   rW   r   nbformat	from_dictcheck_and_sign_save_notebookcheckpointslist_checkpointscreate_checkpoint
_save_filer   r   	ExceptionrX   r   run_post_save_hooksr   r*   r{   r/   rS   r   r   r   validation_messager+   r+   r,   r     sT   


zFileContentsManager.savec              
     s  | d} |}tj} jst| jrtdd|d| } 	|s.td| fdd} j
r} jsKtjdkrK||rKtdd	|  |sYtd
d| d jd| zt| W dS  ty| } z	tdd| |d}~ww tj|r js||rtdd	|  jd|    t| W d   dS 1 sw   Y  dS  jd|    || W d   dS 1 sw   Y  dS )Delete file at path.rM   r    Cannot delete file or directory r_   ra   c                   s6   t j| rt jdd }tt | |h rdS dS Ncheckpoint_dirTF)r'   r/   r2   getattrr   setr   )rS   cp_dirr)   r+   r,   is_non_empty_dir  s
   z9FileContentsManager.delete_file.<locals>.is_non_empty_dirwin32Directory %s not empty  Permission denied: %sNSending %s to trashsend2trash failed: %sRemoving directory %sUnlinking file %s)rP   rQ   r'   unlinkro   r   r$   r
   rq   r   delete_to_trashalways_delete_dirsysplatformrY   rW   r   r	   rV   r/   r2   r   shutilrmtree)r*   r/   rS   rmrz   r   r   r+   r)   r,   delete_file  sB   






"

"zFileContentsManager.delete_filec              
   C  s,  | d}| d}||krdS | |}| |}| js2t|| js)t|| jr2tdd|tj	|rEt
||sEtdd| z|   t|| W d   W dS 1 s]w   Y  W dS  tjym     ty}   tdd| d ty } ztd	d
| d| |d}~ww zRename a file.rM   Nr   z Cannot rename file or directory i  zFile already exists: %sra   z"File or directory does not exist: r"   zUnknown error renaming file: r   )rP   rQ   ro   r   r$   r
   rq   r'   r/   r   r   r   r  moveFileNotFoundErrorr   r*   old_pathnew_pathnew_os_pathold_os_pathr   r+   r+   r,   rename_fileD  s4   






&zFileContentsManager.rename_filec                 C  s   t d| j S )z+Get the information string for the manager.z*Serving notebooks from local directory: %s)r   r$   r)   r+   r+   r,   info_stringb  s   zFileContentsManager.info_stringc                 C  s2   |  |r|S d|v r|ddd }|S d}|S )zIReturn the initial API path of  a kernel associated with a given notebookrM   r   r   r6   r^   ry   r*   r/   r{   
parent_dirr+   r+   r,   get_kernel_pathf  s   
z#FileContentsManager.get_kernel_pathc                   s   t |}|d}|dur|d}d|v r|dd\}}nd}|}| |}|d dkr4| | nt j||dS |du}td|}	|sJ|}| 	|r^td|}
t j
|
|d	d
}	| d|	 }| j|||	|dS aA  
        Copy an existing file or directory and return its new model.
        If to_path not specified, it will be the parent directory of from_path.
        If copying a file and to_path is a directory, filename/directoryname will increment `from_path-Copy#.ext`.
        Considering multi-part extensions, the Copy# part will be placed before the first dot for all the extensions except `ipynb`.
        For easier manual searching in case of notebooks, the Copy# part will be placed before the last dot.
        from_path must be a full path to a file or directory.
        rM   Nr   r6   r   r~   )	from_pathto_path.z-Copy)insert)r  to_path_originalto_namer  )strrP   ry   r   check_folder_sizerA   copyr   subr^   increment_filename	_copy_dirr*   r  r  r  r/   from_dir	from_namer{   is_destination_specifiedr  rd   rC   r+   r,   r  m  s4   	



zFileContentsManager.copyc           	      C  s   z%|  |d}|  |d d| }t|| | j|dd}W |S  tyH } z| jd|  t	dd| d| d|d	}~ww )
`
        handles copying directories
        returns the model for the copied directory
        rM   Fr   OSError in _copy_dir: r   Can't copy 'z' into Folder ''N
rQ   rP   r  copytreer   rV   rW   rX   r
   rq   	r*   r  r  r  r  os_from_path
os_to_pathr{   errr+   r+   r,   r    s    zFileContentsManager._copy_dirc                 C  sn   | j d d }t| | |}t dkr|d n|}||kr5tdd| j  d| d| | ddS )	
        limit the size of folders being copied to be no more than the
        trait max_copy_folder_size_mb to prevent a timeout error
           Darwinr   4
                    Can't copy folders larger than MB,
                    "" is 
                N	max_copy_folder_size_mbint_get_dir_sizerQ   r   systemr
   rq   _human_readable_sizer*   r/   limit_bytesrj   r+   r+   r,   r    s   z%FileContentsManager.check_folder_sizer  c                 C  s   z7t  dkrtjdd|gdddj }ntjddd|gdddj }| jd|  |d	 d
}W |S  t	yH   | j
d| Y dS w )M
        calls the command line program du to get the directory size
        r0  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   r9  
subprocessrunstdoutsplitrW   rp   decoder   rs   )r*   r/   resultrj   r+   r+   r,   r8    s4   
z!FileContentsManager._get_dir_sizec                 C  sN   |dkrdS g d}|rt t|d nd}|d|d >  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   r7  mathlog2r*   rj   unitsorderr+   r+   r,   r:    s
    z(FileContentsManager._human_readable_sizeTTNFTFTNNFr6   r%   r  )*__name__
__module____qualname____doc__r   r$   r   r6  r   r-   r   r4   r@   rB   rG   r   r   r   rJ   rL   r   rY   r\   r^   r   r|   r   r   r   r   r   r   r  r  r  r  r  r  r  r8  r:  __classcell__r+   r+   rC   r,   r    /   sb    


	





2
@
(

6
42
,
r    c                      s   e Zd ZdZeddd Zd7ddZd8d
dZd9ddZd:ddZ	d;ddZ
d;ddZdd Zdd Zdd Zdd Zdd Zd<dd Zd< fd!d"	Zd=d*d+Zd>d.d/Zd?d@d1d2ZdAd5d6Z  ZS )BAsyncFileContentsManagerzAn async file contents manager.rE   c                 C  rF   r%   )r   r)   r+   r+   r,   rG     rH   z3AsyncFileContentsManager._checkpoints_class_defaultTc                   s0  |  |}d| }tj|std|| js,t|| jr,| j	
d| td|| |}d|d< d|d< |rg  |d< }|}ttj|I dH }|D ]}	z	tj||	}W n tyt }
 z| j	d	|	|
 W Y d}
~
qOd}
~
ww zttj|I dH }W n. ty }
 z"|
jtjkr| j	d
| n|
jtjkr| j	d||
 W Y d}
~
qOd}
~
ww t|jst|jst|js| j	d| qOz"| |	r| jst||ds|| j| d|	 ddI dH  W qO ty }
 z|
jtj tjfvr| j	jd|dd W Y d}
~
qOd}
~
ww d|d< |S r}   )!rQ   r'   r/   r2   r
   rq   ro   r   r$   rW   rp   r|   r   r   r   r   rs   rn   rV   r   r   r   r   r   r   r   r   r   r   r   r   r   r   )r*   r/   rg   rS   rz   r{   r   r   dir_contentsrd   r   r   r+   r+   r,   r     sx   


	



$	z#AsyncFileContentsManager._dir_modelNFc           
        s   |  |}d|d< | |}t|d |d< d}|rB| j||ddI dH \}}}|d du r;dd	d
| }||d< |j||d |r^|du rS| |dI dH \}}	|jdi | | |S r   r   r   r+   r+   r,   r   1  s0   

z$AsyncFileContentsManager._file_modelc           
        s   |  |}d|d< | |}d}|r7i }| j|d|ddI dH \}}| || ||d< d|d	< | || |rS|du rH| |d
I dH \}}	|jdi | | |S )zBuild a notebook model

        if content is requested, the notebook content will be populated
        as a JSON structure (not double-serialized)
        r   r   Nr   Tr   rg   r   rh   r   r+   r   r   r+   r+   r,   r   Y  s&   

z(AsyncFileContentsManager._notebook_modelc                   s   | d}| |stdd| | |}tj|r:|dvr/tjd| d| dd| j||d	I d
H }n3|dksG|d
u rS|	drS| j
|||dI d
H }n|dkratjdd| dd| j||||dI d
H }| jd|dd |S )r   rM   ra   zNo such file or directory: %sr   r   r   r   r   r   Nr   r   r   r~   r   r   r   r   r   )rP   r   r
   rq   rQ   r'   r/   r2   r   r   r   r   r   )r*   r/   rg   r   rh   r   rS   r{   r+   r+   r,   r   u  s,   


zAsyncFileContentsManager.getr6   c                   s   | j st|| jrtdd| tj|s8|   t	tj
|I dH  W d   dS 1 s1w   Y  dS tj|sFtdd| | jd| dS )r   r   z!Cannot create hidden directory %rNr   r   )ro   r   r$   r
   rq   r'   r/   r   r   r   r   r2   rW   r   r   r+   r+   r,   r     s   
"z(AsyncFileContentsManager._save_directoryc              
     s  | d}| j||d d|vrtddd|vr'|d dkr'tdd| |}| jd	| i }zc|d d
krft|d }| 	|| | j
|||dI dH  | j|I dH se| |I dH  n1|d dkr|| ||d |dI dH  n|d dkr| |||I dH  n
tdd|d  W n+ tjy     ty } z| jjd||dd tdd| d| |d}~ww d}|d d
kr| j||d |dd}| j|ddI dH }|r||d< | j||d | jd|dd |S )r   rM   r   r   r   r   rg   r~   r   r   r   r   Nr   rh   r   r   Tr   r"   r   r   r   r   Fr   r   r   r   r   )rP   r   r
   rq   rQ   rW   r   r   r   r   r   r   r   r   r   r   r   r   rX   r   r   r   r   r+   r+   r,   r     sR   

 zAsyncFileContentsManager.savec              
     s  | d} |}tj} js t| jr tdd|tj	
|s.tdd|  fdd} jr jsNtjdkrN||I d	H rNtdd
|  |s\tdd| d	 jd| zt| W d	S  ty } z	tdd| |d	}~ww tj	|r js||I d	H rtdd
|  jd|    ttj|I d	H  W d	   d	S 1 sw   Y  d	S  jd|    t||I d	H  W d	   d	S 1 sw   Y  d	S )r   rM   r   r   ra   z$File or directory does not exist: %sc                   sD   t j| r t jdd }ttt j| I d H }||h r dS dS r   )r'   r/   r2   r   r   r   r   r   )rS   r   rh  r)   r+   r,   r     s   
z>AsyncFileContentsManager.delete_file.<locals>.is_non_empty_dirr   Nr   r   r   r   r   r   r   )rP   rQ   r'   r   ro   r   r$   r
   rq   r/   r   r   r   r   r   rY   rW   r   r	   rV   r2   r   r   r  r  )r*   r/   rS   r  r   r   r+   r)   r,   r    sJ   





"
"z$AsyncFileContentsManager.delete_filec              
     s6  | d}| d}||krdS | |}| |}| js3t|| js*t|| jr3tdd|tj	|rFt
||sFtdd| z#|   ttj||I dH  W d   W dS 1 sbw   Y  W dS  tjyr     ty   tdd| d ty } ztd	d
| d| |d}~ww r  )rP   rQ   ro   r   r$   r
   rq   r'   r/   r   r   r   r   r  r  r  r   r  r+   r+   r,   r    s6   






&z$AsyncFileContentsManager.rename_filec                   s$   | d}| j|d}tj|S )z(Does a directory exist at the given pathrM   rN   r]   rR   r+   r+   r,   r^   7     
z#AsyncFileContentsManager.dir_existsc                   s"   | d}| |}tj|S )z#Does a file exist at the given pathrM   rZ   rR   r+   r+   r,   r\   =  s   

z$AsyncFileContentsManager.file_existsc                   s$   | d}| j|d}t|| jS )z"Is path a hidden directory or filerM   rN   rO   rR   r+   r+   r,   r   C  ri  z"AsyncFileContentsManager.is_hiddenc                   s:   |  |I dH r|S d|v r|ddd }|S d}|S )zHReturn the initial API path of a kernel associated with a given notebookNrM   r   r   r6   r  r  r+   r+   r,   r  I  s   z(AsyncFileContentsManager.get_kernel_pathc                   s  t |}|d}|dur|d}d|v r |dd\}}nd}|}| |I dH }|d dkr;| |I dH  ntj| ||dI dH S |du}td|}	|sT|}| 	|I dH rntd|}
t
 j|
|d	d
I dH }	| d|	 }| j|||	|dI dH S r  )r  rP   ry   r   r  r   r  r   r  r^   rA   r  r  r   rC   r+   r,   r  P  s6   	

zAsyncFileContentsManager.copyr  r  r  r  r  returndict[str, t.Any]c           	        s   z(|  |d}|  |d d| }t|| | j|ddI dH }W |S  tyL } z| jd|  t	dd| d| d	|d}~ww )
r$  rM   Fr   Nr%  r   r&  z' into read-only Folder 'r'  r(  r*  r+   r+   r,   r  |  s"   z"AsyncFileContentsManager._copy_dirr/   Nonec                   s|   | j d d }t| | |I dH }t dkr|d n|}||kr<tdd| j  d| d| |I dH  ddS )	r.  r/  Nr0  r   r1  r2  r3  r4  r5  r;  r+   r+   r,   r    s    z*AsyncFileContentsManager.check_folder_sizer  c                   s   zOt  dkrd|g}ndd|g}tjdg|R tjjtjjdI dH }| I dH \}}| I dH }| j	d|  |d	ksEJ |
d
 d	 }W |S  tya   | jd| Y dS w )r=  r0  r?  rB  rC  r>  )rJ  stderrNrD  r   rE  rF  rG  )r   r9  asynciocreate_subprocess_execrH  PIPEcommunicatewaitrW   rp   rL  rK  r   rs   )r*   r/   argsprocrJ  r   rM  rj   r+   r+   r,   r8    s2   

z&AsyncFileContentsManager._get_dir_sizerj   r7  c                   sP   |dkrdS g d}|rt t|d nd}|d|d >  dd||  S rN  rV  rY  r+   r+   r,   r:    s    z-AsyncFileContentsManager._human_readable_sizer\  r]  r^  r_  r`  r%   )
r  r  r  r  r  r  r  r  rj  rk  )r/   r  rj  rl  ra  )r/   r  rj  r  )rj   r7  rj  r  )rb  rc  rd  re  r   rG   r   r   r   r   r   r   r  r  r^   r\   r   r  r  r  r  r8  r:  rf  r+   r+   rC   r,   rg    s*    


A
(

1
16

,
rg  )>re  
__future__r   rn  r   rW  r   r'   r   r  r   rH  r   typingtr:   r   pathlibr   r   anyio.to_threadr   jupyter_core.pathsr   r   r   r	   tornador
   	traitletsr   r   r   r   r   r   jupyter_serverr   rt   jupyter_server.base.handlersr   jupyter_server.transutilsr   jupyter_server.utilsr   filecheckpointsr   r   fileior   r   managerr   r   r   os.pathr   ImportErrorr   _script_exporterr    rg  r+   r+   r+   r,   <module>   sT          ?