U
    dG                     @  s  U d Z ddlmZ ddlmZmZ ddlmZmZm	Z	 ddl
mZmZmZmZmZ ddlmZ ddlmZmZ ddlmZmZmZ eeef Ze	eef Zee ZeG d	d
 d
e Z!dddddZ"dddddZ#dddddZ$ddddddZ%ddddddZ&ddddddZ'ddd dd!d"Z(e) e) e) fd#d#d#d$d%d&Z*e) e) e) fd#d#d#d$d'd(Z+e) e) e) fd#d#d#d$d)d*Z,e) e) e) fd#d#d#d$d+d,Z-e) e) e) fd#d#d#d$d-d.Z.e) e) e) fd#d#d#d$d/d0Z/e) e) e) fd#d#d#d$d1d2Z0ed3e"e*d4d5d6d7d8d9d:d;d<d=d>hd?d@dAdBhdCdDdEdFhdGe%e.d4d5d6d7d8d9d:d;d<d=d>hd?d@dAdBhdCdDdEdFhdGdHZ1edIe"e+dJd4d5d6d7d8d:d;d<d=d>hd?d@dAhdCdDdEdFhdGe&e/dJd4d5d6d7d8d:d;d<d=d>hd?d@dAhdCdDdEdFhdGdHZ2edKe#e,dJd4d5d7d8d:d;d<hd?d@dAhdLdEdFhdGe'e0dJd4d5d7d8d:d;d<hd?d@dAhdLdEdFhdGdHZ3edMe#e,dJd4d5d:d;hd?d@dAhdLdEdFhdGe'e0dJd4d5d:d;hd?d@dAhdLdEdFhdGdHZ4edNe$e-d:hd?d@dAhdLdEdFhdGe(e0dJd4d:hd?d@dAhdLdEdFhdGdHZ5edOe$e-dPhdLdEdFhdQe(e0dJd4hdPhdLdEdFhdGdHZ6edRdS dTe1fdUe2fdVe3fdWe4fdXe5fdYe6ffD Z7dZe8d[< did]d^d^d_d`daZ9eG dbdc dcZ:dddedfdgdhZ;d\S )jzI
Referencing implementations for JSON Schema specs (historic & current).
    )annotations)SequenceSet)AnyIterableUnion)AnchorRegistryResourceSpecification
exceptions)frozen)ResolvedResolver)URIr   Mappingc                   @  s   e Zd ZU dZded< dS )UnknownDialectz
    A dialect identifier was found for a dialect unknown by this library.

    If it's a custom ("unofficial") dialect, be sure you've registered it.
    r   uriN)__name__
__module____qualname____doc____annotations__ r   r   :/tmp/pip-unpacked-wheel-bqy97nzo/referencing/jsonschema.pyr      s   
r   Schemaz
URI | Nonecontentsreturnc                 C  s   t | trd S | dS )N$id)
isinstanceboolget)r   r   r   r   
_dollar_id$   s    
r#   c                 C  s:   t | tsd| krd S | d}|d k	r6|ds6|S d S )N$refr   #)r    r!   r"   
startswithr   idr   r   r   _legacy_dollar_id*   s
    
r)   ObjectSchemac                 C  s0   d| krd S |  d}|d k	r,|ds,|S d S )Nr$   r(   r%   )r"   r&   r'   r   r   r   
_legacy_id2   s
    
r+   zSpecification[Schema]zIterable[AnchorType[Schema]])specificationr   r   c                 c  s^   t |trd S |d}|d k	r4t|| |dV  |d}|d k	rZt|| |dV  d S )N$anchornameresourcez$dynamicAnchor)r    r!   r"   r   create_resourceDynamicAnchor)r,   r   anchorZdynamic_anchorr   r   r   _anchor:   s    


r4   zIterable[Anchor[Schema]]c                 C  s8   t |trg S |d}|d kr$g S t|| |dgS )Nr-   r.   )r    r!   r"   r   r1   )r,   r   r3   r   r   r   _anchor_2019O   s    

