
    f                         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    Z/var/www/cvtools/html/venv/lib/python3.12/site-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d}t        |      |j                  d      j                  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_msgs        r   _inkscape_version_defaultz-SVG2PDFPreprocessor._inkscape_version_default6   sx    ]]K(????

 MMO	<<1@Cs##}}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_versionr0   s      r   _command_defaultz$SVG2PDFPreprocessor._command_defaultT   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 d}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$   r.   s         r   _inkscape_defaultz%SVG2PDFPreprocessor._inkscape_defaultf   s    j)$  <<8#ww~~o.&& ww~~l+##<<7"..tV5N5NOI7~~i1_`!..tR8; O	 % 75',$67s   -/C C7c                 h   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                  |      rQt	        |d	      5 }t!        j"                  |j%                               j'                  d
      cddd       cddd       S d}
t)        |
      # 1 sw Y   6x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rbr    z%Inkscape svg to pdf conversion failedr   )r   r<   r=   joinopenwrite
isinstancer0   list
format_mapr	   formatr"   callstrr>   base64encodebytesreadr)   	TypeError)r   data_formatdatatmpdirinput_filenamefoutput_filenametemplate_varssfull_cmdr.   s              r   convert_figurez"SVG2PDFPreprocessor.convert_figure}   sj     ! 	%VWW\\&,?NncF; q !ggll6<@O.<_]M$,,-SWS_S_`aALL)H-)HI`` /4<<..>OOHJx,EF ww~~o./40 HA!--affh7>>wGH H-	% 	%4 >n$/  aH H H-	% 	% 	%sN   /F(F AF(!!FA,F(.2F 	F(3F( F
	F(F	F((F1N)__name__
__module____qualname____doc__r   r   r   r   tagr   r/   r   r   r0   r6   r$   rJ   ri   r   r   r   r   r   "   s     ]  [! ! 
 
cc   
4 !
4 	DF 
cc   Y  @AEETERHZ ,!%r   r   )rm   r\   r<   r"   r:   shutilr   tempfiler   	traitletsr   r   r   r   nbconvert.utils.ior	   convertfiguresr   r@   r?   r;   rA   ImportError_winregr   r   r   r   <module>rv      sq     	  
  ' 3 3 - 6 L G<<7!
|%4 |%	  ! !s   A 	A'&A'