a
    ø£Be1k  ã                $   @  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
mZ ddlmZ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mZmZ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% ddl&m'Z' dgZ(ej) *e+¡Z,ej) -ej) .e,d¡¡Z/e%j0e%j1e%j2e%j3e%j4e%j5e%j6e%j7e%j8e%j9e%j:e%j;e%j<e%j=e%j>e%j?e%j@e%jAe%jBe%jCe%jDe%jEe%jFe%jGe%jHe%jIe%jJe%jKejLdd„ dd„ dd„ e%jMe%jNe%jOdœ#ZPdd„ ZQdd„ ZRG dd„ deƒZSG dd„ de'ƒZTdS )zˆThis module defines TemplateExporter, a highly configurable converter
that uses Jinja2 to export notebook files into different formats.
é    )ÚannotationsN)ÚPath)Ú
BaseLoaderÚChoiceLoaderÚ
DictLoaderÚEnvironmentÚFileSystemLoaderÚTemplateNotFound)Újupyter_path)ÚNotebookNode)ÚBoolÚDictÚ	HasTraitsÚListÚUnicodeÚdefaultÚobserveÚvalidate)ÚConfig)Úimport_item©Úfiltersé   )ÚExporterzjinja2.ext.loopcontrolsz
../../.gitc                 C  s   t  t| ƒ¡S ©N©ÚhtmlÚescapeÚstr©Ús© r!   úg/home/yuhw/wc/wire-cell-python/wcpy/lib/python3.9/site-packages/nbconvert/exporters/templateexporter.pyÚ<lambda>I   ó    r#   c                 C  s   t jt| ƒdd�S )NF)Úquoter   r   r!   r!   r"   r#   J   r$   c                 C  s   |   dd¡S )Nú/z\/)Úreplacer   r!   r!   r"   r#   K   r$   )#ÚindentÚmarkdown2htmlÚmarkdown2asciidocÚ	ansi2htmlZfilter_data_typeÚ	get_linesZhighlight2htmlZhighlight2latexÚipython2pythonÚ
posix_pathÚmarkdown2latexÚmarkdown2rstÚcomment_linesÚ
strip_ansiÚstrip_dollarsÚstrip_files_prefixÚ	html2textÚ
add_anchorÚ
ansi2latexÚ	wrap_textÚescape_latexÚcitation2latexÚpath2urlÚadd_promptsÚ
ascii_onlyÚprevent_list_blocksÚget_metadataÚconvert_pandocZ
json_dumpsZescape_htmlZescape_html_keep_quotesZescape_html_scriptÚ
clean_htmlÚstrip_trailing_newlineÚtext_base64c                 C  sl   |  ¡ D ]^\}}t|tƒrH|| vr*i | |< t| | |ƒ | | sf| |= q|du r^|  |d¡ q|| |< q| S )z]Recursively update one dictionary using another.
    None values will delete their keys.
    N)ÚitemsÚ
isinstanceÚdictÚrecursive_updateÚpop)ÚtargetÚnewÚkÚvr!   r!   r"   rG   T   s    

rG   c                 C  s   t j| tdd� dS )zEmit a deprecation warning.é   ©Ú
stacklevelN)ÚwarningsÚwarnÚDeprecationWarning)Úmsgr!   r!   r"   Ú
deprecatedj   s    rT   c                   @  s(   e Zd ZdZdd„ Zdd„ Zdd„ ZdS )	ÚExtensionTolerantLoaderaN  A template loader which optionally adds a given extension when searching.

    Constructor takes two arguments: *loader* is another Jinja loader instance
    to wrap. *extension* is the extension, which will be added to the template
    name if finding the template without it fails. This should include the dot,
    e.g. '.tpl'.
    c                 C  s   || _ || _dS )zInitialize the loader.N)ÚloaderÚ	extension)ÚselfrV   rW   r!   r!   r"   Ú__init__x   s    z ExtensionTolerantLoader.__init__c                 C  sR   z| j  ||¡W S  tyL   | | j¡r4t|ƒd‚| j  ||| j ¡ Y S 0 dS )zGet the source for a template.N)rV   Ú
