ó
ù2¢\c           @   sg  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 m Z 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 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 m  Z  m Z m! Z! d d l" m# Z# d d l$ m% Z% d d l& m' Z' e j( d ƒ Z) d e f d „  ƒ  YZ* d S(   s#   A base class for contents managers.iÿÿÿÿ(   t   fnmatchN(   t	   HTTPErrort   RequestHandleri   (   t   FilesHandleri   (   t   Checkpoints(   t   LoggingConfigurable(   t   signt   validatet   ValidationError(   t   new_notebook(   t   import_item(
   t   Anyt   Boolt   Dictt   Instancet   Listt
   TraitErrort   Typet   UnicodeR   t   default(   t   string_types(   t   IPythonHandler(   t   _s   \-Copy\d*\.t   ContentsManagerc        
   B   sÌ  e  Z d  Z e d d e ƒZ e e d e d d ƒZ e	 e
 j ƒ Z d „  Z e e ƒ  d d d d	 d
 d d g d e d d ƒZ e e d ƒ d e d d ƒZ e d d e d d ƒZ e d d e d d ƒZ e d= d e d e d d ƒZ e d ƒ d „  ƒ Z d „  Z e e d e ƒZ e	 e d e ƒZ e d e ƒ Z e  d ƒ d „  ƒ Z! e  d ƒ d „  ƒ Z" e e# d e$ d e d e d d ƒZ% e d e d d ƒ Z& d  „  Z' d! „  Z( d" „  Z) d# d$ „ Z* d% „  Z+ e d= d= d& „ Z, d' „  Z- d( „  Z. d) „  Z/ d* „  Z0 d+ „  Z1 d, „  Z2 d- „  Z3 d= d. „ Z4 d# d# d/ „ Z5 d0 „  Z6 d# d# d# d1 „ Z7 d= d# d2 „ Z8 d= d3 „ Z9 d4 „  Z: d5 „  Z; d# d6 „ Z< d# d7 „ Z= d8 „  Z> d9 „  Z? d: „  Z@ d; „  ZA d< „  ZB RS(>   sÙ  Base class for serving files and directories.

    This serves any text or binary file,
    as well as directories,
    with special handling for JSON notebook documents.

    Most APIs take a path argument,
    which is always an API-style unicode path,
    and always refers to a directory.

    - unicode, not url-escaped
    - '/'-separated
    - leading and trailing '/' will be stripped
    - if unspecified, path defaults to '',
      indicating the root path.

    t   /t   configt   helps   Allow access to hidden filesc         C   s   t  j d |  ƒ S(   Nt   parent(   R   t   NotebookNotary(   t   self(    (    sA   lib/python2.7/site-packages/notebook/services/contents/manager.pyt   _notary_default@   s    u   __pycache__s   *.pycs   *.pyos	   .DS_Stores   *.sos   *.dylibs   *~sC   
        Glob patterns to hide in file and directory listings.
    t   Untitleds4   The base name used when creating untitled notebooks.t   untitleds0   The base name used when creating untitled files.s   Untitled Folders6   The base name used when creating untitled directories.t
   allow_nonesu  Python callable or importstring thereof

        To be called on a contents model prior to save.

        This can be used to process the structure,
        such as removing notebook outputs or other side effects that
        should not be saved.

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

            hook(path=path, model=model, contents_manager=self)

        - model: the model to be saved. Includes file contents.
          Modifying this dict will affect the file that is stored.
        - path: the API path of the save destination
        - contents_manager: this ContentsManager instance
        t   pre_save_hookc         C   sJ   | d } t  | t ƒ r+ t |  j ƒ } n  t | ƒ sF t d ƒ ‚ n  | S(   Nt   values   pre_save_hook must be callable(   t
   isinstanceR   R
   R"   t   callableR   (   R   t   proposalR#   (    (    sA   lib/python2.7/site-packages/notebook/services/contents/manager.pyt   _validate_pre_save_hookj   s    
c         K   ss   |  j  ro y6 |  j j d | ƒ |  j  d | d | d |  |  Wqo t k
 rk |  j j d | d t ƒqo Xn  d S(   s0   Run the pre-save hook if defined, and log errorss   Running pre-save hook on %st   modelt   patht   contents_managers   Pre-save hook failed on %st   exc_infoN(   R"   t   logt   debugt	   Exceptiont   errort   True(   R   R(   R)   t   kwargs(    (    sA   lib/python2.7/site-packages/notebook/services/contents/manager.pyt   run_pre_save_hooks   s    	#t   checkpointsc         C   s   |  j  |  j   S(   N(   t   checkpoints_classt   checkpoints_kwargs(   R   (    (    sA   lib/python2.7/site-packages/notebook/services/contents/manager.pyt   _default_checkpoints€   s    R5   c         C   s   t  d |  d |  j ƒ S(   NR   R,   (   t   dictR,   (   R   (    (    sA   lib/python2.7/site-packages/notebook/services/contents/manager.pyt   _default_checkpoints_kwargs„   s    t   klasssv  handler class to use when serving raw file requests.

        Default is a fallback that talks to the ContentsManager API,
        which may be inefficient, especially for large files.

        Local files-based ContentsManagers can use a StaticFileHandler subclass,
        which will be much more efficient.

        Access to these files should be Authenticated.
        sÆ   Extra parameters to pass to files_handler_class.

        For example, StaticFileHandlers generally expect a `path` argument
        specifying the root directory from which to serve files.
        c         C   s2   g  } |  j  r. | j d |  j  |  j f ƒ n  | S(   s[   Return additional handlers

        Default: self.files_handler_class on /files/.*
        s   /files/(.*)(   t   files_handler_classt   appendt   files_handler_params(   R   t   handlers(    (    sA   lib/python2.7/site-packages/notebook/services/contents/manager.pyt   get_extra_handlers¢   s
    	c         C   s
   t  ‚ d S(   s=  Does a directory exist at the given path?

        Like os.path.isdir

        Override this method in subclasses.

        Parameters
        ----------
        path : string
            The path to check

        Returns
        -------
        exists : bool
            Whether the path does indeed exist.
        N(   t   NotImplementedError(   R   R)   (    (    sA   lib/python2.7/site-packages/notebook/services/contents/manager.pyt
   dir_exists±   s    c         C   s
   t  ‚ d S(   s/  Is path 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 is hidden.

        N(   R?   (   R   R)   (    (    sA   lib/python2.7/site-packages/notebook/services/contents/manager.pyt	   is_hiddenÄ   s    t    c         C   s   t  d ƒ ‚ d S(   sA  Does a file exist at the given path?

        Like os.path.isfile

        Override this method in subclasses.

        Parameters
        ----------
        path : string
            The API path of a file to check for.

        Returns
        -------
        exists : bool
            Whether the file exists.
        s!   must be implemented in a subclassN(   R?   (   R   R)   (    (    sA   lib/python2.7/site-packages/notebook/services/contents/manager.pyt   file_existsÕ   s    c         C   s   |  j  | ƒ p |  j | ƒ S(   s0  Does a file or directory exist at the given path?

        Like os.path.exists

        Parameters
        ----------
        path : string
            The API path of a file or directory to check for.

        Returns
        -------
        exists : bool
            Whether the target exists.
        (   RC   R@   (   R   R)   (    (    sA   lib/python2.7/site-packages/notebook/services/contents/manager.pyt   existsè   s    c         C   s   t  d ƒ ‚ d S(   s   Get a file or directory model.s!   must be implemented in a subclassN(   R?   (   R   R)   t   contentt   typet   format(    (    sA   lib/python2.7/site-packages/notebook/services/contents/manager.pyt   getù   s    c         C   s   t  d ƒ ‚ d S(   sí   
        Save a file or directory model to path.

        Should return the saved model with no content.  Save implementations
        should call self.run_pre_save_hook(model=model, path=path) prior to
        writing any data.
        s!   must be implemented in a subclassN(   R?   (   R   R(   R)   (    (    sA   lib/python2.7/site-packages/notebook/services/contents/manager.pyt   saveý   s    c         C   s   t  d ƒ ‚ d S(   s%   Delete the file or directory at path.s!   must be implemented in a subclassN(   R?   (   R   R)   (    (    sA   lib/python2.7/site-packages/notebook/services/contents/manager.pyt   delete_file  s    c         C   s   t  d ƒ ‚ d S(   s   Rename a file or directory.s!   must be implemented in a subclassN(   R?   (   R   t   old_patht   new_path(    (    sA   lib/python2.7/site-packages/notebook/services/contents/manager.pyt   rename_file  s    c         C   sH   | j  d ƒ } | s' t d d ƒ ‚ n  |  j | ƒ |  j j | ƒ d S(   s7   Delete a file/directory and any associated checkpoints.R   i  s   Can't delete rootN(   t   stripR   RJ   R3   t   delete_all_checkpoints(   R   R)   (    (    sA   lib/python2.7/site-packages/notebook/services/contents/manager.pyt   delete  s
    c         C   s'   |  j  | | ƒ |  j j | | ƒ d S(   s<   Rename a file and any checkpoints associated with that file.N(   RM   R3   t   rename_all_checkpoints(   R   RK   RL   (    (    sA   lib/python2.7/site-packages/notebook/services/contents/manager.pyt   rename  s    c         C   sb   | j  d ƒ } | j d | ƒ j  d ƒ } | | k rI |  j | | ƒ n  |  j | d t ƒ} | S(   s®   Update the file's path

        For use in PATCH requests, to enable renaming a file without
        re-uploading its contents. Only used for renaming at the moment.
        R   R)   RE   (   RN   RH   RR   t   False(   R   R(   R)   RL   (    (    sA   lib/python2.7/site-packages/notebook/services/contents/manager.pyt   update  s    c         C   s   d S(   Ns   Serving contents(    (   R   (    (    sA   lib/python2.7/site-packages/notebook/services/contents/manager.pyt   info_string,  s    c         C   s   d S(   sU  Return the API path for the kernel
        
        KernelManagers can turn this value into a filesystem path,
        or ignore it altogether.

        The default value here will start kernels in the directory of the
        notebook server. FileContentsManager overrides this to use the
        directory containing the notebook.
        RB   (    (   R   R)   R(   (    (    sA   lib/python2.7/site-packages/notebook/services/contents/manager.pyt   get_kernel_path/  s    
c         C   sª   | j  d ƒ } | j d ƒ \ } } } | | } xr t j ƒ  D]d } | r_ d j | | ƒ }	 n d }	 d j d | d |	 d | ƒ }
 |  j d	 j | |
 ƒ ƒ s> Pq> q> W|
 S(
   sÂ  Increment a filename until it is unique.

        Parameters
        ----------
        filename : unicode
            The name of a file, including extension
        path : unicode
            The API path of the target's directory
        insert: unicode
            The characters to insert after the base filename

        Returns
        -------
        name : unicode
            A filename that is unique, based on the input filename.
        R   t   .s   {}{}RB   u   {basename}{insert}{suffix}t   basenamet   insertt   suffixu   {}/{}(   RN   t	   partitiont	   itertoolst   countRG   RD   (   R   t   filenameR)   RY   RX   t   dott   extRZ   t   it   insert_it   name(    (    sA   lib/python2.7/site-packages/notebook/services/contents/manager.pyt   increment_filename;  s    
c         C   s`   y t  | d ƒ WnG t k
 r[ } d j | j t j | j d d d d „  ƒƒ | d <n X| S(   s&   Add failed-validation message to modelRE   u"   Notebook validation failed: {}:
{}t   indenti   R   c         S   s   d S(   Ns	   <UNKNOWN>(    (   t   obj(    (    sA   lib/python2.7/site-packages/notebook/services/contents/manager.pyt   <lambda>b  RB   t   message(   t   validate_nbR   RG   Rh   t   jsont   dumpst   instance(   R   R(   t   e(    (    sA   lib/python2.7/site-packages/notebook/services/contents/manager.pyt   validate_notebook_model\  s    2c         C   s7  | j  d ƒ } |  j | ƒ s4 t d d | ƒ ‚ n  i  } | rM | | d <n  | d k rl | j d d ƒ n | j d d ƒ d } | d d	 k r¤ |  j } d
 } nU | d d k rÆ |  j } d } n3 | d d k râ |  j } n t d d | d ƒ ‚ |  j | | | d | ƒ} d j | | ƒ } |  j	 | | ƒ S(   sñ   Create a new untitled file or directory in path
        
        path must be a directory
        
        File extension can be specified.
        
        Use `new` to create files with a fully specified path (including filename).
        R   i”  s   No such directory: %sRF   s   .ipynbt   notebookt   fileRB   t	   directoryt    i  s   Unexpected model type: %rRY   u   {0}/{1}(
   RN   R@   R   t
   setdefaultt   untitled_directoryt   untitled_notebookt   untitled_fileRd   RG   t   new(   R   R)   RF   R`   R(   RY   R    Rc   (    (    sA   lib/python2.7/site-packages/notebook/services/contents/manager.pyt   new_untitledf  s,    					c         C   sÓ   | j  d ƒ } | d k r$ i  } n  | j d ƒ rF | j d d ƒ n | j d d ƒ d | k r½ | d d k r½ | d d k rœ t ƒ  | d <d | d	 <q½ d
 | d <d | d <d | d	 <n  |  j | | ƒ } | S(   s¢   Create a new file or directory and return its model with no content.
        
        To create a new untitled entity in a directory, use `new_untitled`.
        R   s   .ipynbRF   Ro   Rp   RE   Rq   Rj   RG   RB   t   textN(   RN   t   Nonet   endswithRs   R	   RI   (   R   R(   R)   (    (    sA   lib/python2.7/site-packages/notebook/services/contents/manager.pyRw   Œ  s    	

c   	      C   s*  | j  d ƒ } | d k	 r- | j  d ƒ } n  d | k rT | j d d ƒ \ } } n d } | } |  j | ƒ } | j d d ƒ | j d d ƒ | d d k r± t d d	 ƒ ‚ n  | d k rÆ | } n  |  j | ƒ rt j d
 | ƒ } |  j	 | | d d ƒ} d j
 | | ƒ } n  |  j | | ƒ } | S(   s  Copy an existing file and return its new model.

        If to_path not specified, it will be the parent directory of from_path.
        If to_path is a directory, filename will increment `from_path-Copy#.ext`.

        from_path must be a full path to a file.
        R   i   RB   R)   Rc   RF   Rq   i  s   Can't copy directoriesu   .RY   s   -Copyu   {0}/{1}N(   RN   Rz   t   rsplitRH   t   popR   R@   t   copy_patt   subRd   RG   RI   (	   R   t	   from_patht   to_pathR)   t   from_dirt	   from_nameR(   Rc   t   to_name(    (    sA   lib/python2.7/site-packages/notebook/services/contents/manager.pyt   copy§  s(    	c         C   s   |  j  j |  j ƒ  ƒ d  S(   N(   R,   t   infoRU   (   R   (    (    sA   lib/python2.7/site-packages/notebook/services/contents/manager.pyt   log_infoÉ  s    c         C   sS   |  j  | ƒ } | d } |  j j d | ƒ |  j j | t ƒ |  j | | ƒ d S(   s„   Explicitly trust a notebook

        Parameters
        ----------
        path : string
            The path of a notebook
        RE   s   Trusting notebook %sN(   RH   R,   t   warningt   notaryt
   mark_cellsR0   t   check_and_sign(   R   R)   R(   t   nb(    (    sA   lib/python2.7/site-packages/notebook/services/contents/manager.pyt   trust_notebookÌ  s
    
c         C   s<   |  j  j | ƒ r% |  j  j | ƒ n |  j j d | ƒ d S(   s  Check for trusted cells, and sign the notebook.

        Called as a part of saving notebooks.

        Parameters
        ----------
        nb : dict
            The notebook dict
        path : string
            The notebook's path (for logging)
        s   Notebook %s is not trustedN(   R‰   t   check_cellsR   R,   Rˆ   (   R   RŒ   R)   (    (    sA   lib/python2.7/site-packages/notebook/services/contents/manager.pyR‹   Ú  s    c         C   sE   |  j  j | ƒ } | s. |  j j d | ƒ n  |  j  j | | ƒ d S(   s$  Mark cells as trusted if the notebook signature matches.

        Called as a part of loading notebooks.

        Parameters
        ----------
        nb : dict
            The notebook object (in current nbformat)
        path : string
            The notebook's path (for logging)
        s   Notebook %s is not trustedN(   R‰   t   check_signatureR,   Rˆ   RŠ   (   R   RŒ   R)   t   trusted(    (    sA   lib/python2.7/site-packages/notebook/services/contents/manager.pyt   mark_trusted_cellsë  s    c            s   t  ‡  f d †  |  j Dƒ ƒ S(   s:   Should this file/directory name be displayed in a listing?c         3   s   |  ] } t  ˆ  | ƒ Vq d  S(   N(   R    (   t   .0t   glob(   Rc   (    sA   lib/python2.7/site-packages/notebook/services/contents/manager.pys	   <genexpr>þ  s    (   t   anyt
   hide_globs(   R   Rc   (    (   Rc   sA   lib/python2.7/site-packages/notebook/services/contents/manager.pyt   should_listü  s    c         C   s   |  j  j |  | ƒ S(   s   Create a checkpoint.(   R3   t   create_checkpoint(   R   R)   (    (    sA   lib/python2.7/site-packages/notebook/services/contents/manager.pyR—     s    c         C   s   |  j  j |  | | ƒ d S(   s'   
        Restore a checkpoint.
        N(   R3   t   restore_checkpoint(   R   t   checkpoint_idR)   (    (    sA   lib/python2.7/site-packages/notebook/services/contents/manager.pyR˜     s    c         C   s   |  j  j | ƒ S(   N(   R3   t   list_checkpoints(   R   R)   (    (    sA   lib/python2.7/site-packages/notebook/services/contents/manager.pyRš     s    c         C   s   |  j  j | | ƒ S(   N(   R3   t   delete_checkpoint(   R   R™   R)   (    (    sA   lib/python2.7/site-packages/notebook/services/contents/manager.pyR›     s    N(C   t   __name__t
   __module__t   __doc__R   R0   t   root_dirR   RS   t   allow_hiddenR   R   R   R‰   R   R   R•   R   Ru   Rv   Rt   R   Rz   R"   R   R'   R2   R   R   R4   R3   R   R5   R   R6   R8   R   R   R:   R<   R>   R@   RA   RC   RD   RH   RI   RJ   RM   RP   RR   RT   RU   RV   Rd   Rn   Rx   Rw   R…   R‡   R   R‹   R‘   R–   R—   R˜   Rš   R›   (    (    (    sA   lib/python2.7/site-packages/notebook/services/contents/manager.pyR   (   sv   											
							
						!	
&"						(+   Rž   R    R\   Rj   t   ost   ret   tornado.webR   R   t   files.handlersR   R3   R   t   traitlets.config.configurableR   t   nbformatR   R   Ri   R   t   nbformat.v4R	   t   ipython_genutils.importstringR
   t	   traitletsR   R   R   R   R   R   R   R   R   t   ipython_genutils.py3compatR   t   notebook.base.handlersR   t   notebook.transutilsR   t   compileR~   R   (    (    (    sA   lib/python2.7/site-packages/notebook/services/contents/manager.pyt   <module>   s$   F