o
    i3                     @   s2  d Z ddlZzddlmZ W n ey   ddlmZ Y nw 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 ) zTornado 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}|r3 fdd|D }|r1tdd| dS  fd	d
|D }|rGt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'.
    >	   namepathtypeformatcontentcreatedmimetypewritablelast_modifiedi  zMissing Model Keys: r   r   c                    s   g | ]
} | d u r|qS N .0keymodelr   d/var/www/edux/Edux_v2/venv/lib/python3.10/site-packages/jupyter_server/services/contents/handlers.py
<listcomp>3   s    z"validate_model.<locals>.<listcomp>zKeys unexpectedly None: c                    s"   i | ]} | d ur| | qS r   r   r   r   r   r   
<dictcomp>:   s   " z"validate_model.<locals>.<dictcomp>zKeys unexpectedly not None: N)setkeysr   	HTTPError)r   expect_contentrequired_keysmissing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_RESOURCE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 )zReturn 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)r5   
set_headerfinishjsondumpsr   )r4   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r(td	d
| | jddd}|dvr;td	d| t|p?d}|jsXt||I dH rXtdd|dt| j j||||dI dH }t	||d | j
|dd dS )zReturn 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   Nr7   >   Nfilenotebook	directoryr@   r   >   Ntextbase64  zFormat %r is invalidr   1>   0rF   zContent %r is invalidi  zfile or directory z does not exist)r   r   r   r   r#   F)r=   )contents_managerget_query_argumentr   r"   intallow_hiddenr   	is_hiddengetr(   r>   )r4   r   cmr   r   content_strr   r   r   r   r   rN   ^   s2   
zContentsHandler.getc                    s   | j }|  }|du rtdd|d}|r9|js9t||I dH s0t||I dH r9tdd|t|||I dH }t	|dd | 
| dS )z?PATCH renames a file or directory without re-uploading content.NrE   zJSON body missingr   z Cannot rename file or directory FrH   )rI   get_json_bodyr   r"   rN   rL   r   rM   updater(   r>   )r4   r   rO   r   old_pathr   r   r   patch   s$   
zContentsHandler.patchNc                    sX   | 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   FrH   )	loginfor   r   rI   copy
set_statusr(   r>   )r4   rU   rV   r   r   r   r   _copy   s   
zContentsHandler._copyc                    sL   | 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 %sNrW   FrH   )rX   rY   r   rI   newr[   r(   r>   )r4   r   r   r   r   r   _upload   s   
zContentsHandler._uploadc                    sV   | 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 %sr@   )r   r   extNrW   FrH   )rX   rY   r   rI   new_untitledr[   r(   r>   )r4   r   r   r_   r   r   r   r   _new_untitled   s   

zContentsHandler._new_untitledc                    sZ   | dd}|r|dkr| jd| t| j||I dH }t|dd | | dS )zSave an existing file.chunkNzSaving file at %sFrH   )rN   rX   rY   r   rI   saver(   r>   )r4   r   r   rb   r   r   r   _save   s   zContentsHandler._savec                    s   | j }t||I dH }|rtdd|  }|rm|d}|rN|jsCt||I dH s:t||I dH rCtdd|| 	||I dH  dS |dd}|dd}|d	vr`d
}| j
|||dI dH  dS | 
|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
        NrE   z&Cannot POST to files, use PUT instead.rU   zCannot copy file or directory r_   r?   r   >   Nr?   r@   rA   rB   r@   )r   r_   )rI   r   file_existsr   r"   rQ   rN   rL   rM   r\   ra   )r4   r   rO   rf   r   rU   r_   r   r   r   r   post   s,   
zContentsHandler.postc                    s   |   }| j}|rl|drtdd|js=|dr*t||dI dH s4t||I dH r=tdd|t| j|I dH }|ddd	vrTd
|d< |ra| 	||I dH  dS | 
||I dH  dS | |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.
        rU   rE   zCannot copy with PUT, only POSTr   Nz Cannot create file or directory r   r?   >   Nr?   r@   rA   rB   r@   )rQ   rI   rN   r   r"   rL   r   rM   rf   re   r^   ra   )r4   r   r   rO   existsr   r   r   put   s*   
zContentsHandler.putc                    sl   | j }|jst||I dH rtdd|| jd| t||I dH  | 	d | 
  dS )zdelete a file in the given pathNrE   z Cannot delete file or directory z	delete %s   )rI   rL   r   rM   r   r"   rX   warningdeleter[   r:   r4   r   rO   r   r   r   rl     s   
zContentsHandler.delete)Tr?   r   )r?   r?   )r*   r+   r,   r-   r5   r>   r   authenticatedr   rN   rT   r\   r^   ra   re   rg   ri   rl   r   r   r   r   r0   H   s.    

	$


	'"r0   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                    s8   | j }t||I dH }tj|td}| | dS )z get lists checkpoints for a fileNr7   )rI   r   list_checkpointsr;   r<   r   r:   )r4   r   rO   checkpointsdatar   r   r   rN   "  s
   zCheckpointsHandler.getc                    sl   | 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 checkpointNr7   api/contentsrr   idr6   rW   )rI   r   create_checkpointr;   r<   r   r   r2   r
   r9   r[   r:   )r4   r   rO   
checkpointrs   r=   r   r   r   rg   +  s   

zCheckpointsHandler.postNrn   )	r*   r+   r,   r-   r   ro   r   rN   rg   r   r   r   r   rp     s    rp   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                    4   | j }t|||I dH  | d |   dS )z&post restores a file from a checkpointNrj   )rI   r   restore_checkpointr[   r:   r4   r   checkpoint_idrO   r   r   r   rg   A  
   
zModifyCheckpointsHandler.postc                    ry   )z+delete clears a checkpoint for a given fileNrj   )rI   r   delete_checkpointr[   r:   r{   r   r   r   rl   J  r}   zModifyCheckpointsHandler.deleteN)	r*   r+   r,   r-   r   ro   r   rg   rl   r   r   r   r   rx   >  s    rx   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/contentsrt   N)rX   rk   redirectr   r2   r
   r3   r   r   r   rN   Y  s   zNotebooksRedirectHandler.getN)
r*   r+   r,   r-   SUPPORTED_METHODSrN   ri   rT   rg   rl   r   r   r   r   r   T  s
    r   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                    s2   | j }t||I dH  | d |   dS )zTrust a notebook by path.NrW   )rI   r   trust_notebookr[   r:   rm   r   r   r   rg   d  s
   
zTrustNotebooksHandler.postNrn   )	r*   r+   r,   r-   r   ro   r   r.   rg   r   r   r   r   r   a  s
    r   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;   jupyter_client.jsonutilr   ImportErrorr   jupyter_core.utilsr   tornador   jupyter_server.authr   jupyter_server.base.handlersr   r   r	   jupyter_server.utilsr
   r   r.   r(   r)   r0   rp   rx   r   r   _checkpoint_id_regexdefault_handlersr   r   r   r   <module>   s<    * X


