a
    ø£Ben
  ã                   @   sL   d Z ddlZddlmZ G dd„ deƒZdd„ Zd	d
„ Zdd„ Zdd„ Z	dS )z/API for reading notebooks of different versionsé    Né   )ÚValidationErrorc                   @   s   e Zd ZdZdS )ÚNotJSONErrorz1An error raised when an object is not valid JSON.N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__© r	   r	   úR/home/yuhw/wc/wire-cell-python/wcpy/lib/python3.9/site-packages/nbformat/reader.pyr      s   r   c              
   K   sp   zt j| fi |¤Ž}W nT tyj } z<d| ›�}t|ƒdkrL|dd… d }t|ƒ|‚W Y d}~n
d}~0 0 |S )z Parse a JSON string into a dict.z%Notebook does not appear to be JSON: éP   NéM   z...)ÚjsonÚloadsÚ
ValueErrorÚlenr   )ÚsÚkwargsÚnb_dictÚeÚmessager	   r	   r
   Ú
parse_json   s    
 r   c                 C   s    |   dd¡}|   dd¡}||fS )záGet the version of a notebook.

    Parameters
    ----------
    nb : dict
        NotebookNode or dict containing notebook data.

    Returns
    -------
    Tuple containing major (int) and minor (int) version numbers
    Znbformatr   Znbformat_minorr   )Úget)ÚnbÚmajorÚminorr	   r	   r
   Úget_version!   s    r   c           	   
   K   s”   ddl m}m} t| fi |¤Ž}t|ƒ\}}||v r„z|| j||d�W S  ty€ } z d|› �}t|ƒd‚W Y d}~q�d}~0 0 n|d| ƒ‚dS )aQ  Read a notebook from a json string and return the
    NotebookNode object.

    This function properly reads notebooks of any version.  No version
    conversion is performed.

    Parameters
    ----------
    s : unicode | bytes
        The raw string or bytes object to read the notebook from.

    Returns
    -------
    nb : NotebookNode
        The notebook that was read.

    Raises
    ------
    ValidationError
        Notebook JSON for a given version is missing an expected key and cannot be read.
    NBFormatError
        Specified major version is invalid or unsupported.
    r   )ÚNBFormatErrorÚversions)r   z8The notebook is invalid and is missing an expected key: NzUnsupported nbformat version %s)Ú r   r   r   r   Zto_notebook_jsonÚAttributeErrorr   )	r   r   r   r   r   r   r   r   Úmsgr	   r	   r
   Úreads2   s    
"r!   c                 K   s   t |  ¡ fi |¤ŽS )aZ  Read a notebook from a file and return the NotebookNode object.

    This function properly reads notebooks of any version.  No version
    conversion is performed.

    Parameters
    ----------
    fp : file
        Any file-like object with a read method.

    Returns
    -------
    nb : NotebookNode
        The notebook that was read.
    )r!   Úread)Úfpr   r	   r	   r
   r"   X   s    r"   )
r   r   Ú	validatorr   r   r   r   r   r!   r"   r	   r	   r	   r
   Ú<module>   s   &