a
    ø£Be‰2  ã                   @  sÎ   d Z ddlmZ ddlZddlZddlZddlZddlZddlZ	ddl
Z
ddl
mZmZ ddlmZmZmZmZmZ ddlmZ ddlmZ ddlmZ G d	d
„ d
ejƒZG dd„ deƒZG dd„ deƒZdS )zeThis module defines a base Exporter class. For Jinja template-based export,
see templateexporter.py.
é    )ÚannotationsN)ÚNotebookNodeÚ	validator)ÚBoolÚ	HasTraitsÚListÚ
TraitErrorÚUnicode©ÚConfig)ÚLoggingConfigurable)Úimport_itemc                   @  s   e Zd ZdZdd„ ZdS )ÚResourcesDictzA default dict for resources.c                 C  s   dS )zHandle missing value.Ú © )ÚselfÚkeyr   r   ú_/home/yuhw/wc/wire-cell-python/wcpy/lib/python3.9/site-packages/nbconvert/exporters/exporter.pyÚ__missing__   s    zResourcesDict.__missing__N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   r   r   r      s   r   c                      s(   e Zd ZdZdZdZ‡ fdd„Z‡  ZS )ÚFilenameExtensionz A trait for filename extensions.r   z*a filename extension, beginning with a dotc                   s6   t ƒ  ||¡}|r2| d¡s2d}t| | j|¡ƒ‚|S )zValidate the file name.Ú.z8FileExtension trait '{}' does not begin with a dot: {!r})ÚsuperÚvalidateÚ
startswithr   ÚformatÚname)r   ÚobjÚvalueÚmsg©Ú	__class__r   r   r   &   s
    zFilenameExtension.validate)r   r   r   r   Údefault_valueZ	info_textr   Ú__classcell__r   r   r#   r   r       s   r   c                      s.  e Zd ZU dZeddd�jdd�Zedd�jdd�Zeddd�jdd�Z	d	Z
d
Zded< edd�jdd�Zded< eƒ Zded< eg d¢dd�jdd�Zded< d1‡ fdd„	Zedd„ ƒZd2dddddœdd„Zd3dd ddd!œd"d#„Zd4dd ddd$œd%d&„Zd5d'd(„Zd)d*„ Zd+d,„ Zd-d.„ Zd/d0„ Z‡  ZS )6ÚExporterzÃ
    Class containing methods that sequentially run a list of preprocessors on a
    NotebookNode object and then return the modified NotebookNode object and
    accompanying resources dict.
    Tz<Disable this exporter (and any exporters inherited from it).)Úhelp)Úconfigz4Extension of the file that should be written to diskFz^Reduces the number of validation steps so that it only occurs after all preprocesors have run.r   NÚstrÚexport_from_notebookz7List of preprocessors, by name or namespace, to enable.z	List[str]ÚpreprocessorsÚ_preprocessors)z-nbconvert.preprocessors.TagRemovePreprocessorz/nbconvert.preprocessors.RegexRemovePreprocessorz/nbconvert.preprocessors.ClearOutputPreprocessorz+nbconvert.preprocessors.ExecutePreprocessorz(nbconvert.preprocessors.coalesce_streamsz+nbconvert.preprocessors.SVG2PDFPreprocessorz)nbconvert.preprocessors.LatexPreprocessorz3nbconvert.preprocessors.HighlightMagicsPreprocessorz1nbconvert.preprocessors.ExtractOutputPreprocessorz6nbconvert.preprocessors.ExtractAttachmentsPreprocessorz1nbconvert.preprocessors.ClearMetadataPreprocessorzZList of preprocessors available by default, by name, namespace,
        instance, or type.zList[t.Any]Údefault_preprocessorsc                   s<   | j }|r| |¡ tƒ jf d|i|¤Ž |  ¡  i | _dS )zô
        Public constructor

        Parameters
        ----------
        config : ``traitlets.config.Config``
            User configuration instance.
        `**kw`
            Additional keyword arguments passed to parent __init__

        r)   N)Údefault_configÚmerger   Ú__init__Ú_init_preprocessorsÚ_nb_metadata)r   r)   ÚkwZwith_default_configr#   r   r   r1   k   s    
zExporter.__init__c                 C  s   t ƒ S )Nr
   )r   r   r   r   r/   €   s    zExporter.default_configr   zt.Any | Nonezt.Anyz%tuple[NotebookNode, dict[str, t.Any]])ÚnbÚ	resourcesr4   Úreturnc                 K  sš   t  |¡}|  |¡}d|d v r4|d d  ¡ |d< |  ||¡\}}d}|dur†| di ¡ dd¡}| di ¡ dd¡}tj ||¡}|j	| j
|< ||fS )av  
        Convert a notebook from a notebook node instance.

        Parameters
        ----------
        nb : :class:`~nbformat.NotebookNode`
            Notebook node (dict-like with attr-access)
        resources : dict
            Additional resources that can be accessed read/write by
            preprocessors and filters.
        `**kw`
            Ignored

        ÚlanguageÚmetadatar   Nr   Úpath)ÚcopyÚdeepcopyÚ_init_resourcesÚlowerÚ_preprocessÚgetÚosr:   Újoinr9   r3   )r   r5   r6   r4   Znb_copyÚnotebook_namer   r:   r   r   r   Úfrom_notebook_node„   s    

zExporter.from_notebook_nodezdict[str, t.Any] | None)Úfilenamer6   r4   r7   c           
      K  sì   |du rt ƒ }d|vs"|d dkr,t ƒ |d< tj |¡\}}tj |¡d }||d d< ||d d< tjjtj |¡tjj	d�}t