get_sourcer	   ÚendswithrW   )rX   ÚenvironmentÚtemplater!   r!   r"   rZ   }   s    
z"ExtensionTolerantLoader.get_sourcec                 C  s
   | j  ¡ S )zList available templates.)rV   Úlist_templates©rX   r!   r!   r"   r^   †   s    z&ExtensionTolerantLoader.list_templatesN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__rY   rZ   r^   r!   r!   r!   r"   rU   o   s   	rU   c                      s’  e Zd ZU dZejdd ee ¡ ƒ¡ d�ZdZ	dodd„Z
edd	„ ƒZdZdpd
d„Zedd„ ƒZe‡ fdd„ƒZedd�jddd�Zedddd�jddd�Zeddd�jdd�Zeddd�jdd�ZdZdZedƒdd„ ƒZedƒd d!„ ƒZedƒd"d#„ ƒZed$ƒd%d&„ ƒZ e!d'gƒjddd(�Z"e!ƒ jddd(�Z#d)e$d*< e!g ƒjddd(�Z%d)e$d+< ed*ƒd,d-„ ƒZ&eƒ jddd(�Z'e!e(d.d/ƒd0d�jdd�Z)eƒ jddd(�Z'ed1ƒd2d3„ ƒZ*edd4d�jdd5�Z+edd6d�jdd5�Z,edd7d�jdd5�Z-edd8d�jdd5�Z.edd9d�jdd5�Z/edd:d�jdd5�Z0edd;d�jdd5�Z1edd<d�jdd5�Z2edd=d�jdd5�Z3e!d>d�jdd�Z4d?e$d@< e5dAd�jddd(�Z6e!dBd�jdd5�Z7d)e$dC< edCƒdDdE„ ƒZ8dq‡ fdFdG„	Z9dHdI„ Z:drdJdKdLdMdNœ‡ fdOdP„Z;dsdLdKdLdMdQœ‡ fdRdS„Z<dtdTdKdLdMdUœ‡ fdVdW„Z=dXdY„ Z>dZd[„ Z?d\d]„ Zd^d_„ Z@‡ fd`da„ZAdbdc„ ZBeddƒdudedf„ƒZCeDdgdh„ ƒZEdidj„ ZFdkdl„ ZG‡ fdmdn„ZH‡  ZIS )vÚTemplateExporteraá  
    Exports notebooks into other file formats.  Uses Jinja 2 templating engine
    to output new formats.  Inherit from this class if you are creating a new
    template type along with new filters/preprocessors.  If the filters/
    preprocessors provided by default suffice, there is no need to inherit from
    this class.  Instead, override the template_file and file_extension
    traits via a config file.

    Filters available by default for templates:

    {filters}
    z- z
    - r   Nc                 C  s
   d | _ d S r   )Ú_template_cached©rX   Úchanger!   r!   r"   Ú_invalidate_template_cachež   s    z+TemplateExporter._invalidate_template_cachec                 C  s   | j d u r|  ¡ | _ | j S r   )re   Ú_load_templater_   r!   r!   r"   r]   ¡   s    

zTemplateExporter.templatec                 C  s   d | _ |  ¡  d S r   )Ú_environment_cachedrh   rf   r!   r!   r"   Ú_invalidate_environment_cache©   s    z.TemplateExporter._invalidate_environment_cachec                 C  s   | j d u r|  ¡ | _ | j S r   )rj   Ú_create_environmentr_   r!   r!   r"   r\   ­   s    

