a
    ø£Be¶3  ã                   @   s2  d Z ddlZzddlmZ W n ey:   ddlmZ Y n0 ddlmZ ddlm	Z	 ddl
mZ ddlmZmZmZ dd	lmZmZ d
Zdd„ ZG dd„ deƒZG dd„ deƒZG dd„ deƒZG dd„ deƒZG dd„ deƒZG dd„ deƒZdZde efde› de› �efde efde efdefgZdS ) z‘Tornado handlers for the contents web service.

Preliminary documentation at https://github.com/ipython/ipython/wiki/IPEP-27%3A-Contents-Service
é    N)Újson_default)Údate_default)Úensure_async)Úweb)Ú
authorized)Ú
APIHandlerÚJupyterHandlerÚ
path_regex)Ú
url_escapeÚurl_path_joinÚcontentsc                    s�   h d£}|t ˆ  ¡ ƒ }|r.t dd|› �¡‚ddg}|rd‡ fdd„|D ƒ}|rŒt dd|› �¡‚n(‡ fd	d
„|D ƒ}|rŒt dd|› �¡‚dS )z 
    Validate a model returned by a ContentsManager method.

    If expect_content is True, then we expect non-null entries for 'content'
    and 'format'.
    >	   ÚpathÚcreatedÚcontentÚtypeÚnameÚmimetypeÚformatÚwritableÚlast_modifiediô  zMissing Model Keys: r   r   c                    s   g | ]}ˆ | d u r|‘qS ©N© ©Ú.0Úkey©Úmodelr   úl/home/yuhw/wc/wire-cell-python/wcpy/lib/python3.9/site-packages/jupyter_server/services/contents/handlers.pyÚ
<listcomp>3   ó    z"validate_model.<locals>.<listcomp>zKeys unexpectedly None: c                    s"   i | ]}ˆ | d ur|ˆ | “qS r   r   r   r   r   r   Ú
<dictcomp>:   r   z"validate_model.<locals>.<dictcomp>zKeys unexpectedly not None: N)ÚsetÚkeysr   Ú	HTTPError)r   Úexpect_contentÚrequired_keysÚmissingZmaybe_none_keysÚerrorsr   r   r   Úvalidate_model   s*    þþþr(   c                   @   s   e Zd ZdZeZdS )ÚContentsAPIHandlerzA contents API handler.N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__ÚAUTH_RESOURCEZauth_resourcer   r   r   r   r)   B   s   r)   c                   @   sª   e Zd ZdZdd„ Zddd„Zejeddd	„ƒƒZ	ejedd
d„ƒƒZ
ddd„Zdd„ Zddd„Zdd„ Zejed dd„ƒƒZejed!dd„ƒƒZejed"dd„ƒƒZdS )#ÚContentsHandlerzA contents handler.c                 C   s   t | jddt|ƒƒS )z«Return the full URL location of a file.

        Parameters
        ----------
        path : unicode
            The API path of the file, such as "foo/bar.txt".
        Úapir   )r   Úbase_urlr
   ©Úselfr   r   r   r   Úlocation_urlK   s    zContentsHandler.location_urlTc                 C   sR   |r|   |d ¡}|  d|¡ |  d|d ¡ |  dd¡ |  tj|td�¡ dS )	zBFinish a JSON request with a model, setting relevant headers, etc.r   ÚLocationzLast-Modifiedr   zContent-Typezapplication/json©ÚdefaultN)r4   Ú
set_headerÚfinishÚjsonÚdumpsr   )r3   r   Úlocationr   r   r   Ú_finish_modelU   s    zContentsHandler._finish_modelÚ c                 Ã   sê   |pd}| j }| jddd�}|dvr(d}| jddd�}|dvrNt d	d
| ¡‚| jddd�}|dvrtt d	d| ¡‚t|p|dƒ}|js®t| |¡ƒI dH r®t dd|›d�¡‚t| j j||||d�ƒI dH }t	||d� | j
|dd� dS )z©Return a model for a file or directory.

        A directory model contains a list of models (without content)
        of the files and directories it contains.
        r>   r   Nr6   >   NÚ	directoryÚnotebookÚfilerA   r   >   NÚbase64Útexté�  zFormat %r is invalidr   Ú1>   rE   Ú0zContent %r is invalidi”  zfile or directory z does not exist)r   r   r   r   ©r$   F)r<   )Úcontents_managerZget_query_argumentr   r#   ÚintÚallow_hiddenr   Ú	is_hiddenÚgetr(   r=   )r3   r   Úcmr   r   Zcontent_strr   r   r   r   r   rL   ^   s0    üÿ
