
    Ud6                         d Z ddlmZ ddlmZmZmZmZ ddlm	Z	m
Z
 ddlmZ g dZeeeedZdd	lmZ dd
lmZ ddlmZmZ ddlmZ ddlmZ ddlmZmZ  G d de      Z eded      ZddZ edfdZ!ddZ"edfdZ#y)znThe Jupyter notebook format

Use this module to read or write notebook files as particular nbformat versions.
    )
get_logger   )v1v2v3v4)__version__version_info)Sentinel)versionsvalidateValidationErrorconvert	from_dictNotebookNodecurrent_nbformatcurrent_nbformat_minorNBFormatError
NO_CONVERTreadsreadwriteswriter
   r	   r   )r            )reader)r   )r   r   )nbformat)nbformat_minor)r   r   c                       e Zd Zy)r   N)__name__
__module____qualname__     3/usr/lib/python3/dist-packages/nbformat/__init__.pyr   r   2   s    r%   r   r   zKValue to prevent nbformat to convert notebooks to most recent version.
    Nc                     t        j                  | fi |}|t        urt        ||      }	 t	        |       |S # t
        $ r:}t               j                  d|       t        |t              r||d<   Y d}~|S d}~ww xY w)a9  Read a notebook from a string and return the NotebookNode object as the given version.

    The string can contain a notebook of any version.
    The notebook will be returned `as_version`, converting, if necessary.

    Notebook format errors will be logged.

    Parameters
    ----------
    s : unicode
        The raw unicode string to read the notebook from.
    as_version : int
        The version of the notebook format to return.
        The notebook will be converted, if necessary.
        Pass nbformat.NO_CONVERT to prevent conversion.
    capture_validation_error : dict, optional
        If provided, a key of "ValidationError" with a
        value of the ValidationError instance will be added
        to the dictionary.

    Returns
    -------
    nb : NotebookNode
        The notebook that was read.
    Notebook JSON is invalid: %sr   N)
r   r   r   r   r   r   r   error
isinstancedict)s
as_versioncapture_validation_errorkwargsnbes         r&   r   r   ?   s    4 
a	"6	"B#R$<
 I	  <91=.5:;$%67I	<s   9 	A</A77A<c                 0   |t         urt        | |      } nt        j                  |       \  }}	 t	        |        t        |   j                  | fi |S # t
        $ r9}t               j                  d|       t        |t              r||d<   Y d}~Vd}~ww xY w)a  Write a notebook to a string in a given format in the given nbformat version.

    Any notebook format errors will be logged.

    Parameters
    ----------
    nb : NotebookNode
        The notebook to write.
    version : int, optional
        The nbformat version to write.
        If unspecified, or specified as nbformat.NO_CONVERT,
        the notebook's own version will be used and no conversion performed.
    capture_validation_error : dict, optional
        If provided, a key of "ValidationError" with a
        value of the ValidationError instance will be added
        to the dictionary.

    Returns
    -------
    s : unicode
        The notebook as a JSON string.
    r(   r   N)r   r   r   get_versionr   r   r   r)   r*   r+   r   writes_json)r0   versionr.   r/   _r1   s         r&   r   r   e   s    . j R!''+
<
 G((6v66	  <91=.5:;$%67<s   A 	B/BBc                     	 | j                         }t        ||fi |S # t        $ rD t        | d      5 }t        |j                         ||fi |cddd       cY S # 1 sw Y   nxY wY Zw xY w)ar  Read a notebook from a file as a NotebookNode of the given version.

    The string can contain a notebook of any version.
    The notebook will be returned `as_version`, converting, if necessary.

    Notebook format errors will be logged.

    Parameters
    ----------
    fp : file or str
        A file-like object with a read method that returns unicode (use
        ``io.open()`` in Python 2), or a path to a file.
    as_version : int
        The version of the notebook format to return.
        The notebook will be converted, if necessary.
        Pass nbformat.NO_CONVERT to prevent conversion.
    capture_validation_error : dict, optional
        If provided, a key of "ValidationError" with a
        value of the ValidationError instance will be added
        to the dictionary.

    Returns
    -------
    nb : NotebookNode
        The notebook that was read.
    utf-8encodingN)r   AttributeErroropenr   )fpr-   r.   r/   buffs         r&   r   r      s    8Sggi
 j":EfEE	  S"w' 	S1:/GR6R	S 	S 	S 	SSs&     A-A
A-A'	#A-,A-c                    t        | ||fi |}t        |t              r|j                  d      }	 |j	                  |       |j                  d      s|j	                  d       yy# t        $ rZ t        |dd      5 }|j	                  |       |j                  d      s|j	                  d       ddd       Y y# 1 sw Y   Y yxY ww xY w)a  Write a notebook to a file in a given nbformat version.

    The file-like object must accept unicode input.

    Parameters
    ----------
    nb : NotebookNode
        The notebook to write.
    fp : file or str
        Any file-like object with a write method that accepts unicode, or
        a path to write a file.
    version : int, optional
        The nbformat version to write.
        If nb is not this version, it will be converted.
        If unspecified, or specified as nbformat.NO_CONVERT,
        the notebook's own version will be used and no conversion performed.
    capture_validation_error : dict, optional
        If provided, a key of "ValidationError" with a
        value of the ValidationError instance will be added
        to the dictionary.
    utf8
wr8   r9   N)r   r*   bytesdecoder   endswithr;   r<   )r0   r=   r5   r.   r/   r,   r?   s          r&   r   r      s    , 	r74??A!UHHV
zz$HHTN   "cG, 	GGAJ::d#	 	 	s)   3A& &C	=4B;1C	;C	 C	C	)N)$__doc__traitlets.logr    r   r   r   r   _versionr	   r
   sentinelr   __all__r   r   	converterr   notebooknoder   r   r   r   r   r   	validatorr   r   
ValueErrorr   r!   r   r   r   r   r   r$   r%   r&   <module>rQ      s    %   / * 
				   1 , 8 0	J 	
 
#L "D !7H"FJ %t "r%   