ó
ù2¢\c           @   s  d  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
 m Z m Z d d l m Z d d l m Z d d	 l m Z d
 „  Z d „  Z d „  Z d e
 f d „  ƒ  YZ d e
 f d „  ƒ  YZ d Z d e e f d e e f e f g Z d S(   s   Tornado handlers for nbconvert.iÿÿÿÿN(   t   webt   escape(   t   app_logi   (   t   IPythonHandlert   FilesRedirectHandlert
   path_regex(   t	   from_dict(   t
   cast_bytes(   t   textc         C   s\   g  } xO t  j |  ƒ D]> \ } } } | j g  | D] } t  j j | | ƒ ^ q2 ƒ q W| S(   N(   t   ost   walkt   extendt   patht   join(   t   output_files_dirt   filest   dirpatht   dirnamest	   filenamest   f(    (    s:   lib/python2.7/site-packages/notebook/nbconvert/handlers.pyt   find_resource_files   s    3c         C   s  | j  d d ƒ } | s t St j j | ƒ d d } |  j | ƒ |  j d d ƒ |  j d d ƒ t j	 ƒ  } t
 j | d d	 d
 t
 j ƒ} t j j | ƒ d | d } | j | t | d ƒ ƒ x6 | j ƒ  D]( \ }	 }
 | j t j j |	 ƒ |
 ƒ qÑ W| j ƒ  |  j | j ƒ  ƒ t S(   sÏ   Zip up the output and resource files and respond with the zip file.

    Returns True if it has served a zip file, False if there are no resource
    files, in which case we serve the plain output file.
    t   outputsi    s   .zips   Content-Types   application/zips   Cache-Controls.   no-store, no-cache, must-revalidate, max-age=0t   modet   wt   compressiont   output_extensions   utf-8N(   t   gett   Nonet   FalseR	   R   t   splitextt   set_attachment_headert
   set_headert   iot   BytesIOt   zipfilet   ZipFilet   ZIP_DEFLATEDt   writestrR   t   itemst   basenamet   closet   finisht   getvaluet   True(   t   handlert   namet   outputt	   resourcest   output_filest   zip_filenamet   buffert   zipft   output_filenamet   filenamet   data(    (    s:   lib/python2.7/site-packages/notebook/nbconvert/handlers.pyt   respond_zip   s      
c         K   sÈ   y d d l  m } Wn) t k
 r? } t j d d | ƒ ‚ n Xy | |  ƒ } Wn' t k
 ry t j d d |  ƒ ‚ n Xy | |   SWn9 t k
 rÃ } t j d | ƒ t j d d | ƒ ‚ n Xd S(	   s+   get an exporter, raising appropriate errorsiÿÿÿÿ(   t   get_exporteriô  s   Could not import nbconvert: %si”  u   No exporter for format: %ss    Could not construct Exporter: %sN(	   t   nbconvert.exporters.baseR8   t   ImportErrorR    t	   HTTPErrort   KeyErrort	   ExceptionR   t	   exception(   t   formatt   kwargsR8   t   et   Exporter(    (    s:   lib/python2.7/site-packages/notebook/nbconvert/handlers.pyR8   9   s    t   NbconvertFileHandlerc           B   s/   e  Z d Z e d „  ƒ Z e j d „  ƒ Z RS(   t   GETc         C   s   t  t |  ƒ j d S(   Ns   ; sandbox allow-scripts(   t   superRC   t   content_security_policy(   t   self(    (    s:   lib/python2.7/site-packages/notebook/nbconvert/handlers.pyRF   Q   s    c         C   sE  t  | d |  j d |  j ƒ} | j d ƒ } t |  j d ƒ rl |  j j | ƒ } t j j	 | ƒ \ } } n d  } |  j j d | ƒ } | d } | d d k r± t j |  | ƒ S| d	 }	 |  j d
 | d ƒ | d j t j ƒ }
 t j j | ƒ d } i i | d 6|
 d 6d 6|  j j d d 6} | r>| | d d <n  y | j |	 d | ƒ\ } } Wn< t k
 r›} |  j j d | ƒ t j d d | ƒ ‚ n Xt |  | | | ƒ rµd  S|  j d d ƒ j ƒ  d k rt j j | ƒ d | d } |  j | ƒ n  | j r$|  j d d | j ƒ n  |  j d d ƒ |  j | ƒ d  S(   Nt   configt   logt   /t   _get_os_pathR   R-   t   typet   notebookt   contents   Last-Modifiedt   last_modifiedi    t   modified_datet   metadatat
   config_dirR/   s   nbconvert failed: %siô  t   downloadt   falset   trueR   s   Content-Types   %s; charset=utf-8s   Cache-Controls.   no-store, no-cache, must-revalidate, max-age=0(    R8   RH   RI   t   stript   hasattrt   contents_managerRK   R	   R   t   splitR   R   R   t   redirect_to_filesR   t   strftimeR   t   date_formatR   t   applicationt   settingst   from_notebook_nodeR=   R>   R    R;   R7   t   get_argumentt   lowerR   t   output_mimetypeR)   (   RG   R?   R   t   exportert   os_patht   ext_resources_dirR'   t   modelR-   t   nbt   mod_datet   nb_titlet   resource_dictR.   R/   RA   R5   (    (    s:   lib/python2.7/site-packages/notebook/nbconvert/handlers.pyR   X   sL    

		(   RD   (   t   __name__t
   __module__t   SUPPORTED_METHODSt   propertyRF   R    t   authenticatedR   (    (    (    s:   lib/python2.7/site-packages/notebook/nbconvert/handlers.pyRC   M   s   t   NbconvertPostHandlerc           B   s/   e  Z d Z e d „  ƒ Z e j d „  ƒ Z RS(   t   POSTc         C   s   t  t |  ƒ j d S(   Ns   ; sandbox allow-scripts(   RE   Rp   RF   (   RG   (    (    s:   lib/python2.7/site-packages/notebook/nbconvert/handlers.pyRF   ›   s    c   	      C   s  t  | d |  j ƒ} |  j ƒ  } | j d d ƒ } t | d ƒ } yK | j | d i i | | j d ƒ  d 6d 6|  j j d d 6ƒ\ } } Wn) t	 k
 r¹ } t
 j d	 d
 | ƒ ‚ n Xt |  | | | ƒ rÓ d  S| j rö |  j d d | j ƒ n  |  j | ƒ d  S(   NRH   R-   s   notebook.ipynbRN   R/   t   .RQ   RR   iô  s   nbconvert failed: %ss   Content-Types   %s; charset=utf-8(   R8   RH   t   get_json_bodyR   R   R_   t   rfindR]   R^   R=   R    R;   R7   Rb   R   R)   (	   RG   R?   Rc   Rf   R-   t   nbnodeR.   R/   RA   (    (    s:   lib/python2.7/site-packages/notebook/nbconvert/handlers.pyt   post¢   s     !		(   Rq   (   Rk   Rl   Rm   Rn   RF   R    Ro   Rv   (    (    (    s:   lib/python2.7/site-packages/notebook/nbconvert/handlers.pyRp   ˜   s   s   (?P<format>\w+)s   /nbconvert/%ss   /nbconvert/%s%s(   t   __doc__R    R	   R"   t   tornadoR    R   t   tornado.logR   t   base.handlersR   R   R   t   nbformatR   t   ipython_genutils.py3compatR   t   ipython_genutilsR   R   R7   R8   RC   Rp   t   _format_regext   default_handlers(    (    (    s:   lib/python2.7/site-packages/notebook/nbconvert/handlers.pyt   <module>   s$   			K)