zTemplateExporter.environmentc                   s<   t ddiddidœƒ}tƒ jr8tƒ j ¡ }| |¡ |}|S )NÚenabledT)ZRegexRemovePreprocessorZTagRemovePreprocessor)r   ÚsuperÚdefault_configÚcopyÚmerge)rX   ÚcÚc2©Ú	__class__r!   r"   ro   ³   s    þÿ
zTemplateExporter.default_configzName of the template to use)ÚhelpT)ÚconfigÚaffects_templatez Name of the template file to use)Ú
allow_nonerv   Ú zraw template string©Úaffects_environmentFz%Enable Jinja async template executionz<memory>Útemplate_namec                 C  st   |d }|rp|  d¡rptjd| j› d�tdd� tj | j¡\}| _|rXtj |¡\}}|rptj 	|¡rp|g| _
|S )NÚvalueú.tplz 5.x style template name passed 'úƒ'. Use --template-name for the template directory with a index.<ext>.j2 file and/or --template-file to denote a different template.rM   rN   )r[   rP   rQ   r}   rR   ÚosÚpathÚsplitÚtemplate_fileÚisabsÚextra_template_basedirs)rX   rg   r}   Ú	directoryr!   r!   r"   Ú_template_name_validateÒ   s    ýz(TemplateExporter._template_name_validater„   c                 C  s‚   |d }|dkr| j | _d S tj |¡}tj |¡r~tj |¡\}| _|g| j¢| _| jr~| j d¡r~t	j
d|› d�tdd� d S )NrJ   r   r   z 5.x style template file passed 'r€   rM   rN   )Zdefault_templater„   r�   r‚   ÚabspathÚisfilerƒ   Úextra_template_pathsr[   rP   rQ   rR   )rX   rg   rJ   Ú	full_pathr‡   r!   r!   r"   Ú_template_file_changedâ   s    
ýz'TemplateExporter._template_file_changedc                 C  s   | j rd| j  S d S )NÚindex)Útemplate_extensionr_   r!   r!   r"   Ú_template_file_defaultö   s    z'TemplateExporter._template_file_defaultÚraw_templatec                 C  s   |d s| j | _|  ¡  d S )NrJ   )Ú_last_template_filer„   rh   rf   r!   r!   r"   Ú_raw_template_changedû   s    z&TemplateExporter._raw_template_changedÚ.)rw   r|   z	List[str]r†   r‹   c                 C  s
   t  ¡ gS r   )r�   Úgetcwdr_   r!   r!   r"   Ú _default_extra_template_basedirs  s    z1TemplateExporter._default_extra_template_basedirsÚ	nbconvertÚ	templatesz*Path where templates can be installed too.r�   c                 C  s   | j r| j d S | j S d S )Nz.j2)Zfile_extensionr_   r!   r!   r"   Ú_template_extension_default  s    
z,TemplateExporter._template_extension_defaultzNThis allows you to exclude code cell inputs from all templates if set to True.)rw   zKThis allows you to exclude input prompts from all templates if set to True.zOThis allows you to exclude code cell outputs from all templates if set to True.zLThis allows you to exclude output prompts from all templates if set to True.zSThis allows you to exclude output of stdin stream from lab template if set to True.zHThis allows you to exclude code cells from all templates if set to True.zLThis allows you to exclude markdown cells from all templates if set to True.zGThis allows you to exclude raw cells from all templates if set to True.zKThis allows you to exclude unknown cells from all templates if set to True.z[Jinja loaders to find templates. Will be tried in order before the default FileSystem ones.zList[t.Any]Úextra_loaderszVDictionary of filters, by name and namespace, to add to the Jinja
        environment.z>formats of raw cells to be included in this Exporter's output.Úraw_mimetypesc                 C  s
   | j dgS )Nrz   )Úoutput_mimetyper_   r!   r!   r"   Ú_raw_mimetypes_defaultN  s    z'TemplateExporter._raw_mimetypes_defaultc                   sN   t ƒ jf d|i|¤Ž |  | jt| jdd�ƒ¡ |  | jt| jdd�ƒ¡ dS )a™  
        Public constructor

        Parameters
        ----------
        config : config
            User configuration instance.
        extra_loaders : list[of Jinja Loaders]
            ordered list of Jinja loader to find templates. Will be tried in order
            before the default FileSystem ones.
        template_file : str (optional, kw arg)
            Template to use when exporting.
        rw   Tr{   )rx   N)rn   rY   r   rk   ÚlistZtraitsrh   )rX   rw   Úkwrt   r!   r"   rY   S  s
    ÿzTemplateExporter.__init__c                 C  sš   |   ¡ �8 | jr$| j| jkr$| j| _| jr2| j| _W d  ƒ n1 sF0    Y  | jsbd}t|ƒ‚| j}| j d|¡ | j dtj	 
