
    Xh;                        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y)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 ]C  \  }}}|j                  |D cg c]"  }t         j                  j	                  ||      $ c}       E |S c c}w )z'Find the resource files in a directory.)oswalkextendpathjoin)output_files_dirfilesdirpath_	filenamesfs         U/var/www/html/myenv/lib/python3.12/site-packages/jupyter_server/nbconvert/handlers.pyfind_resource_filesr      sZ    E!#)9!: DI	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                  ||j                  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writestrencodeitemsbasenameclosefinishgetvalue)handlernameoutput	resourcesoutput_fileszip_filenamebufferzipfoutput_filenamefilenamedatas              r   respond_ziprA   &   s"    ==D1L 77##D)!,v5L!!,/~'89(XY ZZ\F??69M9MNDgg&&t,Q/)<N2OOOMM/6==#9:&,,. 8$bgg&&x0$78JJLNN6??$%    c                 b   	 ddl m} 	  ||       }	  |d	i |S # t        $ r}t        j                  dd|z        |d}~ww xY w# t
        $ r}t        j                  dd| z        |d}~ww xY w# t        $ r5}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: %s )	nbconvert.exporters.baserD   ImportErrorr   	HTTPErrorKeyError	Exceptionr   	exception)formatkwargsrD   eexporters        r   rD   rD   D   s    N9O'
P!&!!  NmmC!AA!EFAMN
  OmmC!=!FGQNO  P<hGmmC!Ca!GHaOPs@    A A0 	A=A	A-A((A-0	B.90B))B.c                   F    e Zd ZdZeZdZej                  e	d               Z
y)NbconvertFileHandlerzAn nbconvert file handler.)GETc                    K   | j                          t        || j                  | j                        |j	                  d      }t        | j                  d      r>| j                  j                  |      }t        j                  j                  |      \  }}nd}t        | j                  j                  |             d{   }|d   }|d   dk7  rt        j                  | |      S |d	   | j                  d
|d          |d   j!                  t"              }t        j                  j%                  |      d   }	|	|d| j&                  j(                  d   d|r|d   d<   	 t+        fd       d{   \  }
}t5        | ||
|      ry| j7                  dd      j9                         dk(  r9t        j                  j%                  |      d   |d   z   }| j;                  |       j<                  r| j                  ddj<                  z         | j                  dd       | j?                  |
       y7 {7 # t,        $ r;}| j                  j/                  d|       t1        j2                  dd|z        |d}~ww xY ww)z(Get a notebook file in a desired format.)configlog/_get_os_pathN)r   r7   typenotebookcontentzLast-Modifiedlast_modifiedr   )r7   modified_date
config_dirmetadatar^   r`   r   c                  *     j                        S )Nr9   )from_notebook_node)rP   nbresource_dicts   r   <lambda>z*NbconvertFileHandler.get.<locals>.<lambda>   s    33B-3P rB   znbconvert failed: %rrE   nbconvert failed: %sdownloadfalsetruer%   r   %s; charset=utf-8r    r!   ) check_xsrf_cookierD   rU   rV   striphasattrcontents_managerrX   r   r   splitr   r&   r
   redirect_to_filesr)   strftimedate_formatr'   applicationsettingsr   rK   rL   r   rI   rA   get_argumentlowerr(   output_mimetyper4   )selfrM   r   os_pathext_resources_dirr2   modelr7   mod_datenb_titler8   r9   rO   r?   rP   rd   re   s                 @@@r   r&   zNbconvertFileHandler.get_   s]     	 t{{Izz# 4((.9++88>G*,''--*@'x $"4#8#8#<#<$#<#GHHV}=J&'99$EE9)?@ )22;?77##D)!, "*HE**33LA

 0AM*%f-	H&.P' !FI tT695 Z1779VCww''-a09=O3PPH&&x0 ##OON,?(BZBZ,Z[)YZFY I0!  	HHH5q9--%;a%?@aG	HsJ   CJIB$J-I  II B;JI 	J6JJJN)__name__
__module____qualname____doc__AUTH_RESOURCEauth_resourceSUPPORTED_METHODSr   authenticatedr   r&   rF   rB   r   rR   rR   Y   s0    $!M :  :rB   rR   c                   F    e Zd ZdZdZeZej                  e	d               Z
y)NbconvertPostHandlerzAn nbconvert post handler.)POSTc                    K   t        | j                         j                         }|J |j                  dd      t	        |d         	 t         fd       d{   \  }}t         ||      ryj                  r j                  d	d
j                  z          j                  |       y7 S# t        $ r}t        j                  dd|z        |d}~ww xY ww)z,Convert a notebook file to a desired format.)rU   Nr7   znotebook.ipynbr[   c            	           j                  dd j                  d       ij                  j                  d   d      S )Nr7   .r^   r_   rb   )rc   rfindrt   ru   )rP   r7   nbnodery   s   r   rf   z+NbconvertPostHandler.post.<locals>.<lambda>   sK    33%+T2CDJJsO-D$E&*&6&6&?&?&M 4  rB   rE   rg   r   rk   )rD   rU   get_json_bodyr&   r   r   rK   r   rI   rA   rx   r)   r4   )	ry   rM   r|   r8   r9   rO   rP   r7   r   s	   `     @@@r   postzNbconvertPostHandler.post   s       t{{;""$   yy!125+,	H&.' !FI tT695 ##OON,?(BZBZ,Z[F'!  	H--%;a%?@aG	Hs=   AC(B= 'B;(B= /AC(;B= =	C%C  C%%C(N)r   r   r   r   r   r   r   r   r   r   r   rF   rB   r   r   r      s0    $!!M  rB   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.auth.decoratorr   base.handlersr
   r   r   r   platformrs   r   rA   rD   rR   r   _format_regexdefault_handlersrF   rB   r   <module>r      s    % 
 	 
  $ +    4 L L <<7KK<P*B> BJ%> %X # %';<M?:,/1EF rB   