a
    ø£BeÀ  ã                   @   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Únamez«Indicates 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 dS )z#Get the current key for a mask key.r   N)Ú
isinstanceÚstrÚlen)ÚselfZmask_key© r   úh/home/yuhw/wc/wire-cell-python/wcpy/lib/python3.9/site-packages/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 urˆ   |¡’qS )N)r   ©Ú.0Úk©r   r   r   Ú	<setcomp>4   ó    z9ClearMetadataPreprocessor.current_mask.<locals>.<setcomp>r   ©r   Úmaskr   r   r   Úcurrent_mask2   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>8   s   þz:ClearMetadataPreprocessor.nested_masks.<locals>.<dictcomp>r   r   r   r   r   Únested_masks6   s    
þz&ClearMetadataPreprocessor.nested_masksc                 c   sp   |   |¡}|  |¡}|D ]R\}}| |¡}||v r|du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|v 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_cellK   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   r)   W   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)r2   Z	traitletsr   r   Úbaser   r   r   r   r   r   Ú<module>   s   