ó
'=[c           @   s  d  Z  d d l Z d d l Z d d l m Z m Z m Z d d l m Z d d l m	 Z	 d   Z
 d d	 d
  Z d   Z d   Z i d d 6d d 6d d 6d d 6d d 6d d 6d d 6d d 6Z d   Z d   Z d   Z d    Z d!   Z d"   Z d#   Z d S($   s-   Code for converting notebooks to and from v3.i˙˙˙˙Ni   (   t   nbformatt   nbformat_minort   NotebookNode(   t   v3(   t
   get_loggerc         C   sZ   d d l  m } m } y | |  d | Wn) | k
 rU } t   j d | |  n Xd S(   s(   Log validation errors, if there are any.i˙˙˙˙(   t   validatet   ValidationErrort   versions"   Notebook JSON is not valid v%i: %sN(   R    R   R   R   t   error(   t   nbR   R   R   t   e(    (    s2   lib/python2.7/site-packages/nbformat/v4/convert.pyt   _warn_if_invalid   s
    i   i    c         C   sE  | d k rý t  |  |  |  j d d  } |  j d d  } | pF d |  j _ | pX d |  j _ t |  _ t |  _ g  |  d <} xB |  j d g   D]. } x% | d D] } | j t	 |   qĽ Wq W|  j j d d  |  j j d	 d  t  |  t  |  S| d
 k r1| t k r$| |  j _ n  t |  _ |  St
 d |   d S(   sP  Convert a notebook to 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).
    i   t   orig_nbformatt   orig_nbformat_minori    t   cellst
   worksheetst   namet    t	   signaturei   sZ   Cannot convert a notebook directly from v%s to v4.  Try using the nbformat.convert module.N(   R   t   popt   Nonet   metadataR   R   R    R   t   appendt   upgrade_cellt
   ValueError(   R	   t   from_versiont
   from_minorR   R   R   t   wst   cell(    (    s2   lib/python2.7/site-packages/nbformat/v4/convert.pyt   upgrade   s.    			c         C   s  |  j  d t    |  j d k r |  j d d  d |  k rW |  j d  |  j d <n  |  j d d  |  _ |  j d d  |  _ t |  j	  |  _	 n |  j d k rý d	 |  _ |  j d
 d  } d j
 d d | d d j |  j d d  j     |  _ n |  j d k rd	 |  _ n  |  S(   sö   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   t   codet   languageR   t	   collapsedt   inputt   prompt_numbert   headingt   markdownt   leveli   u   {hashes} {single_line}t   hashest   #t   single_linet    t   sourcet   htmlN(   t
   setdefaultR   t	   cell_typeR   R   R*   R   t   execution_countt   upgrade_outputst   outputst   formatt   joint   gett
   splitlines(   R   R%   (    (    s2   lib/python2.7/site-packages/nbformat/v4/convert.pyR   L   s"    		
*c         C   s  |  j  d k ro d |  _ |  j d d  |  _ |  j d d  |  _ |  j j d t  |  _ t	 |  j
  |  _
 n |  j  d k rđ |  j d d  } d | k rđ | j d	  rđ t j d
 |  j   \ } } d |  _  | |  _ t |  |  _ qđ n  |  j d d  |  S(   s  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   t   pythonR*   R   R.   R    R$   s   
R'   s   (#+)\s*(.*)R#   t   attachmentsN(   R-   R   R   R!   R   R"   R   t   FalseR    t   downgrade_outputsR0   R3   t
   startswitht   ret   matcht   groupsR*   t   lenR%   (   R   R*   t   prefixt   text(    (    s2   lib/python2.7/site-packages/nbformat/v4/convert.pyt   downgrade_cellk   s    			s
   text/plainR?   s	   text/htmlR+   s   image/svg+xmlt   svgs	   image/pngt   pngs
   image/jpegt   jpegs
   text/latext   latexs   application/jsont   jsons   application/javascriptt
   javascriptc         C   sC   x< t  j   D]. \ } } | |  k r |  j |  |  | <q q W|  S(   s4   convert dict with v3 aliases to plain mime-type keys(   t	   _mime_mapt   itemsR   (   t   dt   aliast   mime(    (    s2   lib/python2.7/site-packages/nbformat/v4/convert.pyt   to_mime_key   s    c         C   sD   i  } x7 t  j   D]) \ } } | |  k r |  | | | <q q W| S(   s.   convert dict with mime-type keys to v3 aliases(   RG   RH   (   RI   t   d2RJ   RK   (    (    s2   lib/python2.7/site-packages/nbformat/v4/convert.pyt   from_mime_key   s
    c         C   s  |  d d d h k r;|  j  d t    |  d d k r\ d |  d <|  j d d  |  d <n  i  } xB t |   D]4 } | d d d h k r qo n  |  j |  | | <qo Wt |  | |  d <t |  j  d	 | k rî t j | d	  | d	 <n  x d D]? } | | k rő t	 | | t
  rő | | j d  | | <qő qő WnF |  d d k rXd |  d <n) |  d d k r|  j d d  |  d <n  |  S(   sË   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
    t   output_typet   pyoutt   display_dataR   t   execute_resultR"   R.   t   datas   application/jsons	   image/pngs
   image/jpegt   asciit   pyerrR   t   streamt   stdoutR   N(   s	   image/pngs
   image/jpeg(   R,   R   R   R   t   listRL   R   RE   t   loadst
   isinstancet   bytest   decode(   t   outputRS   t   key(    (    s2   lib/python2.7/site-packages/nbformat/v4/convert.pyt   upgrade_output    s.    	


!c         C   sú   |  d d d h k rł |  d d k rI d |  d <|  j  d d  |  d <n  |  j  d i   } d | k r t j | d  | d <n  t |  } |  j |  t |  j d	 i    nC |  d d
 k rĐ d |  d <n& |  d d k rö |  j  d  |  d <n  |  S(   sĐ   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
    RO   RR   RQ   RP   R.   R"   RS   s   application/jsonR   R   RU   RV   R   N(   R   R   RE   t   dumpsRN   t   updateR3   (   R]   RS   (    (    s2   lib/python2.7/site-packages/nbformat/v4/convert.pyt   downgrade_outputÄ   s    	
c         C   s   g  |  D] } t  |  ^ q S(   s,   upgrade outputs of a code cell from v3 to v4(   R_   (   R0   t   op(    (    s2   lib/python2.7/site-packages/nbformat/v4/convert.pyR/   ß   s    c         C   s   g  |  D] } t  |  ^ q S(   s.   downgrade outputs of a code cell to v3 from v4(   Rb   (   R0   Rc   (    (    s2   lib/python2.7/site-packages/nbformat/v4/convert.pyR8   ă   s    c         C   sĎ   |  j  t  k r |  St |  t   t j  |  _  t j |  _ g  |  j d  D] } t |  ^ qH } t j d |  g |  _ |  j j	 d d  t |  t j   |  j j d t   |  _
 |  j j d t  |  _ |  S(   s   Convert a v4 notebook to v3.

    Parameters
    ----------
    nb : NotebookNode
        The Python representation of the notebook to convert.
    R   R   R   R   R   (   R    R   R   R   R   R@   t   new_worksheetR   R   R,   R   R   (   R	   R   R   (    (    s2   lib/python2.7/site-packages/nbformat/v4/convert.pyt	   downgradeç   s    ((   t   __doc__RE   R:   t   nbbaseR    R   R   R   t   traitlets.logR   R   R   R   R@   RG   RL   RN   R_   Rb   R/   R8   Re   (    (    (    s2   lib/python2.7/site-packages/nbformat/v4/convert.pyt   <module>   s2   	3		
			$			