
    Mh                     x    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 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   )JsonSchemaDefinitionExceptionc                 V    |                      d|                      dd                    S )z9
    Originally ID was `id` and since v7 it's `$id`.
    $idid )get)schemas    [/var/www/html/test/jupyter/venv/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^^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 ||         |           }nddlm}  ||           }|                                                                pd}	 t          j        |	                                
                    |                    }n5# t          $ r(}t          d                    | |                    d}~ww xY w	 |                                 n# |                                 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.
    r   )urlopenzutf-8z{} failed to decode: {}N)r"   r#   schemeurllib.requestr(   infoget_content_charsetjsonloadsreaddecode
ValueErrorr   r   close)r%   handlersr)   resultr(   reqencodingexcs           r   resolve_remoter8   0   s    s##*F!&!#&&******gcll88::1133>w	Z

 1 1( ; ;==FF 	\ 	\ 	\/0I0P0PQTVY0Z0Z[[[	\  IIKKKKCIIKKKKMs*   *9B$ #C/ $
C.#CCC/ /D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cacher3   walk)selfr<   r   r>   r?   r3   s         r   __init__zRefResolver.__init__P   sG     ! (

 		&r   c                 b     | t          |t                    rt          |          nd|fd|i|S )zA
        Construct a resolver from a JSON schema object.
        r
   r3   )r   dictr   )clsr   r3   kwargss       r   from_schemazRefResolver.from_schemab   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)rA   rH   	old_scopes      r   in_scopezRefResolver.in_scopen   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"   rJ   r=   	urldefragr&   r>   r<   r   r8   r3   r?   rL   r   )rA   rM   new_urir%   r   r   old_base_uri
old_schemas           r   	resolvingzRefResolver.resolvingz   s{      "4#8#>> *733X 	49S>>TZ//Z	#/FF 	4t},,[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=   )rA   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)rA   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"   rJ   r=   r   rL   r>   r&   itemsrD   r@   )rA   r`   rM   rX   items        r   r@   zRefResolver.walk   s    dD!! 	$Dt^^
4< = =^v,C#+D,A3GGDLLLtmmtt||F4LL#1N1N|vd||,, ( (?C
9T%:;;<#zz|| ( (GAt!$-- (		$(( ( ( ( ( ( ( ( ( ( ( ( ( ( ( ( ( (  ::<< $ $4dD)) $IIdOOO$ $s   *A!DD DN)__name__
__module____qualname____doc__rB   classmethodrG   
contextlibcontextmanagerrc   rL   rS   rU   r_   rD   r@    r   r   r:   r:   J   s         
 02    $ *, 	
 	
 	
 [	
 	.c 	. 	. 	. 	. BS B B B B20 0 0  $ $ $ $ $ $ $r   r:   )ri   rk   r-   rZ   urllibr   r"   urllib.parser   
exceptionsr   r   r   r&   r8   r:   rm   r   r   <module>rq      s          				 $ $ $ $ $ $             5 5 5 5 5 53 3 3  &+ + +  4h$ h$ h$ h$ h$ h$ h$ h$ h$ h$r   