
    od                         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
 ddlmZ d Zd	 Zd
 Zd Z G d d          ZdS )z
JSON Schema URI resolution scopes and dereferencing

https://tools.ietf.org/id/draft-zyp-json-schema-04.html#rfc.section.7

Code adapted from https://github.com/Julian/jsonschema
    N)parse)unquote)urlopen   )JsonSchemaDefinitionExceptionc                 V    |                      d|                      dd                    S )z9
    Originally ID was `id` and since v7 it's `$id`.
    $idid )get)schemas    ;lib/python3.11/site-packages/fastjsonschema/ref_resolver.pyget_idr      s&     ::eVZZb11222    c                    |                     d          }|r"t          |                              d          ng }|D ]}|                    dd                              dd          }t	          | t
                    r| t          |                   } W|| v r	| |         } dt          d                    |                    | S )zk
    Return definition from path.

    Path is unescaped according https://tools.ietf.org/html/rfc6901
    /~1~0~zUnresolvable ref: {})	lstripr   splitreplace
isinstancelistintr   format)r   fragmentpartsparts       r   resolve_pathr       s     s##H,4<GH##C((("E U U||D#&&..tS99fd## 	UCII&FFV^ 	UD\FF/0F0M0Md0S0STTTMr   c                 N    t          j        |                                           S N)urlparseurlsplitgeturl)uris    r   	normalizer'   -   s    S!!((***r   c                    t          j        |           j        }||v r ||         |           }nt          |           }|                                                                pd}	 t          j        |                                	                    |                    }n5# t          $ r(}t          d                    | |                    d}~ww xY w|S )z
    Resolve a remote ``uri``.

    .. note::

        urllib library is used to fetch requests from the remote ``uri``
        if handlers does notdefine otherwise.
    zutf-8z{} failed to decode: {}N)r#   r$   schemer   infoget_content_charsetjsonloadsreaddecode
ValueErrorr   r   )r&   handlersr)   resultreqencodingexcs          r   resolve_remoter6   1   s     s##*F \!&!#&&cll88::1133>w	\Z

 1 1( ; ;==FF 	\ 	\ 	\/0I0P0PQTVY0Z0Z[[[	\Ms   (9B" "
C,#CCc                       e Zd ZdZi di fdZei fd            Zej        de	fd            Z
ej        de	fd            Zd	 Zd
 ZdefdZdS )RefResolverz"
    Resolve JSON References.
    Tc                     || _         || _        || _        || _        || _        || _        |                     |           dS )a  
        `base_uri` is URI of the referring document from the `schema`.
        `store` is an dictionary that will be used to cache the fetched schemas
        (if `cache=True`).

        Please notice that you can have caching problems when compiling schemas
        with colliding `$ref`. To force overwriting use `cache=False` or
        explicitly pass the `store` argument (with a brand new dictionary)
        N)base_uriresolution_scoper   storecacher1   walk)selfr:   r   r<   r=   r1   s         r   __init__zRefResolver.__init__M   sG     ! (

 		&r   c                 b     | t          |t                    rt          |          nd|fd|i|S )zA
        Construct a resolver from a JSON schema object.
        r   r1   )r   dictr   )clsr   r1   kwargss       r   from_schemazRefResolver.from_schema_   sP    
 s(66>F6NNNB
 
 
 	
 
 	
r   scopec              #   |   K   | j         }t          j        ||          | _         	 dV  || _         dS # || _         w xY w)z:
        Context manager to handle current scope.
        N)r;   r#   urljoin)r?   rF   	old_scopes      r   in_scopezRefResolver.in_scopek   sU      
 )	 ( 0E B B	.EEE$-D!!!ID!----s   2 	;refc              #   b  K   t          j        | j        |          }t          j        |          \  }}|r1t	          |          | j        v r| j        t	          |                   }nH|r|| j        k    r| j        }n3t          || j	                  }| j
        r|| j        t	          |          <   | j        | j        }}||c| _        | _        	 |                     |          5  t          ||          V  ddd           n# 1 swxY w Y   ||c| _        | _        dS # ||c| _        | _        w xY w)zt
        Context manager which resolves a JSON ``ref`` and enters the
        resolution scope of this ref.
        N)r#   rH   r;   	urldefragr'   r<   r:   r   r6   r1   r=   rJ   r    )r?   rK   new_urir&   r   r   old_base_uri
old_schemas           r   	resolvingzRefResolver.resolvingw   s      "4#8#>> *733X 	49S>>TZ/ 	4Z	#/FF 	4t}, 	4[FF#C77Fz 4-3
9S>>*#'=$+j%(&"t{	Bs## 5 5"68444445 5 5 5 5 5 5 5 5 5 5 5 5 5 5 *6z&DM4;;;z&DM4;AAAAs0   D "D5D DD D	D D.c                 *    t          | j                  S r"   )r'   r;   )r?   s    r   get_urizRefResolver.get_uri   s    .///r   c                 &   dt          | j                                      dd                              dd                              dd          z   }t          j        dd|          }|                                                    d          }|S )zK
        Get current scope and return it as a valid function name.
        	validate_r   _r   "r   z($[^a-zA-Z]|[^a-zA-Z0-9]))r   r;   r   resublowerrstrip)r?   names     r   get_scope_namezRefResolver.get_scope_name   s     WT%:;;CCD#NNVVW[]`aaiijmoqrrrv2C>>zz||""3''r   nodec                    t          |t                    rdS d|v rBt          |d         t                    r'|d         }t          j        | j        |          |d<   dS d|v sd|v rt          t          |          t                    r|                     t          |                    5  || j        t          | j                  <   |
                                D ]/\  }}t          |t                    r|                     |           0	 ddd           dS # 1 swxY w Y   dS |
                                D ]/\  }}t          |t                    r|                     |           0dS )zR
        Walk thru schema and dereferencing ``id`` and ``$ref`` instances
        z$refr	   r
   N)r   boolstrr#   rH   r;   r   rJ   r<   r'   itemsrB   r>   )r?   r^   rK   rV   items        r   r>   zRefResolver.walk   s    dD!! 	$Dt^ 	$
4< = = 	$v,C#+D,A3GGDLLLtm 		$tt| 		$F4LL#1N1N 		$vd||,, ( (?C
9T%:;;<#zz|| ( (GAt!$-- (		$(( ( ( ( ( ( ( ( ( ( ( ( ( ( ( ( ( (  ::<< $ $4dD)) $IIdOOO$ $s   *A!DD DN)__name__
__module____qualname____doc__r@   classmethodrE   
contextlibcontextmanagerra   rJ   rQ   rS   r]   rB   r>    r   r   r8   r8   G   s         
 02    $ *, 	
 	
 	
 [	
 	.c 	. 	. 	. 	. BS B B B B20 0 0  $ $ $ $ $ $ $r   r8   )rg   ri   r,   rX   urllibr   r#   urllib.parser   urllib.requestr   
exceptionsr   r   r    r'   r6   r8   rk   r   r   <module>rp      s          				 $ $ $ $ $ $             " " " " " " 5 5 5 5 5 53 3 3  &+ + +  ,h$ h$ h$ h$ h$ h$ h$ h$ h$ h$r   