o
    i                     @   s  d 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l+m,Z,m-Z- ddl.m/Z/m0Z0m1Z1 zddl2m3Z3 W n e4y   ddl&m5Z3 Y nw dZ6G dd de-e0Z7G dd de7e,e/Z8dS )z?A contents manager that uses the local file system for storage.    N)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dJd.d/ZdLd0d1ZdMd3d4ZdMd5d6Zd7d8 Z d9d: Z!d;d< Z"dNd=d>Z#dN fd?d@	Z$dAdB Z%dCdD Z&dOdFdGZ'dHdI Z(  Z)S )PFileContentsManagerzA file contents manager.T)config  z&The max folder size that can be copied)r    helproot_dirc                 C   s&   z| j jW S  ty   t  Y S w N)parentr#   AttributeErrorosgetcwdself r+   g/var/www/edux/Edux_v2/venv/lib/python3.10/site-packages/jupyter_server/services/contents/filemanager.py_default_root_dir1   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_dir8   s   z&FileContentsManager._validate_root_dirpreferred_dirc                 C   s   z| j j}|| j jkrd }W n
 ty   Y dS w |d urAtjdtdd zt|}|| j	 W S  t
y@   td| d w dS )NzqServerApp.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#   r&   warningswarnFutureWarningr   relative_toas_posix
ValueErrorr   )r*   r.   r/   r+   r+   r,   _default_preferred_dirB   s,   z*FileContentsManager._default_preferred_dirc                    s    t |d | j|d< t |S )Nr.   )r   r#   super_validate_preferred_dir)r*   r3   	__class__r+   r,   rA   X   s   z+FileContentsManager._validate_preferred_dircheckpoints_classc                 C      t S r$   )r   r)   r+   r+   r,   _checkpoints_class_default^      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   rE   r$   r   r)   r+   r+   r,   _files_handler_class_defaultt   rG   z0FileContentsManager._files_handler_class_defaultfiles_handler_paramsc                 C   s
   d| j iS )Nr/   )r#   r)   r+   r+   r,   _files_handler_params_defaultx   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.
        rL   rM   z'Failed to check write permissions on %sF)rO   rP   r'   accessW_OKOSErrorlogerrorrQ   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.
        rL   rO   rP   r'   r/   isfilerQ   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.
        rL   rM   rO   rP   r'   r/   r2   rQ   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.
        rL   rM   )rO   rP   r   rQ   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< |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 %srL   namer/   last_modifiedcreatedcontentformatmimetypesizewritable)rP   r'   lstatallow_hiddenr   r#   rV   infor	   	HTTPErrorst_sizer>   rU   warningtzutcfromtimestampst_mtimer   UTCst_ctimersplitrX   )	r*   r/   rR   rm   four_o_fourri   rd   re   modelr+   r+   r,   _base_model   sF   


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yp }	 z| j	d	||	 W Y d}	~	qKd}	~	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}	~	qKd}	~	ww t|
jst|
jst|
js| j	d| qKz| |r| jst||
ds|| j| d| dd W qK ty }	 z|	jtjtjfvr| j	jd|dd W Y d}	~	qKd}	~	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: %rr`   z4Refusing to serve hidden directory %r, via 404 Error	directorytypeNri   rf   z"failed to decode filename '%s': %rz%s doesn't existzError stat-ing %s: %rz%s not a regular file)stat_resrL   F)r/   rf   z+Unknown error checking if file %r is hiddenTexc_infojsonrg   ) rP   r'   r/   r2   r	   rn   rl   r   r#   rV   rm   ry   listdirjoinUnicodeDecodeErrorrp   rk   rU   errnoENOENTEACCESstatS_ISLNKst_modeS_ISREGS_ISDIRdebugshould_listr   appendgetELOOP)r*   r/   rf   rR   rw   rx   contentsos_dirrc   estr+   r+   r,   
_dir_model  st   



	



	zFileContentsManager._dir_modelNc                 C   sv   |  |}d|d< | |}t|d |d< |r9| ||\}}|d du r2ddd| }||d< |j||d	 |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
        filer|   r   rh   Nz
text/plainzapplication/octet-stream)textbase64rf   rg   ry   rP   	mimetypes
guess_type
_read_fileupdater*   r/   rf   rg   rx   rR   default_mimer+   r+   r,   _file_modelN  s$   


zFileContentsManager._file_modelc                 C   s`   |  |}d|d< | |}|r.i }| j|d|d}| || ||d< d|d< | || |S )Build a notebook model

        if content is requested, the notebook content will be populated
        as a JSON structure (not double-serialized)
        notebookr|      
