
    f                     :    d Z ddlmZmZ ddlmZ  G d de      Zy)zFModule containing a preprocessor that removes metadata from code cells    )BoolSet   )Preprocessorc                       e Zd ZdZ edd      j                  d      Z edd      j                  d      Z edgd      j                  d      Z	 ed      j                  d      Z
d	 Zd
 Zd Zd Zd Z fdZ xZS )ClearMetadataPreprocessorzE
    Removes all the metadata from all code cells in a notebook.
    TzRFlag to choose if cell metadata is to be cleared in addition to notebook metadata.)help)configzRFlag to choose if notebook metadata is to be cleared in addition to cell metadata.)language_infonamezIndicates the key paths to preserve when deleting metadata across both cells and notebook metadata fields. Tuples of keys can be passed to preserved specific nested valuesc                 N    t        |t              r|S t        |      dk(  ry|d   S )z#Get the current key for a mask key.r   N)
isinstancestrlen)selfmask_keys     `/var/www/cvtools/html/venv/lib/python3.12/site-packages/nbconvert/preprocessors/clearmetadata.pycurrent_keyz%ClearMetadataPreprocessor.current_key(   s*    h$Ox=A{    c                 n    |D ch c]%  }| j                  |      | j                  |      ' c}S c c}w )z Get the current mask for a mask.)r   r   maskks      r   current_maskz&ClearMetadataPreprocessor.current_mask1   s1    -1UT5E5Ea5H5T  #UUUs   22c                     |D ci c]:  }|r6t        |t              s&t        |      dkD  r| j                  |d         |dd < c}S c c}w )z Get the nested masks for a mask.r   r   N)r   r   r   r   r   s      r   nested_masksz&ClearMetadataPreprocessor.nested_masks5   sT     
As+A
 QqT"AabE)
 	
 
s   ?Ac              #     K   | j                  |      }| j                  |      }|D ]b  \  }}|j                  |      }||v s|?t        |t              s/|t	        | j                  |j                         |            f ]||f d yw)z-Get the nested filter for items given a mask.N)r   r   getr   dictnested_filteritems)r   r!   r   keep_currentkeep_nested_lookupr   vkeep_nesteds           r   r    z'ClearMetadataPreprocessor.nested_filter=   s     ((.!..t4 	DAq,003KL *!!T*d&8&8K&P!QQQQ$J	s   A BB7Bc                     | j                   rU|j                  dk(  rFd|v rBt        | j                  |j                  j                         | j                              |_        ||fS )zO
        All the code cells are returned with an empty metadata field.
        codemetadata)clear_cell_metadata	cell_typer   r    r(   r!   preserve_cell_metadata_mask)r   cell	resources
cell_indexs       r   preprocess_cellz)ClearMetadataPreprocessor.preprocess_cellJ   s[     ##&(@T! $&&t}}':':'<d>^>^_! Yr   c                     t         |   ||      \  }}| j                  rFd|v rBt        | j	                  |j
                  j                         | j                              |_        ||fS )ax  
        Preprocessing to apply on each notebook.

        Must return modified nb, resources.

        Parameters
        ----------
        nb : NotebookNode
            Notebook being converted
        resources : dictionary
            Additional resources used in the conversion process.  Allows
            preprocessors to pass variables into the Jinja engine.
        r(   )super
preprocessclear_notebook_metadatar   r    r(   r!   preserve_nb_metadata_mask)r   nbr-   	__class__s      r   r2   z$ClearMetadataPreprocessor.preprocessV   se     *2y9I''J",<""2;;#4#4#68V8VWBK 9}r   )__name__
__module____qualname____doc__r   tagr)   r3   r   r4   r+   r   r   r   r    r/   r2   __classcell__)r6   s   @r   r   r      s     b 
cc  #b 
cc  !$	"#E! 
cc  #&E# 
cc  V

 r   r   N)r:   	traitletsr   r   baser   r    r   r   <module>r@      s    L
   ^ ^r   