o
    i                     @   s~   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d	 Zd
d Zdd Zdd ZG d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                 C   s   |  d|  ddS )z9
    Originally ID was `id` and since v7 it's `$id`.
    $idid )get)schema r   V/var/www/edux/Edux_v2/venv/lib/python3.10/site-packages/fastjsonschema/ref_resolver.pyget_id   s   r   c                 C   sx   | d}|rt|dng }|D ]'}|dddd}t| tr*| t| } q|| v r3| | } qt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partr   r   r   resolve_path   s   


r   c                 C   s   t |  S N)urlparseurlsplitgeturl)urir   r   r   	normalize-   s   r#   c              
   C   s   t | j}||v r|| | }|S t| }|  pd}zt| 	|}W |S  t
y? } ztd| |d}~ww )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excr   r   r   resolve_remote1   s   	r1   c                   @   sv   e Zd ZdZi di fddZei fddZejde	fdd	Z
ejd
e	fddZdd Zdd ZdefddZdS )RefResolverz"
    Resolve JSON References.
    Tc                 C   s2   || _ || _|| _|| _|| _|| _| | 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cacher,   walk)selfr3   r   r5   r6   r,   r   r   r   __init__M   s   
zRefResolver.__init__c                 K   s(   | t |tr
t|nd|fd|i|S )zA
        Construct a resolver from a JSON schema object.
        r	   r,   )r   dictr   )clsr   r,   kwargsr   r   r   from_schema_   s   zRefResolver.from_schemascopec                 c   s2    | j }t||| _ z	dV  W || _ dS || _ w )z:
        Context manager to handle current scope.
        N)r4   r   urljoin)r8   r>   	old_scoper   r   r   in_scopek   s   zRefResolver.in_scoperefc              	   c   s   t | j|}t |\}}|r t|| jv r | jt| }n|r'|| jkr+| j}nt|| j	}| j
r;|| jt|< | j| j}}||| _| _z/| | t||V  W d   n1 s`w   Y  W ||| _| _dS W ||| _| _dS ||| _| _w )zt
        Context manager which resolves a JSON ``ref`` and enters the
        resolution scope of this ref.
        N)r   r?   r4   	urldefragr#   r5   r3   r   r1   r,   r6   rA   r   )r8   rB   new_urir"   r   r   old_base_uri
old_schemar   r   r   	resolvingw   s&   "zRefResolver.resolvingc                 C   s
   t | jS r   )r#   r4   )r8   r   r   r   get_uri   s   
zRefResolver.get_uric                 C   sF   dt | jdddddd }t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   r4   r   resublowerrstrip)r8   namer   r   r   get_scope_name   s   &zRefResolver.get_scope_namenodec                 C   s   t |trdS d|v r!t |d tr!|d }t| j||d< dS d|v s)d|v ret t|tre| t|$ || jt	| j< |
 D ]\}}t |trR| | qDW d   dS 1 s^w   Y  dS |
 D ]\}}t |trw| | qidS )zR
        Walk thru schema and dereferencing ``id`` and ``$ref`` instances
        z$refr   r   N)r   boolstrr   r?   r4   r   rA   r5   r#   itemsr:   r7   )r8   rR   rB   rJ   itemr   r   r   r7      s&   


"

zRefResolver.walkN)__name__
__module____qualname____doc__r9   classmethodr=   
contextlibcontextmanagerrT   rA   rG   rH   rQ   r:   r7   r   r   r   r   r2   G   s    	r2   )rZ   r\   r'   rL   urllibr   r   urllib.parser   urllib.requestr   
exceptionsr   r   r   r#   r1   r2   r   r   r   r   <module>   s    