U
    kuf
  ã                   @  sN   d Z ddlmZ ddlmZmZ dd„ Zddd	„Zd
d„ Zdd„ Z	dd„ Z
dS )z8Code for converting notebooks to and from the v2 format.é    )Úannotationsé   )ÚnbformatÚnbformat_minorc                 C  sl   t | tƒr*|  ¡ D ]\}}t|ƒ| |< qn>t | tƒrTt| ƒD ]\}}t|ƒ| |< q<nt | tƒrh|  d¡} | S )z^There should be no bytes objects in a notebook

    v2 stores png/jpeg as b64 ascii bytes
    Úascii)Ú
isinstanceÚdictÚitemsÚ_unbytesÚlistÚ	enumerateÚbytesÚdecode)ÚobjÚkÚvÚi© r   ú7/tmp/pip-unpacked-wheel-zj9qk9v2/nbformat/v3/convert.pyr
   
   s    



r
   é   c                 C  s‚   |dkrNt | _ t| _d| _t| ƒ} | d D ]}|d D ]}| di ¡ q6q*| S |dkrn|tkrd|| _t| _| S d| }t|ƒ‚dS )aP  Convert a notebook to v3.

    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   Ú
worksheetsÚcellsÚmetadataé   zZCannot convert a notebook directly from v%s to v3.  Try using the nbformat.convert module.N)r   r   Zorig_nbformatr
   Ú
setdefaultZorig_nbformat_minorÚ
ValueError)ÚnbZfrom_versionZ
from_minorÚwsÚcellÚmsgr   r   r   Úupgrade   s&    ÿÿr    c                 C  s*   d| _ |  dd¡}d| d | j | _dS )z-turn heading cell into corresponding markdownÚmarkdownÚlevelr   ú#ú N)Ú	cell_typeÚpopÚsource)r   r"   r   r   r   Úheading_to_md>   s    r(   c                 C  s
   d| _ dS )zlet raw passthrough as markdownr!   N)r%   )r   r   r   r   Ú	raw_to_mdE   s    r)   c                 C  sV   | j dkr| S d| _ | jD ]6}|jD ]*}|jdkr<t|ƒ q$|jdkr$t|ƒ q$q| S )z”Convert a v3 notebook to v2.

    Parameters
    ----------
    nb : NotebookNode
        The Python representation of the notebook to convert.
    r   r   ÚheadingÚraw)r   r   r   r%   r(   r)   )r   r   r   r   r   r   Ú	downgradeJ   s    





r,   N)r   r   )Ú__doc__Ú
__future__r   Znbbaser   r   r
   r    r(   r)   r,   r   r   r   r   Ú<module>   s   
#