B
    Wc`\5                 @   s  d dl mZmZ d dlZd dlZd dlZd dlZd dlmZ d dl	m
Z
mZ eddgZe Ze ZG dd deZG d	d
 d
eZG dd deZejddG dd deZG dd deZG dd deZG dd deZG dd deZeefddZe ZefddZdS )    )defaultdictdequeN)_utils)PY3	iteritemsZanyOfZoneOfc            	       s   e Zd Zedddeeeddf	 fdd	Zdd Zdd ZerBeZnd	d
 Ze	dd Z
edd Zedd Zdd Zdd Z  ZS )_Error Nc                s   t t| |||||||||	|

 || _t| | _| _t|	 | _| _t	|| _
| | _| _|| _|| _|| _|| _|
| _x|D ]
}| |_qW d S )N)superr   __init__messager   pathrelative_pathschema_pathrelative_schema_pathlistcontextcause	__cause__	validatorvalidator_valueinstanceschemaparent)selfr   r   r   r   r   r   r   r   r   r   error)	__class__r   4lib/python3.7/site-packages/jsonschema/exceptions.pyr
      s.    


z_Error.__init__c             C   s   d| j j| jf S )Nz<%s: %r>)r   __name__r   )r   r   r   r   __repr__:   s    z_Error.__repr__c             C   s   | j | j| j| jf}tdd |D r,| jS tj| jdd}tj| jdd}| jt	d
 | j | jtt| jd d t|| jt| jt|f  S )Nc             s   s   | ]}|t kV  qd S )N)_unset).0mr   r   r   	<genexpr>A   s    z%_Error.__unicode__.<locals>.<genexpr>H   )widthzl

            Failed validating %r in %s%s:
            %s

            On %s%s:
            %s
            )r   r   r   r   anyr   pprintpformattextwrapdedentrstrip!_word_for_schema_in_error_messager   Zformat_as_indexr   r   indent#_word_for_instance_in_error_messager   )r   Zessential_for_verbosepschema	pinstancer   r   r   __unicode__=   s    
z_Error.__unicode__c             C   s   t | dS )Nzutf-8)unicodeencode)r   r   r   r   __str__[   s    z_Error.__str__c             C   s   | f |  S )N)	_contents)clsotherr   r   r   create_from^   s    z_Error.create_fromc             C   s2   | j }|d kr| jS t| j}|t|j |S )N)r   r   r   
extendleftreversedabsolute_path)r   r   r   r   r   r   r;   b   s    
z_Error.absolute_pathc             C   s2   | j }|d kr| jS t| j}|t|j |S )N)r   r   r   r9   r:   absolute_schema_path)r   r   r   r   r   r   r<   l   s    
z_Error.absolute_schema_pathc             K   s4   x.t |D ]"\}}t| |tkr
t| || q
W d S )N)r   getattrr   setattr)r   kwargskvr   r   r   _setv   s    z_Error._setc                s   d}t  fdd|D S )N)
r   r   r   r   r   r   r   r   r   r   c             3   s   | ]}|t  |fV  qd S )N)r=   )r    attr)r   r   r   r"      s    z#_Error._contents.<locals>.<genexpr>)dict)r   Zattrsr   )r   r   r5   {   s    z_Error._contents)r   
__module____qualname__r   r
   r   r1   r   r4   classmethodr8   propertyr;   r<   rB   r5   __classcell__r   r   )r   r   r      s&   

r   c               @   s   e Zd ZdZdZdS )ValidationErrorr   r   N)r   rE   rF   r,   r.   r   r   r   r   rJ      s   rJ   c               @   s   e Zd ZdZdZdS )SchemaErrorZ
metaschemar   N)r   rE   rF   r,   r.   r   r   r   r   rK      s   rK   T)hashc               @   s   e Zd Ze Zdd ZdS )RefResolutionErrorc             C   s
   t | jS )N)str_cause)r   r   r   r   r4      s    zRefResolutionError.__str__N)r   rE   rF   rC   ZibrO   r4   r   r   r   r   rM      s   rM   c               @   s.   e Zd Zdd Zdd Zer"eZndd ZdS )UndefinedTypeCheckc             C   s
   || _ d S )N)type)r   rQ   r   r   r   r
      s    zUndefinedTypeCheck.__init__c             C   s
   d| j  S )Nz'Type %r is unknown to this type checker)rQ   )r   r   r   r   r1      s    zUndefinedTypeCheck.__unicode__c             C   s   t | dS )Nzutf-8)r2   r3   )r   r   r   r   r4      s    zUndefinedTypeCheck.__str__N)r   rE   rF   r
   r1   r   r4   r   r   r   r   rP      s
   rP   c               @   s.   e Zd Zdd Zdd Zer"eZndd ZdS )UnknownTypec             C   s   || _ || _|| _d S )N)rQ   r   r   )r   rQ   r   r   r   r   r   r
      s    zUnknownType.__init__c             C   sF   t j| jdd}t j| jdd}td | jt	|t	|f S )Nr#   )r$   z
            Unknown type %r for validator with schema:
            %s

            While checking instance:
            %s
            )
r'   r(   r   r   r)   r*   r+   rQ   r   r-   )r   r/   r0   r   r   r   r1      s
    zUnknownType.__unicode__c             C   s   t | dS )Nzutf-8)r2   r3   )r   r   r   r   r4      s    zUnknownType.__str__N)r   rE   rF   r
   r1   r   r4   r   r   r   r   rR      s
   rR   c                   s8   e Zd Zd fdd	Zdd Zer(eZndd Z  ZS )	FormatErrorNc                s(   t t| || || _| | _| _d S )N)r	   rS   r
   r   r   r   )r   r   r   )r   r   r   r
      s    zFormatError.__init__c             C   s   | j S )N)r   )r   r   r   r   r1      s    zFormatError.__unicode__c             C   s   | j dS )Nzutf-8)r   r3   )r   r   r   r   r4      s    zFormatError.__str__)N)r   rE   rF   r
   r1   r   r4   rI   r   r   )r   r   rS      s
   rS   c               @   sZ   e Zd ZdZeZdddZdd Zdd Zd	d
 Z	dd Z
dd Zdd Zedd ZdS )	ErrorTreezG
    ErrorTrees make it easier to check which validations failed.

    r   c             C   sT   i | _ t| j| _x<|D ]4}| }x|jD ]}|| }q(W ||j |j< |j|_qW d S )N)errorsr   r   r5   r   r   r   	_instance)r   rU   r   Z	containerelementr   r   r   r
      s    
zErrorTree.__init__c             C   s
   || j kS )zD
        Check whether ``instance[index]`` has any errors.

        )r5   )r   indexr   r   r   __contains__   s    zErrorTree.__contains__c             C   s&   | j tk	r|| kr| j |  | j| S )a]  
        Retrieve the child tree one level down at the given ``index``.

        If the index is not in the instance that this tree corresponds to and
        is not known by this tree, whatever error would be raised by
        ``instance.__getitem__`` will be propagated (usually this is some
        subclass of `exceptions.LookupError`.

        )rV   r   r5   )r   rX   r   r   r   __getitem__   s    
zErrorTree.__getitem__c             C   s   || j |< d S )N)r5   )r   rX   valuer   r   r   __setitem__   s    zErrorTree.__setitem__c             C   s
   t | jS )zZ
        Iterate (non-recursively) over the indices in the instance with errors.

        )iterr5   )r   r   r   r   __iter__   s    zErrorTree.__iter__c             C   s   | j S )z*
        Same as `total_errors`.

        )total_errors)r   r   r   r   __len__  s    zErrorTree.__len__c             C   s   d| j jt| f S )Nz<%s (%s total errors)>)r   r   len)r   r   r   r   r     s    zErrorTree.__repr__c             C   s&   t dd t| jD }t| j| S )zU
        The total number of errors in the entire tree, including children.

        c             s   s   | ]\}}t |V  qd S )N)ra   )r    _Ztreer   r   r   r"     s    z)ErrorTree.total_errors.<locals>.<genexpr>)sumr   r5   ra   rU   )r   Zchild_errorsr   r   r   r_     s    zErrorTree.total_errorsN)r   )r   rE   rF   __doc__r   rV   r
   rY   rZ   r\   r^   r`   r   rH   r_   r   r   r   r   rT      s   
rT   c                s    fdd}|S )Nc                s    | j }t| j |k| kfS )N)r   ra   r   )r   r   )strongweakr   r   	relevance  s    zby_relevance.<locals>.relevancer   )rf   re   rg   r   )re   rf   r   by_relevance  s    rh   c             C   sR   t | } t| d }|d krd S tt|g| |d}x|jrLt|j|d}q6W |S )N)key)r]   nextmax	itertoolschainr   min)rU   ri   Zbestr   r   r   
best_match#  s    
ro   )collectionsr   r   rl   r'   r)   rC   Z
jsonschemar   Zjsonschema.compatr   r   	frozensetZWEAK_MATCHESZSTRONG_MATCHESZUnsetr   	Exceptionr   rJ   rK   srM   rP   rR   rS   objectrT   rh   rg   ro   r   r   r   r   <module>   s*   qL