zContentsHandler.getc                 Ã   s    | j }|  ¡ }|du r"t dd¡‚| d¡}|rp|jspt| |¡ƒI dH s^t| |¡ƒI dH rpt dd|›�¡‚t| ||¡ƒI dH }t	|dd� |  
|¡ dS )z?PATCH renames a file or directory without re-uploading content.NrD   zJSON body missingr   z Cannot rename file or directory FrG   )rH   Úget_json_bodyr   r#   rL   rJ   r   rK   Úupdater(   r=   )r3   r   rM   r   Úold_pathr   r   r   Úpatch„   s"    
ÿþüüzContentsHandler.patchNc                 Ã   sV   | j  dj||pdd�¡ t| j ||¡ƒI dH }|  d¡ t|dd� |  |¡ dS )z6Copy a file, optionally specifying a target directory.z Copying {copy_from} to {copy_to}r>   )Ú	copy_fromÚcopy_toNéÉ   FrG   )	ÚlogÚinfor   r   rH   ÚcopyÚ
set_statusr(   r=   )r3   rR   rS   r   r   r   r   Ú_copy›   s    þÿ
zContentsHandler._copyc                 Ã   sJ   | j  d|¡ t| j ||¡ƒI dH }|  d¡ t|dd� |  |¡ dS )z#Handle upload of a new file to pathzUploading file to %sNrT   FrG   )rU   rV   r   rH   ÚnewrX   r(   r=   )r3   r   r   r   r   r   Ú_upload¨   s
    
zContentsHandler._uploadc                 Ã   sT   | j  d|pd|¡ t| jj|||d�ƒI dH }|  d¡ t|dd� |  |¡ dS )z#Create a new, empty untitled entityzCreating new %s in %srA   )r   r   ÚextNrT   FrG   )rU   rV   r   rH   Znew_untitledrX   r(   r=   )r3   r   r   r\   r   r   r   r   Ú_new_untitled°   s    ÿ

zContentsHandler._new_untitledc                 Ã   sX   |  dd¡}|r|dkr&| j d|¡ t| j ||¡ƒI dH }t|dd� |  |¡ dS )zSave an existing file.ÚchunkNéÿÿÿÿzSaving file at %sFrG   )rL   rU   rV   r   rH   Úsaver(   r=   )r3   r   r   r^   r   r   r   Ú_saveº   s    zContentsHandler._savec                 Ã   sê   | j }t| |¡ƒI dH }|r*t dd¡‚|  ¡ }|rÖ| d¡}|rš|js†t| |¡ƒI dH srt| |¡ƒI dH r†t dd|›�¡‚qÔ|  	||¡I dH  qæ| dd¡}| dd¡}|d	vr¾d
}| j
|||d�I dH  n|  
|¡I dH  dS )aZ  Create a new file in the specified path.

        POST creates new files. The server always decides on the name.

        POST /api/contents/path
          New untitled, empty file or directory.
        POST /api/contents/path
          with body {"copy_from" : "/path/to/OtherNotebook.ipynb"}
          New copy of OtherNotebook in path
        NrD   z&Cannot POST to files, use PUT instead.rR   zCannot copy file or directory r\   r>   r   >   Nr>   r?   r@   rA   rA   )r   r\   )rH   r   Úfile_existsr   r#   rN   rL   rJ   rK   rY   r]   )r3   r   rM   rb   r   rR   r\   r   r   r   r   ÚpostÃ   s*    
ÿþzContentsHandler.postc                 Ã   sæ   |   ¡ }| j}|rÒ| d¡r(t dd¡‚|jsx| d¡rRt| | d¡¡ƒI dH sft| |¡ƒI dH rxt dd|›�¡‚t| j |¡ƒI dH }| dd¡d	vr¦d
|d< |r¾|  	||¡I dH  qâ|  
||¡I dH  n|  |¡I dH  dS )aµ  Saves the file in the location specified by name and path.

        PUT is very similar to POST, but the requester specifies the name,
        whereas with POST, the server picks the name.

        PUT /api/contents/path/Name.ipynb
          Save notebook at ``path/Name.ipynb``. Notebook structure is specified
          in `content` key of JSON request body. If content is not specified,
          create a new empty notebook.
        rR   rD   zCannot copy with PUT, only POSTr   Nz Cannot create file or directory r   r>   >   Nr>   r?   r@   rA   rA   )rN   rH   rL   r   r#   rJ   r   rK   rb   ra   r[   r]   )r3   r   r   rM   Úexistsr   r   r   Úputì   s(    