as_versioncapture_validation_errorrf   r   rg   ry   rP   _read_notebookmark_trusted_cellsvalidate_notebook_modelr*   r/   rf   rx   rR   validation_errornbr+   r+   r,   _notebook_modeln  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 rc|drc| j||d
}n|dkrqtjdd| dd	| j|||d}| jd|dd |S )  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.

        Returns
        -------
        model : dict
            the contents model. If content=True, returns the contents
            of the file or directory as well.
        rL   r^   r`   r_   Nr{      is a directory, not a bad typereasonrf   r   N.ipynbr{   %s is not a directoryr   r   actionr/   data)rO   rP   r   r	   rn   rl   r   r#   rV   rm   r'   r/   r2   r   endswithr   r   emit)r*   r/   rf   r|   rg   rR   rw   rx   r+   r+   r,   r     s.   


zFileContentsManager.getr8   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)rl   r   r#   r	   rn   r'   r/   r   perm_to_403mkdirr2   rV   r   r*   rR   rx   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.rL   rx   r/   r|   r   No file type providedrf   r{   No file content providedz Cannot create file or directory 	Saving %sr   r   r   rg   Unhandled contents type: %sError while saving file: %s %sTr~   r!   $Unexpected error while saving file:  Nr   messageFr   rx   rR   saver   r   )rO   run_pre_save_hooksr	   rn   rP   rl   r   r#   rV   r   nbformat	from_dictcheck_and_sign_save_notebookcheckpointslist_checkpointscreate_checkpoint
_save_filer   r   	ExceptionrW   r   run_post_save_hooksr   r*   rx   r/   rR   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|dd } fdd	} j
rt jsOtjd
krO||rOtdd| ||rm |s`tdd|  jd| t| dS  jd| 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.rL   r    Cannot delete file or directory r^   r`   c                 S   s6   t jdv rdS t| j}ttjdj}||kS N>   win32darwinT~)sysplatformr'   r   st_devr/   
expanduserrR   file_devhome_devr+   r+   r,   _check_trash  s
   
z5FileContentsManager.delete_file.<locals>._check_trashc                    s6   t j| rt jdd }tt | |h rdS dS Ncheckpoint_dirTF)r'   r/   r2   getattrr   setr   )rR   cp_dirr)   r+   r,   is_non_empty_dir  s
   z9FileContentsManager.delete_file.<locals>.is_non_empty_dirr   Directory %s not empty  Permission denied: %sSending %s to trashN<Skipping trash for %s, on different device to home directoryRemoving directory %sUnlinking file %s)rO   rP   r'   unlinkrl   r   r#   r	   rn   r   delete_to_trashalways_delete_dirr   r   rX   rV   r   r   rp   r/   r2   r   shutilrmtree)r*   r/   rR   rmrw   r   r   r+   r)   r,   delete_file  sD   





"

"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 } ztdd| d	| |d}~ww 
zRename a file.rL   Nr   z Cannot rename file or directory i  zFile already exists: %sr!   zUnknown error renaming file: r   )rO   rP   rl   r   r#   r	   rn   r'   r/   r   r   r   r  mover   r*   old_pathnew_pathnew_os_pathold_os_pathr   r+   r+   r,   rename_file9  s0   






&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_stringU  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 notebookrL   r   r   r8   r]   rv   r*   r/   rx   
parent_dirr+   r+   r,   get_kernel_pathY  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.
        rL   Nr   r8   r|   r{   )	from_pathto_path.z-Copy)insert)r  to_path_originalto_namer  )strrO   rv   r   check_folder_sizer@   copyr   subr]   increment_filename	_copy_dirr*   r  r  r  r/   from_dir	from_namerx   is_destination_specifiedr  rc   rB   r+   r,   r  `  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
        rL   Fr   OSError in _copy_dir: r   Can't copy 'z' into Folder ''N
