
    ~f                     p    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y)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                 F    | j                  d| j                  dd            S )z9
    Originally ID was `id` and since v7 it's `$id`.
    $idid )get)schemas    V/var/www/cvtools/html/venv/lib/python3.12/site-packages/fastjsonschema/ref_resolver.pyget_idr      s      ::eVZZb122    c                 :   |j                  d      }|rt        |      j                  d      ng }|D ]f  }|j                  dd      j                  dd      }t	        | t
              r| t        |         } D|| v r| |   } Nt        dj                  |             | 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,4GH##C("E U||D#&..tS9fd#CI&FV^D\F/0F0M0Md0STTU Mr   c                 H    t        j                  |       j                         S N)urlparseurlsplitgeturl)uris    r   	normalizer&   ,   s    S!((**r   c                    t        j                  |       j                  }||v r ||   |       }|S ddlm}  ||       }|j                         j                         xs d}	 t        j                  |j                         j                  |            }|S # t        $ r }t        dj                  | |            d}~ww xY w)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   )r%   handlersr)   resultr(   reqencodingexcs           r   resolve_remoter7   0   s     s#**F!&!#& M 	+cl88:113>w	\ZZ
 1 1( ;=F M  	\/0I0P0PQTVY0Z[[	\s   "2B 	B?B::B?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y)RefResolverz"
    Resolve JSON References.
    Tc                 z    || _         || _        || _        || _        || _        || _        | j                  |       y)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cacher2   walk)selfr;   r   r=   r>   r2   s         r   __init__zRefResolver.__init__N   s;     ! (

 		&r   c                 P     | t        |t              rt        |      nd|fd|i|S )zA
        Construct a resolver from a JSON schema object.
        r
   r2   )r   dictr   )clsr   r2   kwargss       r   from_schemazRefResolver.from_schema`   s:    
 (6F6NB
 
 	
 	
r   scopec              #      K   | j                   }t        j                  ||      | _         	 d || _         y# || _         w xY ww)z:
        Context manager to handle current scope.
        N)r<   r"   urljoin)r@   rG   	old_scopes      r   in_scopezRefResolver.in_scopel   s@     
 ))	 ( 0 0E B	.$-D!ID!s   (A7 A	A  Arefc              #     K   t        j                  | j                  |      }t        j                  |      \  }}|r0t	        |      | j
                  v r| j
                  t	        |         }nX|r|| j                  k(  r| j                  }n:t        || j                        }| j                  r|| j
                  t	        |      <   | j                  | j                  }}||c| _        | _        	 | j                  |      5  t        ||       ddd       ||c| _        | _        y# 1 sw Y   xY w# ||c| _        | _        w xY ww)zt
        Context manager which resolves a JSON ``ref`` and enters the
        resolution scope of this ref.
        N)r"   rI   r<   	urldefragr&   r=   r;   r   r7   r2   r>   rK   r   )r@   rL   new_urir%   r   r   old_base_uri
old_schemas           r   	resolvingzRefResolver.resolvingx   s     ""4#8#8#> **73X9S>TZZ/ZZ	#/Ft}},[[F#C7Fzz-3

9S>*#'==$++j%(&"t{	Bs# 5"68445 *6z&DM4;5 5 *6z&DM4;s6   C*E-D1 >D%D1 E%D.*D1 1EEc                 ,    t        | j                        S r!   )r&   r<   )r@   s    r   get_urizRefResolver.get_uri   s    ..//r   c                    dt        | j                        j                  dd      j                  dd      j                  dd      z   }t        j                  dd|      }|j                         j                  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   sr     WT%:%:;CCD#NVVW[]`aiijmoqrrvv2C>zz|""3'r   nodec                 v   t        |t              ryd|v r<t        |d   t              r)|d   }t        j                  | j
                  |      |d<   yd|v sd|v rt        t        |      t              r| j                  t        |            5  || j                  t        | j
                        <   |j                         D ]'  \  }}t        |t              s| j                  |       ) 	 ddd       y|j                         D ]'  \  }}t        |t              s| j                  |       ) y# 1 sw Y   yxY w)zR
        Walk thru schema and dereferencing ``id`` and ``$ref`` instances
        z$refr   r	   N)r   boolstrr"   rI   r<   r   rK   r=   r&   itemsrC   r?   )r@   r_   rL   rW   items        r   r?   zRefResolver.walk   s    dD!t^
4< =v,C#++D,A,A3GDLtmtt|F4L#1Nvd|, (?C

9T%:%:;<#zz| (GAt!$-		$(( (  ::< $4dD)IIdO$( (s   A	D/D//D8N)__name__
__module____qualname____doc__rA   classmethodrF   
contextlibcontextmanagerrb   rK   rR   rT   r^   rC   r?    r   r   r9   r9   H   s    
 02 $ *, 	
 	
 	.c 	. 	. BS B B20$ $r   r9   )rh   rj   r-   rY   urllibr   r"   urllib.parser   
exceptionsr   r   r   r&   r7   r9   rl   r   r   <module>rp      s>      	 $   53&+0h$ h$r   