a
    ø£Be6  ã                   @   sú   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„ 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.
é    )Ú
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"   úT/home/yuhw/wc/wire-cell-python/wcpy/lib/python3.9/site-packages/nbformat/__init__.pyr   2   s   r   r   zKValue to prevent nbformat to convert notebooks to most recent version.
    Nc              
   K   sz   t j| fi |¤Ž}|tur$t||ƒ}zt|ƒ W nD tyt } z,tƒ  d|¡ t|t	ƒr`||d< W Y d}~n
d}~0 0 |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   ?   s    

r   c              
   K   sŠ   |t urt| |ƒ} nt | ¡\}}zt| ƒ W nD tyr } z,tƒ  d|¡ t|t	ƒr^||d< W Y d}~n
d}~0 0 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   Zwrites_json)r,   Úversionr*   r+   Ú_r-   r"   r"   r#   r   e   s    
r   c                 K   s|   z|   ¡ }W nZ tyf   t| dd��,}t|  ¡ ||fi |¤ŽW  d  ƒ  Y S 1 sX0    Y  Y n0 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.
    úutf-8©Ú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¡sH| d¡ W n^ ty¨   t|ddd��.}| |¡ | d¡s†| d¡ W d  ƒ n1 sš0    Y  Y n0 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.
    Úutf8Ú
Úwr1   r2   N)r   r&   ÚbytesÚdecoder   Úendswithr4   r5   )r,   r6   r/   r*   r+   r(   r8   r"   r"   r#   r   ®   s    





r   )N)N)$Ú__doc__Ztraitlets.logr   Ú r   r   r   r   Ú_versionr   r	   Úsentinelr
   Ú__all__r   r   Ú	converterr   Znotebooknoder   r   r   r   r   r   Ú	validatorr   r   Ú
ValueErrorr   r   r   r   r   r   r   r"   r"   r"   r#   Ú<module>   s4   üý
&$
%