o
    ^i                     @   s   d Z ddl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mZmZmZ ddlmZ dd	lmZ d
ZdZejdkr]zddlZW n ey\   ddlZY nw G dd deZdS )zcModule containing a preprocessor that converts outputs in the notebook from
one format to another.
    N)Path)which)TemporaryDirectory)ListUnicodeUniondefault)FormatSafeDict   )ConvertFiguresPreprocessorz:/Applications/Inkscape.app/Contents/Resources/bin/inkscapez2/Applications/Inkscape.app/Contents/MacOS/inkscapewin32c                   @   s   e Zd ZdZeddd Zeddd Zedd	jd
dZ	eddd Z
ee e gdd	jd
dZeddd Zedd	jd
dZeddd Zdd ZdS )SVG2PDFPreprocessorzD
    Converts all of the outputs in a notebook from SVG to PDF.
    from_formatc                 C      dS )Nzimage/svg+xml selfr   r   Z/home/ubuntu/hpml_nyu/venv/lib/python3.10/site-packages/nbconvert/preprocessors/svg2pdf.py_from_format_default)      z(SVG2PDFPreprocessor._from_format_default	to_formatc                 C   r   )Nzapplication/pdfr   r   r   r   r   _to_format_default-   r   z&SVG2PDFPreprocessor._to_format_defaultzeThe version of inkscape being used.

        This affects how the conversion command is run.
        )helpT)configinkscape_versionc                 C   sP   t j| jdgt jt jd}| \}}|jdkrd}t||ddd S )Nz	--version)stdoutstderrr   z,Unable to find inkscape executable --versionutf-8 r
   )	
subprocessPopeninkscapePIPEcommunicate
returncodeRuntimeErrordecodesplit)r   poutput_msgr   r   r   _inkscape_version_default8   s   
z-SVG2PDFPreprocessor._inkscape_version_defaulta  
        The command to use for converting SVG to PDF

        This traitlet is a template, which will be formatted with the keys
        to_filename and from_filename.

        The conversion call must read the SVG from {from_filename},
        and write a PDF to {to_filename}.

        It could be a List (recommended) or a String. If string, it will
        be passed to a shell for execution.
        commandc                 C   sR   | j dd }| jg}t|dk r|d |d n|d |d |S )N.r   r
   z--without-guiz--export-pdf={to_filename}z--export-filename={to_filename}z{from_filename})r   r'   r!   intappend)r   major_versionr-   r   r   r   _command_defaultV   s   


z$SVG2PDFPreprocessor._command_defaultz"The path to Inkscape, if necessaryr!   c           	   
   C   sV  t jdkr7td tj}zt|d}t|dd dd }tj	
|r)|W S W n tttfy6   Y nw dtjd< td}t jdkr|rt jd	k rz2t  }t| j|k}|d
d tjdtjtjD v }|r}|s}tjdtdd d }W n ty   d }Y nw |d ur|S t jdkrtj	
trtS tj	
trtS d}t|)Nr   z)SOFTWARE\Classes\inkscape.svg\DefaultIcon r   ,1"NODEFAULTCURRENTDIRECTORYINEXEPATHr!   )      c                 S   s   h | ]}t | qS r   )r   resolve).0r(   r   r   r   	<setcomp>   s    z8SVG2PDFPreprocessor._inkscape_default.<locals>.<setcomp>PATHzshutil.which('inkscape') resolved to an executable in the current working directory even though CWD is not on PATH. Ignoring this result for security reasons (CVE-2025-53000).   )
stackleveldarwinz+Inkscape executable not found in safe paths)sysplatformwinregConnectRegistryHKEY_LOCAL_MACHINEOpenKeyQueryValueExr'   ospathisfileFileNotFoundErrorOSError
IndexErrorenvironr   version_infor   cwdr9   parentgetdefpathpathsepwarningswarnRuntimeWarning	ExceptionINKSCAPE_APP_v1INKSCAPE_APP)	r   	wr_handlerkeyinkscape_fullinkscape_pathrO   in_cwdcwd_on_pathr+   r   r   r   _inkscape_defaulth   sP   


z%SVG2PDFPreprocessor._inkscape_defaultc           	   	      s8  t  }tj|d}t|ddd}|| W d   n1 s"w   Y  tj|d}||d t| jtrD fdd	| jD }n	| jj	di  }t
j|t|td
 tj|rt|d}t| dW  d   W  d   S 1 s~w   Y  nd}t|W d   dS 1 sw   Y  dS )zN
        Convert a single SVG figure to PDF.  Returns converted data.
        z
figure.svgwutf8)encodingNz
figure.pdf)from_filenameto_filenamec                    s    g | ]}| td i  qS )r   )
format_mapr	   )r:   stemplate_varsr   r   
<listcomp>   s     z6SVG2PDFPreprocessor.convert_figure.<locals>.<listcomp>)shellrbr   z%Inkscape svg to pdf conversion failedr   )r   rG   rH   joinopenwrite
isinstancer-   listformatr   callstrrI   base64encodebytesreadr&   	TypeError)	r   data_formatdatatmpdirinput_filenamefoutput_filenamefull_cmdr+   r   rh   r   convert_figure   s*   
"z"SVG2PDFPreprocessor.convert_figureN)__name__
__module____qualname____doc__r   r   r   r   tagr   r,   r   r   r-   r2   r!   r`   r   r   r   r   r   r   $   s2    





8r   )r   ru   rG   r   r@   rT   pathlibr   shutilr   tempfiler   	traitletsr   r   r   r   nbconvert.utils.ior	   convertfiguresr   rY   rX   rA   rB   ImportError_winregr   r   r   r   r   <module>   s*    
