Ë
    :vžj   ã                   ó  — 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/workspace/moltex-experiments/.pixi/envs/default/lib/python3.12/site-packages/jupyter_server/nbconvert/handlers.pyÚfind_resource_filesr      sW   € à€EÜ!#§¡Ð)9Ö!:Ñˆ��IØ�‰¹	ÓB¹	°1”b—g‘g—l‘l 7¨AÕ.¸	Ñ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)zÏZip 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Ó1€LÙØô —7‘7×#Ñ# DÓ)¨!Ñ,¨vÑ5€LØ×!Ñ! ,Ô/Ø×Ñ�~Ð'8Ô9Ø×Ñ�Ð(XÔYô �Z‰Z‹\€FÜ�?‰?˜6¨¼×9MÑ9MÔN€DÜ—g‘g×&Ñ& tÓ,¨QÑ/°)Ð<NÑ2OÑO€OØ‡M�M�/ 6§=¡=°Ó#9Ô:Ø&×,Ñ,Ö.‰ˆ�$Ø�‰”b—g‘g×&Ñ& xÓ0°$Õ7ð /à‡J�J„Là‡N�N�6—?‘?Ó$Ô%Øó    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Ü�m‰m˜CÐ!AÀAÑ!EÓFÈAÐMûðNûô
 ò Oä�m‰m˜CÐ!=ÀÑ!FÓGÈQÐNûðOûô ò PÜ×ÑÐ<¸hÔGÜ�m‰m˜CÐ!CÀaÑ!GÓHÈaÐ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   ø€ ð �=‰=×ÑÐ4°dÔ;Ü‘7Ñ2Ð5NÑ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Ñ3°BÀ-Ð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¯©Ó)9¸VÑ)C�Ô&à�z‰z˜#‹ˆô �4×(Ñ(¨.Ô9Ø×+Ñ+×8Ñ8¸Ó>ˆGÜ+-¯7©7¯=©=¸Ó+AÑ(Ð™yà $Ðä" 4×#8Ñ#8×#<Ñ#<À$Ð#<Ó#GÓH×HˆØ�V‰}ˆØ�‰=˜JÒ&ä'×9Ñ9¸$ÀÓEÐEà�9Ñˆà�‰˜¨¨Ñ)?Ô@ð ˜Ñ)×2Ñ2´;Ó?ˆÜ—7‘7×#Ñ# DÓ)¨!Ñ,ˆð "*¸HÑEØ×*Ñ*×3Ñ3°LÑAñ
ˆñ
 Ø0AˆM˜*Ñ% fÑ-ð	HÜ&.ÝPó'÷ !ÑˆF�Iô �t˜T 6¨9Ô5Øð ×Ñ˜Z¨Ó1×7Ñ7Ó9¸VÒCÜ—w‘w×'Ñ'¨Ó-¨aÑ0°9Ð=OÑ3PÑPˆHØ×&Ñ& xÔ0ð ×#Ò#Ø�O‰O˜NÐ,?À(×BZÑBZÑ,ZÔ[à�‰˜Ð)YÔZØ�‰�FÕðY Iùð0!ùô ò 	HØ�H‰H×ÑÐ5°qÔ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Øà%+¨TÐ2C°D·J±J¸s³OÐ-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¯{©{Ô;ˆà×"Ñ"Ó$ˆØÐ Ð Ð Ø�y‰y˜Ð!1Ó2ˆÜ˜5 Ñ+Ó,ˆð	HÜ&.öó'÷ !ÑˆF�Iô �t˜T 6¨9Ô5Øð ×#Ò#Ø�O‰O˜NÐ,?À(×BZÑBZÑ,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   