
    Ibm                         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 Zd Zd Z G d de      Z G d de      ZdZde efde e efgZy)zTornado handlers for nbconvert.    N)genwebescape)app_log   )IPythonHandlerFilesRedirectHandler
path_regex)maybe_future)	from_dict)
cast_bytes)textc           
          g }t        j                  |       D ]C  \  }}}|j                  |D cg c]"  }t         j                  j	                  ||      $ c}       E |S c c}w )N)oswalkextendpathjoin)output_files_dirfilesdirpathdirnames	filenamesfs         =/usr/lib/python3/dist-packages/notebook/nbconvert/handlers.pyfind_resource_filesr      sZ    E(*0@(A D$9	B1bggll7A.BCDL Cs   'A 
c                    |j                  dd      }|syt        j                  j                  |      d   dz   }| j	                  |       | j                  dd       | j                  dd	       t        j                         }t        j                  |d
t        j                        }t        j                  j                  |      d   |d   z   }|j                  |t        |d             |j                         D ]4  \  }	}
|j                  t        j                  j                  |	      |
       6 |j                          | j!                  |j#                                y)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.zipContent-Typezapplication/zipCache-Control.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	resourcesoutput_fileszip_filenamebufferzipfoutput_filenamefilenamedatas              r   respond_zipr@      s     ==D1L 77##D)!,v5L!!,/~'89(XY ZZ\F??69M9MNDgg&&t,Q/)<N2OOOMM/:fg#>?&,,. 8$bgg&&x0$78JJLNN6??$%    c                 b   	 ddl m} 	  ||       }	  |d
i |S # t        $ r}t        j                  dd|       |d}~ww xY w# t
        $ r}t        j                  dd|        |d}~ww xY w# t        $ r5}t        j                  d|       t        j                  dd	|       |d}~ww xY w)z+get an exporter, raising appropriate errorsr   )get_exporter  zCould not import nbconvert: Ni  zNo exporter for format: z Could not construct Exporter: %szCould not construct Exporter:  )	nbconvert.exporters.baserC   ImportErrorr   	HTTPErrorKeyError	Exceptionr   	exception)formatkwargsrC   eExporters        r   rC   rC   :   s    L9M'
N!&!!  LmmC#?s!CD!KL
  MmmC#;F8!DE1LM  N<hGmmC#A!!EFAMNs@    A A0 	A=A	A-A((A-0	B.90B))B.c                   n     e Zd ZdZe fd       Zej                  ej                  d               Z
 xZS )NbconvertFileHandler)GETc                     t         |   dz   S Nz; sandbox allow-scriptssupercontent_security_policyself	__class__s    r   rW   z,NbconvertFileHandler.content_security_policyR        w.1JJJrA   c              #     K   t        || j                  | j                        }|j                  d      }t	        | j
                  d      r>| j
                  j                  |      }t        j                  j                  |      \  }}nd }t        | j
                  j                  |            }|d   }|d   dk7  rt        j                  | |      S |d   }	| j                  d	|d
          |d
   j                  t         j"                        }
t        j                  j%                  |      d   }||
d| j&                  j(                  d   d}|r||d   d<   	 |j+                  |	|      \  }}t5        | |||      ry | j7                  dd      j9                         dk(  r9t        j                  j%                  |      d   |d   z   }| j;                  |       |j<                  r| j                  d|j<                   d       | j                  dd       | j?                  |       y # t,        $ r;}| j                  j/                  d|       t1        j2                  dd|       |d }~ww xY ww)N)configlog/_get_os_path)r   r6   typenotebookcontentzLast-Modifiedlast_modifiedr   )r6   modified_date
config_dirmetadatarf   rh   r   r8   znbconvert failed: %srD   nbconvert failed: downloadfalsetruer%   r   ; charset=utf-8r    r!   ) rC   r]   r^   striphasattrcontents_managerr`   r   r   splitr   r&   r	   redirect_to_filesr)   strftimer   date_formatr'   applicationsettingsfrom_notebook_noderJ   rK   r   rH   r@   get_argumentlowerr(   output_mimetyper3   )rY   rL   r   exporteros_pathext_resources_dirr1   modelr6   nbmod_datenb_titleresource_dictr7   r8   rN   r>   s                    r   r&   zNbconvertFileHandler.getX   sa      t{{Izz# 4((.9++88>G*,''--*@'x $"4#8#8#<#<$#<#GHHV}=J&'99$EE9)?@ )2243C3CD77##D)!, !!) **33LA
 0AM*%f-	F ( ; ;' !< !FI tT695 Z1779VCww''-a09=O3PPH&&x0 ##OON'778HJ 	)YZF%  	FHH5q9--'9!%=>AE	Fs+   EI5H. 4B:I5.	I276I--I22I5)__name__
__module____qualname__SUPPORTED_METHODSpropertyrW   r   authenticatedr   	coroutiner&   __classcell__rZ   s   @r   rQ   rQ   N   sC     K K
 	]]=  =rA   rQ   c                   P     e Zd ZdZe fd       Zej                  d        Z xZ	S )NbconvertPostHandler)POSTc                     t         |   dz   S rT   rU   rX   s    r   rW   z,NbconvertPostHandler.content_security_policy   r[   rA   c           	         t        || j                        }| j                         }|j                  dd      }t	        |d         }	 |j                  |d|d |j                  d       i| j                  j                  d   d      \  }}t        | |||      ry |j                  r| j                  d|j                   d       | j                  |       y # t        $ r}t        j                  d	d
|       |d }~ww xY w)N)r]   r6   znotebook.ipynbrc   .rf   rg   ri   rD   rj   r   rn   )rC   r]   get_json_bodyr&   r   rx   rfindrv   rw   rJ   r   rH   r@   r{   r)   r3   )	rY   rL   r|   r   r6   nbnoder7   r8   rN   s	            r   postzNbconvertPostHandler.post   s   t{{;""$yy!125+,	F ( ; ;F#T*:4::c?%;="..77EO ; !FI tT695 ##OON'778HJ 	F  	F--'9!%=>AE	Fs   	AC 	D"C<<D)
r   r   r   r   r   rW   r   r   r   r   r   s   @r   r   r      s7    !K K
 	 rA   r   z(?P<format>\w+)z/nbconvert/)__doc__r*   r   r,   tornador   r   r   tornado.logr   base.handlersr   r	   r
   utilsr   nbformatr   ipython_genutils.py3compatr   ipython_genutilsr   r   r@   rC   rQ   r   _format_regexdefault_handlersrE   rA   r   <module>r      s    %
 
 	  $ $   !  1 !:N(I> IV!> !P # 	M?#%9:M?:,/1EF rA   