B
    £'=[~  ã               @   s    d Z ddlmZmZ ddlmZ ddlmZmZm	Z	 dd„ Z
ddd	d
ddgZdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ ZG dd„ deƒZG dd„ deƒZdS )z3Base classes and utilities for readers and writers.é    )ÚencodestringÚdecodestring)Ú	py3compat)Ústr_to_bytesÚunicode_typeÚstring_typesc             C   sj   xd| j D ]Z}xT|jD ]J}|jdkrx:|jD ]0}d|krDt|jdƒ|_d|kr*t|jdƒ|_q*W qW qW | S )zôRestore bytes of image data from unicode-only formats.
    
    Base64 encoding is handled elsewhere.  Bytes objects in the notebook are
    always b64-encoded. We DO NOT encode/decode around file formats.
    
    Note: this is never used
    ÚcodeÚpngÚasciiÚjpeg)Ú
worksheetsÚcellsÚ	cell_typeÚoutputsr   r	   r   )ÚnbÚwsÚcellÚoutput© r   ú1lib/python3.7/site-packages/nbformat/v3/rwbase.pyÚrestore_bytes   s    
r   ÚtextZhtmlZsvgZlatexZ
javascriptZjsonc             C   s*   | r| d   d¡rd | ¡S d | ¡S dS )zšjoin lines that have been written by splitlines()
    
    Has logic to protect against `splitlines()`, which
    should have been `splitlines(True)`
    r   )Ú
úÚ r   N)ÚendswithÚjoin)Úlinesr   r   r   Ú_join_lines#   s    
r   c             C   sÀ   xº| j D ]°}xª|jD ] }|jdkr„d|krBt|jtƒrBt|jƒ|_xp|jD ]4}x.tD ]&}| 	|d¡}t|tƒrTt|ƒ||< qTW qJW qx.dD ]&}| 	|d¡}t|tƒrŠt|ƒ||< qŠW qW qW | S )a2  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.
    r   ÚinputN)ÚsourceÚrendered)
r   r   r   Ú
isinstancer   Úlistr   r   Ú_multiline_outputsÚget)r   r   r   r   ÚkeyÚitemr   r   r   Úrejoin_lines1   s    





r(   c             C   sÆ   xÀ| j D ]¶}x°|jD ]¦}|jdkrˆd|krDt|jtƒrD|j d¡|_xt|jD ]6}x0tD ](}| 	|d¡}t|tƒrV| d¡||< qVW qLW qx0dD ](}| 	|d¡}t|tƒrŽ| d¡||< qŽW qW qW | 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   r   TN)r    r!   )
r   r   r   r"   r   r   Ú
splitlinesr   r$   r%   )r   r   r   r   r&   r'   r   r   r   Úsplit_linesM   s    




r*   c             C   sš   x”| j D ]Š}x„|jD ]z}|jdkrxj|jD ]`}d|kr\t|jtƒrP|j d¡|_t|jƒ|_d|kr*t|j	tƒr~|j	 d¡|_	t|j	ƒ|_	q*W qW qW | S )zmRestore all bytes objects in the notebook from base64-encoded strings.
    
    Note: This is never used
    r   r	   r
   r   )
r   r   r   r   r"   r	   r   Úencoder   r   )r   r   r   r   r   r   r   Úbase64_decodei   s    
r,   c             C   sr   xl| j D ]b}x\|jD ]R}|jdkrxB|jD ]8}d|krHt|jƒ d¡|_d|kr*t|jƒ d¡|_q*W qW qW | S )zŠBase64 encode all bytes objects in the notebook.
    
    These will be b64-encoded unicode strings
    
    Note: This is never used
    r   r	   r
   r   )r   r   r   r   r   r	   Údecoder   )r   r   r   r   r   r   r   Úbase64_encode}   s    
r.   c             C   s`   |   dd¡ |   dd¡ xB| d D ]6}x0|d D ]$}| di ¡  dd¡ |  dd¡ q0W q"W | 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_minorr   r   ZmetadataZtrusted)Úpopr%   )r   r   r   r   r   r   Ústrip_transient   s    r0   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   t dƒ‚dS )zRead a notebook from a string.z'loads must be implemented in a subclassN)ÚNotImplementedError)ÚselfÚsÚkwargsr   r   r   Úreads¢   s    zNotebookReader.readsc             K   s0   |  ¡ }tjs"t|tƒs"t |¡}| j|f|ŽS )z'Read a notebook from a file like object)Úreadr   ÚPY3r"   r   Ústr_to_unicoder6   )r3   Úfpr5   Únbsr   r   r   r7   ¦   s    
zNotebookReader.readN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r6   r7   r   r   r   r   r1   Ÿ   s   r1   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   t dƒ‚dS )zWrite a notebook to a string.z'loads must be implemented in a subclassN)r2   )r3   r   r5   r   r   r   Úwrites±   s    zNotebookWriter.writesc             K   s2   | j |f|Ž}tjs(t|tƒs(t |¡}| |¡S )z&Write a notebook to a file like object)rA   r   r8   r"   r   r9   Úwrite)r3   r   r:   r5   r;   r   r   r   rB   µ   s    
zNotebookWriter.writeN)r<   r=   r>   r?   rA   rB   r   r   r   r   r@   ®   s   r@   N)r?   Úbase64r   r   Zipython_genutilsr   Zipython_genutils.py3compatr   r   r   r   r$   r   r(   r*   r,   r.   r0   Úobjectr1   r@   r   r   r   r   Ú<module>   s   