
    lKj                         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 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)Path)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    p/Users/ahmed/devFolder/Ultron/claude-voice/.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_default8   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_versionr1   s      r   _command_defaultz$SVG2PDFPreprocessor._command_defaultV   sm    --33C8;==/}!NN?+NN78NN<=()r   z"The path to Inkscape, if necessaryr%   c                    t         j                  dk(  rt        j                  d t        j                        }	 t        j
                  |d      }t        j                  |d      d   j                  d      d   }t        j                  j                  |      r|S 	 dt        j                  d<   t        d      }t         j                  dk(  r|rt         j                   d	k  r	 t#        j$                         j'                         }t#        |      j'                         j(                  |k(  }|t        j                  j+                  d
t        j,                        j                  t        j.                        D ch c]  }t#        |      j'                          c}v }|r |st1        j2                  dt4        d       d }||S t         j                  dk(  rRt        j                  j                  t8              rt8        S t        j                  j                  t:              rt:        S d}	t        |	      # t        t        t        f$ r Y w xY wc c}w # t6        $ r d }Y w xY w)Nr   z)SOFTWARE\Classes\inkscape.svg\DefaultIcon r   ,1"NODEFAULTCURRENTDIRECTORYINEXEPATHr%   )      PATHzshutil.which('inkscape') resolved to an executable in the current working directory even though CWD is not on PATH. Ignoring this result for security reasons (CVE-2025-53000).   )
stackleveldarwinz+Inkscape executable not found in safe paths)sysplatformwinregConnectRegistryHKEY_LOCAL_MACHINEOpenKeyQueryValueExr+   ospathisfileFileNotFoundErrorOSError
IndexErrorenvironr   version_infor   cwdresolveparentgetdefpathpathsepwarningswarnRuntimeWarning	ExceptionINKSCAPE_APP_v1INKSCAPE_APP)
r   	wr_handlerkeyinkscape_fullinkscape_pathrR   in_cwdr,   cwd_on_pathr/   s
             r   _inkscape_defaultz%SVG2PDFPreprocessor._inkscape_defaulth   s    <<7"..tV5N5NOI~~i1]^ & 3 3D" =a @ F Fs KA N77>>-0(( 1 <?

78j)
 <<7"}9I9IG9S%hhj((*m,446==D!/1zz~~fbjj/Q/W/WXZXbXb/c&/c!DGOO%/c&  +MMH '#$ %)M
 $   <<8#ww~~o.&& ww~~l+##;$$[ &w
; &  % $%s7   A!H5 #BI 9 I&I 5III I#"I#c                 n   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                  di |}	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   9x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   rJ   rK   joinopenwrite
isinstancer1   list
format_mapr
   formatr#   callstrrL   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   sM     !VWW\\&,?NncF;q < !ggll6<@O.<_]M$,,-SWS_S_`S_aALL)H-)HIS_` /4<<..??OOHJx,EF ww~~o./40A!--affh7>>wG 10- "!4 >n$/ <; a 100- "!!sN   /F+FAF+!!FA/F+12F#	F+6F+F	F+F	F++F4N)__name__
__module____qualname____doc__r	   r   r   r   tagr   r0   r   r   r1   r7   r%   rd   r   r   r   r   r   r   $   s     ]  [! ! 
 
cc   
4 !
4 	DF 
cc   Y  @AEETERHZ6% 6%p!%r   r   )r   rv   rJ   r#   rC   rX   pathlibr   shutilr   tempfiler   	traitletsr   r   r   r	   nbconvert.utils.ior
   convertfiguresr   r]   r\   rD   rE   ImportError_winregr   r   r   r   <module>r      sw     	  
    ' 3 3 - 6 L G<<7!
^%4 ^%	  ! !s   A% %	A10A1