ó
‹'=[c           @   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 „  Z
 d d d d	 d
 d g Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d e f d „  ƒ  YZ d e f d „  ƒ  YZ d S(   s3   Base classes and utilities for readers and writers.iÿÿÿÿ(   t   encodestringt   decodestring(   t	   py3compat(   t   str_to_bytest   unicode_typet   string_typesc         C   sš   x“ |  j  D]ˆ } x | j D]t } | j d k r x\ | j D]N } d | k rc t | j d ƒ | _ n  d | k r9 t | j d ƒ | _ q9 q9 Wq q Wq
 W|  S(   sô   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
    t   codet   pngt   asciit   jpeg(   t
   worksheetst   cellst	   cell_typet   outputsR   R   R	   (   t   nbt   wst   cellt   output(    (    s1   lib/python2.7/site-packages/nbformat/v3/rwbase.pyt   restore_bytes   s    't   textt   htmlt   svgt   latext
   javascriptt   jsonc         C   s7   |  r& |  d j  d ƒ r& d j |  ƒ Sd j |  ƒ Sd S(   sš   join lines that have been written by splitlines()
    
    Has logic to protect against `splitlines()`, which
    should have been `splitlines(True)`
    i    s   
s   u    u   
N(   s   
s   (   t   endswitht   join(   t   lines(    (    s1   lib/python2.7/site-packages/nbformat/v3/rwbase.pyt   _join_lines#   s    c         C   s  x|  j  D]} xú | j D]ï } | j d k r¾ d | k rb t | j t ƒ rb t | j ƒ | _ n  x¤ | j D]K } xB t D]: } | j	 | d ƒ } t | t ƒ ry t | ƒ | | <qy qy Wql Wq xH d d g D]: } | j	 | d ƒ } t | t ƒ rË t | ƒ | | <qË qË Wq Wq
 W|  S(   s2  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   t   inputt   sourcet   renderedN(   R
   R   R   t
   isinstanceR   t   listR   R   t   _multiline_outputst   gett   None(   R   R   R   R   t   keyt   item(    (    s1   lib/python2.7/site-packages/nbformat/v3/rwbase.pyt   rejoin_lines1   s    
c         C   s  x|  j  D]} x| j D]ø } | j d k rÄ d | k re t | j t ƒ re | j j t ƒ | _ n  xª | j D]N } xE t	 D]= } | j
 | d ƒ } t | t ƒ r| | j t ƒ | | <q| q| Wqo Wq xK d d g D]= } | j
 | d ƒ } t | t ƒ rÑ | j t ƒ | | <qÑ qÑ Wq Wq
 W|  S(   sá   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   R   R   N(   R
   R   R   R    R   R   t
   splitlinest   TrueR   R"   R#   R$   (   R   R   R   R   R%   R&   (    (    s1   lib/python2.7/site-packages/nbformat/v3/rwbase.pyt   split_linesM   s    !"c         C   sè   xá |  j  D]Ö } xÍ | j D]Â } | j d k r xª | j D]œ } d | k rŠ t | j t ƒ ru | j j d ƒ | _ n  t | j ƒ | _ n  d | k r9 t | j	 t ƒ rÀ | j	 j d ƒ | _	 n  t | j	 ƒ | _	 q9 q9 Wq q Wq
 W|  S(   sm   Restore 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   t   encodeR   R	   (   R   R   R   R   (    (    s1   lib/python2.7/site-packages/nbformat/v3/rwbase.pyt   base64_decodei   s    $c         C   s¦   xŸ |  j  D]” } x‹ | j D]€ } | j d k r xh | j D]Z } d | k ri t | j ƒ j d ƒ | _ n  d | k r9 t | j ƒ j d ƒ | _ q9 q9 Wq q Wq
 W|  S(   sŠ   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   t   decodeR	   (   R   R   R   R   (    (    s1   lib/python2.7/site-packages/nbformat/v3/rwbase.pyt   base64_encode}   s    -c         C   sz   |  j  d d ƒ |  j  d d ƒ xS |  d D]G } x> | d D]2 } | j d i  ƒ j  d d ƒ | j  d d ƒ q< Wq+ W|  S(   ss   Strip transient values that shouldn't be stored in files.

    This should be called in *both* read and write.
    t   orig_nbformatt   orig_nbformat_minorR
   R   t   metadatat   trustedN(   t   popR$   R#   (   R   R   R   (    (    s1   lib/python2.7/site-packages/nbformat/v3/rwbase.pyt   strip_transient   s    t   NotebookReaderc           B   s    e  Z d  Z d „  Z d „  Z RS(   s   A class for reading notebooks.c         K   s   t  d ƒ ‚ d S(   s   Read a notebook from a string.s'   loads must be implemented in a subclassN(   t   NotImplementedError(   t   selft   st   kwargs(    (    s1   lib/python2.7/site-packages/nbformat/v3/rwbase.pyt   reads¢   s    c         K   sH   | j  ƒ  } t j r8 t | t ƒ r8 t j | ƒ } n  |  j | |  S(   s'   Read a notebook from a file like object(   t   readR   t   PY3R    R   t   str_to_unicodeR:   (   R7   t   fpR9   t   nbs(    (    s1   lib/python2.7/site-packages/nbformat/v3/rwbase.pyR;   ¦   s    (   t   __name__t
   __module__t   __doc__R:   R;   (    (    (    s1   lib/python2.7/site-packages/nbformat/v3/rwbase.pyR5   Ÿ   s   	t   NotebookWriterc           B   s    e  Z d  Z d „  Z d „  Z RS(   s   A class for writing notebooks.c         K   s   t  d ƒ ‚ d S(   s   Write a notebook to a string.s'   loads must be implemented in a subclassN(   R6   (   R7   R   R9   (    (    s1   lib/python2.7/site-packages/nbformat/v3/rwbase.pyt   writes±   s    c         K   sK   |  j  | |  } t j r> t | t ƒ r> t j | ƒ } n  | j | ƒ S(   s&   Write a notebook to a file like object(   RD   R   R<   R    R   R=   t   write(   R7   R   R>   R9   R?   (    (    s1   lib/python2.7/site-packages/nbformat/v3/rwbase.pyRE   µ   s    (   R@   RA   RB   RD   RE   (    (    (    s1   lib/python2.7/site-packages/nbformat/v3/rwbase.pyRC   ®   s   	N(   RB   t   base64R    R   t   ipython_genutilsR   t   ipython_genutils.py3compatR   R   R   R   R"   R   R'   R*   R,   R.   R4   t   objectR5   RC   (    (    (    s1   lib/python2.7/site-packages/nbformat/v3/rwbase.pyt   <module>   s   							