
    f                     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     e/var/www/cvtools/html/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                 B   d|v r|j                   D ]  }| j                  j                  d|       |j                   |   D ]/  }|j                   |   |   j                  d      }t	        |      } n t
        j                  j                  | j                  |      }|| 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utf-8/zattachment:)r   logdebugencoder   ospathjoinr   r   sepreplacesource)	r   cellr&   indexfnamemimetypedatadecodednew_filenames	            r   preprocess_cellz.ExtractAttachmentsPreprocessor.preprocess_cellG   s    D )) W:EB !% 0 0 7 H++E28<CCGLD'oG	  "ww||DNNEBCJ	$112<@ 77;;#%#/#7#7S#IL"kk11-%2GV1W4 Yr   )__name__
__module____qualname____doc__r   tagr    r   r   r   r$   r9   __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>rC      s&    
  # \\ \r   