a
    ø£Bed®  ã                   @   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  e4�y`   ddl&m5Z3 Y n0 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dLd.d/„ZdMd0d1„ZdNd3d4„ZdOd5d6„Zd7d8„ Z d9d:„ Z!d;d<„ Z"dPd=d>„Z#dQ‡ fd?d@„	Z$dAdB„ Z%dCdD„ Z&dRdFdG„Z'dHdI„ Z(‡  Z)S )SÚ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*   úo/home/yuhw/wc/wire-cell-python/wcpy/lib/python3.9/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 |¡s8td| ƒ‚|S )NÚvaluez%r is not a directory)r&   ÚpathÚisabsÚabspathÚisdirr   )r)   Úproposalr-   r*   r*   r+   Ú_validate_root_dir7   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y8   Y nV0 |d urŽtjdtdd� zt|ƒ}| | j¡ 	¡ W S  t
yŒ   td| ƒd ‚Y n0 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%   r4   r#   ÚAttributeErrorÚwarningsÚwarnÚFutureWarningr   Úrelative_toÚas_posixÚ
ValueErrorr   )r)   r-   r.   r*   r*   r+   Ú_default_preferred_dirA   s(    ýz*FileContentsManager._default_preferred_dirc                    s    t |d | jƒ|d< tƒ  |¡S )Nr-   )r   r#   ÚsuperÚ_validate_preferred_dir)r)   r2   ©Ú	__class__r*   r+   rA   Y   s    z+FileContentsManager._validate_preferred_dirÚcheckpoints_classc                 C   s   t S r$   )r   r(   r*   r*   r+   Ú_checkpoints_class_default_   s    z.FileContentsManager._checkpoints_class_defaultz°If 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.Zfiles_handler_classc                 C   s   t S r$   r   r(   r*   r*   r+   Ú_files_handler_class_defaultu   s    z0FileContentsManager._files_handler_class_defaultZfiles_handler_paramsc                 C   s
   d| j iS )Nr.   )r#   r(   r*   r*   r+   Ú_files_handler_params_defaulty   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   sN   |  d¡}| j|d�}zt |tj¡W S  tyH   | j d|¡ Y dS 0 dS )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.
        rH   rI   z'Failed to check write permissions on %sFN)rK   rL   r&   ÚaccessÚW_OKÚOSErrorÚlogÚerrorrM   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.
        rH   ©rK   rL   r&   r.   ÚisfilerM   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.
        rH   rI   ©rK   rL   r&   r.   r1   rM   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.
        rH   rI   )rK   rL   r   rM   r*   r*   r+   r   Ì   s    
zFileContentsManager.existsc           	      C   s~  |   |¡}t |¡}d| }| jsHt|| jƒrH| j d|¡ t 	d|¡‚z
|j
}W n& ttfyx   | j d¡ d}Y n0 zt |j¡}W n> ttfyÈ   | j d|j|¡ tdddd	d	tjd
�}Y n0 zt |j¡}W n@ ttf�y   | j d|j|¡ tdddd	d	tjd
�}Y n0 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 %srH   éÿÿÿÿÚnamer.   Úlast_modifiedÚcreatedÚcontentÚformatÚmimetypeÚsizeÚwritable)rL   r&   ÚlstatÚallow_hiddenr   r#   rR   Úinfor	   Ú	HTTPErrorÚst_sizer>   rQ   ÚwarningÚtzÚutcfromtimestampÚst_mtimer   ÚUTCÚst_ctimeÚrsplitrT   )	r)   r.   rN   ri   Úfour_o_fourre   r`   ra   Úmodelr*   r*   r+   Ú_base_modelß   s@    