| j¡¡ | j |¡S )z’Load the Jinja template object from the template file

        This is triggered by various trait changes that would change the template.
        NzNo template_file specified!zAttempting to load template %sz    template_paths: %s)Zhold_trait_notificationsr„   Ú_raw_template_keyr’   r‘   Ú
ValueErrorÚlogÚdebugr�   ÚpathsepÚjoinÚtemplate_pathsr\   Zget_template)rX   rS   r„   r!   r!   r"   ri   h  s    
&zTemplateExporter._load_templater   zdict[str, t.Any] | Nonezt.Anyztuple[str, dict[str, t.Any]])ÚfilenameÚ	resourcesrŸ   Úreturnc                   s   t ƒ j||fi |¤ŽS )z#Convert a notebook from a filename.)rn   Úfrom_filename)rX   r§   r¨   rŸ   rt   r!   r"   rª   �  s    zTemplateExporter.from_filename)Úfile_streamr¨   rŸ   r©   c                   s   t ƒ j||fi |¤ŽS )zConvert a notebook from a file.)rn   Ú	from_file)rX   r«   r¨   rŸ   rt   r!   r"   r¬   ‡  s    zTemplateExporter.from_filer   )Únbr¨   rŸ   r©   c                   s”   t ƒ j||fi |¤Ž\}}| d| j¡ | j | j | j | j | j | j	 | j
 | j | j | joh| jdœ
|d< | jj||d�}| d¡}||fS )a5  
        Convert a notebook from a notebook node instance.

        Parameters
        ----------
        nb : :class:`~nbformat.NotebookNode`
            Notebook node
        resources : dict
            Additional resources that can be accessed read/write by
            preprocessors and filters.
        r›   )
Zinclude_codeZinclude_markdownZinclude_rawZinclude_unknownZinclude_inputZinclude_outputZinclude_output_stdinZinclude_input_promptZinclude_output_promptZ	no_promptZglobal_content_filter)r­   r¨   z
)rn   Úfrom_notebook_nodeÚ
setdefaultr›   Úexclude_code_cellÚexclude_markdownÚexclude_rawÚexclude_unknownÚexclude_inputÚexclude_outputÚexclude_output_stdinÚexclude_input_promptÚexclude_output_promptr]   ÚrenderÚlstrip)rX   r­   r¨   rŸ   Znb_copyÚoutputrt   r!   r"   r®   �  s     
ö

z#TemplateExporter.from_notebook_nodec           	      C  s¸   |du rd}t |ƒ‚t|tƒ}| }|rJt|tfƒrJt|ƒ}|  |||¡S |rft|dƒrf||j|< |S |rŽt|t	ƒrŽ|| d�}|  |||¡ n&|r¨|ƒ }|  |||¡ nd}t |ƒ‚dS )á9  
        Register a filter.
        A filter is a function that accepts and acts on one string.
        The filters are accessible within the Jinja templating engine.

        Parameters
        ----------
        name : str
            name to give the filter in the Jinja engine
        filter : filter
        NÚfilterÚ__call__)Úparent)
Ú	TypeErrorrE   Útyper   r   Ú_register_filterÚhasattrr   Ú
issubclassr   )	rX   ÚenvironÚnameÚjinja_filterrS   ÚisclassZconstructedZ
filter_clsZfilter_instancer!   r!   r"   rÂ   ¯  s&    


z!TemplateExporter._register_filterc                 C  s   |   | j||¡S )r¼   )rÂ   r\   )rX   rÆ   rÇ   r!   r!   r"   Úregister_filterÞ  s    z TemplateExporter.register_filterc                 C  s   t  ¡ S )a|  Override in subclasses to provide extra filters.

        This should return an iterable of 2-tuples: (name, class-or-function).
        You should call the method on the parent class and include the filters
        it provides.

        If a name is repeated, the last filter provided wins. Filters from
        user-supplied config win over filters provided by classes.
        )Údefault_filtersrD   r_   r!   r!   r"   rÊ   ì  s    
