
    Mhq                        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    rdZndZd Zd Zd Z G d de          Z G d de          ZdZdez  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                     g }t          j        |           D ]'\  }}|                    fd|D                        (|S )z'Find the resource files in a directory.c                 P    g | ]"}t           j                            |          #S  )ospathjoin).0fdirpaths     a/var/www/html/test/jupyter/venv/lib/python3.11/site-packages/jupyter_server/nbconvert/handlers.py
<listcomp>z'find_resource_files.<locals>.<listcomp>"   s)    BBB1bgll7A..BBB    )r   walkextend)output_files_dirfiles_	filenamesr   s       @r   find_resource_filesr!      sZ    E!#)9!:!: D DIBBBB	BBBCCCCLr   c                 2   |                     dd          }|sdS t          j                            |          d         dz   }|                     |           |                     dd           |                     dd	           t          j                    }t          j	        |d
t          j
                  }t          j                            |          d         |d         z   }|                    ||                    d                     |                                D ]8\  }	}
|                    t          j                            |	          |
           9|                                 |                     |                                           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datas              r   respond_ziprF   &   sf    ==D11L u 7##D))!,v5L!!,///~'8999(XYYY Z\\F?69MNNNDg&&t,,Q/)<N2OOOMM/6==#9#9:::&,,.. 8 8$bg&&x00$7777JJLLLNN6??$$%%%4r   c                 j   	 ddl m} n+# t          $ r}t          j        dd|z            |d}~ww xY w	  ||           }n+# t
          $ r}t          j        dd| z            |d}~ww xY w	  |d	i |S # t          $ r3}t          j        d|           t          j        dd|z            |d}~ww xY 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.baserH   ImportErrorr   	HTTPErrorKeyError	Exceptionr   	exception)formatkwargsrH   eexporters        r   rH   rH   D   s   N9999999 N N NmC!AA!EFFAMNO<'' O O OmC!=!FGGQNOPx!!&!!! P P P<hGGGmC!Ca!GHHaOPs?   	 
1,1A 
A)A$$A)-A5 5
B2?.B--B2c                   J    e Zd ZdZeZdZej        e	d                         Z
dS )NbconvertFileHandlerzAn nbconvert file handler.)GETc                 |  K   |                                   t          || j        | j                  |                    d          }t          | j        d          r=| j                            |          }t          j	        
                    |          \  }}nd}t          | j                            |                     d{V }|d         }|d         dk    rt          j        | |          S |d	         |                     d
|d                    |d                             t"                    }t          j	                            |          d         }	|	|d| j        j        d         d|r|d         d<   	 t+          fd           d{V \  }
}nF# t,          $ r9}| j                            d|           t1          j        dd|z            |d}~ww xY wt5          | ||
|          rdS |                     dd                                          dk    rCt          j	                            |          d         |d         z   }|                     |           j        r|                     ddj        z             |                     dd           |                     |
           dS )z(Get a notebook file in a desired format.)configlog/_get_os_pathN)r   r<   typenotebookcontentzLast-Modifiedlast_modifiedr   )r<   modified_date
config_dirmetadatara   rc   r   c                  2                                    S )Nr>   )from_notebook_node)rS   nbresource_dicts   r   <lambda>z*NbconvertFileHandler.get.<locals>.<lambda>   s    33B-3PP r   znbconvert failed: %rrI   nbconvert failed: %sdownloadfalsetruer*   r$   %s; charset=utf-8r%   r&   ) check_xsrf_cookierH   rX   rY   striphasattrcontents_managerr[   r   r   splitr   r+   r
   redirect_to_filesr.   strftimedate_formatr,   applicationsettingsr   rN   rO   r   rL   rF   get_argumentlowerr-   output_mimetyper9   )selfrP   r   os_pathext_resources_dirr7   modelr<   mod_datenb_titler=   r>   rR   rD   rS   rg   rh   s                 @@@r   r+   zNbconvertFileHandler.get_   s      	   t{IIIzz# 4(.99 	%+88>>G*,'--*@*@'xx $"4#8#<#<$#<#G#GHHHHHHHHV}=J&&'9$EEE9)?@@@ )22;??7##D))!, "*HEE*3LA
 

  	B0AM*%f-	H&.PPPPPP' ' ! ! ! ! ! !FII  	H 	H 	HH5q999-%;a%?@@aG	H tT6955 	4 Z117799VCCw''--a09=O3PPH&&x000 # 	\OON,?(BZ,Z[[[)YZZZFs   F# #
G&-4G!!G&N)__name__
__module____qualname____doc__AUTH_RESOURCEauth_resourceSUPPORTED_METHODSr   authenticatedr   r+   r   r   r   rU   rU   Y   sL        $$!M : : Z : : :r   rU   c                   J    e Zd ZdZdZeZej        e	d                         Z
dS )NbconvertPostHandlerzAn nbconvert post handler.)POSTc                    K   t          | j                                                   }|J |                    dd          t	          |d                   	 t           fd           d{V \  }}n+# t          $ r}t          j        dd|z            |d}~ww xY wt           ||          rdS j
        r                     d	d
j
        z                                  |           dS )z,Convert a notebook file to a desired format.)rX   Nr<   znotebook.ipynbr^   c            	                                dd                     d                   ij        j        d         d          S )Nr<   .ra   rb   re   )rf   rfindrw   rx   )rS   r<   nbnoder|   s   r   ri   z+NbconvertPostHandler.post.<locals>.<lambda>   sT    33%+T2CDJJsOO2C-D$E&*&6&?&M  4   r   rI   rj   r$   rn   )rH   rX   get_json_bodyr+   r   r   rN   r   rL   rF   r{   r.   r9   )	r|   rP   r   r=   r>   rR   rS   r<   r   s	   `     @@@r   postzNbconvertPostHandler.post   s_       t{;;;""$$   yy!1225+,,	H&.      ' ' ! ! ! ! ! !FII  	H 	H 	H-%;a%?@@aG	H tT6955 	F # 	\OON,?(BZ,Z[[[Fs   !B   
B(
B##B(N)r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r      sL        $$!!M  Z   r   r   z(?P<format>\w+)z/nbconvert/%sz/nbconvert/)r   r/   r   sysr1   anyio.to_threadr   jupyter_core.utilsr   nbformatr   tornador   tornado.logr   jupyter_server.auth.decoratorr   base.handlersr
   r   r   r   platformrv   r!   rF   rH   rU   r   _format_regexdefault_handlersr   r   r   <module>r      s   % % 
			 				 



  $ $ $ $ $ $ + + + + + +                   4 4 4 4 4 4 L L L L L L L L L L <7KKK    <P P P*B B B B B> B B BJ% % % % %> % % %X # %';</M/://1EF   r   