zFileContentsManager._base_modelc                 C   sJ  |   |¡}d| }tj |¡s,t d|¡‚n,| jsXt|| jƒrX| j	 
d|¡ t d|¡‚|  |¡}d|d< d|d< |�rFg  |d< }|   |¡}t |¡D �]¢}ztj ||¡}W n@ tyð }	 z(| j	 d	||	¡ W Y d}	~	q˜W Y d}	~	n
d}	~	0 0 zt |¡}
W nn t�yn }	 zT|	jtjk�r0| j	 d
|¡ n|	jtjk�rN| j	 d||	¡ W Y d}	~	q˜W Y d}	~	n
d}	~	0 0 t |
j¡�sªt |
j¡�sªt |
j¡�sª| j	 d|¡ q˜zD|  |¡�rì| j�sÎt||
d��sì| | j|› d|› �dd�¡ W q˜ t�y: }	 z2|	jtjtjfv�r&| j	jd|dd� W Y d}	~	q˜d}	~	0 0 q˜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ÚtypeNre   rb   z"failed to decode filename '%s': %rz%s doesn't existzError stat-ing %s: %rz%s not a regular file)Zstat_resrH   F)r.   rb   z+Unknown error checking if file %r is hiddenT©Úexc_infoÚjsonrc   ) rL   r&   r.   r1   r	   rj   rh   r   r#   rR   ri   ru   ÚlistdirÚjoinÚUnicodeDecodeErrorrl   rg   rQ   ÚerrnoÚENOENTÚEACCESÚstatÚS_ISLNKÚst_modeÚS_ISREGÚS_ISDIRÚdebugÚshould_listr   ÚappendÚgetÚELOOP)r)   r.   rb   rN   rs   rt   ÚcontentsÚos_dirr_   ÚeÚstr*   r*   r+   Ú
_dir_model  sf    


""
ÿ
þ
ýÿ
ÿ"ýzFileContentsManager._dir_modelNc                 C   sv   |   |¡}d|d< |  |¡}t |¡d |d< |rr|  ||¡\}}|d du rdd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
        Úfilerx   r   rd   Nz
text/plainzapplication/octet-stream)ÚtextÚbase64©rb   rc   ©ru   rL   Ú	mimetypesÚ
guess_typeZ
_read_fileÚupdate©r)   r.   rb   rc   rt   rN   Zdefault_mimer*   r*   r+   Ú_file_modelO  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)
        Únotebookrx   é   ©Z
as_versionÚcapture_validation_errorrb   r{   rc   ©ru   rL   Z_read_notebookZmark_trusted_cellsÚvalidate_notebook_model©r)   r.   rb   rt   rN   Úvalidation_errorÚnbr*   r*   r+   Ú_notebook_modelo  s    

ÿz#FileContentsManager._notebook_modelc                 C   s  |  d¡}|  |¡}d| }|  |¡s2t d|¡‚| js^t|| jƒr^| j 	d|¡ t d|¡‚t
j |¡rœ|dvrŒtjd|› d|› �dd	�‚| j||d
�}nV|dks¶|du rÆ| d¡rÆ| j||d
�}n,|dkrât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.
        rH   rZ   r\   r[   ©Nrw   é�  ú is a directory, not a úbad type©Úreason©rb   r�   Nú.ipynbrw   ú%s is not a directoryr•   rŠ   ©Úactionr.   ©Údata)rK   rL   r   r	   rj   rh   r   r#   rR   ri   r&   r.   r1   r�   Úendswithr¦   r›   Úemit)r)   r.   rb   rx   rc   rN   rs   rt   r*   r*   r+   rŠ   …  s.    


ýzFileContentsManager.getr5   c                 C   s’   | j s"t|| jƒr"t dd| ¡‚tj |¡sb|  ¡ � t 	|¡ W d  ƒ qŽ1 sV0    Y  n,tj 
|¡s€t dd| ¡‚n| j d|¡ dS )úcreate a directoryr©   zCannot create directory %rNúNot a directory: %súDirectory %r already exists)rh   r   r#   r	   rj   r&   r.   r   Úperm_to_403Úmkdirr1   rR   r‡   ©r)   rN   rt   r.   r*   r*   r+   Ú_save_directory·  s    
*z#FileContentsManager._save_directoryc              
   C   s  |  d¡}| j||d� d|vr,t dd¡‚d|vrL|d dkrLt dd¡‚|  |¡}| jszt|| jƒrzt dd	|›�¡‚| j 	d
|¡ i }z¬|d dkrÜt
 |d ¡}|  ||¡ | j|||d� | j |¡sÚ|  |¡ nZ|d dk�r|  ||d | d¡¡ n2|d dk�r"|  |||¡ nt dd|d  ¡‚W nj tj�yP   ‚ Y nT t�y¢ } z:| jjd||dd� t dd|› d|› �¡|‚W Y d}~n
