B
    KZ                 @   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mZ ddlmZ dZejd	kryddlZW n ek
r   ddlZY nX G d
d deZdS )zdModule containing a preprocessor that converts outputs in the notebook from 
one format to another.
    N)cast_unicode_py2)TemporaryDirectory)Unicodedefault   )ConvertFiguresPreprocessorz:/Applications/Inkscape.app/Contents/Resources/bin/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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
   >lib/python3.7/site-packages/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_defaulta$  The command to use for converting SVG to PDF
        
        This string is a template, which will be formatted with the keys
        to_filename and from_filename.
        
        The conversion call must read the SVG from {from_flename},
        and write a PDF to {to_filename}.
        )helpT)Zconfigcommandc             C   s
   | j d S )Nz= --without-gui --export-pdf="{to_filename}" "{from_filename}")inkscape)r   r
   r
   r   _command_default5   s    z$SVG2PDFPreprocessor._command_defaultz"The path to Inkscape, if necessaryr   c             C   sx   t jdkrtjtrtS t jdkrttd tj}y t	|d}t
|dd }W n tk
rn   tdY nX |S dS )Ndarwinr   z)SOFTWARE\Classes\inkscape.svg\DefaultIcon r   zInkscape executable not foundr   )sysplatformospathisfileINKSCAPE_APPwinregZConnectRegistryHKEY_LOCAL_MACHINEOpenKeyZQueryValueExFileNotFoundError)r   Z	wr_handleZrkeyr   r
   r
   r   _inkscape_default;   s    

z%SVG2PDFPreprocessor._inkscape_defaultc          
   C   s   t  }tj|d}tj|ddd}|t| W dQ R X tj|d}| jj	||d}t
j|dd	 tj|rt|d
}t| S Q R X ntdW dQ R X dS )zN
        Convert a single SVG figure to PDF.  Returns converted data.
        z
figure.svgwutf8)encodingNz
figure.pdf)Zfrom_filenameZto_filenameT)shellrbz%Inkscape svg to pdf conversion failed)r   r   r   joinioopenwriter   r   format
subprocessZcallr   base64Zencodestringread	TypeError)r   Zdata_formatdataZtmpdirZinput_filenamefZoutput_filenamer#   r
   r
   r   convert_figureK   s    z"SVG2PDFPreprocessor.convert_figureN)__name__
__module____qualname____doc__r   r   r   r   tagr   r   r   r   r0   r
   r
   r
   r   r	      s   r	   )r4   r+   r&   r   r   r*   Zipython_genutils.py3compatr   Ztestpath.tempdirr   Z	traitletsr   r   Zconvertfiguresr   r   r   r   ImportError_winregr	   r
   r
   r
   r   <module>   s    
