ó
ù2¢\c           @   sì  d  Z  d d l m 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 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 m Z m Z m Z 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 d l* m+ Z+ d d l, m- Z- y d d l. m/ Z/ Wn! e0 k
 r¿d d l& m1 Z/ n Xd a3 d „  Z4 d e e f d „  ƒ  YZ5 d S(   s?   A contents manager that uses the local file system for storage.iÿÿÿÿ(   t   datetimeN(   t
   send2trash(   t   webi   (   t   FileCheckpoints(   t   FileManagerMixin(   t   ContentsManageri   (   t   exists(   t   import_item(   t   Anyt   Unicodet   Boolt
   TraitErrort   observet   defaultt   validate(   t   getcwdt   string_types(   t   _tz(   t	   is_hiddent   is_file_hiddent   to_api_path(   t   AuthenticatedFileHandler(   t   _(   t   samefile(   t   samefile_simplec         K   sì   d d l  m } t j d t ƒ |  d d k r4 d St d k rR | d | ƒ a n  | j } t j	 j
 | ƒ \ } } t j | ƒ \ } }	 | |	 j d d	 ƒ }
 | j d
 t |
 | j ƒ ƒ t j |
 d d d ƒ } | j | ƒ Wd QXd S(   sk   convert notebooks to Python script after save with nbconvert

    replaces `jupyter notebook --script`
    iÿÿÿÿ(   t   ScriptExportersE   `_post_save_script` is deprecated and will be removed in Notebook 5.0t   typet   notebookNt   parentt   output_extensions   .txts   Saving script /%st   wt   encodings   utf-8(   t   nbconvert.exporters.scriptR   t   warningst   warnt   DeprecationWarningt   _script_exportert   Nonet   logt   ost   patht   splitextt   from_filenamet   gett   infoR   t   root_dirt   iot   opent   write(   t   modelt   os_patht   contents_managert   kwargsR   R&   t   baset   extt   scriptt	   resourcest   script_fnamet   f(    (    sE   lib/python2.7/site-packages/notebook/services/contents/filemanager.pyt   _post_save_script.   s    	t   FileContentsManagerc           B   s¦  e  Z e d  e ƒ Z e d ƒ d „  ƒ Z e e d  e d d ƒZ	 e
 d ƒ d „  ƒ Z e d$ d  e d e d d ƒZ e d	 ƒ d
 „  ƒ Z d „  Z e d ƒ d „  ƒ Z e d ƒ d „  ƒ Z e e d  e d d ƒZ e d ƒ d „  ƒ Z e d ƒ d „  ƒ Z d „  Z d „  Z d „  Z d „  Z d „  Z e d „ Z e d$ d „ Z e d „ Z e d$ d$ d „ Z d d „ Z  d d „ Z! d  „  Z" d! „  Z# d" „  Z$ d$ d# „ Z% RS(%   t   configR-   c         C   s*   y |  j  j SWn t k
 r% t ƒ  SXd  S(   N(   R   t   notebook_dirt   AttributeErrorR   (   t   self(    (    sE   lib/python2.7/site-packages/notebook/services/contents/filemanager.pyt   _default_root_dirJ   s    t   helps?   DEPRECATED, use post_save_hook. Will be removed in Notebook 5.0t   save_scriptc         C   s+   | d s d  S|  j  j d ƒ t |  _ d  S(   Nt   news‰  
        `--script` is deprecated and will be removed in notebook 5.0.

        You can trigger nbconvert via pre- or post-save hooks:

            ContentsManager.pre_save_hook
            FileContentsManager.post_save_hook

        A post-save hook has been registered that calls:

            jupyter nbconvert --to script [notebook]

        which behaves similarly to `--script`.
        (   R&   t   warningR;   t   post_save_hook(   R@   t   change(    (    sE   lib/python2.7/site-packages/notebook/services/contents/filemanager.pyt   _update_save_scriptR   s
    
	t
   allow_nones   Python callable or importstring thereof

        to be called on the path of a file just saved.

        This can be used to process the file on disk,
        such as converting the notebook to a script or HTML via nbconvert.

        It will be called as (all arguments passed by keyword)::

            hook(os_path=os_path, model=model, contents_manager=instance)

        - path: the filesystem path to the file just written
        - model: the model representing the file
        - contents_manager: this ContentsManager instance
        RF   c         C   sG   | d } t  | t ƒ r( t | ƒ } n  t | ƒ sC t d ƒ ‚ n  | S(   Nt   values   post_save_hook must be callable(   t
   isinstanceR   R   t   callableR   (   R@   t   proposalRJ   (    (    sE   lib/python2.7/site-packages/notebook/services/contents/filemanager.pyt   _validate_post_save_hooky   s    
c         C   sˆ   |  j  r„ y3 |  j j d | ƒ |  j  d | d | d |  ƒ Wq„ t k
 r€ } |  j j d | d t ƒt j d d | ƒ ‚ q„ Xn  d	 S(
   s1   Run the post-save hook if defined, and log errorss   Running post-save hook on %sR2   R1   R3   s   Post-save hook failed o-n %st   exc_infoiô  u1   Unexpected error while running post hook save: %sN(   RF   R&   t   debugt	   Exceptiont   errort   TrueR   t	   HTTPError(   R@   R1   R2   t   e(    (    sE   lib/python2.7/site-packages/notebook/services/contents/filemanager.pyt   run_post_save_hook‚   s    	 c         C   sZ   | d } t  j j | ƒ s1 t  j j | ƒ } n  t  j j | ƒ sV t d | ƒ ‚ n  | S(   s'   Do a bit of validation of the root_dir.RJ   s   %r is not a directory(   R'   R(   t   isabst   abspatht   isdirR   (   R@   RM   RJ   (    (    sE   lib/python2.7/site-packages/notebook/services/contents/filemanager.pyt   _validate_root_dirŒ   s    
t   checkpoints_classc         C   s   t  S(   N(   R   (   R@   (    (    sE   lib/python2.7/site-packages/notebook/services/contents/filemanager.pyt   _checkpoints_class_default—   s    s°   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.t   files_handler_classc         C   s   t  S(   N(   R   (   R@   (    (    sE   lib/python2.7/site-packages/notebook/services/contents/filemanager.pyt   _files_handler_class_default    s    t   files_handler_paramsc         C   s   i |  j  d 6S(   NR(   (   R-   (   R@   (    (    sE   lib/python2.7/site-packages/notebook/services/contents/filemanager.pyt   _files_handler_params_default¤   s    c         C   s1   | j  d ƒ } |  j d | ƒ } t | |  j ƒ S(   sW  Does the API style path correspond to a hidden directory or file?

        Parameters
        ----------
        path : string
            The path to check. This is an API path (`/` separated,
            relative to root_dir).

        Returns
        -------
        hidden : bool
            Whether the path exists and is hidden.
        t   /R(   (   t   stript   _get_os_pathR   R-   (   R@   R(   R2   (    (    sE   lib/python2.7/site-packages/notebook/services/contents/filemanager.pyR   ¨   s    c         C   s.   | j  d ƒ } |  j | ƒ } t j j | ƒ S(   sF  Returns True if the file exists, else returns False.

        API-style wrapper for os.path.isfile

        Parameters
        ----------
        path : string
            The relative path to the file (with '/' as separator)

        Returns
        -------
        exists : bool
            Whether the file exists.
        Ra   (   Rb   Rc   R'   R(   t   isfile(   R@   R(   R2   (    (    sE   lib/python2.7/site-packages/notebook/services/contents/filemanager.pyt   file_existsº   s    c         C   s1   | j  d ƒ } |  j d | ƒ } t j j | ƒ S(   sy  Does the API-style path refer to an extant directory?

        API-style wrapper for os.path.isdir

        Parameters
        ----------
        path : string
            The path to check. This is an API path (`/` separated,
            relative to root_dir).

        Returns
        -------
        exists : bool
            Whether the path is indeed a directory.
        Ra   R(   (   Rb   Rc   R'   R(   RY   (   R@   R(   R2   (    (    sE   lib/python2.7/site-packages/notebook/services/contents/filemanager.pyt
   dir_existsÍ   s    c         C   s+   | j  d ƒ } |  j d | ƒ } t | ƒ S(   sC  Returns True if the path exists, else returns False.

        API-style wrapper for os.path.exists

        Parameters
        ----------
        path : string
            The API path to the file (with '/' as separator)

        Returns
        -------
        exists : bool
            Whether the target exists.
        Ra   R(   (   Rb   Rc   R   (   R@   R(   R2   (    (    sE   lib/python2.7/site-packages/notebook/services/contents/filemanager.pyR   á   s    c         C   sç  |  j  | ƒ } t j | ƒ } y | j } Wn- t t f k
 rZ |  j j d ƒ d } n Xy t	 j
 | j ƒ } WnQ t t f k
 rÄ |  j j d | j | ƒ t d d d d d d t	 j ƒ} n Xy t	 j
 | j ƒ } WnQ t t f k
 r.|  j j d | j | ƒ t d d d d d d t	 j ƒ} n Xi  } | j d d ƒ d	 | d
 <| | d <| | d <| | d <d | d <d | d <d | d <| | d <y t j | t j ƒ | d <Wn. t k
 râ|  j j d | ƒ t | d <n X| S(   s)   Build the common base of a contents models   Unable to get size.s   Invalid mtime %s for %si²  i   i    t   tzinfos   Invalid ctime %s for %sRa   iÿÿÿÿt   nameR(   t   last_modifiedt   createdt   contentt   formatt   mimetypet   sizet   writables'   Failed to check write permissions on %sN(   Rc   R'   t   lstatt   st_sizet
   ValueErrort   OSErrorR&   RE   R%   t   tzt   utcfromtimestampt   st_mtimeR    t   UTCt   st_ctimet   rsplitt   accesst   W_OKRR   t   False(   R@   R(   R2   R,   Rn   Ri   Rj   R1   (    (    sE   lib/python2.7/site-packages/notebook/services/contents/filemanager.pyt   _base_modelô   s@    
%%






c         C   sG  |  j  | ƒ } d | } t j j | ƒ s@ t j d | ƒ ‚ nD t | |  j ƒ r„ |  j r„ |  j	 j
 d | ƒ t j d | ƒ ‚ n  |  j | ƒ } d | d <d | d <| rCg  | d <} |  j  | ƒ } xit j | ƒ D]X} y t j j | | ƒ } Wn, t k
 r'}	 |  j	 j d | |	 ƒ qÚ n Xy t j | ƒ }
 WnT t k
 r‘}	 |	 j t j k ru|  j	 j d	 | ƒ qÚ |  j	 j d
 | |	 ƒ qÚ n Xt j |
 j ƒ rät j |
 j ƒ rät j |
 j ƒ rä|  j	 j d | ƒ qÚ n  |  j | ƒ rÚ t | d |
 ƒrÚ | j |  j d d | | f d t ƒ ƒ qÚ qÚ Wd | d <n  | S(   sp   Build a model for a directory

        if content is requested, will include a listing of the directory
        u   directory does not exist: %ri”  s4   Refusing to serve hidden directory %r, via 404 Errort	   directoryR   Rn   Rk   s"   failed to decode filename '%s': %ss   %s doesn't exists   Error stat-ing %s: %ss   %s not a regular filet   stat_resR(   s   %s/%st   jsonRl   N(    Rc   R'   R(   RY   R   RT   R   R-   t   allow_hiddenR&   R,   R}   R%   t   listdirt   joint   UnicodeDecodeErrorRE   Rp   Rs   t   errnot   ENOENTt   statt   S_ISLNKt   st_modet   S_ISREGt   S_ISDIRRP   t   should_listR   t   appendR+   R|   (   R@   R(   Rk   R2   t   four_o_fourR1   t   contentst   os_dirRh   RU   t   st(    (    sE   lib/python2.7/site-packages/notebook/services/contents/filemanager.pyt
   _dir_model"  sN    


	"c         C   s¯   |  j  | ƒ } d | d <|  j | ƒ } t j | ƒ d | d <| r« |  j | | ƒ \ } } | d d k r’ i d d 6d d 6| } | | d <n  | j d	 | d
 | ƒ n  | S(   s@  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
        t   fileR   i    Rm   s
   text/plaint   texts   application/octet-streamt   base64Rk   Rl   N(   R}   Rc   t	   mimetypest
   guess_typet
   _read_fileR%   t   update(   R@   R(   Rk   Rl   R1   R2   t   default_mime(    (    sE   lib/python2.7/site-packages/notebook/services/contents/filemanager.pyt   _file_model\  s     

	
c         C   s{   |  j  | ƒ } d | d <|  j | ƒ } | rw |  j | d d ƒ} |  j | | ƒ | | d <d | d <|  j | ƒ n  | S(   sœ   Build a notebook model

        if content is requested, the notebook content will be populated
        as a JSON structure (not double-serialized)
        R   R   t
   as_versioni   Rk   R€   Rl   (   R}   Rc   t   _read_notebookt   mark_trusted_cellst   validate_notebook_model(   R@   R(   Rk   R1   R2   t   nb(    (    sE   lib/python2.7/site-packages/notebook/services/contents/filemanager.pyt   _notebook_model|  s    


c         C   s*  | j  d ƒ } |  j | ƒ s7 t j d d | ƒ ‚ n  |  j | ƒ } t j j | ƒ r¡ | d k r‰ t j d d | | f d d ƒ‚ n  |  j	 | d	 | ƒ} n… | d
 k sÈ | d k rà | j
 d ƒ rà |  j | d	 | ƒ} nF | d k rt j d d | d d ƒ‚ n  |  j | d	 | d | ƒ} | S(   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.
        Ra   i”  u   No such file or directory: %sR~   i  u   %s is a directory, not a %st   reasons   bad typeRk   R   s   .ipynbu   %s is not a directoryRl   N(   NR~   (   Rb   R   R   RT   Rc   R'   R(   RY   R%   R’   t   endswithR¡   R›   (   R@   R(   Rk   R   Rl   R2   R1   (    (    sE   lib/python2.7/site-packages/notebook/services/contents/filemanager.pyR+     s     	'	t    c         C   s¬   t  | |  j ƒ r5 |  j r5 t j d d | ƒ ‚ n  t j j | ƒ sj |  j ƒ   t j	 | ƒ Wd QXn> t j j
 | ƒ s• t j d d | ƒ ‚ n |  j j d | ƒ d S(   s   create a directoryi  u!   Cannot create hidden directory %rNu   Not a directory: %ss   Directory %r already exists(   R   R-   R   R   RT   R'   R(   R   t   perm_to_403t   mkdirRY   R&   RP   (   R@   R2   R1   R(   (    (    sE   lib/python2.7/site-packages/notebook/services/contents/filemanager.pyt   _save_directory¹  s    c         C   sV  | j  d ƒ } d | k r0 t j d d ƒ ‚ n  d | k ra | d d k ra t j d d ƒ ‚ n  |  j | ƒ } |  j j d | ƒ |  j d	 | d
 | ƒ yß | d d k rt j | d ƒ } |  j	 | | ƒ |  j
 | | ƒ |  j j | ƒ sw|  j | ƒ qwns | d d k r7|  j | | d | j d ƒ ƒ n@ | d d k r]|  j | | | ƒ n t j d d | d ƒ ‚ Wna t j k
 r‘‚  nK t k
 rÛ} |  j j d | | d t ƒt j d d | | f ƒ ‚ n Xd } | d d k r|  j | ƒ | j d d ƒ } n  |  j | d t ƒ} | r<| | d <n  |  j d	 | d | ƒ | S(   s9   Save the file model and return the model with no content.Ra   R   i  u   No file type providedRk   R~   u   No file content provideds	   Saving %sR1   R(   R   R“   Rl   s   Unhandled contents type: %su   Error while saving file: %s %sRO   iô  u)   Unexpected error while saving file: %s %st   messageR2   N(   Rb   R   RT   Rc   R&   RP   t   run_pre_save_hookt   nbformatt	   from_dictt   check_and_signt   _save_notebookt   checkpointst   list_checkpointst   create_checkpointt
   _save_fileR+   R§   RQ   RR   RS   R%   RŸ   R|   RV   (   R@   R1   R(   R2   R    RU   t   validation_message(    (    sE   lib/python2.7/site-packages/notebook/services/contents/filemanager.pyt   saveÅ  sD    # c            s‹  | j  d ƒ } ˆ  j | ƒ } t j } t j j | ƒ sR t j d d | ƒ ‚ n  d „  } ‡  f d †  } ˆ  j rê t	 j
 d k r§ | | ƒ r§ t j d d | ƒ ‚ n  | | ƒ rÔ ˆ  j j d	 | ƒ t | ƒ d
 Sˆ  j j d | ƒ n  t j j | ƒ rW| | ƒ r!t j d d | ƒ ‚ n  ˆ  j j d | ƒ ˆ  j ƒ   t j | ƒ Wd
 QXn0 ˆ  j j d | ƒ ˆ  j ƒ   | | ƒ Wd
 QXd
 S(   s   Delete file at path.Ra   i”  u$   File or directory does not exist: %sc         S   sS   t  j d d h k r t St j |  ƒ j } t j t j j d ƒ ƒ j } | | k S(   Nt   win32t   darwint   ~(   t   syst   platformRS   R'   R‡   t   st_devR(   t
   expanduser(   R2   t   file_devt   home_dev(    (    sE   lib/python2.7/site-packages/notebook/services/contents/filemanager.pyt   _check_trashý  s
    c            sN   t  j j |  ƒ rJ t ˆ  j d d  ƒ } t t  j |  ƒ ƒ | h rJ t Sn  t	 S(   Nt   checkpoint_dir(
   R'   R(   RY   t   getattrR®   R%   t   setR‚   RS   R|   (   R2   t   cp_dir(   R@   (    sE   lib/python2.7/site-packages/notebook/services/contents/filemanager.pyt   is_non_empty_dir  s
    R´   i  u   Directory %s not emptys   Sending %s to trashNs<   Skipping trash for %s, on different device to home directorys   Removing directory %ss   Unlinking file %s(   Rb   Rc   R'   t   unlinkR(   R   R   RT   t   delete_to_trashR·   R¸   R&   RP   R   RE   RY   R¥   t   shutilt   rmtree(   R@   R(   R2   t   rmR½   RÂ   (    (   R@   sE   lib/python2.7/site-packages/notebook/services/contents/filemanager.pyt   delete_fileõ  s2    		
	

c         C   sú   | j  d ƒ } | j  d ƒ } | | k r. d S|  j | ƒ } |  j | ƒ } t j j | ƒ r‡ t | | ƒ r‡ t j d d | ƒ ‚ n  y' |  j ƒ   t	 j
 | | ƒ Wd QXWnE t j k
 rÇ ‚  n/ t k
 rõ } t j d d | | f ƒ ‚ n Xd S(   s   Rename a file.Ra   Ni™  u   File already exists: %siô  u"   Unknown error renaming file: %s %s(   Rb   Rc   R'   R(   R   R   R   RT   R¥   RÅ   t   moveRQ   (   R@   t   old_patht   new_patht   new_os_patht   old_os_pathRU   (    (    sE   lib/python2.7/site-packages/notebook/services/contents/filemanager.pyt   rename_file.  s    "c         C   s   t  d ƒ |  j S(   Ns*   Serving notebooks from local directory: %s(   R   R-   (   R@   (    (    sE   lib/python2.7/site-packages/notebook/services/contents/filemanager.pyt   info_stringE  s    c         C   sB   |  j  | ƒ r | Sd | k r8 | j d d ƒ d } n d } | S(   sI   Return the initial API path of  a kernel associated with a given notebookRa   i   i    R¤   (   Rf   Ry   (   R@   R(   R1   t
   parent_dir(    (    sE   lib/python2.7/site-packages/notebook/services/contents/filemanager.pyt   get_kernel_pathH  s    N(&   t   __name__t
   __module__R	   RS   R-   R   RA   R
   R|   RC   R   RH   R   R%   RF   R   RN   RV   RZ   R\   RÄ   R^   R`   R   Re   Rf   R   R}   R’   R›   R¡   R+   R§   R³   RÈ   RÎ   RÏ   RÑ   (    (    (    sE   lib/python2.7/site-packages/notebook/services/contents/filemanager.pyR<   F   s:   			
						.: *0	9		(6   t   __doc__R    R…   R.   R'   RÅ   R‡   R·   R!   R–   Rª   R   t   tornadoR   t   filecheckpointsR   t   fileioR   t   managerR   t   utilsR   t   ipython_genutils.importstringR   t	   traitletsR   R	   R
   R   R   R   R   t   ipython_genutils.py3compatR   R   R   R   Rt   t   notebook.utilsR   R   R   t   notebook.base.handlersR   t   notebook.transutilsR   t   os.pathR   t   ImportErrorR   R%   R$   R;   R<   (    (    (    sE   lib/python2.7/site-packages/notebook/services/contents/filemanager.pyt   <module>   s<   4	