rP   rO   r  copytreer   rU   rV   rW   r	   rn   	r*   r  r  r  r  os_from_path
os_to_pathrx   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_sizerP   r   systemr	   rn   _human_readable_sizer*   r/   limit_bytesri   r+   r+   r,   r    s   z%FileContentsManager.check_folder_sizer  c                 C   s   z5t  dkrtjdd|gddj }ntjddd|gddj }| jd|  |d	 d
}W |S  t	yF   | j
d| Y dS w zM
        calls the command line program du to get the directory size
        r/  duz-skT)capture_outputz-sz--block-size=1zcurrent status of du command r   zutf-8z^Not able to get the size of the %s directory. Copying might be slow if the directory is large!0r   r8  
subprocessrunstdoutsplitrV   rm   decoder   rp   r*   r/   resultri   r+   r+   r,   r7    s$   z!FileContentsManager._get_dir_sizec                 C   sJ   |dkrdS g d}|rt t|d nd}d|d|d >  || S z@
        returns folder size in a human readable format
        r   z0 Bytes)BytesKBMBGBTBPB
   z	{:.4g} {}r   r6  mathlog2rg   r*   ri   unitsorderr+   r+   r,   r9    s
   z(FileContentsManager._human_readable_sizeTTNTNNr8   r$   r  )*__name__
__module____qualname____doc__r   r#   r   r5  r   r-   r   r4   r?   rA   rF   r
   r   r   rI   rK   r   rX   r[   r]   r   ry   r   r   r   r   r   r   r  r  r  r  r  r  r  r7  r9  __classcell__r+   r+   rB   r,   r   *   sb    


	





0
@
 

2
5B
,
r   c                
       s   e Zd ZdZeddd Zd2ddZd3d	d
Zd2ddZd4ddZ	d5ddZ
d5ddZdd Zdd Zdd Zdd Zdd Zd6ddZd6 fd d!	Zd"ed#ed$ed%ed&ef
d'd(Zd)ed&dfd*d+Zd7d)ed&efd-d.Zd/ed&efd0d1Z  ZS )8AsyncFileContentsManagerzAn async file contents manager.rD   c                 C   rE   r$   )r   r)   r+   r+   r,   rF     rG   z3AsyncFileContentsManager._checkpoints_class_defaultTc                    s6  |  |}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yw }
 z| j	d	|	|
 W Y d}
~
qRd}
~
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}
~
qRd}
~
ww t|jst|jst|js| j	d| qRz"| |	r| jst||ds|| j| d|	 ddI dH  W qR ty }
 z|
jtj tjfvr
| j	jd|dd W Y d}
~
qRd}
~
ww d|d< |S rz   )!rP   r'   r/   r2   r	   rn   rl   r   r#   rV   rm   ry   r   r   r   r   rp   rk   rU   r   r   r   r   r   r   r   r   r   r   r   r   r   r   )r*   r/   rf   rR   rw   rx   r   r   dir_contentsrc   r   r   r+   r+   r,   r     sx   



	



$	z#AsyncFileContentsManager._dir_modelNc                    s~   |  |}d|d< | |}t|d |d< |r=| ||I dH \}}|d du r6ddd| }||d< |j||d	 |S r   r   r   r+   r+   r,   r     s&   


z$AsyncFileContentsManager._file_modelc                    sh   |  |}d|d< | |}|r2i }| j|d|dI dH }| || ||d< d|d< | || |S )	r   r   r|   r   r   Nrf   r   rg   r   r   r+   r+   r,   r   >  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 }n1|dksG|d
u rR|	drR| j
||d	I d
H }n|dkr`tjdd| dd| j|||dI d
H }| jd|dd |S )r   rL   r`   zNo such file or directory: %sr   r   r   r   r   r   Nr   r   r{   r   r   r   r   r   )rO   r   r	   rn   rP   r'   r/   r2   r   r   r   r   r   )r*   r/   rf   r|   rg   rR   rx   r+   r+   r,   r   T  s(   


