U
    kufÅ  ã                   @  sh   d Z ddlmZ dd„ Zdd„ Zdd„ Zd	d
hZdd„ Zdd„ Zdd„ Z	G dd„ dƒZ
G dd„ dƒZdS )z3Base classes and utilities for readers and writers.é    )Úannotationsc                 C  s   | dkp|   d¡o|  d¡S )z4Is a key a JSON mime-type that should be left alone?zapplication/jsonzapplication/z+json)Ú
startswithÚendswith)Úmime© r   ú6/tmp/pip-unpacked-wheel-zj9qk9v2/nbformat/v4/rwbase.pyÚ_is_json_mime   s    ÿr   c                 C  sL   t |  ¡ ƒD ]:\}}t|ƒst|t ƒrtdd„ |D ƒƒrd |¡| |< q| S )z>Rejoin the multi-line string fields in a mimebundle (in-place)c                 s  s   | ]}t |tƒV  qd S )N)Ú
isinstanceÚstr)Ú.0Úliner   r   r   Ú	<genexpr>   s     z%_rejoin_mimebundle.<locals>.<genexpr>Ú )ÚlistÚitemsr   r	   ÚallÚjoin©ÚdataÚkeyÚvaluer   r   r   Ú_rejoin_mimebundle   s    ÿþýr   c                 C  sÄ   | j D ]¸}d|kr,t|jtƒr,d |j¡|_| di ¡}| ¡ D ]\}}t|ƒ q@| dd¡dkr| dg ¡D ]N}| dd¡}|d	kr˜t| d
i ¡ƒ qn|rnt| dd¡tƒrnd |j¡|_qnq| S )a&  rejoin multiline text into strings

    For reversing effects of ``split_lines(nb)``.

    This only rejoins lines that have been split, so if text objects were not split
    they will pass through unchanged.

    Used when reading JSON files that may have been passed through split_lines.
    Úsourcer   ÚattachmentsÚ	cell_typeNÚcodeÚoutputsÚoutput_type¾   Údisplay_dataÚexecute_resultr   Útext)	Úcellsr	   r   r   r   Úgetr   r   r!   )ÚnbÚcellr   Ú_Ú
attachmentÚoutputr   r   r   r   Úrejoin_lines   s    


r)   zapplication/javascriptzimage/svg+xmlc                 C  sD   t |  ¡ ƒD ]2\}}t|tƒr| d¡s0|tkr| d¡| |< q| S )z9Split multi-line string fields in a mimebundle (in-place)ztext/T)r   r   r	   r
   r   Ú_non_text_split_mimesÚ
splitlinesr   r   r   r   Ú_split_mimebundle=   s    r,   c                 C  s°   | j D ]¤}| dd¡}t|tƒr.| d¡|d< | di ¡}| ¡ D ]\}}t|ƒ qB|jdkr|jD ]D}|j	dkr„t| di ¡ƒ qd|j	dkrdt|j
tƒrd|j
 d¡|_
qdq| S )	zÙsplit likely multiline text into lists of strings

    For file output more friendly to line-based VCS. ``rejoin_lines(nb)`` will
    reverse the effects of ``split_lines(nb)``.

    Used when writing JSON files.
    r   NTr   r   r   r   Ústream)r"   r#   r	   r
   r+   r   r,   r   r   r   r!   )r$   r%   r   r   r&   r'   r(   r   r   r   Úsplit_linesE   s    





r.   c                 C  sH   | j  dd¡ | j  dd¡ | j  dd¡ | jD ]}|j  dd¡ q0| S )zsStrip transient values that shouldn't be stored in files.

    This should be called in *both* read and write.
    Zorig_nbformatNZorig_nbformat_minorÚ	signatureZtrusted)ÚmetadataÚpopr"   )r$   r%   r   r   r   Ústrip_transient_   s    
r2   c                   @  s    e Zd ZdZdd„ Zdd„ ZdS )ÚNotebookReaderzA class for reading notebooks.c                 K  s   d}t |ƒ‚dS )zRead a notebook from a string.z'reads must be implemented in a subclassN©ÚNotImplementedError)ÚselfÚsÚkwargsÚmsgr   r   r   Úreadso   s    zNotebookReader.readsc                 K  s   |  ¡ }| j|f|ŽS )z'Read a notebook from a file like object)Úreadr:   )r6   Úfpr8   Únbsr   r   r   r;   t   s    zNotebookReader.readN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r:   r;   r   r   r   r   r3   l   s   r3   c                   @  s    e Zd ZdZdd„ Zdd„ ZdS )ÚNotebookWriterzA class for writing notebooks.c                 K  s   d}t |ƒ‚dS )zWrite a notebook to a string.z(writes must be implemented in a subclassNr4   )r6   r$   r8   r9   r   r   r   Úwrites}   s    zNotebookWriter.writesc                 K  s   | j |f|Ž}| |¡S )z&Write a notebook to a file like object)rC   Úwrite)r6   r$   r<   r8   r=   r   r   r   rD   ‚   s    zNotebookWriter.writeN)r>   r?   r@   rA   rC   rD   r   r   r   r   rB   z   s   rB   N)rA   Ú
__future__r   r   r   r)   r*   r,   r.   r2   r3   rB   r   r   r   r   Ú<module>   s   þ