B
    &‹d—  ã               @   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`.
    z$idÚidÚ )Úget)Úschema© r   úh/work/yifan.wang/ringdown/master-ringdown-env/lib/python3.7/site-packages/fastjsonschema/ref_resolver.pyÚget_id   s    r   c             C   s~   |  d¡}|rt|ƒ d¡ng }xX|D ]P}| dd¡ dd¡}t| tƒrV| t|ƒ } q&|| krh| | } q&td |¡ƒ‚q&W | S )zk
    Return definition from path.

    Path is unescaped according https://tools.ietf.org/html/rfc6901
    ú/z~1z~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}||kr"|| | ƒ}ndt| ƒ}| ¡  ¡ p8d}yt | ¡  	|¡¡}W n2 t
k
r„ } ztd | |¡ƒ‚W dd}~X Y nX |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Úexcr   r   r   Úresolve_remote1   s    	"r-   c               @   sv   e Zd ZdZi di fdd„Zei fdd„ƒZeje	dœdd	„ƒZ
eje	d
œdd„ƒZdd„ Zdd„ Zedœ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)Úselfr/   r
   r1   r2   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   s,   | j }t ||¡| _ z
dV  W d|| _ X dS )z:
        Context manager to handle current scope.
        N)r0   r   Úurljoin)r4   r:   Z	old_scoper   r   r   Úin_scopek   s
    
zRefResolver.in_scope)Úrefc          	   c   sÌ   t  | j|¡}t  |¡\}}|r>t|ƒ| jkr>| jt|ƒ }n6|rL|| jkrT| j}n t|| j	ƒ}| j
rt|| jt|ƒ< | j| j }}|| | _| _z&|  |¡ t||ƒV  W dQ R X W d|| | _| _X dS )zt
        Context manager which resolves a JSON ``ref`` and enters the
        resolution scope of this ref.
        N)r   r;   r0   Ú	urldefragr   r1   r/   r
   r-   r(   r2   r<   r   )r4   r=   Znew_urir   r   r
   Zold_base_uriZ
old_schemar   r   r   Ú	resolvingw   s    zRefResolver.resolvingc             C   s
   t | jƒS )N)r   r0   )r4   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.
        Z	validate_z~1Ú_z~0ú"r   z($[^a-zA-Z]|[^a-zA-Z0-9]))r   r0   r   ÚreÚsubÚlowerÚrstrip)r4   Únamer   r   r   Úget_scope_name”   s    &zRefResolver.get_scope_name)Únodec          	   C   sà   t |tƒrnÐd|kr>t |d tƒr>|d }t | j|¡|d< nžd|ksNd|kr²t t|ƒtƒr²|  t|ƒ¡@ || jt	| jƒ< x(| 
¡ D ]\}}t |tƒr†|  |¡ q†W W dQ R X n*x(| 
¡ D ]\}}t |tƒr¼|  |¡ q¼W dS )zR
        Walk thru schema and dereferencing ``id`` and ``$ref`` instances
        z$refz$idr   N)r   ÚboolÚstrr   r;   r0   r   r<   r1   r   Úitemsr6   r3   )r4   rI   r=   rA   Úitemr   r   r   r3      s    


zRefResolver.walkN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r5   Úclassmethodr9   Ú
contextlibÚcontextmanagerrK   r<   r?   r@   rH   r6   r3   r   r   r   r   r.   G   s   	r.   )rQ   rS   r#   rC   Úurllibr   r   Úurllib.parser   Úurllib.requestr   Ú
exceptionsr   r   r   r   r-   r.   r   r   r   r   Ú<module>   s   