o
    ^i                     @  s  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 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 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$   L/home/ubuntu/hpml_nyu/venv/lib/python3.10/site-packages/nbformat/__init__.pyr   5   s    r   r   zKValue to prevent nbformat to convert notebooks to most recent version.
    Nc              
   K  s   t j| fi |}|turt||}zt| W |S  tyC } zt d| t|t	r8||d< W Y d}~|S W Y d}~|S d}~w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er$   r$   r%   r   B   s   



r   c              
   K  s   |t ur
t| |} nt| \}}zt|  W n" ty9 } zt d| t|t	r/||d< W Y d}~nd}~ww t
| j| fi |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   writes_json)r.   versionr,   r-   _r/   r$   r$   r%   r   h   s   
r   c                 K  s|   z|   }W n- ty3   t| dd}t|  ||fi |W  d    Y S 1 s,w   Y  Y nw t|||fi |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encodingN)r   AttributeErroropenr   )fpr+   r,   r-   buffr$   r$   r%   r      s   (r   c                 K  s   t | ||fi |}t|tr|d}z|| |ds'|d W dS W dS  tyf   t|jddd"}|| |dsR|d W d   Y dS W d   Y dS 1 s^w   Y  Y dS 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.
    r4   
wr5   N)	r   r(   bytesdecoder   endswithr7   r   r8   )r.   r9   r2   r,   r-   r*   r;   r$   r$   r%   r      s"   





&r   )N)(__doc__
__future__r   pathlibr   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$   r%   <module>   s:    
&
$%