z TemplateExporter.default_filtersc                 C  s°   | j }| j dd |¡¡ g | j¢tt|ƒ| jƒ‘t| j	| j
iƒ‘}tt|ƒt| jd�}tj|jd< |  ¡ D ]\}}|  |||¡ ql| jr¬| j ¡ D ]\}}|  |||¡ q”|S )z:
        Create the Jinja templating environment.
        zTemplate paths:
	%sú
	)rV   Ú
extensionsÚenable_asyncÚuuid4)r¦   r¢   r£   r¥   rš   rU   r   r�   r   r    r‘   r   r   ÚJINJA_EXTENSIONSrÍ   ÚuuidrÎ   ÚglobalsrÊ   rÂ   r   rD   )rX   ÚpathsÚloadersr\   Úkeyr~   Zuser_filterr!   r!   r"   rl   ø  s*    ÿþýýz$TemplateExporter._create_environmentc                   s˜   t ƒ  ¡  |  ¡ }| di ¡}t| ¡ dd„ d�D ]`\}}|d ur2| ¡ }| d¡}t|ƒ}|j	| j
v rz| | j
|j	 ¡ |f i |¤Ž}|  |¡ q2d S )NÚpreprocessorsc                 S  s   | d S )Nr   r!   )Úxr!   r!   r"   r#     r$   z6TemplateExporter._init_preprocessors.<locals>.<lambda>)rÔ   rÁ   )rn   Ú_init_preprocessorsÚ	_get_confÚgetÚsortedrD   rp   rH   r   r`   rw   ÚupdateZregister_preprocessor)rX   ÚconfrÕ   Ú_ÚpreprocessorÚkwargsZpreprocessor_clsrt   r!   r"   r×     s    

z$TemplateExporter._init_preprocessorsc              	   C  sb   i }t t| jƒD ]L}|d }| ¡ r| ¡ � }t|t |¡ƒ}W d   ƒ q1 sR0    Y  q|S )Nú	conf.json)Úmapr   r¦   ÚexistsÚopenrG   ÚjsonÚload)rX   rÜ   r‚   Z	conf_pathÚfr!   r!   r"   rØ   )  s    
0zTemplateExporter._get_confr¦   c                 C  s  g }|   ¡ }|  ¡ }|D ]z}| jD ],}tj ||¡}|rDtj |¡r"| |¡ q"|D ]<}tj |dd¡}tj ||¡}|r†tj |¡rT| |¡ qTq|D ]D}| |¡ tj |dd¡}| |¡ tj |ddd¡}	| |	¡ q˜g }
| jD ]}|rütj |¡rè|
 |¡ qè|| j	 |
 S )Nr—   r˜   Úcompatibility)
Úget_prefix_root_dirsÚget_template_namesr†   r�   r‚   r¥   râ   ÚappendÚtemplate_data_pathsr‹   )rX   ZpruneÚ	root_dirsrÒ   Útemplate_namesr}   Úbase_dirr‚   Úroot_dirZcompatibility_dirZadditional_pathsr!   r!   r"   Ú_template_paths2  s0    



z TemplateExporter._template_pathsc                 C  s*   |dkrddiS |dkr&dddidœS d	S )
zGet the base template config.Zdisplay_priorityÚbase_templateÚbaseÚfullZclassicz	text/htmlT)rñ   Ú	mimetypesNr!   )ÚclsrÆ   r!   r!   r"   Ú$get_compatibility_base_template_confU  s    z5TemplateExporter.get_compatibility_base_template_confc              	   C  sf  g }|   ¡ }| j}i }|du�rþ| |¡ i }d}| jD ]t}tj ||¡}tj |¡rZd}tj |d¡}	tj |	¡r8t|	ƒ� }
t	t
 |
¡|ƒ}W d  ƒ q81 s¢0    Y  q8|D ]z}tj |dd|¡}tj |¡rØd}tj |d¡}	tj |	¡r²t|	ƒ� }
t	t
 |
