
    lKj                     N    d Z ddlZddlmZ ddlmZmZ ddlmZ  G d de      Z	y)	zF
Module that extracts attachments from notebooks into their own files
    N)	b64decode)BoolUnicode   )Preprocessorc                        e Zd ZdZ edd      j                  d      Z edd      j                  d      Z fd	Z	 fd
Z
d Z xZS )ExtractAttachmentsPreprocessorz
    Extracts attachments from all (markdown and raw) cells in a notebook.
    The extracted attachments are stored in a directory ('attachments' by default).
    https://nbformat.readthedocs.io/en/latest/format_description.html#cell-attachments
    z{notebook_name}_attachmentsz:Directory to place attachments if use_separate_dir is True)helpT)configFznWhether to use output_files_dir (which ExtractOutput also uses) or create a separate directory for attachmentsc                 @    t        |   di | d| _        d| _        y)z$
        Public constructor
         attachmentsN )super__init__	path_nameresources_item_key)selfkw	__class__s     {/Users/ahmed/devFolder/Ultron/claude-voice/.venv/lib/python3.12/site-packages/nbconvert/preprocessors/extractattachments.pyr   z'ExtractAttachmentsPreprocessor.__init__"   s)     	2  	    c                 D   | j                   r@| j                  j                  |d         | _        | j                  |d<   i |d<   d| _        n|d   | _        d| _        t        || j                     t              si || j                  <   t        | !  ||      \  }}||fS )zL
        Determine some settings and apply preprocessor to notebook
        
unique_key)notebook_nameattachment_files_dirr   output_files_diroutputs)	use_separate_dirattachments_directory_templateformatr   r   
isinstancedictr   
preprocess)r   nb	resourcesr   s      r   r$   z)ExtractAttachmentsPreprocessor.preprocess/   s       !@@GG'5 H DN 15I,-')Im$&3D# ''9:DN&/D# )D$;$;<dC13Id--.*2y9I9}r   c                 ^   d|v r|j                   D ]  }| j                  j                  d|       t        j                  j                  |      }|s| j                  j                  d|       ^||k7  r| j                  j                  d||       |j                   |   D ]/  }|j                   |   |   j                  d      }t        |      } n t        j                  j                  | j                  |      }	|	|| j                     v r| j                  j                  d||       || j                     |	<   t        j                  j                  dk7  r*|	j                  t        j                  j                  d      }	|j                  j                  d|z   |	      |_         ||fS )	aB  
        Extract attachments to individual files and
        change references to them.
        E.g.
        '![image.png](attachment:021fdd80.png)'
        becomes
        '![image.png]({path_name}/021fdd80.png)'
        Assumes self.path_name and self.resources_item_key is set properly (usually in preprocess).
        r   zEncountered attachment %sz>Attachment filename '%s' is invalid (empty basename), skippingzGAttachment filename '%s' contained path components, using basename '%s'zutf-8zXAttachment filename '%s' (from '%s') overwrites a previous attachment with the same name/zattachment:)r   logdebugospathbasenamewarningencoder   joinr   r   sepreplacesource)
r   cellr&   indexfname
safe_fnamemimetypedatadecodednew_filenames
             r   preprocess_cellz.ExtractAttachmentsPreprocessor.preprocess_cellG   s    D )):EB  WW--e4
!HH$$X &HH$$a" !% 0 0 7H++E28<CCGLD'oG	 !8  "ww||DNNJG9T-D-D#EEHH$$8"	 DK	$112<@ 77;;#%#/#7#7S#IL"kk11-%2GV] *` Yr   )__name__
__module____qualname____doc__r   tagr    r   r   r   r$   r<   __classcell__)r   s   @r   r	   r	      s_     &-%I& 
cc #
 6 
cc	 

0;r   r	   )
r@   r+   base64r   	traitletsr   r   baser   r	   r   r   r   <module>rF      s&    
  # r\ rr   