B
    9dm                 @   s   d Z ddlZddlZddlZddlmZ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d
lmZ dd Zdd Zdd ZG dd deZG dd deZdZde efde e efgZdS )zTornado handlers for nbconvert.    N)genwebescape)app_log   )IPythonHandlerFilesRedirectHandler
path_regex)maybe_future)	from_dict)
cast_bytes)textc                s:   g }x0t | D ]"\ }}| fdd|D  qW |S )Nc                s   g | ]}t j |qS  )ospathjoin).0f)dirpathr   h/work/yifan.wang/ringdown/master-ringdown-env/lib/python3.7/site-packages/notebook/nbconvert/handlers.py
<listcomp>   s    z'find_resource_files.<locals>.<listcomp>)r   walkextend)Zoutput_files_dirfilesdirnames	filenamesr   )r   r   find_resource_files   s    r   c             C   s   | dd}|sdS tj|d d }| | | dd | dd	 t }tj	|d
tj
d}tj|d |d  }||t|d x(| D ]\}	}
|tj|	|
 qW |  | |  dS )zZip 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.
    outputsNFr   z.zipzContent-Typezapplication/zipzCache-Controlz.no-store, no-cache, must-revalidate, max-age=0w)modecompressionoutput_extensionzutf-8T)getr   r   splitextset_attachment_header
set_headerioBytesIOzipfileZipFileZIP_DEFLATEDwritestrr   itemsbasenameclosefinishgetvalue)handlernameoutput	resourcesZoutput_fileszip_filenamebufferZzipfZoutput_filenamefilenamedatar   r   r   respond_zip   s     
r9   c          
   K   s   yddl m} W n6 tk
rF } ztdd| |W dd}~X Y nX y|| }W n6 tk
r } ztdd|  |W dd}~X Y nX y
|f |S  tk
r } z$td| tdd	| |W dd}~X Y nX dS )
z+get an exporter, raising appropriate errorsr   )get_exporteri  zCould not import nbconvert: Ni  zNo exporter for format: z Could not construct Exporter: %szCould not construct Exporter: )	Znbconvert.exporters.baser:   ImportErrorr   	HTTPErrorKeyError	Exceptionr   	exception)formatkwargsr:   eZExporterr   r   r   r:   :   s    &&
r:   c                   s8   e Zd ZdZe fddZejej	dd Z
  ZS )NbconvertFileHandler)GETc                s   t  jd S )Nz; sandbox allow-scripts)supercontent_security_policy)self)	__class__r   r   rF   R   s    z,NbconvertFileHandler.content_security_policyc          
   c   s  t || j| jd}|d}t| jdrF| j|}tj	|\}}nd }t
| jj|dV }|d }|d dkr~t| |S |d }	| d	|d
  |d
 tj}
tj|d }||
d| jjd d}|r||d d< y|j|	|d\}}W nF tk
r: } z&| jd| tdd| |W d d }~X Y nX t| |||rPd S | dd dkrtj|d |d  }| | |jr| d|j d | dd | | d S )N)configlog/_get_os_path)r   r2   typenotebookcontentzLast-ModifiedZlast_modifiedr   )r2   Zmodified_date
config_dir)metadatarP   rQ   r   )r4   znbconvert failed: %si  znbconvert failed: downloadfalsetruer!   zContent-Typez; charset=utf-8zCache-Controlz.no-store, no-cache, must-revalidate, max-age=0) r:   rI   rJ   striphasattrZcontents_managerrL   r   r   splitr
   r"   r   Zredirect_to_filesr%   strftimer   Zdate_formatr#   applicationsettingsfrom_notebook_noder>   r?   r   r<   r9   Zget_argumentlowerr$   output_mimetyper/   )rG   r@   r   exporterZos_pathZext_resources_dirr-   modelr2   nbZmod_dateZnb_titleZresource_dictr3   r4   rB   r7   r   r   r   r"   X   sH    
&
zNbconvertFileHandler.get)__name__
__module____qualname__SUPPORTED_METHODSpropertyrF   r   authenticatedr   	coroutiner"   __classcell__r   r   )rH   r   rC   N   s   rC   c                   s2   e Zd ZdZe fddZejdd Z  Z	S )NbconvertPostHandler)POSTc                s   t  jd S )Nz; sandbox allow-scripts)rE   rF   )rG   )rH   r   r   rF      s    z,NbconvertPostHandler.content_security_policyc       	   
   C   s   t || jd}|  }|dd}t|d }y6|j|d|d |d i| jjd dd\}}W n6 t	k
r } zt
d	d
| |W d d }~X Y nX t| |||rd S |jr| d|j d | | d S )N)rI   r2   znotebook.ipynbrO   .rP   )rQ   rP   )r4   i  znbconvert failed: zContent-Typez; charset=utf-8)r:   rI   Zget_json_bodyr"   r   r[   rfindrY   rZ   r>   r   r<   r9   r]   r%   r/   )	rG   r@   r^   r_   r2   Znbnoder3   r4   rB   r   r   r   post   s     &zNbconvertPostHandler.post)
ra   rb   rc   rd   re   rF   r   rf   rm   rh   r   r   )rH   r   ri      s   ri   z(?P<format>\w+)z/nbconvert/)__doc__r&   r   r(   tornador   r   r   Ztornado.logr   Zbase.handlersr   r   r	   utilsr
   Znbformatr   Zipython_genutils.py3compatr   Zipython_genutilsr   r   r9   r:   rC   ri   Z_format_regexZdefault_handlersr   r   r   r   <module>   s$   K(