¡|ƒ}W d  ƒ q²1 �s"0    Y  q²|�sÚ|D ]x}|d }tj |ddd|¡}tj |¡�r8d}tjd	| j› d
�tdd� || _|  |¡}t t| d¡¡| _ �q²�q8|�sÚd |¡}d|›d|› �}t|ƒ‚t	t|ƒ|ƒ}t tj| d¡¡}q|}dd„ | di ¡ ¡ D ƒ}| j�rb| j|v�rb|�rbd |¡}d| j›d| j›d|› �}t|ƒ‚|S )zWFinds a list of template names where each successive template name is the base templateNFTrà   r—   r˜   r   rç   z5.x template name passed 'z1'. Use 'lab' or 'classic' for new template usage.rM   rN   rñ   rË   z$No template sub-directory with name z  found in the following paths:
	c                 S  s   g | ]\}}|r|‘qS r!   r!   )Ú.0Úmimetyperm   r!   r!   r"   Ú
<listcomp>‘  r$   z7TemplateExporter.get_template_names.<locals>.<listcomp>rô   zUnsupported mimetype z for template z, mimetypes supported are: 
	)rè   r}   rê   r†   r�   r‚   r¥   râ   rã   rG   rä   rå   rP   rQ   rR   r„   rö   ÚtÚcastr   rÙ   r¡   rF   ÚAnyrD   rœ   )rX   rí   rì   rñ   Zmerged_confrÜ   Zfound_at_least_onerî   Ztemplate_dirZ	conf_fileræ   rï   Zcompatibility_fileZcompatibility_pathrÒ   rS   rô   Zsupported_mimetypesr!   r!   r"   ré   ^  sl    



0
2
ÿý



z#TemplateExporter.get_template_namesc                 C  s:   g }t r*| tj tj tdddd¡¡¡ | tƒ ¡ |S )zGet the prefix root dirs.z..ÚshareÚjupyter)	ÚDEV_MODErê   r�   r‚   r‰   r¥   ÚROOTÚextendr
   )rX   rì   r!   r!   r"   rè   ˜  s
    "z%TemplateExporter.get_prefix_root_dirsc                   s   t ƒ  |¡}t|d< |S )NrT   )rn   Ú_init_resourcesrT   )rX   r¨   rt   r!   r"   r  ¢  s    z TemplateExporter._init_resources)N)N)N)N)N)N)TN)Jr`   ra   rb   rc   Úformatr¥   rÚ   rÊ   Úkeysre   rh   Úpropertyr]   rj   rk   r\   ro   r   Útagr}   r„   r‘   r   rÍ   r’   r    r   rˆ   r   r�   r   r�   r“   r   r¦   r†   Ú__annotations__r‹   r–   r�   r
   rë   r™   r´   r·   rµ   r¸   r¶   r°   r±   r²   r³   rš   r   r   r›   r�   rY   ri   rª   r¬   r®   rÂ   rÉ   rl   r×   rØ   rð   Úclassmethodrö   ré   rè   r  Ú__classcell__r!   r!   rt   r"   rd   ‹   s  





ÿÿÿ





ÿþ
ÿþÿþþýÿþþýÿþÿþÿþÿþÿýÿýÿþ
 ÿ ÿ ÿ"/	"
:
rd   )Urc   Ú
__future__r   r   rä   r�   Útypingrú   rÐ   rP   Úpathlibr   Zjinja2r   r   r   r   r   r	   Zjupyter_core.pathsr
   Znbformatr   Z	traitletsr   r   r   r   r   r   r   r   Ztraitlets.configr   Ztraitlets.utils.importstringr   r—   r   Zexporterr   rÏ   r‚   ÚdirnameÚ__file__r   râ   r¥   rÿ   r(   r)   r*   r+   ZDataTypeFilterr,   ZHighlight2HTMLZHighlight2Latexr-   r.   r/   r0   r1   r2   r3   r4   r5   r6   r7   r8   r9   r:   r;   r<   r=   r>   r?   r@   ÚdumpsrA   rB   rC   rÊ   rG   rT   rU   rd   r!   r!   r!   r"   Ú<module>   sv    (Û*