ÿÿþzContentsHandler.putc                 Ã   sj   | j }|js2t| |¡ƒI dH r2t dd|›�¡‚| j d|¡ t| |¡ƒI dH  |  	d¡ |  
¡  dS )zdelete a file in the given pathNrD   z Cannot delete file or directory z	delete %séÌ   )rH   rJ   r   rK   r   r#   rU   ÚwarningÚdeleterX   r9   ©r3   r   rM   r   r   r   rh     s    
zContentsHandler.delete)T)r>   )r>   )N)r>   r>   )r>   )r>   )r>   )r*   r+   r,   r-   r4   r=   r   Úauthenticatedr   rL   rQ   rY   r[   r]   ra   rc   re   rh   r   r   r   r   r/   H   s,   

	$


	'"r/   c                   @   s8   e Zd ZdZejeddd„ƒƒZejed	dd„ƒƒZdS )
ÚCheckpointsHandlerzA checkpoints API handler.r>   c                 Ã   s6   | j }t| |¡ƒI dH }tj|td�}|  |¡ dS )z get lists checkpoints for a fileNr6   )rH   r   Zlist_checkpointsr:   r;   r   r9   )r3   r   rM   ÚcheckpointsÚdatar   r   r   rL   "  s    zCheckpointsHandler.getc                 Ã   sj   | j }t| |¡ƒI dH }tj|td�}t| jdt|ƒdt|d ƒƒ}|  	d|¡ |  
d¡ |  |¡ dS )zpost creates a new checkpointNr6   úapi/contentsrl   Úidr5   rT   )rH   r   Zcreate_checkpointr:   r;   r   r   r1   r
   r8   rX   r9   )r3   r   rM   Ú
checkpointrm   r<   r   r   r   rc   +  s    
û
zCheckpointsHandler.postN)r>   )r>   )	r*   r+   r,   r-   r   rj   r   rL   rc   r   r   r   r   rk     s   rk   c                   @   s4   e Zd ZdZejedd„ ƒƒZejedd„ ƒƒZdS )ÚModifyCheckpointsHandlerz#A checkpoints modification handler.c                 Ã   s2   | j }t| ||¡ƒI dH  |  d¡ |  ¡  dS )z&post restores a file from a checkpointNrf   )rH   r   Zrestore_checkpointrX   r9   ©r3   r   Zcheckpoint_idrM   r   r   r   rc   A  s    
zModifyCheckpointsHandler.postc                 Ã   s2   | j }t| ||¡ƒI dH  |  d¡ |  ¡  dS )z+delete clears a checkpoint for a given fileNrf   )rH   r   Zdelete_checkpointrX   r9   rr   r   r   r   rh   J  s    
zModifyCheckpointsHandler.deleteN)	r*   r+   r,   r-   r   rj   r   rc   rh   r   r   r   r   rq   >  s   rq   c                   @   s,   e Zd ZdZdZdd„ Ze Z Z ZZ	dS )ÚNotebooksRedirectHandlerz(Redirect /api/notebooks to /api/contents)ÚGETÚPUTÚPATCHÚPOSTÚDELETEc                 C   s(   | j  d¡ |  t| jdt|ƒƒ¡ dS )zHandle a notebooks redirect.z//api/notebooks is deprecated, use /api/contentsrn   N)rU   rg   Úredirectr   r1   r
   r2   r   r   r   rL   Y  s    zNotebooksRedirectHandler.getN)
r*   r+   r,   r-   ZSUPPORTED_METHODSrL   re   rQ   rc   rh   r   r   r   r   rs   T  s   rs   c                   @   s*   e Zd ZdZejeed�ddd„ƒƒZdS )ÚTrustNotebooksHandlerz"Handles trust/signing of notebooks)Úresourcer>   c                 Ã   s0   | j }t| |¡ƒI dH  |  d¡ |  ¡  dS )zTrust a notebook by path.NrT   )rH   r   Ztrust_notebookrX   r9   ri   r   r   r   rc   d  s    
zTrustNotebooksHandler.postN)r>   )	r*   r+   r,   r-   r   rj   r   r.   rc   r   r   r   r   rz   a  s   rz   z(?P<checkpoint_id>[\w-]+)z/api/contents%s/checkpointsz/api/contentsz/checkpoints/z/api/contents%s/trustz/api/contents%sz/api/notebooks/?(.*))r-   r:   Zjupyter_client.jsonutilr   ÚImportErrorr   Zjupyter_core.utilsr   Útornador   Zjupyter_server.authr   Zjupyter_server.base.handlersr   r   r	   Zjupyter_server.utilsr
   r   r.   r(   r)   r/   rk   rq   rs   rz   Z_checkpoint_id_regexZdefault_handlersr   r   r   r   Ú<module>   s8   * X
þ

ø