B
    '=[!              
   @   s  d dl mZ d dlZd dlZd dlZd dlZd dlZyd dlmZ d dlm	Z
 W n6 ek
r Z zdZeeee W ddZ[X Y nX d dlmZ i Zdd Zd	d
 ZdddZdd ZdddZdd ZdZdZdd ZG dd deZdd Zd ddZdS )!    )print_functionN)ValidationError)Draft4Validatorz

    Jupyter notebook format depends on the jsonschema package:

        https://pypi.python.org/pypi/jsonschema

    Please install it first.
    )import_itemc             C   sp   t | tr@x`|  D ]&\}}|dkr*d}nt|}|| |< qW n,t | trlx t| D ]\}}t|| |< qTW | S )z relax any `additionalProperties`ZadditionalPropertiesT)
isinstancedictitems_relax_additional_propertieslist	enumerate)objkeyvaluei r   1lib/python3.7/site-packages/nbformat/validator.pyr	      s    

r	   c             C   s8   | d d d  ddi | d d d  ddi | S )NZdefinitionsZcellZoneOfz$refz#/definitions/unrecognized_celloutputz!#/definitions/unrecognized_output)append)Zschemar   r   r   _allow_undefined,   s
    

r   Fc             C   s   | dkrddl m} |} td|  }t|dd}|dkr<|}| |f}|tkryt|}W n tk
rl   dS X ||k rt|}t|}t	|t|< |ryt|}W n tk
r   dS X t|}t	|t|< t| S )z%Load the JSON schema into a ValidatorN   )current_nbformatznbformat.v%sZnbformat_minorr   )
 r   r   getattr
validators_get_schema_jsonAttributeErrorr	   r   	Validator)versionversion_minorrelax_add_propsr   vZcurrent_minorZversion_tupleschema_jsonr   r   r   get_validator5   s2    r"   c          	   C   s<   t jt j| j| j}t|}t|}W dQ R X |S )zP
    Gets the json schema from a given imported library a nbformat version.
    N)	ospathjoindirname__file__Znbformat_schemaopenjsonload)r    schema_pathfr!   r   r   r   r   ^   s    
r   c             C   s0   yt | ||| W n tk
r&   dS X dS dS )a  Checks whether the given notebook JSON conforms to the current
    notebook format schema. Returns True if the JSON is valid, and
    False otherwise.

    To see the individual errors that were encountered, please use the
    `validate` function instead.
    FTN)validater   )nbjsonrefr   r   r   r   r   isvalidg   s
    r0   c             C   s    | sdS dd dd | D  S )z
    (from jsonschema._utils.format_as_index, copied to avoid relying on private API)

    Construct a single string containing indexing operations for the indices.

    For example, [1, 2, "foo"] -> [1][2]["foo"]
    r   z[%s]z][c             s   s   | ]}t |V  qd S )N)repr).0indexr   r   r   	<genexpr>   s    z#_format_as_index.<locals>.<genexpr>)r%   )indicesr   r   r   _format_as_indexv   s    	r6      @   c             C   s  t | trdd t|  dt D }t |dtrNdt| d  g|d< t |dtrtdt| d  g|d< t| tkrdt| t  |d	< |S t | trd
d | dt D }t| tkr|dt| t   |S t | tr| dt	 }t| t	kr|d	7 }|S | S dS )zTruncate objects for use in validation tracebacks

    Cell and output lists are squashed, as are long strings, lists, and dicts.
    c             S   s   i | ]\}}t ||qS r   )_truncate_obj)r2   kr    r   r   r   
<dictcomp>   s    z!_truncate_obj.<locals>.<dictcomp>NZcellsz...%i cells...Zoutputsz...%i outputs...z%i keys truncatedz...c             S   s   g | ]}t |qS r   )r9   )r2   itemr   r   r   
<listcomp>   s    z!_truncate_obj.<locals>.<listcomp>z...%i items truncated...)
r   r   r
   r   _ITEM_LIMITgetlenr   str
_STR_LIMIT)r   Z	truncatedr   r   r   r9      s(    

r9   c               @   s8   e Zd ZdZd
ddZdd Zdd Zejd	kr4eZ	dS )NotebookValidationErrorzcSchema ValidationError with truncated representation

    to avoid massive verbose tracebacks.
    Nc             C   s$   || _ t| j d|| _| j j| _d S )Nr/   )originalr   r/   message)selfrD   r/   r   r   r   __init__   s    z NotebookValidationError.__init__c             C   s   t | j|S )N)r   rD   )rF   r   r   r   r   __getattr__   s    z#NotebookValidationError.__getattr__c             C   s`   | j }t|j}d|jdd|j| jp(dtt|j	dd f ddt|j
 tj|dd	gS )
zbCustom str for validation errors

        avoids dumping full schema and notebook to logs
        
r   zFailed validating %r in %s%s:ZnotebookNzOn instance%s:N   )width)rD   r9   instancer%   rE   	validatorr/   r6   r
   Zrelative_schema_pathrelative_pathpprintZpformat)rF   errorrM   r   r   r   __unicode__   s    
z#NotebookValidationError.__unicode__)   )N)
__name__
__module____qualname____doc__rG   rH   rR   sysversion_info__str__r   r   r   r   rC      s   

rC   c          
   C   s   | j d }d}|drt| jtrRd| jkr>| jd d }nd| jkrR| jd }|ryt| j|||d W nf tk
r } z6| j|j | j|_t	|||}|j
dkr||_
|S d}~X Y n tk
r   Y nX t| |S )zGet better ValidationError on oneOf failures

    oneOf errors aren't informative.
    if it's a cell type or output_type error,
    try validating directly based on the type for a better error message
    rJ   NZOfZ	cell_typeZ_cellZoutput_type)r   r   )r+   endswithr   rM   r   r-   r   rO   extendbetter_validation_errorr/   	ExceptionrC   )rQ   r   r   r   r/   Z	sub_errorZbetterr   r   r   r]      s0    





r]   c          
   C   s   |dkr ddl m} || \}}t|||d}|dkrJtd| t dS y&|rd|| dd| iS || S W n. tk
r } zt|||W dd}~X Y nX dS )zChecks whether the given notebook JSON conforms to the current
    notebook format schema.

    Raises ValidationError if not valid.
    N   )get_version)r   z&No schema for validating v%s notebooksz$refz#/definitions/%s)	readerr`   r"   warningswarnUserWarningr-   r   r]   )r.   r/   r   r   r   r`   rN   er   r   r   r-      s    r-   )NNF)NNN)NNNF)Z
__future__r   r)   r#   rP   rX   rb   Z
jsonschemar   r   r   ImportErrorre   Zverbose_msgrA   Zipython_genutils.importstringr   r   r	   r   r"   r   r0   r6   r>   rB   r9   rC   r]   r-   r   r   r   r   <module>   s2   "	
)	
$'