jdkr�d	}nd
}| |¡|d d< t|dd��&}	| j|	fd|i|¤ŽW  d  ƒ S 1 sÞ0    Y  dS )ad  
        Convert a notebook from a notebook file.

        Parameters
        ----------
        filename : str
            Full filename of the notebook file to open and convert.
        resources : dict
            Additional resources that can be accessed read/write by
            preprocessors and filters.
        `**kw`
            Ignored

        Nr9   r   r   r   r:   )ÚtzÚwin32z	%B %d, %Yz
%B %-d, %YÚmodified_datezutf-8)Úencodingr6   )r   rA   r:   ÚsplitÚsplitextÚdatetimeÚfromtimestampÚgetmtimeÚtimezoneÚutcÚsysÚplatformÚstrftimeÚopenÚ	from_file)
r   rE   r6   r4   r:   ÚbasenamerC   rH   Zdate_formatÚfr   r   r   Úfrom_filename¥   s"    
ÿ
zExporter.from_filename)Úfile_streamr6   r4   r7   c                 K  s    | j tj|dd�fd|i|¤ŽS )ab  
        Convert a notebook from a notebook file.

        Parameters
        ----------
        file_stream : file-like object
            Notebook file-like object to convert.
        resources : dict
            Additional resources that can be accessed read/write by
            preprocessors and filters.
        `**kw`
            Ignored

        é   )Z
as_versionr6   )rD   ÚnbformatÚread)r   rY   r6   r4   r   r   r   rU   Í   s    ÿÿÿzExporter.from_filec                 C  s´   |du rd}t |ƒ‚t|tƒ}| }|rFt|tƒrFt|ƒ}|  ||¡S |rnt|dƒrn|r^d|_| j 	|¡ |S |r�t
|tƒr�|  || d�|¡ n |r¤|  |ƒ |¡ nt d| ƒ‚dS )aù  
        Register a preprocessor.
        Preprocessors are classes that act upon the notebook before it is
        passed into the Jinja templating engine. Preprocessors are also
        capable of passing additional information to the Jinja
        templating engine.

        Parameters
        ----------
        preprocessor : `nbconvert.preprocessors.Preprocessor`
            A dotted module name, a type, or an instance
        enabled : bool
            Mark the preprocessor as enabled

        Nzpreprocessor must not be NoneÚ__call__T)ÚparentzBpreprocessor must be callable or an importable constructor, got %r)Ú	TypeErrorÚ
isinstanceÚtyper*   r   Úregister_preprocessorÚhasattrÚenabledr-   ÚappendÚ
issubclassr   )r   Úpreprocessorrd   r"   ÚisclassZconstructedZpreprocessor_clsr   r   r   rb   â   s.    
þÿzExporter.register_preprocessorc                 C  s:   g | _ | jD ]}|  |¡ q| jD ]}| j|dd� q"dS )z{
        Register all of the preprocessors needed for this exporter, disabled
        unless specified explicitly.
        T)rd   N)r-   r.   rb   r,   )r   rg   r   r   r   r2     s
    

zExporter._init_preprocessorsc                 C  s�   |d u rt ƒ }t|t ƒs,t ƒ }| |¡ |}d|v r`t|d t ƒs‚t ƒ }| |d ¡ ||d< n"t ƒ |d< |d d s‚d|d d< | j|d< |S )Nr9   r   ZNotebookZoutput_extension)r   r`   ÚupdateÚfile_extension)r   r6   Znew_resourcesZnew_metadatar   r   r   r=   )  s     




zExporter._init_resourcesc                 C  s<   zt j|dd� W n$ t jy6   | j d|¡ ‚ Y n0 d S )NT)Zrelax_add_propsz)Notebook is invalid after preprocessor %s)r[   r   ZValidationErrorÚlogÚerror)r   Únbcrg   r   r   r   Ú_validate_preprocessorA  s
    zExporter._validate_preprocessorc                 C  sr   t  |¡}t  |¡}ttdƒr,t |¡\}}| jD ]$}|||ƒ\}}| js2|  ||¡ q2| jrj|  ||¡ ||fS )aÛ  
        Preprocess the notebook before passing it into the Jinja engine.
        To preprocess the notebook is to successively apply all the
        enabled preprocessors. Output from each preprocessor is passed
        along to the next one.

        Parameters
        ----------
        nb : notebook node
            notebook that is being exported.
        resources : a dict of additional resources that
            can be accessed read/write by preprocessors
        Ú	normalize)r;   r<   rc   r   ro   r-   Úoptimistic_validationrn   )r   r5   r6   rm   ZrescÚ_rg   r   r   r   r?   H  s    



zExporter._preprocess)N)N)N)N)F)r   r   r   r   r   Útagrd   r   rj   rp   Zoutput_mimetyper+   Ú__annotations__r   r,   r-   r.   r1   Úpropertyr/   rD   rX   rU   rb   r2   r=   rn   r?   r&   r   r   r#   r   r'   3   sV   
ÿÿþþýÿýòð
 ÿ" ÿ) ÿ
8r'   )r   Ú
__future__r   Úcollectionsr;   rL   rA   rQ   ÚtypingÚtr[   r   r   Z	traitletsr   r   r   r   r	   Ztraitlets.configr   Ztraitlets.config.configurabler   Ztraitlets.utils.importstringr   Údefaultdictr   r   r'   r   r   r   r   Ú<module>   s    