d}~0 0 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.rH   ©rt   r.   rx   r©   úNo file type providedrb   rw   úNo file content providedz Cannot create file or directory ú	Saving %sr�   ©r    r’   rc   úUnhandled contents type: %súError while saving file: %s %sTry   r!   ú$Unexpected error while saving file: ú N©r¤   ÚmessageFr®   ©rt   rN   Úsaver±   r³   )rK   Úrun_pre_save_hooksr	   rj   rL   rh   r   r#   rR   r‡   ÚnbformatÚ	from_dictÚcheck_and_signÚ_save_notebookÚcheckpointsÚlist_checkpointsÚcreate_checkpointÚ
_save_filerŠ   r½   Ú	ExceptionrS   r¢   Úrun_post_save_hooksr¶   ©r)   rt   r.   rN   r¤   r¥   rŽ   Zvalidation_messager*   r*   r+   rË   Ã  sL    

0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s˜tjdkr˜||ƒr˜t dd	| ¡‚ˆ  |¡s´t d
d| ¡d‚ˆ j d|¡ zt|ƒ W n8 t�y } zt dd| ¡|‚W Y d}~n
d}~0 0 dS tj |¡�r€ˆ j�s<||ƒ�r<t dd	| ¡‚ˆ j d|¡ ˆ  ¡ � t |¡ W d  ƒ n1 �st0    Y  n@ˆ j d|¡ ˆ  ¡ � ||ƒ W d  ƒ n1 �s¶0    Y  dS )úDelete file at path.rH   r©   ú Cannot delete file or directory rZ   r\   c                    s6   t j | ¡r2tˆ jdd ƒ}tt  | ¡ƒ|h r2dS dS ©NZcheckpoint_dirTF)r&   r.   r1   ÚgetattrrÑ   Úsetr|   )rN   Ú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 trashzsend2trash failed: %súRemoving directory %súUnlinking file %s)rK   rL   r&   Úunlinkrh   r   r#   r	   rj   r   Údelete_to_trashÚalways_delete_dirÚsysÚplatformrT   rR   r‡   r   rQ   r.   r1   rº   ÚshutilÚrmtree)r)   r.   rN   Úrmrs   rÞ   rŽ   r*   r(   r+   Údelete_file÷  s:    




(
,
zFileContentsManager.delete_filec              
   C   s  |  d¡}|  d¡}||kr dS |  |¡}|  |¡}| jsdt|| jƒsRt|| jƒrdt dd|›�¡‚tj 	|¡rŠt
||ƒsŠt dd| ¡‚z8|  ¡ � t ||¡ W d  ƒ n1 s¶0    Y  W nT tjyØ   ‚ Y n@ t�y } z&t dd|› d	|› �¡|‚W Y d}~n
d}~0 0 dS ©
zRename a file.rH   Nr©   z Cannot rename file or directory i™  zFile already exists: %sr!   zUnknown error renaming file: rÇ   )rK   rL   rh   r   r#   r	   rj   r&   r.   r   r   rº   rë   ÚmoverÕ   ©r)   Úold_pathÚnew_pathZnew_os_pathZold_os_pathrŽ   r*   r*   r+   Úrename_file)  s*    




ÿ
ÿ
.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_stringE  s    zFileContentsManager.info_stringc                 C   s.   |   |¡r|S d|v r&| dd¡d nd}|S )zIReturn the initial API path of  a kernel associated with a given notebookrH   r   r   r5   ©rY   rr   ©r)   r.   rt   Ú
parent_dirr*   r*   r+   Úget_kernel_pathI  s    
z#FileContentsManager.get_kernel_pathc                    sÜ   t |ƒ}| d¡}|dur$| d¡}d|v r>| dd¡\}}nd}|}|  |¡}|d dkrh|  |¡ ntƒ j||d�S |du}t d|¡}	|s”|}|  	|¡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.
        rH   Nr   r5   rx   rw   )Ú	from_pathÚto_pathÚ.z-Copy)Úinsert)rû   Úto_path_originalÚto_namerü   )ÚstrrK   rr   rŠ   Úcheck_folder_sizer@   Úcopyr   ÚsubrY   Úincrement_filenameÚ	_copy_dir©r)   rû   rü   rÿ   r.   Zfrom_dirÚ	from_namert   Zis_destination_specifiedr   r_   rB   r*   r+   r  P  s4    	