r5   c                 C  sD   t |trg S |dd}|ds(g S t|dd  | |dgS )Nr    r%      r.   )r    r!   r"   r&   r   r1   r,   r   r(   r   r   r   _legacy_anchor_in_dollar_id`   s    


r9   zSpecification[ObjectSchema]zIterable[Anchor[ObjectSchema]]c                 C  s6   | dd}|dsg S t|dd  | |dgS )Nr(   r6   r%   r7   r.   )r"   r&   r   r1   r8   r   r   r   _legacy_anchor_in_idq   s    

r:   zSet[str])in_valuein_subvaluesin_subarrayc                   s   ddd fdd}|S )z
    Create a callable returning JSON Schema specification-style subschemas.

    Relies on specifying the set of keywords containing subschemas in their
    values, in a subobject's values, or in a subarray.
    r   Iterable[ObjectSchema]r   c                 3  sr   t | trd S D ]}|| kr| | V  q D ]}|| kr.| | E d H  q.D ]}|| krN| |  E d H  qNd S N)r    r!   values)r   eachr=   r<   r;   r   r   subresources_of   s    
z)_subresources_of.<locals>.subresources_ofr   r;   r<   r=   rC   r   rB   r   _subresources_of   s    rE   c                   s   ddd fdd}|S )O
    Specifically handle older drafts where there are some funky keywords.
    r   r>   r   c                 3  s   t | trd S D ]}|| kr| | V  q D ]}|| kr.| | E d H  q.D ]}|| krN| |  E d H  qN| d}|d k	rt |tr|E d H  n|V  d S Nitems)r    r!   r@   r"   r   )r   rA   rH   rB   r   r   rC      s     


z:_subresources_of_with_crazy_items.<locals>.subresources_ofr   rD   r   rB   r   !_subresources_of_with_crazy_items   s    	rI   c                   s   ddd fdd}|S )rF   r   r>   r   c                 3  s   t | trd S D ]}|| kr| | V  q D ]}|| kr.| | E d H  q.D ]}|| krN| |  E d H  qN| d}|d k	rt |tr|E d H  n|V  | d}|d k	rt| }t|d }t |tr|V  |E d H  d S NrH   dependencies)r    r!   r@   r"   r   iternextr   r   rA   rH   rK   r@   valuerB   r   r   rC      s.    





zG_subresources_of_with_crazy_items_dependencies.<locals>.subresources_ofr   rD   r   rB   r   ._subresources_of_with_crazy_items_dependencies   s    	rP   c                   s   ddd fdd}|S )zT
    Specifically handle even older drafts where there are some funky keywords.
    r*   r>   r   c                 3  s   D ]}|| kr| | V  q D ]}|| kr | | E d H  q D ]}|| kr@| |   E d H  q@| d}|d k	rt|tr|E d H  n|V  | d}|d k	rt|  }t|d }t|tr|V  |E d H  dD ]}| |}t|tr|V  qd S )NrH   rK   )additionalItemsadditionalProperties)r@   r"   r    r   rL   rM   r   rN   rB   r   r   rC      s2    







