a
    ø£Beç  ã                   @   s˜   d Z ddlZddlZddlZddlmZ ddlmZ ddlm	Z	m
Z
 G dd„ dƒZG dd	„ d	eƒZd
eefdeefgZdd„ eD ƒZdd„ Zdd„ ZdS )z[
Common validator wrapper to provide a uniform usage of other schema validation
libraries.
é    N)ÚJsonSchemaException)ÚDraft4Validator)Ú	ErrorTreeÚValidationErrorc                   @   s6   e Zd ZdZdZdd„ Zdd„ Zddd	„Zd
d„ ZdS )ÚJsonSchemaValidatorzA json schema validator.Ú
jsonschemac                 C   s   || _ t|ƒ| _| j| _dS ©zInitialize the validator.N)Z_schemaÚ_JsonSchemaValidatorÚ_default_validatorÚ
_validator©ÚselfÚschema© r   úW/home/yuhw/wc/wire-cell-python/wcpy/lib/python3.9/site-packages/nbformat/json_compat.pyÚ__init__   s    
zJsonSchemaValidator.__init__c                 C   s   | j  |¡ dS )úValidate incoming data.N)r
   Úvalidate)r   Údatar   r   r   r      s    zJsonSchemaValidator.validateNc                 C   sB   |du r| j  |¡S t| j dƒr4| j j|d� |¡S | j  ||¡S )ú%Iterate over errors in incoming data.NÚevolve)r   )r
   Úiter_errorsÚhasattrr   )r   r   r   r   r   r   r       s
    zJsonSchemaValidator.iter_errorsc                 C   s
   t |d�S )ú$Create an error tree for the errors.)Úerrors)r   )r   r   r   r   r   Ú
error_tree(   s    zJsonSchemaValidator.error_tree)N)	Ú__name__Ú
__module__Ú__qualname__Ú__doc__Únamer   r   r   r   r   r   r   r   r      s   
r   c                       sB   e Zd ZdZdZ‡ fdd„Zdd„ Zd‡ fdd	„	Zd
d„ Z‡  Z	S )ÚFastJsonSchemaValidatorz(A schema validator using fastjsonschema.Úfastjsonschemac                    s   t ƒ  |¡ t |¡| _dS r   )Úsuperr   r"   Úcompiler   r   ©Ú	__class__r   r   r   2   s    z FastJsonSchemaValidator.__init__c              
   C   sL   z|   |¡ W n8 tyF } z tt|ƒ|jd�|‚W Y d}~n
d}~0 0 dS )r   ©Zschema_pathN)r   Ú_JsonSchemaExceptionr   ÚstrÚpath)r   r   Úerrorr   r   r   r   7   s    z FastJsonSchemaValidator.validateNc              
      sj   |durt ƒ  ||¡S g }| j}z||ƒ W n8 tyd } z tt|ƒ|jd�g}W Y d}~n
d}~0 0 |S )r   Nr'   )r#   r   r   r(   r   r)   r*   )r   r   r   r   Zvalidate_funcr+   r%   r   r   r   >   s    *z#FastJsonSchemaValidator.iter_errorsc                 C   s   d}t |ƒ‚dS )r   z>JSON schema error introspection not enabled for fastjsonschemaN)ÚNotImplementedError)r   r   Úmsgr   r   r   r   L   s    z"FastJsonSchemaValidator.error_tree)N)
r   r   r   r   r    r   r   r   r   Ú__classcell__r   r   r%   r   r!   -   s   r!   r"   r   c                 C   s   g | ]}|d  ‘qS )r   r   )Ú.0Úitemr   r   r   Ú
<listcomp>\   ó    r1   c                 C   sZ   | t vr d| › dt › �}t|ƒ‚tD ]\}}}|r$| |kr$|  S q$d| ›�}t|ƒ‚d S )NzInvalid validator 'z' value!
Valid values are: zMissing validator for )Ú
VALIDATORSÚ
ValueErrorÚ_VALIDATOR_MAP)Úvalidator_namer-   r    ÚmoduleZvalidator_clsr   r   r   Ú_validator_for_name_   s    

r8   c                  C   s   t j dd¡} t| ƒS )zU
    Return the default validator based on the value of an environment variable.
    ZNBFORMAT_VALIDATORr"   )ÚosÚenvironÚgetr8   )r6   r   r   r   Úget_current_validatorl   s    r<   )r   r9   r"   r   r   r(   r   r	   Zjsonschema.exceptionsr   r   r   r!   r5   r3   r8   r<   r   r   r   r   Ú<module>   s   ,þ