o
    i/                     @   s  d Z ddlZddlmZmZ ddlmZmZmZ zddl	m
Z
 W n ey/   ddl	mZ
 Y nw ddlmZmZm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dZde defde de efde d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)genweb)maybe_futureurl_path_join
url_escape)json_default)date_default)IPythonHandler
APIHandler
path_regexc                    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   ^/var/www/edux/Edux_v2/venv/lib/python3.10/site-packages/notebook/services/contents/handlers.py
<listcomp>5   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   s2   
r'   c                   @   s   e Zd Zdd ZdddZejejdddZ	ejejdd	d
Z
ejdddZejdd ZejdddZejdd ZejejdddZejejdddZejejdddZdS )ContentsHandlerc                 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".
        apicontents)r   base_urlr   selfr   r   r   r   location_urlJ   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)r.   
set_headerfinishjsondumpsr   )r-   r   locationr   r   r   _finish_modelV   s   zContentsHandler._finish_model c                 c   s    |pd}| j }| jddd}|dvrtdd|d| jd	dd}|d
vr2tdd|d| jddd}|dvrGtdd|dt|}||r]|js]tdd|dt| j j||||dV }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.
        r8   r   Nr0   >   Nfilenotebook	directory  zType z is invalidr   >   Ntextbase64zFormat r   1>   0r?   zContent   zfile or directory z does not exist)r   r   r   r   r"   F)r6   )contents_managerget_query_argumentr   r!   int	is_hiddenallow_hiddenr   getr'   r7   )r-   r   cmr   r   r   r   r   r   r   rH   _   s(   
zContentsHandler.getc                 c   s    | j }|  }|d}|r%||s||r%|js%tdd||du r/tddt|||V }t	|dd | 
| dS )z?PATCH renames a file or directory without re-uploading content.r   r<   z Cannot rename file or directory NzJSON body missingFrB   )rC   get_json_bodyrH   rF   rG   r   r!   r   updater'   r7   )r-   r   rI   r   old_pathr   r   r   patch|   s   
zContentsHandler.patchNc                 c   sV    | j d| d|pd  t| j||V }| d t|dd | | dS )z6Copy a file, optionally specifying a target directory.zCopying z to r8      FrB   N)loginfor   rC   copy
set_statusr'   r7   )r-   	copy_fromcopy_tor   r   r   r   _copy   s   
zContentsHandler._copyc                 c   sH    | j d| t| j||V }| d t|dd | | dS )z#Handle upload of a new file to pathzUploading file to %srN   FrB   N)rO   rP   r   rC   newrR   r'   r7   )r-   r   r   r   r   r   _upload   s   
zContentsHandler._uploadc                 c   sR    | j d|pd| t| jj|||dV }| d t|dd | | dS )z#Create a new, empty untitled entityzCreating new %s in %sr9   )r   r   extrN   FrB   N)rO   rP   r   rC   new_untitledrR   r'   r7   )r-   r   r   rX   r   r   r   r   _new_untitled   s   
zContentsHandler._new_untitledc                 c   sV    | dd}|r|dkr| jd| t| j||V }t|dd | | dS )zSave an existing file.chunkNzSaving file at %sFrB   )rH   rO   rP   r   rC   saver'   r7   )r-   r   r   r[   r   r   r   _save   s   zContentsHandler._savec           	      c   s    | j }t||V }|rtddt||V }|s'tdd| |  }|d}|rH||s<||rH|j	sHtdd||durs|d}|dd	}|d
d	}|rh| 
||V  dS | j|||dV  dS | |V  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
        r<   z&Cannot POST to files, use PUT instead.rA   zNo such directory: rS   zCannot copy file or directory NrX   r8   r   )r   rX   )rC   r   file_existsr   r!   