üzFileContentsManager.copyc           	      C   s    zH|   | d¡¡}|   | d¡¡› d|› �}t ||¡ | j|dd�}W nR tyš } z:| j d|› �¡ t 	dd|› d|› d�¡|‚W Y d	}~n
d	}~0 0 |S )
ú`
        handles copying directories
        returns the model for the copied directory
        rH   Fr®   úOSError in _copy_dir: r©   úCan't copy 'z' into Folder 'ú'N©
rL   rK   rë   ÚcopytreerŠ   rQ   rR   rS   r	   rj   ©	r)   rû   rÿ   r   rü   Zos_from_pathZ
os_to_pathrt   Úerrr*   r*   r+   r  |  s    þýzFileContentsManager._copy_dirc                 C   sn   | j d d }t|  |  |¡¡ƒ}t ¡ dkr6|d n|}||krjt 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_sizerL   rê   Úsystemr	   rj   Ú_human_readable_size©r)   r.   Zlimit_bytesre   r*   r*   r+   r  �  s    ÿþþþz%FileContentsManager.check_folder_sizerý   c                 C   s”   zlt  ¡ 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 n" t	yŽ   | j 
d|¡ Y dS 0 |S ©zM
        calls the command line program du to get the directory size
        r  Zduz-skT)Úcapture_outputÚcheckz-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ê   r  Ú
subprocessÚrunÚstdoutÚsplitrR   ri   ÚdecoderÕ   rl   ©r)   r.   Úresultre   r*   r*   r+   r  ¢  s"    ÿÿþ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)ÚBytesZKBÚMBÚGBÚTBÚPBé
   r   z.4grÇ   ©r  ÚmathÚlog2©r)   re   ZunitsÚorderr*   r*   r+   r  »  s
    z(FileContentsManager._human_readable_size)T)TN)T)TNN)r5   )r5   )N)N)rý   )*Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r#   r   r  r   r,   r   r3   r?   rA   rE   r
   rç   rè   rF   rG   r   rT   rW   rY   r   ru   r�   r›   r¦   rŠ   r½   rË   rî   rô   rõ   rù   r  r  r  r  r  Ú__classcell__r*   r*   rB   r+   r   *   s`   


	

ýý


0
@
 

2

