ó
ù2¢\c           @   s4  d  Z  d d l Z d d l m Z m Z d d l m Z m Z d d l m	 Z	 d d l
 m Z m Z m Z d „  Z d e f d	 „  ƒ  YZ d
 e f d „  ƒ  YZ d e f d „  ƒ  YZ d e f d „  ƒ  YZ d e f d „  ƒ  YZ d Z d e e f d e e f e f d e e f d e e f d e f g Z d S(   s‘   Tornado handlers for the contents web service.

Preliminary documentation at https://github.com/ipython/ipython/wiki/IPEP-27%3A-Contents-Service
iÿÿÿÿN(   t   gent   web(   t   url_path_joint
   url_escape(   t   date_default(   t   IPythonHandlert
   APIHandlert
   path_regexc      	      s  d d d d d d d d d	 h	 } | t  ˆ  j ƒ  ƒ } | r^ t j d
 d j d | ƒ ƒ ‚ n  d d	 g } | rÃ g  | D] } ˆ  | d k rw | ^ qw } | r t j d
 d j d | ƒ ƒ ‚ q n= ‡  f d †  | Dƒ } | r t j d
 d j d | ƒ ƒ ‚ n  d S(   s    
    Validate a model returned by a ContentsManager method.

    If expect_content is True, then we expect non-null entries for 'content'
    and 'format'.
    t   namet   patht   typet   writablet   createdt   last_modifiedt   mimetypet   contentt   formatiô  u   Missing Model Keys: {missing}t   missingu   Keys unexpectedly None: {keys}t   keysc            s-   i  |  ]# } ˆ  | d  k	 r ˆ  | | “ q S(   N(   t   None(   t   .0t   key(   t   model(    sB   lib/python2.7/site-packages/notebook/services/contents/handlers.pys
   <dictcomp>8   s   	u"   Keys unexpectedly not None: {keys}N(   t   setR   R   t	   HTTPErrorR   R   (   R   t   expect_contentt   required_keysR   t   maybe_none_keysR   t   errors(    (   R   sB   lib/python2.7/site-packages/notebook/services/contents/handlers.pyt   validate_model   s6    	)
