a
    ø£BeÏ  ã                   @   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r¦zddlZW n ey¤   ddlZY n0 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   úb/home/yuhw/wc/wire-cell-python/wcpy/lib/python3.9/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_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 ur|S tjdkr>tj t¡r.tS tj t¡r>tS tjdkrœt 	d tj
¡}z t |d¡}t |d¡d }W n  ty–   d}t|ƒd ‚Y n0 |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           	   	      s8  t ƒ ��}tj |d¡}t|ddd��}| |¡ W d  ƒ n1 sF0    Y  tj |d¡}||dœ‰ t| jtƒrŠ‡ fdd	„| jD ƒ}n| jj	ˆ Ž }t
j|t|tƒd
� tj |¡�rt|dƒ�2}t | ¡ ¡ d¡W  d  ƒ W  d  ƒ S 1 � sü0    Y  nd}t|ƒ‚W d  ƒ n1 �s*0    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f i ˆ ¤Ž¡‘qS r   )Ú
format_mapr   )Ú.0Ús©Ztemplate_varsr   r   Ú
<listcomp>�   ó    z6SVG2PDFPreprocessor.convert_figure.<locals>.<listcomp>)ÚshellÚrbr   z%Inkscape svg to pdf conversion failed)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    
(
Dz"SVG2PDFPreprocessor.convert_figureN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   r   Útagr   r'   r   r   r(   r-   r   r<   rX   r   r   r   r   r   "   s0   

ÿû

þò

r   )r\   rR   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"   
