U
    ꥡc(                     @   s   d dl mZmZmZ d dlmZ d dlZd dlZd dlZd dl	Z	e	j
dkrXd dlmZ nd dlZG dd deZG dd	 d	Zd
d Zdd Zdd Zdd Zdd Zdd Zdd Zdd Ze e fddZdd Zdd Zd d! ZdS )"    )MappingMutableMappingSequence)urlsplitN)   	   )	resourcesc                   @   sP   e Zd ZdZdd Zdd Zdd Zdd	 Zd
d Zdd Z	dd Z
dd ZdS )URIDictz8
    Dictionary which uses normalized URIs as keys.
    c                 C   s   t | S N)r   geturlselfuri r   5/tmp/pip-unpacked-wheel-xq_7ditv/jsonschema/_utils.py	normalize   s    zURIDict.normalizec                 O   s   t  | _| jj|| d S r
   )dictstoreupdate)r   argskwargsr   r   r   __init__   s    zURIDict.__init__c                 C   s   | j | | S r
   r   r   r   r   r   r   __getitem__   s    zURIDict.__getitem__c                 C   s   || j | |< d S r
   r   )r   r   valuer   r   r   __setitem__   s    zURIDict.__setitem__c                 C   s   | j | |= d S r
   r   r   r   r   r   __delitem__!   s    zURIDict.__delitem__c                 C   s
   t | jS r
   )iterr   r   r   r   r   __iter__$   s    zURIDict.__iter__c                 C   s
   t | jS r
   )lenr   r   r   r   r   __len__'   s    zURIDict.__len__c                 C   s
   t | jS r
   )reprr   r   r   r   r   __repr__*   s    zURIDict.__repr__N)__name__
__module____qualname____doc__r   r   r   r   r   r   r!   r#   r   r   r   r   r	      s   r	   c                   @   s   e Zd ZdZdd ZdS )UnsetzG
    An as-of-yet unset attribute or unprovided default parameter.
    c                 C   s   dS )Nz<unset>r   r   r   r   r   r#   3   s    zUnset.__repr__N)r$   r%   r&   r'   r#   r   r   r   r   r(   .   s   r(   c                 C   s.   t td|  d}|jdd}t|S )zC
    Load a schema from ./schemas/``name``.json and return it.
    zschemas/z.jsonzutf-8)encoding)r   files__package__joinpath	read_textjsonloads)namepathdatar   r   r   load_schema7   s    r3   c                 C   s(   |s| S |  dd dd |D  dS )aA  
    Construct a single string containing indexing operations for the indices.

    For example for a container ``bar``, [1, 2, "foo"] -> bar[1][2]["foo"]

    Arguments:

        container (str):

            A word to use for the thing being indexed

        indices (sequence):

            The indices to format.
    [z][c                 s   s   | ]}t |V  qd S r
   r"   ).0indexr   r   r   	<genexpr>T   s     z"format_as_index.<locals>.<genexpr>])join)	containerindicesr   r   r   format_as_indexA   s    r=   c                 c   sL   | di }d| di }| D ]$}||kr"|r@t||r@q"|V  q"dS )z
    Return the set of additional properties for the given ``instance``.

    Weeds out properties that should have been validated by ``properties`` and
    / or ``patternProperties``.

    Assumes ``instance`` is dict-like already.
    
properties|patternPropertiesN)getr:   research)instanceschemar>   patternspropertyr   r   r   find_additional_propertiesW   s    
rH   c                 C   s2   t | dkrd}nd}ddd t| D |fS )z@
    Create an error message for extra items or properties.
       waswerez, c                 s   s   | ]}t |V  qd S r
   r5   )r6   extrar   r   r   r8   s   s     zextras_msg.<locals>.<genexpr>)r    r:   sorted)extrasZverbr   r   r   
extras_msgj   s    rO   c                 C   s   t | tr| gS | S )z]
    Wrap ``thing`` in a list if it's a single str.

    Otherwise, return it unchanged.
    )