t   ContentsHandlerc           B   s  e  Z d  „  Z e d „ Z e j e j d d „ ƒ ƒ Z	 e j e j d d „ ƒ ƒ Z
 e j d d „ ƒ Z e j d „  ƒ Z e j d d d „ ƒ Z e j d „  ƒ Z e j e j d d	 „ ƒ ƒ Z e j e j d d
 „ ƒ ƒ Z e j e j d d „ ƒ ƒ Z RS(   c         C   s   t  |  j d d t | ƒ ƒ S(   s«   Return the full URL location of a file.

        Parameters
        ----------
        path : unicode
            The API path of the file, such as "foo/bar.txt".
        t   apit   contents(   R   t   base_urlR   (   t   selfR	   (    (    sB   lib/python2.7/site-packages/notebook/services/contents/handlers.pyt   location_urlE   s    c         C   sp   | r, |  j  | d ƒ } |  j d | ƒ n  |  j d | d ƒ |  j d d ƒ |  j t j | d t ƒƒ d S(	   sB   Finish a JSON request with a model, setting relevant headers, etc.R	   t   Locations   Last-ModifiedR   s   Content-Types   application/jsont   defaultN(   R#   t
   set_headert   finisht   jsont   dumpsR   (   R"   R   t   location(    (    sB   lib/python2.7/site-packages/notebook/services/contents/handlers.pyt   _finish_modelQ   s    t    c      
   c   s9  | p	 d } |  j  d d d ƒ} | d d d d h k rR t j d d | ƒ ‚ n  |  j  d	 d d ƒ} | d d
 d h k r• t j d d | ƒ ‚ n  |  j  d d d ƒ} | d d h k rÕ t j d d | ƒ ‚ n  t | ƒ } t j |  j j d | d | d	 | d | ƒ ƒ V} t	 | d | ƒ|  j
 | d t ƒd S(   s©   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
   R%   t	   directoryt   filet   notebooki  u   Type %r is invalidR   t   textt   base64u   Format %r is invalidR   t   1t   0u   Content %r is invalidR	   R   R*   N(   t   get_query_argumentR   R   R   t   intR    t   maybe_futuret   contents_managert   getR   R+   t   False(   R"   R	   R
   R   R   R   (    (    sB   lib/python2.7/site-packages/notebook/services/contents/handlers.pyR8   Z   s    c         c   ss   |  j  } |  j ƒ  } | d k r6 t j d d ƒ ‚ n  t j | j | | ƒ ƒ V} t | d t	 ƒ|  j
 | ƒ d S(   s?   PATCH renames a file or directory without re-uploading content.i  u   JSON body missingR   N(   R7   t   get_json_bodyR   R   R   R    R6   t   updateR   R9   R+   (   R"   R	   t   cmR   (    (    sB   lib/python2.7/site-packages/notebook/services/contents/handlers.pyt   patchu   s    	c         c   su   |  j  j d j d | d | p! d ƒ ƒ t j |  j j | | ƒ ƒ V} |  j d ƒ t | d t	 ƒ|  j
 | ƒ d S(   s6   Copy a file, optionally specifying a target directory.u    Copying {copy_from} to {copy_to}t	   copy_fromt   copy_toR,   iÉ   R   N(   t   logt   infoR   R    R6   R7   t   copyt
   set_statusR   R9   R+   (   R"   R>   R?   R   (    (    sB   lib/python2.7/site-packages/notebook/services/contents/handlers.pyt   _copy   s    c         c   s`   |  j  j d | ƒ t j |  j j | | ƒ ƒ V} |  j d ƒ t | d t ƒ|  j	 | ƒ d S(   s#   Handle upload of a new file to pathu   Uploading file to %siÉ   R   N(
   R@   RA   R    R6   R7   t   newRC   R   R9   R+   (   R"   R   R	   (    (    sB   lib/python2.7/site-packages/notebook/services/contents/handlers.pyt   _upload   s
    c         c   su   |  j  j d | p d | ƒ t j |  j j d | d | d | ƒ ƒ V} |  j d ƒ t | d t ƒ|  j	 | ƒ d S(	   s#   Create a new, empty untitled entityu   Creating new %s in %sR.   R	   R
   t   extiÉ   R   N(
   R@   RA   R    R6   R7   t   new_untitledRC   R   R9   R+   (   R"   R	   R
   RG   R   (    (    sB   lib/python2.7/site-packages/notebook/services/contents/handlers.pyt   _new_untitled–   s
    +c         c   s{   | j  d d ƒ } | s% | d k r; |  j j d | ƒ n  t j |  j j | | ƒ ƒ V} t | d t	 ƒ|  j
 | ƒ d S(   s   Save an existing file.t   chunkiÿÿÿÿu   Saving file at %sR   N(   R8   R   R@   RA   R    R6   R7   t   saveR   R9   R+   (   R"   R   R	   RJ   (    (    sB   lib/python2.7/site-packages/notebook/services/contents/handlers.pyt   _saveŸ   s    c         c   sé   |  j  } | j | ƒ r- t j d d ƒ ‚ n  | j | ƒ sU t j d d | ƒ ‚ n  |  j ƒ  } | d	 k	 r× | j d ƒ } | j d d ƒ } | j d d ƒ } | rº |  j | | ƒ Vqå |  j	 | d | d | ƒVn |  j	 | ƒ Vd	 S(
   sZ  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
        i  s&   Cannot POST to files, use PUT instead.i”  s   No such directory: %sR>   RG   R,   R
   N(
   R7   t   file_existsR   R   t
   dir_existsR:   R   R8   RD   RI   (   R"   R	   R<   R   R>   RG   R
   (    (    sB   lib/python2.7/site-packages/notebook/services/contents/handlers.pyt   post©   s    	c         c   s­   |  j  ƒ  } | r’ | j d ƒ r6 t j d d ƒ ‚ n  t j |  j j | ƒ ƒ V} | ru t j |  j | | ƒ ƒ Vq© t j |  j	 | | ƒ ƒ Vn t j |  j
 | ƒ ƒ Vd S(   sµ  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.
        R>   i  s   Cannot copy with PUT, only POSTN(   R:   R8   R   R   R    R6   R7   RM   RL   RF   RI   (   R"   R	   R   t   exists(    (    sB   lib/python2.7/site-packages/notebook/services/contents/handlers.pyt   putÌ   s    c         c   sN   |  j  } |  j j d | ƒ t j | j | ƒ ƒ V|  j d ƒ |  j ƒ  d S(   s   delete a file in the given paths	   delete %siÌ   N(   R7   R@   t   warningR    R6   t   deleteRC   R'   (   R"   R	   R<   (    (    sB   lib/python2.7/site-packages/notebook/services/contents/handlers.pyRS   å   s
    	N(   t   __name__t
   __module__R#   t   TrueR+   R   t   authenticatedR    t	   coroutineR8   R=   R   RD   RF   RI   RL   RO   RQ   RS   (    (    (    sB   lib/python2.7/site-packages/notebook/services/contents/handlers.pyR   C   s.   		
	
!t   CheckpointsHandlerc           B   sD   e  Z e j e j d  d „ ƒ ƒ Z e j e j d  d „ ƒ ƒ Z RS(   R,   c         c   sH   |  j  } t j | j | ƒ ƒ V} t j | d t ƒ} |  j | ƒ d S(   s    get lists checkpoints for a fileR%   N(   R7   R    R6   t   list_checkpointsR(   R)   R   R'   (   R"   R	   R<   t   checkpointst   data(    (    sB   lib/python2.7/site-packages/notebook/services/contents/handlers.pyR8   ò   s    	c         c   s   |  j  } t j | j | ƒ ƒ V} t j | d t ƒ} t |  j d t	 | ƒ d t	 | d ƒ ƒ } |  j
 d | ƒ |  j d ƒ |  j | ƒ d S(   s   post creates a new checkpointR%   s   api/contentsR[   t   idR$   iÉ   N(   R7   R    R6   t   create_checkpointR(   R)   R   R   R!   R   R&   RC   R'   (   R"   R	   R<   t
   checkpointR\   R*   (    (    sB   lib/python2.7/site-packages/notebook/services/contents/handlers.pyRO   û   s    	(   RT   RU   R   RW   R    RX   R8   RO   (    (    (    sB   lib/python2.7/site-packages/notebook/services/contents/handlers.pyRY   ð   s   t   ModifyCheckpointsHandlerc           B   s>   e  Z e j e j d  „  ƒ ƒ Z e j e j d „  ƒ ƒ Z RS(   c         c   s>   |  j  } t j | j | | ƒ ƒ V|  j d ƒ |  j ƒ  d S(   s&   post restores a file from a checkpointiÌ   N(   R7   R    R6   t   restore_checkpointRC   R'   (   R"   R	   t   checkpoint_idR<   (    (    sB   lib/python2.7/site-packages/notebook/services/contents/handlers.pyRO     s    	c         c   s>   |  j  } t j | j | | ƒ ƒ V|  j d ƒ |  j ƒ  d S(   s+   delete clears a checkpoint for a given fileiÌ   N(   R7   R    R6   t   delete_checkpointRC   R'   (   R"   R	   Rb   R<   (    (    sB   lib/python2.7/site-packages/notebook/services/contents/handlers.pyRS     s    	(   RT   RU   R   RW   R    RX   RO   RS   (    (    (    sB   lib/python2.7/site-packages/notebook/services/contents/handlers.pyR`   	  s   t   NotebooksRedirectHandlerc           B   s/   e  Z d  Z d Z d „  Z e Z Z Z Z RS(   s(   Redirect /api/notebooks to /api/contentst   GETt   PUTt   PATCHt   POSTt   DELETEc         C   s0   |  j  j d ƒ |  j t |  j d | ƒ ƒ d  S(   Ns/   /api/notebooks is deprecated, use /api/contentss   api/contents(   R@   RR   t   redirectR   R!   (   R"   R	   (    (    sB   lib/python2.7/site-packages/notebook/services/contents/handlers.pyR8   "  s
    	(   Re   Rf   Rg   Rh   Ri   (	   RT   RU   t   __doc__t   SUPPORTED_METHODSR8   RQ   R=   RO   RS   (    (    (    sB   lib/python2.7/site-packages/notebook/services/contents/handlers.pyRd     s   	t   TrustNotebooksHandlerc           B   s,   e  Z d  Z e j e j d d „ ƒ ƒ Z RS(   s$    Handles trust/signing of notebooks R,   c         c   s;   |  j  } t j | j | ƒ ƒ V|  j d ƒ |  j ƒ  d  S(   NiÉ   (   R7   R    R6   t   trust_notebookRC   R'   (   R"   R	   R<   (    (    sB   lib/python2.7/site-packages/notebook/services/contents/handlers.pyRO   0  s    	(   RT   RU   Rk   R   RW   R    RX   RO   (    (    (    sB   lib/python2.7/site-packages/notebook/services/contents/handlers.pyRm   -  s   s   (?P<checkpoint_id>[\w-]+)s   /api/contents%s/checkpointss   /api/contents%s/checkpoints/%ss   /api/contents%s/trusts   /api/contents%ss   /api/notebooks/?(.*)(   Rk   R(   t   tornadoR    R   t   notebook.utilsR   R   t   jupyter_client.jsonutilR   t   notebook.base.handlersR   R   R   R   R   RY   R`   Rd   Rm   t   _checkpoint_id_regext   default_handlers(    (    (    sB   lib/python2.7/site-packages/notebook/services/contents/handlers.pyt   <module>   s$   	.­