o
    i@                     @   s   d 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mZmZ dZejdkrJdZnd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)run_sync)ensure_async)	from_dict)web)app_log)
authorized   )FilesRedirectHandlerJupyterHandler
path_regex	nbconvertwin32z	%B %d, %Yz
%B %-d, %Yc                    s6   g }t | D ]\ }}| fdd|D  q|S )z'Find the resource files in a directory.c                    s   g | ]	}t j |qS  )ospathjoin).0fdirpathr   \/var/www/edux/Edux_v2/venv/lib/python3.10/site-packages/jupyter_server/nbconvert/handlers.py
<listcomp>!   s    z'find_resource_files.<locals>.<listcomp>)r   walkextend)output_files_dirfiles_	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  }|||d | D ]\}	}
|tj|	|
 qK|  | |  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.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writestrencodeitemsbasenameclosefinishgetvalue)handlernameoutput	resourcesoutput_fileszip_filenamebufferzipfoutput_filenamefilenamedatar   r   r   respond_zip%   s    
rB   c              
   K   s   zddl m} W n ty } z	tdd| |d}~ww z|| }W n ty9 } z	tdd|  |d}~ww z|d	i |W S  ty] } ztd| tdd| |d}~ww )
z+get an exporter, raising appropriate errorsr   )get_exporter  zCould not import nbconvert: %sNi  zNo exporter for format: %sz Could not construct Exporter: %sr   )	nbconvert.exporters.baserC   ImportErrorr   	HTTPErrorKeyError	Exceptionr   	exception)formatkwargsrC   eExporterr   r   r   rC   C   s&   rC   c                   @   s*   e Zd ZdZeZdZeje	dd Z
dS )NbconvertFileHandlerzAn nbconvert file handler.)GETc              
      s  |    t|| j| jd |d}t| jdr(| j|}tj	
|\}}nd}t| jj|dI dH }|d }|d dkrFt| |S |d	 | d
|d  |d t}tj	|d }	|	|d| jjd d|ru|d d< zt fddI dH \}
}W n ty } z| jd| tdd| |d}~ww t| ||
|rdS | dd dkrtj	|d |d  }| |  jr| dd j  | dd | |
 dS )z(Get a notebook file in a desired format.)configlog/_get_os_pathN)r   r8   typenotebookcontentzLast-Modifiedlast_modifiedr   )r8   modified_date
config_dirmetadatarZ   r\   r   c                      s    j dS )Nr:   )from_notebook_noder   exporternbresource_dictr   r   <lambda>   s    z*NbconvertFileHandler.get.<locals>.<lambda>znbconvert failed: %rrD   nbconvert failed: %sdownloadfalsetruer&   r    %s; charset=utf-8r!   r"   ) check_xsrf_cookierC   rQ   rR   striphasattrcontents_managerrT   r   r   splitr   r'   r	   redirect_to_filesr*   strftimedate_formatr(   applicationsettingsr   rI   rJ   r   rG   rB   get_argumentlowerr)   output_mimetyper5   )selfrK   r   os_pathext_resources_dirr3   modelr8   mod_datenb_titler9   r:   rM   r@   r   r_   r   r'   ^   sN   


zNbconvertFileHandler.getN)__name__
__module____qualname____doc__AUTH_RESOURCEauth_resourceSUPPORTED_METHODSr   authenticatedr   r'   r   r   r   r   rO   X       rO   c                   @   s*   e Zd ZdZdZeZeje	dd Z
dS )NbconvertPostHandlerzAn nbconvert post handler.)POSTc              
      s   t |jd  }|ddt|d zt fddI dH \}}W n ty? } z	tdd	| |d}~ww t	||rIdS  j
rUd
d j
  | dS )z,Convert a notebook file to a desired format.)rQ   r8   znotebook.ipynbrW   c                      s.    j dd d ijjd ddS )Nr8   .rZ   r[   r]   )r^   rfindrq   rr   r   r`   r8   nbnoderv   r   r   rc      s    
z+NbconvertPostHandler.post.<locals>.<lambda>NrD   rd   r    rh   )rC   rQ   get_json_bodyr'   r   r   rI   r   rG   rB   ru   r*   r5   )rv   rK   ry   r9   r:   rM   r   r   r   post   s$   	zNbconvertPostHandler.postN)r|   r}   r~   r   r   r   r   r   r   r   r   r   r   r   r   r      r   r   z(?P<format>\w+)z/nbconvert/%sz/nbconvert/)r   r+   r   sysr-   anyio.to_threadr   jupyter_core.utilsr   nbformatr   tornador   tornado.logr   jupyter_server.authr   base.handlersr	   r
   r   r   platformrp   r   rB   rC   rO   r   _format_regexdefault_handlersr   r   r   r   <module>   s2    
E+