zJ_subresources_of_with_crazy_aP_items_dependencies.<locals>.subresources_ofr   rD   r   rB   r   1_subresources_of_with_crazy_aP_items_dependencies   s    	rS   c                   s&   ||B  ddddd fdd}|S )NSequence[int | str]_Resolver[Any]Resource[Any]segmentsresolversubresourcer   c                   sB   t | }|D ]*}|kr| ks.t|d d kr|  S q||S r?   )rL   rM   in_subresourcerX   rY   rZ   Z	_segmentssegmentZin_childr;   r   r   maybe_in_subresource  s    
z3_maybe_in_subresource.<locals>.maybe_in_subresourcer   r;   r<   r=   r_   r   r^   r   _maybe_in_subresource  s    ra   c                   s&   ||B  ddddd fdd}|S )NrT   rU   rV   rW   c                   sd   t | }|D ]L}|dkr2t|jtr2||  S |kr| ksPt|d d kr|  S q||S rG   rL   r    r   r   r[   rM   r\   r^   r   r   r_   +  s    

z?_maybe_in_subresource_crazy_items.<locals>.maybe_in_subresourcer   r`   r   r^   r   !_maybe_in_subresource_crazy_items$  s    rc   c                   s&   ||B  ddddd fdd}|S )NrT   rU   rV   rW   c                   sl   t | }|D ]T}|dks |dkr:t|jtr:||  S |kr| ksXt|d d kr|  S q||S rJ   rb   r\   r^   r   r   r_   G  s     

zL_maybe_in_subresource_crazy_items_dependencies.<locals>.maybe_in_subresourcer   r`   r   r^   r   ._maybe_in_subresource_crazy_items_dependencies@  s    rd   zdraft2020-12rR   containsZcontentSchemaelseifrH   notZpropertyNamesZthenZunevaluatedItemsZunevaluatedPropertiesZallOfZanyOfoneOfZprefixItemsz$defsZdependentSchemasZpatternPropertiesZ
properties)r;   r=   r<   )r/   Zid_ofrC   Z
anchors_inr_   zdraft2019-09rQ   zdraft-07Zdefinitionszdraft-06zdraft-04zdraft-03Zextends)r=   r<   c                 C  s   i | ]\}}|t |qS r   )r
   opaque).0
dialect_idr,   r   r   r   
<dictcomp>  s    rm   z,https://json-schema.org/draft/2020-12/schemaz,https://json-schema.org/draft/2019-09/schemaz&http://json-schema.org/draft-07/schemaz&http://json-schema.org/draft-06/schemaz&http://json-schema.org/draft-04/schemaz&http://json-schema.org/draft-03/schemazRegistry[Specification[Schema]]_SPECIFICATIONSNr   zSpecification[Any])rl   defaultr   c                 C  s2   t | d}|dk	r|jS |dkr.t| |S )z
    Retrieve the `Specification` with the given dialect identifier.

    Raises:

        `UnknownDialect`

            if the given ``dialect_id`` isn't known
    r%   N)rn   r"   rstripr   r   )rl   ro   r0   r   r   r   specification_with-  s    rq   c                   @  s2   e Zd ZU dZded< ded< dddd	d
ZdS )r2   z4
    Dynamic anchors, introduced in draft 2020.
    strr/   zResource[Schema]r0   _Resolver[Schema]_Resolved[Schema]rY   r   c              	   C  sl   | j }| D ]H\}}z||| jj}W n tjk
rD   Y qY nX t|tr|j }qt	|j
||dS )z2
        Resolve this anchor dynamically.
        )r   rY   )r0   dynamic_scoper3   r/   rO   r   ZNoSuchAnchorr    r2   	_Resolvedr   r[   )selfrY   lastr   registryr3   r   r   r   resolveK  s    

zDynamicAnchor.resolveN)r   r   r   r   r   r{   r   r   r   r   r2   B  s   
r2   rs   rt   ru   c                 C  sb   |  d}t|jtr^|jdr^|  D ]2\}}|  |}t|jtrT|jdsX q^|}q*|S )u   
    Recursive references (via recursive anchors), present only in draft 2019.

    As per the 2019 specification (§ 8.2.4.2.1), only the ``#`` recursive
    reference is supported (and is therefore assumed to be the relevant
    reference).
    r%   z$recursiveAnchor)lookupr    r   r   r"   rv   )rY   Zresolvedr   _Znext_resolvedr   r   r   lookup_recursive_ref]  s    


r~   )N)<r   
__future__r   collections.abcr   r   typingr   r   r   Zreferencingr   r	   r
   r   r   Zreferencing._attrsr   Zreferencing._corer   rw   r   Z	_ResolverZreferencing.typingr   Z
AnchorTyper   rr   r*   r!   r   ZSchemaRegistry	Exceptionr   r#   r)   r+   r4   r5   r9   r:   	frozensetrE   rI   rP   rS   ra   rc   rd   ZDRAFT202012ZDRAFT201909ZDRAFT7ZDRAFT6ZDRAFT4ZDRAFT3rn   r   rq   r2   r~   r   r   r   r   <module>   s  
!(+

22

" 