42
,
r   c                       sè   e Zd ZdZedƒdd„ ƒZd.dd„Zd/d	d
„Zd0dd„Zd1dd„Z	d2dd„Z
d3dd„Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zd4dd„Zd5‡ fd d!„	Zeeeeed"œd#d$„Zedd%œd&d'„Zd6eed%œd)d*„Zeed+œd,d-„Z‡  ZS )7ÚAsyncFileContentsManagerzAn async file contents manager.rD   c                 C   s   t S r$   )r   r(   r*   r*   r+   rE   Ë  s    z3AsyncFileContentsManager._checkpoints_class_defaultTc                 Ã   sd  |   |¡}d| }tj |¡s,t d|¡‚n,| jsXt|| jƒrX| j	 
d|¡ t d|¡‚|  |¡}d|d< d|d< |�r`g  |d< }|   |¡}ttj|ƒI dH }|D �]°}	ztj ||	¡}W n@ tyü }
 z(| j	 d	|	|
¡ W Y d}
~
q¤W Y d}
~
n
d}
~
0 0 zttj|ƒI dH }W nn t�y‚ }
 zT|
jtjk�rD| j	 d
|¡ n|
jtjk�rb| j	 d||
¡ W Y d}
~
q¤W Y d}
~
n
d}
~
0 0 t |j¡�s¾t |j¡�s¾t |j¡�s¾| j	 d|¡ q¤zJ|  |	¡�r| j�sât||d��s| | j|› d|	› �dd�I dH ¡ W q¤ t�yT }
 z2|
jtj tjfv�r@| j	jd|dd� W Y d}
~
q¤d}
~
0 0 q¤d|d< |S rv   )!rL   r&   r.   r1   r	   rj   rh   r   r#   rR   ri   ru   r   r|   r}   r~   rl   rg   rQ   r   r€   r�   r‚   rƒ   r„   r…   r†   r‡   rˆ   r   r‰   rŠ   r‹   )r)   r.   rb   rN   rs   rt   rŒ   r�   Údir_contentsr_   rŽ   r�   r*   r*   r+   r�   Ï  sh    



""
ÿ
þ
ýÿ
ÿ(ýz#AsyncFileContentsManager._dir_modelNc                 Ã   s|   |   |¡}d|d< |  |¡}t |¡d |d< |rx|  ||¡I dH \}}|d du rjdddœ| }||d< |j||d	� |S r‘   r–   rš   r*   r*   r+   r›     s$    


þýþz$AsyncFileContentsManager._file_modelc                 Ã   sf   |   |¡}d|d< |  |¡}|rbi }| j|d|d�I dH }|  ||¡ ||d< d|d< |  ||¡ |S )	rœ   r�   rx   rž   rŸ   Nrb   r{   rc   r¡   r£   r*   r*   r+   r¦   0  s    

ÿz(AsyncFileContentsManager._notebook_modelc                 Ã   sê   |  d¡}|  |¡s$t dd| ¡‚|  |¡}tj |¡rr|dvr\tjd|› d|› �dd�‚| j||d	�I d
H }nb|dksŒ|d
u r¢| 	d¡r¢| j
||d	�I d
H }n2|dkr¾tjdd| dd�‚| j|||d�I d
H }| jd|dœd� |S )r§   rH   r\   zNo such file or directory: %sr¨   r©   rª   r«   r¬   r®   Nr�   r¯   rw   r°   r•   rŠ   r±   r³   )rK   r   r	   rj   rL   r&   r.   r1   r�   rµ   r¦   r›   r¶   )r)   r.   rb   rx   rc   rN   rt   r*   r*   r+   rŠ   F  s&    


ýzAsyncFileContentsManager.getr5   c                 Ã   sš   | j s"t|| jƒr"t dd| ¡‚tj |¡sj|  ¡ �" t	tj
|ƒI dH  W d  ƒ q–1 s^0    Y  n,tj |¡sˆt dd| ¡‚n| j d|¡ dS )r·   r©   z!Cannot create hidden directory %rNr¸   r¹   )rh   r   r#   r	   rj   r&   r.   r   rº   r   r»   r1   rR   r‡   r¼   r*   r*   r+   r½   s  s    
2z(AsyncFileContentsManager._save_directoryc              
   Ã   s  |  d¡}| j||d� d|vr,t dd¡‚d|vrL|d dkrLt dd¡‚|  |¡}| j d	|¡ i }zÈ|d d
krÊt |d ¡}|  	||¡ | j
|||d�I dH  | j |¡I dH sÈ|  |¡I dH  nd|d dkrö|  ||d | d¡¡I dH  n8|d dk�r|  |||¡I dH  nt dd|d  ¡‚W nj tj�yH   ‚ Y nT t�yš } z:| jjd||dd� t dd|› d|› �¡|‚W Y d}~n
d}~0 0 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¾   rH   r¿   rx   r©   rÀ   rb   rw   rÁ   rÂ   r�   rÃ   Nr’   rc   rÄ   rÅ   Try   r!   rÆ   rÇ   rÈ   rÉ   Fr®   rÊ   rË   r±   r³   )rK   rÌ   r	   rj   rL   rR   r‡   rÍ   rÎ   rÏ   rÐ   rÑ   rÒ   rÓ   rÔ   rŠ   r½   rÕ   rS   r¢   rÖ   r¶   r×   r*   r*   r+   rË     sH    

 0zAsyncFileContentsManager.savec              
   ƒ   sÞ  |  d¡}ˆ  |¡}tj}ˆ js>t|ˆ jƒr>t dd|›�¡‚tj	 
|¡sZt dd| ¡‚‡ fdd„}ˆ j�rˆ jsœtjdkrœ||ƒI d	H rœt dd
| ¡‚ˆ  |¡s¸t dd| ¡d	‚ˆ j d|¡ zt|ƒ W n8 t�y
 } zt dd| ¡|‚W Y d	}~n
d	}~0 0 d	S tj	 |¡�r’ˆ j�sF||ƒI d	H �rFt dd
| ¡‚ˆ j d|¡ ˆ  ¡ �" ttj|ƒI d	H  W d	  ƒ n1 �s†0    Y  nHˆ j d|¡ ˆ  ¡ �  t||ƒI d	H  W d	  ƒ n1 �sÐ0    Y  d	S )rØ   rH   r©   rÙ   r\   z$File or directory does not exist: %sc                 “   sB   t j | ¡r>tˆ jdd ƒ}ttt j| ƒI d H ƒ}||h r>dS dS rÚ   )r&   r.   r1   rÛ   rÑ   rÜ   r   r|   )rN   rÝ   r=  r(   r*   r+   rÞ   ¼  s    
z>AsyncFileContentsManager.delete_file.<locals>.is_non_empty_dirrß   Nrà   rá   râ   rã   zsend2trash f`1ailed: %srä   rå   )rK   rL   r&   ræ   rh   r   r#   r	   rj   r.   r   rç   rè   ré   rê   rT   rR   r‡   r   rQ   r1   rº   r   rë   rì   )r)   r.   rN   rí   rÞ   rŽ   r*   r(   r+   rî   °  sB    