isinstancestr)thingr   r   r   ensure_listv   s    
rS   c                    s.   t | t  krdS t fdd|  D S )zI
    Check if two mappings are equal using the semantics of `equal`.
    Fc                 3   s(   | ] \}}| kot | | V  qd S r
   equal)r6   keyr   twor   r   r8      s   z!_mapping_equal.<locals>.<genexpr>)r    allitemsZonerX   r   rW   r   _mapping_equal   s
    r\   c                 C   s,   t | t |krdS tdd t| |D S )zJ
    Check if two sequences are equal using the semantics of `equal`.
    Fc                 s   s   | ]\}}t ||V  qd S r
   rT   )r6   ijr   r   r   r8      s     z"_sequence_equal.<locals>.<genexpr>)r    rY   zipr[   r   r   r   _sequence_equal   s    r`   c                 C   sh   t | tst |tr| |kS t | tr:t |tr:t| |S t | trXt |trXt| |S t| t|kS )z
    Check if two things are equal evading some Python type hierarchy semantics.

    Specifically in JSON Schema, evade `bool` inheriting from `int`,
    recursing into sequences to do the same.
    )rP   rQ   r   r`   r   r\   unboolr[   r   r   r   rU      s    

rU   c                 C   s   | dkr|S | dkr|S | S )zH
    A hack to make True and 1 and False and 0 unique for ``uniq``.
    TFr   )elementtruefalser   r   r   ra      s
    ra   c              	   C   s   zJt dd | D }t|dd}t||D ]\}}t||r, W dS q,W nV ttfk
r   g }| D ]4}t|}|D ]}t||rv  Y dS qv|| qfY nX dS )z
    Check if all of a container's elements are unique.

    Tries to rely on the container being recursively sortable, or otherwise
    falls back on (slow) brute force.
    c                 s   s   | ]}t |V  qd S r
   )ra   )r6   r]   r   r   r   r8      s     zuniq.<locals>.<genexpr>rI   NFT)	rM   	itertoolsislicer_   rU   NotImplementedError	TypeErrorra   append)r;   sortZslicedr]   r^   seener   r   r   uniq   s    

rm   c                 C   s  |  |drg S g }d|kr.ttdt|S d|krx| j|d \}}| j| z|t| ||7 }W 5 | j  X d|kr|ttdt|d 7 }d|kr
| j	|d d
|r|t| ||d 7 }d|kr|t| ||d 7 }nd	|kr
|t| ||d	 7 }d
D ]F}||krt|D ],\}}| j	|| d
|r$|| q$qdD ]F}||krZ|| D ],}	t| ||	}
|
sp|t| ||	7 }qpqZ|S )z
    Get all indexes of items that get evaluated under the current schema

    Covers all keywords related to unevaluatedItems: items, prefixItems, if,
    then, else, contains, unevaluatedItems, allOf, oneOf, anyOf
    booleanrZ   r   $refZprefixItemsifrE   thenelse)containsZunevaluatedItemsZallOfoneOfZanyOf)is_typelistranger    resolverresolve
push_scope	pop_scope%find_evaluated_item_indexes_by_schemaevolveis_valid	enumerateri   descend)	validatorrD   rE   Zevaluated_indexesscoperesolvedkeywordkv	subschemaerrsr   r   r   r~      sh      

    
  

  r~   c                 C   sr  |  |drg S g }d|kr^| j|d \}}| j| z|t| ||7 }W 5 | j  X dD ]}||krb|  || dr| D ],\}}| j|| d||ir|	| q|  || drb||  D ]0\}}	||kr| j|	d|| r|	| qqbd|krn| D ]V\}}|d  D ]>\}
}t
|
|r*| j|d d||ir*|	| q*qd|kr|d  D ](\}}	||krq|t| ||	7 }qdD ]F}||kr|| D ],}	t| ||	}|s|t| ||	7 }qȐqd	|krn| j|d	 d|rP|t| ||d	 7 }d
|krn|t| ||d
 7 }nd|krn|t| ||d 7 }|S )a  
    Get all keys of items that get evaluated under the current schema

    Covers all keywords related to unevaluatedProperties: properties,
    additionalProperties, unevaluatedProperties, patternProperties,
    dependentSchemas, allOf, oneOf, anyOf, if, then, else
    rn   ro   )r>   ZadditionalPropertiesZunevaluatedPropertiesrq   objectr@   ZdependentSchemasru   rp   rr   rs   )rw   rz   r{   r|   r}   &find_evaluated_property_keys_by_schemarZ   r   r   ri   rB   rC   rx   r   )r   rD   rE   Zevaluated_keysr   r   r   rG   r   r   pattern_r   r   r   r   r     s      



  

  
  
  
  r   )collections.abcr   r   r   urllib.parser   re   r.   rB   sysversion_info	importlibr   Zimportlib_resourcesr	   r(   r3   r=   rH   rO   rS   r\   r`   rU   r   ra   rm   r~   r   r   r   r   r   <module>   s,   
	
	=