a
    ø£Beó$  ã                	   @   s¼   d Z ddlZddlZddlmZ ddlmZmZ ddlm	Z	mZm
Z
mZ dd„ Zd&d	d
„Zdd„ Zdd„ ZdddddddddœZdd„ Zdd„ Zdd„ Zdd„ Zd d!„ Zd"d#„ Zd$d%„ ZdS )'z-Code for converting notebooks to and from v3.é    N)Ú
get_logger)Úv3Ú	validatoré   )ÚNotebookNodeÚnbformatÚnbformat_minorÚrandom_cell_idc              
   C   sZ   ddl m}m} z|| |d� W n4 |yT } ztƒ  d||¡ W Y d}~n
d}~0 0 dS )z(Log validation errors, if there are any.r   )ÚValidationErrorÚvalidate)Úversionz"Notebook JSON is not valid v%i: %sN)r   r
   r   r   Úerror)Únbr   r
   r   Úe© r   úV/home/yuhw/wc/wire-cell-python/wcpy/lib/python3.9/site-packages/nbformat/v4/convert.pyÚ_warn_if_invalid   s
    r   c           	      C   sJ  |s| d }|s>d| vr6|dkr0d}t  |¡‚q>d}n| d }|dkrðt| |ƒ |  dd¡}|  d	d¡}|pnd| j_|pzd| j_t| _t| _g  | d
< }|  dg ¡D ] }|d
 D ]}| 	t
|ƒ¡ q°q¤| j dd¡ | j dd¡ t| tƒ | S |dk�r:|tk�r| S |dk �r(| jD ]}tƒ |_�q|| j_t| _| S td| ƒ‚dS )aW  Convert a notebook to latest v4.

    Parameters
    ----------
    nb : NotebookNode
        The Python representation of the notebook to convert.
    from_version : int
        The original version of the notebook to convert.
    from_minor : int
        The original minor version of the notebook to convert (only relevant for v >= 3).
    r   r   é   zEThe v4 notebook does not include the nbformat minor, which is needed.r   é   Úorig_nbformatNÚorig_nbformat_minorÚcellsÚ
worksheetsÚnameÚ Ú	signatureé   zZCannot convert a notebook directly from v%s to v4.  Try using the nbformat.convert module.)r   r
   r   ÚpopÚmetadatar   r   r   r   ÚappendÚupgrade_cellr   r	   ÚidÚ
ValueError)	r   Zfrom_versionZ
from_minorÚmsgr   r   r   ÚwsÚcellr   r   r   Úupgrade   sN    





ÿÿr&   c                 C   sÆ   |   dtƒ ¡ tƒ | _| jdkrn|  dd¡ d| v rD|  d¡| jd< |  dd¡| _|  dd¡| _t	| j
ƒ| _
nT| jd	kr²d
| _|  dd¡}djd| d |  dd¡ ¡ ¡d�| _n| jdkrÂd
| _| S )zöupgrade a cell from v3 to v4

    heading cell:
        - -> markdown heading
    code cell:
        - remove language metadata
        - cell.input -> cell.source
        - cell.prompt_number -> cell.execution_count
        - update outputs
    r   ÚcodeÚlanguager   Ú	collapsedÚinputÚprompt_numberNÚheadingÚmarkdownÚlevelr   z{hashes} {single_line}ú#ú Úsource)ÚhashesZsingle_lineÚhtml)Ú
setdefaultr   r	   r!   Ú	cell_typer   r   r1   Úexecution_countÚupgrade_outputsÚoutputsÚformatÚjoinÚgetÚ
splitlines)r%   r.   r   r   r   r    e   s&    

þ

r    c                 C   sÈ   | j dkrJd| _|  dd¡| _|  dd¡| _| j dd¡| _t| jƒ| _nb| j d	kr¬|  	dd¡}d
|vr¬| 
d¡r¬t d|¡}|dusŠJ ‚| ¡ \}}d| _ || _t|ƒ| _|  dd¡ |  dd¡ | S )a  downgrade a cell from v4 to v3

    code cell:
        - set cell.language
        - cell.input <- cell.source
        - cell.prompt_number <- cell.execution_count
        - update outputs
    markdown cell:
        - single-line heading -> heading cell
    r'   Úpythonr1   r   r6   Nr)   Fr-   Ú
