
    b5                         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	 ddlZ G d de      Zy# e$ r ddlZY w xY w)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                   R   e Zd ZdZ ed      d        Z ed      d        Z ed      j                  d	      Z	 ed
      d        Z
 e e        e       gd      j                  d	      Z ed      d        Z ed      j                  d	      Z ed      d        Zd Zy)SVG2PDFPreprocessorzD
    Converts all of the outputs in a notebook from SVG to PDF.
    from_formatc                      y)Nzimage/svg+xml selfs    A/usr/lib/python3/dist-packages/nbconvert/preprocessors/svg2pdf.py_from_format_defaultz(SVG2PDFPreprocessor._from_format_default&   s        	to_formatc                      y)Nzapplication/pdfr   r   s    r   _to_format_defaultz&SVG2PDFPreprocessor._to_format_default*   s     r   zeThe version of inkscape being used.

        This affects how the conversion command is run.
        )helpT)configinkscape_versionc                 "   t        j                  | j                  dgt         j                  t         j                        }|j	                         \  }}|j
                  dk7  rt        d      |j                  d      j                  d      d   S )Nz	--version)stdoutstderrr   z,Unable to find inkscape executable --versionzutf-8 r   )	
subprocessPopeninkscapePIPEcommunicate
returncodeRuntimeErrordecodesplit)r   poutput_s       r   _inkscape_version_defaultz-SVG2PDFPreprocessor._inkscape_version_default5   sr    ]]K(
 MMO	<<1MNN}}W%++C033r   a  
        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                    | j                   j                  d      d   }| j                  g}t        |      dk  r#|j	                  d       |j	                  d       n|j	                  d       |j	                  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/   s      r   _command_defaultz$SVG2PDFPreprocessor._command_defaultP   sm    --33C8;==/}!NN?+NN78NN<=()r   z"The path to Inkscape, if necessaryr$   c                    t        d      }||S t        j                  dk(  rRt        j                  j                  t              rt        S t        j                  j                  t              rt        S t        j                  dk(  rVt        j                  d t        j                        }	 t        j                  |d      }t        j                  |d      d   }|S y# t        $ r t        d      w xY w)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winregConnectRegistryHKEY_LOCAL_MACHINEOpenKeyQueryValueExFileNotFoundError)r   inkscape_path	wr_handlerkeyr$   s        r   _inkscape_defaultz%SVG2PDFPreprocessor._inkscape_defaultb   s    j)$  <<8#ww~~o.&& ww~~l+##<<7"..tV5N5NOII~~i1_`!..tR8; O % I'(GHHIs   -/C C4c                 F   t               5 }t        j                  j                  |d      }t	        |dd      5 }|j                  |       ddd       t        j                  j                  |d      }||d}t        | j                  t              r2| j                  D cg c]  }|j                  t        di |       }	}n | j                  j                  | }	t        j                  |	t        |	t                     t        j                  j                  |      rBt	        |d	      5 }t!        j"                  |j%                               cddd       cddd       S t'        d
      # 1 sw Y   %xY wc c}w # 1 sw Y   nxY w	 ddd       y# 1 sw Y   yxY w)zN
        Convert a single SVG figure to PDF.  Returns converted data.
        z
figure.svgwutf8)encodingNz
figure.pdf)from_filenameto_filename)shellrbz%Inkscape svg to pdf conversion failedr   )r   r;   r<   joinopenwrite
isinstancer/   list
format_mapr
   formatr"   callstrr=   base64encodebytesread	TypeError)
r   data_formatdatatmpdirinput_filenamefoutput_filenametemplate_varssfull_cmds
             r   convert_figurez"SVG2PDFPreprocessor.convert_figurex   s^     ! 	IV  WW\\&,?NncF; q !ggll6<@O.<_]M$,,-SWS_S_`aALL)H-)HI`` /4<<..>OOHJx,EF ww~~o./40 8A!--affh78 8/	I 	I6   GHH-  a8 8 8/	I 	I 	IsN   /FE/AF!!E<A,F.#F	F$F/E9	4FF
	FF N)__name__
__module____qualname____doc__r   r   r   r   tagr   r.   r   r   r/   r5   r$   rI   rh   r   r   r   r   r   !   s     ]  [! ! 
 
cc   4 !4 	DF 
cc   Y  @AEETERHZ *!Ir   r   )rl   r[   r;   r"   r9   shutilr   tempfiler   	traitletsr   r   r   r   utils.ior
   convertfiguresr   r?   r>   r:   r@   ImportError_winregr   r   r   r   <module>ru      ss     	  
  ' 3 3 % 6 L G<<7!
xI4 xI	  ! !s   A 	A'&A'