
    lKj                         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         r/Users/ahmed/devFolder/Ultron/claude-voice/.venv/lib/python3.12/site-packages/jupyter_server/nbconvert/handlers.pyfind_resource_filesr      sW    E!#)9!:I	B	1bggll7A.	BC ";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:&,,.$bgg&&x0$7 /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                   b     e Zd ZdZeZdZe fd       Ze	j                  ed               Z xZS )NbconvertFileHandlerzAn nbconvert file handler.)GETc                 j    | j                   j                  dd      rt        |   dz   S t        |   S Nnbconvert_csp_sandboxTz; sandbox allow-scriptssettingsr&   supercontent_security_policyself	__class__s    r   rZ   z,NbconvertFileHandler.content_security_policy_   6     ==4d;725NNNw..rB   c                   K   | j                          t        || j                  | j                        |dk(  r,| j	                  dd      }||j                         dk(  _        |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<   	 t1        fd       d{   \  }}t;        | |||      ry| j	                  dd      j                         dk(  r9t        j                  j+                  |      d   |d   z   }| j=                  |       j>                  r| j%                  ddj>                  z         | j%                  dd       | jA                  |       y7 {7 # t2        $ r;}| j                  j5                  d|       t7        j8                  dd|z        |d}~ww xY ww) a5  Get a notebook file in a desired format.

        Parameters
        ----------
        download: bool, optional
            If true, set Content-Disposition: attachment
        sanitize_html: bool, optional (html format only)
            If true, sanitize HTML (sets sanitize_html flag on nbconvert)
        )configloghtmlsanitize_htmlNtrue/_get_os_path)r   r7   typenotebookcontentzLast-Modifiedlast_modifiedr   )r7   modified_date
config_dirmetadatarl   rn   r   c                  *     j                        S )Nr9   )from_notebook_node)rP   nbresource_dicts   r   <lambda>z*NbconvertFileHandler.get.<locals>.<lambda>   s    33B-3PrB   znbconvert failed: %rrE   nbconvert failed: %sdownloadfalser%   r   %s; charset=utf-8r    r!   )!check_xsrf_cookierD   r`   ra   get_argumentlowerrc   striphasattrcontents_managerrf   r   r   splitr   r&   r
   redirect_to_filesr)   strftimedate_formatr'   applicationrX   r   rK   rL   r   rI   rA   r(   output_mimetyper4   )r\   rM   r   sanitizeos_pathext_resources_dir	_basenamemodelr7   mod_datenb_titler8   r9   rO   r?   rP   rr   rs   s                  @@@r   r&   zNbconvertFileHandler.getg   s     	 t{{IV(($?H#)1)9V)C&zz# 4((.9++88>G+-77==+A(y $"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   C3J?8I39B$J?I8 1I62I8 9B;J?6I8 8	J<6J77J<<J?)__name__
__module____qualname____doc__AUTH_RESOURCEauth_resourceSUPPORTED_METHODSpropertyrZ   r   authenticatedr   r&   __classcell__r]   s   @r   rR   rR   Y   sG    $!M / / 	F  FrB   rR   c                   b     e Zd ZdZdZeZe fd       Ze	j                  ed               Z xZS )NbconvertPostHandlerzAn nbconvert post handler.)POSTc                 j    | j                   j                  dd      rt        |   dz   S t        |   S rU   rW   r[   s    r   rZ   z,NbconvertPostHandler.content_security_policy   r^   rB   c                    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.)r`   Nr7   znotebook.ipynbri   c            	           j                  dd j                  d       ij                  j                  d   d      S )Nr7   .rl   rm   rp   )rq   rfindr   rX   )rP   r7   nbnoder\   s   r   rt   z+NbconvertPostHandler.post.<locals>.<lambda>   sI    33%+T2CDJJsO-D$E&*&6&6&?&?&M 4 rB   rE   ru   r   rx   )rD   r`   get_json_bodyr&   r   r   rK   r   rI   rA   r   r)   r4   )	r\   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()r   r   r   r   r   r   r   r   rZ   r   r   r   r   r   r   s   @r   r   r      sE    $!!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   platformr   r   rA   rD   rR   r   _format_regexdefault_handlersrF   rB   r   <module>r      s    % 
 	 
  $ +    4 L L <<7KK<P*V> Vr-> -h # %';<M?:,/1EF rB   