ÿþý
(
4
z$AsyncFileContentsManager.delete_filec              
   Ã   s$  |  d¡}|  d¡}||kr dS |  |¡}|  |¡}| jsdt|| jƒsRt|| jƒrdt dd|›�¡‚tj 	|¡rŠt
||ƒsŠt dd| ¡‚z@|  ¡ �$ ttj||ƒI dH  W d  ƒ n1 s¾0    Y  W nT tjyà   ‚ Y n@ t�y } z&t dd|› d	|› �¡|‚W Y d}~n
d}~0 0 dS rï   )rK   rL   rh   r   r#   r	   rj   r&   r.   r   r   rº   r   rë   rð   rÕ   rñ   r*   r*   r+   rô   æ  s*    




ÿ
ÿ
6z$AsyncFileContentsManager.rename_filec                 Ã   s"   |  d¡}| j|d�}tj |¡S )z(Does a directory exist at the given pathrH   rI   rX   rM   r*   r*   r+   rY     s    
z#AsyncFileContentsManager.dir_existsc                 Ã   s    |  d¡}|  |¡}tj |¡S )z#Does a file exist at the given pathrH   rU   rM   r*   r*   r+   rW     s    

z$AsyncFileContentsManager.file_existsc                 Ã   s"   |  d¡}| j|d�}t|| jƒS )z"Is path a hidden directory or filerH   rI   rJ   rM   r*   r*   r+   r     s    
z"AsyncFileContentsManager.is_hiddenc                 Ã   s4   |   |¡I dH r|S d|v r,| dd¡d nd}|S )zHReturn the initial API path of a kernel associated with a given notebookNrH   r   r   r5   rö   r÷   r*   r*   r+   rù     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rt|  |¡I dH  ntj| ||d�I dH S |du}t d|¡}	|s¦|}|  	|¡I dH rÚt d|¡}
t
ƒ j|
|d	d
�I dH }	|› d|	› �}| j|||	|d�I dH S rú   )r  rK   rr   rŠ   r  r   r  r   r  rY   r@   r  r  r  rB   r*   r+   r    s4    	

üzAsyncFileContentsManager.copy)rû   rÿ   r   rü   Úreturnc           	      Ã   s¦   zN|   | d¡¡}|   | d¡¡› d|› �}t ||¡ | j|dd�I dH }W nR ty  } z:| j d|› �¡ t 	dd|› d|› d	�¡|‚W Y d}~n
d}~0 0 |S )
r	  rH   Fr®   Nr
  r©   r  z' into read-only Folder 'r  r  r  r*   r*   r+   r  G  s    þýz"AsyncFileContentsManager._copy_dir)r.   r>  c                 Ã   sz   | j d d }t|  |  |¡¡I dH ƒ}t ¡ dkr<|d n|}||krvt dd| j › d|› d|  |¡I dH › d�¡‚dS )	r  r  Nr  r©   r  r  r  r  r  r  r*   r*   r+   r  \  s    ÿþþþz*AsyncFileContentsManager.check_folder_sizerý   c                 Ã   s”   zlt  ¡ 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 n" t	yŽ   | j 
d|¡ Y dS 0 |S r  r#  r)  r*   r*   r+   r  o  s"    ÿÿþz&AsyncFileContentsManager._get_dir_size)re   r>  c                 Ã   sN   |dkrdS g d¢}|r*t t |¡d ƒnd}|d|d >  d›d|| › �S r+  r2  r5  r*   r*   r+   r  ˆ  s
    z-AsyncFileContentsManager._human_readable_size)T)TN)T)TNN)r5   )r5   )N)N)rý   )r7  r8  r9  r:  r   rE   r�   r›   r¦   rŠ   r½   rË   rî   rô   rY   rW   r   rù   r  r  Údictr  r  r  r  r  r;  r*   r*   rB   r+   r<  È  s,   

A
 

-

16
-þr<  )9r:  r   r3  r—   r&   rê   rë   r‚   r$  ré   r9   r   Úpathlibr   rÍ   Zanyio.to_threadr   Zjupyter_core.pathsr   r   r   r   Útornador	   Z	traitletsr
   r   r   r   r   r   Zjupyter_serverr   rm   Zjupyter_server.base.handlersr   Zjupyter_server.transutilsr   Zjupyter_server.utilsr   Zfilecheckpointsr   r   Zfileior   r   Úmanagerr   r   r   Úos.pathr   ÚImportErrorr   Z_script_exporterr   r<  r*   r*   r*   r+   Ú<module>   sJ         #