r/   z(#+)\s*(.*)r,   r!   Úattachments)r5   r(   r   r*   r+   r   r)   Údowngrade_outputsr8   r;   Ú
startswithÚreÚmatchÚgroupsr1   Úlenr.   )r%   r1   rC   ÚprefixÚtextr   r   r   Údowngrade_cell†   s$    


rH   z
text/plainz	text/htmlzimage/svg+xmlú	image/pngú
image/jpegz
text/latexúapplication/jsonzapplication/javascript)rG   r3   ÚsvgZpngZjpegÚlatexÚjsonÚ
javascriptc                 C   s,   t  ¡ D ]\}}|| v r|  |¡| |< q| S )z4convert dict with v3 aliases to plain mime-type keys)Ú	_mime_mapÚitemsr   )ÚdÚaliasÚmimer   r   r   Úto_mime_key±   s    rU   c                 C   s.   i }t  ¡ D ]\}}|| v r| | ||< q|S )z.convert dict with mime-type keys to v3 aliases)rP   rQ   )rR   Zd2rS   rT   r   r   r   Úfrom_mime_key¹   s
    rV   c                 C   s  | d dv rÐ|   dtƒ ¡ | d dkr>d| d< |  dd¡| d< i }t| ƒD ]}|d	v rXqJ|  |¡||< qJt|ƒ || d
< t| jƒ d|v rœt |d ¡|d< dD ],}||v r t|| t	ƒr ||  
d¡||< q n4| d dkræd| d< n| d dk�r|  dd¡| d< | S )zËupgrade a single code cell output from v3 to v4

    - pyout -> execute_result
    - pyerr -> error
    - output.type -> output.data.mime/type
    - mime-type keys
    - stream.stream -> stream.name
    Úoutput_type>   Údisplay_dataÚpyoutr   rY   Úexecute_resultr+   Nr6   >   r   rW   r6   ÚdatarK   )rI   rJ   ÚasciiÚpyerrr   ÚstreamÚstdoutr   )r4   r   r   ÚlistrU   r   rN   ÚloadsÚ
isinstanceÚbytesÚdecode)Úoutputr[   Úkeyr   r   r   Úupgrade_outputÂ   s.    	

rg   c                 C   s®   | d dv rz| d dkr0d| d< |   dd¡| d< |   di ¡}d	|v rVt |d	 ¡|d	< t|ƒ}|  |¡ t|  d
i ¡ƒ n0| d dkr�d| d< n| d dkrª|   d¡| d< | S )zÐdowngrade a single code cell output to v3 from v4

    - pyout <- execute_result
    - pyerr <- error
    - output.data.mime/type -> output.type
    - un-mime-type keys
    - stream.stream <- stream.name
    rW   >   rX   rZ   rZ   rY   r6   Nr+   r[   rK   r   r   r]   r^   r   )r   rN   ÚdumpsrV   Úupdater;   )re   r[   r   r   r   Údowngrade_outputç   s    	

rj   c                 C   s   dd„ | D ƒS )z,upgrade outputs of a code cell from v3 to v4c                 S   s   g | ]}t |ƒ‘qS r   )rg   ©Ú.0Úopr   r   r   Ú
<listcomp>  ó    z#upgrade_outputs.<locals>.<listcomp>r   ©r8   r   r   r   r7     s    r7   c                 C   s   dd„ | D ƒS )z.downgrade outputs of a code cell to v3 from v4c                 S   s   g | ]}t |ƒ‘qS r   )rj   rk   r   r   r   rn   
  ro   z%downgrade_outputs.<locals>.<listcomp>r   rp   r   r   r   r@     s    r@   c                 C   sŠ   | j t kr| S t| t ƒ tj | _ tj| _dd„ |  d¡D ƒ}tj|d�g| _| j dd¡ t| tj ƒ | j dt ¡| _	| j dt¡| _
| S )	z”Convert a v4 notebook to v3.

    Parameters
    ----------
    nb : NotebookNode
        The Python representation of the notebook to convert.
    c                 S   s   g | ]}t |ƒ‘qS r   )rH   )rl   r%   r   r   r   rn     ro   zdowngrade.<locals>.<listcomp>r   )r   r   r   r   r   )r   r   r   r   r   Znew_worksheetr   r   r4   r   r   )r   r   r   r   r   Ú	downgrade  s    

rq   )NN)Ú__doc__rN   rB   Ztraitlets.logr   r   r   r   Znbbaser   r   r	   r   r&   r    rH   rP   rU   rV   rg   rj   r7   r@   rq   r   r   r   r   Ú<module>   s2   

K! ø	%