o
    i                     @   s4   d Z ddlmZmZ ddlmZ G dd deZdS )zFModule containing a preprocessor that removes metadata from code cells    )BoolSet   )Preprocessorc                       s   e Zd ZdZedddjddZedddjddZedgddjddZ	eddjddZ
d	d
 Zdd Zdd Zdd Zdd Z fddZ  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                 C   s&   t |tr|S t|dkrdS |d S )z#Get the current key for a mask key.r   N)
isinstancestrlen)selfmask_key r   `/var/www/edux/Edux_v2/venv/lib/python3.10/site-packages/nbconvert/preprocessors/clearmetadata.pycurrent_key(   s
   
z%ClearMetadataPreprocessor.current_keyc                        fdd|D S )z Get the current mask for a mask.c                    s$   h | ]}  |d ur  |qS )N)r   .0kr   r   r   	<setcomp>3   s   $ z9ClearMetadataPreprocessor.current_mask.<locals>.<setcomp>r   r   maskr   r   r   current_mask1   s   z&ClearMetadataPreprocessor.current_maskc                    r   )z Get the nested masks for a mask.c                    s>   i | ]}|rt |tst|d kr |d |d d qS )r   r   N)r   r   r   r   r   r   r   r   
<dictcomp>7   s    z:ClearMetadataPreprocessor.nested_masks.<locals>.<dictcomp>r   r   r   r   r   nested_masks5   s   
z&ClearMetadataPreprocessor.nested_masksc                 c   sr    |  |}| |}|D ])\}}||}||v r6|dur1t|tr0|t| | |fV  q||fV  qdS )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_nestedr   r   r   r    =   s   




z'ClearMetadataPreprocessor.nested_filterc                 C   s:   | j r|jdkrd|v rt| |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_indexr   r   r   preprocess_cellJ   s   z)ClearMetadataPreprocessor.preprocess_cellc                    sB   t  ||\}}| jrd|v rt| |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__r   r   r0   V   s   z$ClearMetadataPreprocessor.preprocess)__name__
__module____qualname____doc__r   tagr(   r1   r   r2   r*   r   r   r   r    r.   r0   __classcell__r   r   r4   r   r      s>    	r   N)r9   	traitletsr   r   baser   r   r   r   r   r   <module>   s    