zAsyncFileContentsManager.getr8   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   )rl   r   r#   r	   rn   r'   r/   r   r   r   r   r2   rV   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   rL   r   r|   r   r   rf   r{   r   r   r   r   Nr   rg   r   r   Tr~   r!   r   r   r   r   Fr   r   r   r   r   )rO   r   r	   rn   rP   rV   r   r   r   r   r   r   r   r   r   r   r   r   rW   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| dd } fdd	} jrz jsRtjd
krR||I dH rRtdd| ||I dH rs |sftdd|  jd| t| dS  jd| 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   rL   r   r   r`   z$File or directory does not exist: %sc                    sH   t jdv rdS ttj| I d H j}ttjtjdI d H j}||kS r   )r   r   r   r'   r   r   r/   r   r   r+   r+   r,   r     s   
z:AsyncFileContentsManager.delete_file.<locals>._check_trashc                    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   )rR   r   ra  r)   r+   r,   r     s   
z>AsyncFileContentsManager.delete_file.<locals>.is_non_empty_dirr   Nr   r   r   r   r   r   r   )rO   rP   r'   r   rl   r   r#   r	   rn   r/   r   r   r   r   r   rX   rV   r   r   rp   r2   r   r   r  r  )r*   r/   rR   r  r   r   r+   r)   r,   r    sL   




"
"z$AsyncFileContentsManager.delete_filec              
      s  | 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 } ztdd| d	| |d}~ww r  )rO   rP   rl   r   r#   r	   rn   r'   r/   r   r   r   r   r  r  r   r  r+   r+   r,   r    s2   






&z$AsyncFileContentsManager.rename_filec                    s$   | d}| j|d}tj|S )z(Does a directory exist at the given pathrL   rM   r\   rQ   r+   r+   r,   r]         
z#AsyncFileContentsManager.dir_existsc                    s"   | d}| |}tj|S )z#Does a file exist at the given pathrL   rY   rQ   r+   r+   r,   r[   &  s   

z$AsyncFileContentsManager.file_existsc                    s$   | d}| j|d}t|| jS )z"Is path a hidden directory or filerL   rM   rN   rQ   r+   r+   r,   r   ,  rb  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 notebookNrL   r   r   r8   r  r  r+   r+   r,   r  2  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  rO   rv   r   r  r   r  r   r  r]   r@   r  r  r  rB   r+   r,   r  9  s6   	

zAsyncFileContentsManager.copyr  r  r  r  returnc           	         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#  rL   Fr   Nr$  r   r%  z' into read-only Folder 'r&  r'  r)  r+   r+   r,   r  e  s"   z"AsyncFileContentsManager._copy_dirr/   c                    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.  Nr/  r   r0  r1  r2  r3  r4  r:  r+   r+   r,   r  z  s    z*AsyncFileContentsManager.check_folder_sizer  c                    s   z5t  dkrtjdd|gddj }ntjddd|gddj }| jd|  |d	 d
}W |S  t	yG   | j
d| Y dS w r<  r@  rF  r+   r+   r,   r7    s&   z&AsyncFileContentsManager._get_dir_sizeri   c                    sL   |dkrdS g d}|rt t|d nd}d|d|d >  || S rH  rP  rS  r+   r+   r,   r9    s   z-AsyncFileContentsManager._human_readable_sizerV  rW  rX  rY  r$   rZ  )r[  r\  r]  r^  r   rF   r   r   r   r   r   r   r  r  r]   r[   r   r  r  r  dictr  r  r7  r6  r9  r_  r+   r+   rB   r,   r`    s>    


A
 

-
1F
,
r`  )9r^  r   rQ  r   r'   r   r  r   rA  r   r9   r   pathlibr   r   anyio.to_threadr   jupyter_core.pathsr   r   r   r   tornador	   	traitletsr
   r   r   r   r   r   jupyter_serverr   rq   jupyter_server.base.handlersr   jupyter_server.transutilsr   jupyter_server.utilsr   filecheckpointsr   r   fileior   r   managerr   r   r   os.pathr   ImportErrorr   _script_exporterr   r`  r+   r+   r+   r,   <module>   sN          1