U
    kuf                     @  s0  d Z ddlmZ ddlm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 d	d
dddddddddddddddg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 de Z!ede"d"Z#d(d$dZ$e#d#fd%dZ%d)d&dZ&e#d#fd'dZ'd#S )*znThe Jupyter notebook format

Use this module to read or write notebook files as particular nbformat versions.
    )annotations)Path)
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                   @  s   e Zd ZdS )r   N)__name__
__module____qualname__ r$   r$   5/tmp/pip-unpacked-wheel-zj9qk9v2/nbformat/__init__.pyr   5   s   zKValue to prevent nbformat to convert notebooks to most recent version.
    Nc              
   K  st   t j| f|}|tk	r t||}zt| W nB tk
rn } z$t d| t|t	r^||d< W 5 d}~X Y nX |S )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er$   r$   r%   r   B   s    

c              
   K  s   |t k	rt| |} nt| \}}zt|  W nB tk
rp } z$t d| t|t	r`||d< W 5 d}~X Y nX t
| j| f|S )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   Zwrites_json)r.   versionr,   r-   _r/   r$   r$   r%   r   h   s    
c                 K  sj   z|   }W nL tk
rX   t| dd&}t|  ||f|W  5 Q R   Y S Q R X Y nX t|||f|S )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.
    utf8encoding)r   AttributeErroropenr   )fpr+   r,   r-   buffr$   r$   r%   r      s    0c                 K  s   t | ||f|}t|tr$|d}z"|| |dsD|d W nP tk
r   t|jddd$}|| |ds|d W 5 Q R X Y nX dS )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.
    r3   
wr4   N)	r   r(   bytesdecoder   endswithr6   r   r7   )r.   r8   r1   r,   r-   r*   r:   r$   r$   r%   r      s    





)N)N)(__doc__
__future__r   pathlibr   Ztraitlets.logr    r   r   r   r	   _versionr
   r   sentinelr   __all__r   r   	converterr   Znotebooknoder   r   r   r   r    r   Z	validatorr   r   
ValueErrorr   r!   r   r   r   r   r   r$   r$   r$   r%   <module>   sZ   
&$
%