o
    lh                     @   sr   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 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   )JsonSchemaDefinitionExceptionc                 C   s   |  d|  ddS )z9
    Originally ID was `id` and since v7 it's `$id`.
    $idid )get)schema r   o/var/www/html/construction_image-detection-poc/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 ddlm} || }|  p#d}z(zt|	 
|}W n tyE } ztd| |d}~ww W |  |S |  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   close)r!   handlersr$   resultr#   reqencodingexcr   r   r   resolve_remote0   s"   	
r3   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)selfr5   r
   r7   r8   r.   r   r   r   __init__P   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_schemab   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)r6   r   urljoin)r:   r@   	old_scoper   r   r   in_scopen   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   rA   r6   	urldefragr"   r7   r5   r
   r3   r.   r8   rC   r   )r:   rD   new_urir!   r   r
   old_base_uri
old_schemar   r   r   	resolvingz   s&   "zRefResolver.resolvingc                 C   s
   t | jS r   )r"   r6   )r:   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   r6   r   resublowerrstrip)r:   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   rA   r6   r   rC   r7   r"   itemsr<   r9   )r:   rT   rD   rL   itemr   r   r   r9      s&   


"

zRefResolver.walkN)__name__
__module____qualname____doc__r;   classmethodr?   
contextlibcontextmanagerrV   rC   rI   rJ   rS   r<   r9   r   r   r   r   r4   J   s    	r4   )r\   r^   r(   rN   urllibr   r   urllib.parser   
exceptionsr   r   r   r"   r3   r4   r   r   r   r   <module>   s    