U
    nuf                     @   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.)Z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)selfZmask_key r   I/tmp/pip-unpacked-wheel-nxpdnluh/nbconvert/preprocessors/clearmetadata.pycurrent_key(   s
    
z%ClearMetadataPreprocessor.current_keyc                    s    fdd|D S )z Get the current mask for a mask.c                    s$   h | ]}  |d k	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                    s    fdd|D S )z Get the nested masks for a mask.c                    s>   i | ]6}|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   sp   |  |}| |}|D ]R\}}||}||kr|dk	r`t|trj|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   Zkeep_currentZkeep_nested_lookupr   vZkeep_nestedr   r   r   r   =   s    



z'ClearMetadataPreprocessor.nested_filterc                 C   s:   | j r2|jdkr2d|kr2t| |j | j|_||fS )zO
        All the code cells are returned with an empty metadata field.
        codemetadata)clear_cell_metadataZ	cell_typer   r   r!   r   preserve_cell_metadata_mask)r   cell	resourcesZ
cell_indexr   r   r   preprocess_cellJ   s    z)ClearMetadataPreprocessor.preprocess_cellc                    sB   t  ||\}}| jr:d|k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   r(   V   s    z$ClearMetadataPreprocessor.preprocess)__name__
__module____qualname____doc__r   tagr"   r)   r   r*   r#   r   r   r   r   r&   r(   __classcell__r   r   r,   r   r      s<   	r   N)r1   Z	traitletsr   r   baser   r   r   r   r   r   <module>   s   