dir_existsrJ   rH   rF   rG   rU   rZ   )	r-   r   rI   r_   r`   r   rS   rX   r   r   r   r   post   s(   

zContentsHandler.postc                 c   s    |   }| j}|rT|drtdd|dr3||s'||dr3|js3tdd|t| j|V }|rIt| 	||V  dS t| 
||V  dS t| |V  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.
        rS   r<   zCannot copy with PUT, only POSTr   z Cannot create file or directory N)rJ   rC   rH   r   r!   rF   rG   r   r_   r^   rW   rZ   )r-   r   r   rI   existsr   r   r   put   s   
*zContentsHandler.putc                 c   s^    | j }||r|jstdd|| jd| t||V  | 	d | 
  dS )zdelete a file in the given pathr<   z Cannot delete file or directory z	delete %s   N)rC   rF   rG   r   r!   rO   warningr   deleterR   r3   r-   r   rI   r   r   r   rf      s   
zContentsHandler.delete)Tr8   r   )r8   r8   )__name__
__module____qualname__r.   r7   r   authenticatedr   	coroutinerH   rM   rU   rW   rZ   r^   ra   rc   rf   r   r   r   r   r(   H   s4    
	

	&r(   c                   @   s8   e Zd ZejejdddZejejdddZdS )CheckpointsHandlerr8   c                 c   s4    | j }t||V }tj|td}| | dS )z get lists checkpoints for a filer0   N)rC   r   list_checkpointsr4   r5   r   r3   )r-   r   rI   checkpointsdatar   r   r   rH     s
   zCheckpointsHandler.getc                 c   sh    | j }t||V }tj|td}t| jdt|dt|d }| 	d| | 
d | | dS )zpost creates a new checkpointr0   api/contentsrp   idr/   rN   N)rC   r   create_checkpointr4   r5   r   r   r+   r   r2   rR   r3   )r-   r   rI   
checkpointrq   r6   r   r   r   ra     s   
zCheckpointsHandler.postNrh   )	ri   rj   rk   r   rl   r   rm   rH   ra   r   r   r   r   rn     s    rn   c                   @   s4   e Zd Zejejdd Zejejdd ZdS )ModifyCheckpointsHandlerc                 c   0    | j }t|||V  | d |   dS )z&post restores a file from a checkpointrd   N)rC   r   restore_checkpointrR   r3   r-   r   checkpoint_idrI   r   r   r   ra     
   
zModifyCheckpointsHandler.postc                 c   rw   )z+delete clears a checkpoint for a given filerd   N)rC   r   delete_checkpointrR   r3   ry   r   r   r   rf   '  r{   zModifyCheckpointsHandler.deleteN)	ri   rj   rk   r   rl   r   rm   ra   rf   r   r   r   r   rv     s    rv   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| d S )Nz//api/notebooks is deprecated, use /api/contentsrr   )rO   re   redirectr   r+   r,   r   r   r   rH   5  s   
zNotebooksRedirectHandler.getN)
ri   rj   rk   __doc__SUPPORTED_METHODSrH   rc   rM   ra   rf   r   r   r   r   r}   1  s
    r}   c                   @   s&   e Zd ZdZejejdddZdS )TrustNotebooksHandlerz$ Handles trust/signing of notebooks r8   c                 c   s.    | j }t||V  | d |   d S )NrN   )rC   r   trust_notebookrR   r3   rg   r   r   r   ra   C  s
   
zTrustNotebooksHandler.postNrh   )	ri   rj   rk   r   r   rl   r   rm   ra   r   r   r   r   r   @  s
    r   z(?P<checkpoint_id>[\w-]+)z/api/contentsz/checkpointsz/checkpoints/z/trustz/api/notebooks/?(.*))r   r4   tornador   r   notebook.utilsr   r   r   jupyter_client.jsonutilr   ImportErrorr   notebook.base.handlersr	   r
   r   r'   r(   rn   rv   r}   r   _checkpoint_id_regexdefault_handlersr   r   r   r   <module>   s4    . <