ó
§]c           @   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 rÐ y d d l Z WqÐ e k
 rÌ d d l Z qÐ Xn  d
 e f d     YZ d S(   sd   Module containing a preprocessor that converts outputs in the notebook from 
one format to another.
iÿÿÿÿN(   t   cast_unicode_py2(   t   TemporaryDirectory(   t   Unicodet   defaulti   (   t   ConvertFiguresPreprocessors:   /Applications/Inkscape.app/Contents/Resources/bin/inkscapet   win32t   SVG2PDFPreprocessorc           B   s¡   e  Z d  Z e d  d    Z e d  d    Z e d d  j d e  Z	 e d  d	    Z
 e d d
  j d e  Z e d  d    Z d   Z RS(   sD   
    Converts all of the outputs in a notebook from SVG to PDF.
    t   from_formatc         C   s   d S(   Ns   image/svg+xml(    (   t   self(    (    s>   lib/python2.7/site-packages/nbconvert/preprocessors/svg2pdf.pyt   _from_format_default#   s    t	   to_formatc         C   s   d S(   Ns   application/pdf(    (   R   (    (    s>   lib/python2.7/site-packages/nbconvert/preprocessors/svg2pdf.pyt   _to_format_default'   s    t   helps%  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_filename},
        and write a PDF to {to_filename}.
        t   configt   commandc         C   s   |  j  d S(   Ns=    --without-gui --export-pdf="{to_filename}" "{from_filename}"(   t   inkscape(   R   (    (    s>   lib/python2.7/site-packages/nbconvert/preprocessors/svg2pdf.pyt   _command_default5   s    s"   The path to Inkscape, if necessaryR   c         C   s    t  j d k r( t j j t  r( t Sn  t  j d k r t j d  t j	  } y, t j
 | d  } t j | d  d } Wn t k
 r t d   n X| Sd S(   Nt   darwinR   s)   SOFTWARE\Classes\inkscape.svg\DefaultIcont    i    s   Inkscape executable not foundR   (   t   syst   platformt   ost   patht   isfilet   INKSCAPE_APPt   winregt   ConnectRegistryt   Nonet   HKEY_LOCAL_MACHINEt   OpenKeyt   QueryValueExt   FileNotFoundError(   R   t	   wr_handlet   rkeyR   (    (    s>   lib/python2.7/site-packages/nbconvert/preprocessors/svg2pdf.pyt   _inkscape_default;   s    c         C   sî   t    ß } t j j | d  } t j | d d d  } | j t |   Wd QXt j j | d  } |  j j	 d | d |  } t
 j | d	 t t j j |  rØ t | d
   } t j | j    SWd QXn t d   Wd QXd S(   sN   
        Convert a single SVG figure to PDF.  Returns converted data.
        s
   figure.svgt   wt   encodingt   utf8Ns
   figure.pdft   from_filenamet   to_filenamet   shellt   rbs%   Inkscape svg to pdf conversion failed(   R   R   R   t   joint   iot   opent   writeR    R   t   formatt
   subprocesst   callt   TrueR   t   base64t   encodestringt   readt	   TypeError(   R   t   data_formatt   datat   tmpdirt   input_filenamet   ft   output_filenameR(   (    (    s>   lib/python2.7/site-packages/nbconvert/preprocessors/svg2pdf.pyt   convert_figureK   s    	(   t   __name__t
   __module__t   __doc__R   R	   R   R   t   tagR1   R   R   R   R"   R<   (    (    (    s>   lib/python2.7/site-packages/nbconvert/preprocessors/svg2pdf.pyR      s   (   R?   R2   R+   R   R   R/   t   ipython_genutils.py3compatR    t   testpath.tempdirR   t	   traitletsR   R   t   convertfiguresR   R   R   R   t   ImportErrort   _winregR   (    (    (    s>   lib/python2.7/site-packages/nbconvert/preprocessors/svg2pdf.pyt   <module>   s    