U
    nuf                     @   s   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
mZmZmZ ddlmZ ddlmZ d	Zd
ZejdkrzddlZW n ek
r   ddlZY nX G dd deZdS )zcModule containing a preprocessor that converts outputs in the notebook from
one format to another.
    N)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.
    Zfrom_formatc                 C   s   dS )Nzimage/svg+xml selfr   r   C/tmp/pip-unpacked-wheel-nxpdnluh/nbconvert/preprocessors/svg2pdf.py_from_format_default'   s    z(SVG2PDFPreprocessor._from_format_defaultZ	to_formatc                 C   s   dS )Nzapplication/pdfr   r   r   r   r   _to_format_default+   s    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_default6   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_defaultT   s    


z$SVG2PDFPreprocessor._command_defaultz"The path to Inkscape, if necessaryr   c                 C   s   t d}|d k	r|S tjdkr>tjtr.tS tjtr>tS tjdkrt	d tj
}z t|d}t|dd }W n" tk
r   d}t|d Y nX |S dS )Nr   darwinr   z)SOFTWARE\Classes\inkscape.svg\DefaultIcon r   zInkscape executable not found)r   sysplatformospathisfileINKSCAPE_APP_v1INKSCAPE_APPwinregZConnectRegistryHKEY_LOCAL_MACHINEOpenKeyQueryValueExFileNotFoundError)r   Zinkscape_pathZ	wr_handleZrkeyr   r&   r   r   r   _inkscape_defaultf   s$    

z%SVG2PDFPreprocessor._inkscape_defaultc           	         s   t  }tj|d}t|ddd}|| W 5 Q R X tj|d}||d t| jtrt fdd| jD }n| jj	  }t
j|t|td	 tj|rt|d
.}t| dW  5 Q R  W  5 Q R  S Q R X nd}t|W 5 Q R X dS )zN
        Convert a single SVG figure to PDF.  Returns converted data.
        z
figure.svgwutf8)encodingz
figure.pdf)from_filenameto_filenamec                    s   g | ]}| tf  qS r   )
format_mapr   ).0sZtemplate_varsr   r   
<listcomp>   s     z6SVG2PDFPreprocessor.convert_figure.<locals>.<listcomp>)shellrbr   z%Inkscape svg to pdf conversion failedN)r   r2   r3   joinopenwrite
isinstancer(   listformatr   callstrr4   base64encodebytesreadr!   	TypeError)	r   Zdata_formatdataZtmpdirZinput_filenamefZoutput_filenameZfull_cmdr&   r   rE   r   convert_figure}   s    
4z"SVG2PDFPreprocessor.convert_figureN)__name__
__module____qualname____doc__r   r   r   r   tagr   r'   r   r   r(   r-   r   r<   rW   r   r   r   r   r   "   s0   





r   )r[   rQ   r2   r   r0   shutilr   tempfiler   Z	traitletsr   r   r   r   Znbconvert.utils.ior   Zconvertfiguresr
   r6   r5   r1   r7   ImportError_winregr   r   r   r   r   <module>   s"   
