
    M?                     T   d dl mZ d dlZd dlZd dlZ	 d dlZd dlmZm	Z	 d dl
mZmZmZmZmZmZmZmZmZmZ d dlmZ d dlmZmZmZ  ej4                         Zi Z ej:                         Zd Zd(dZ d)d	Z! e  ejD                  d
      i de	jF                  de	jH                  de	jJ                  de	jL                  de	jN                  de	jP                  de	jR                  de	jT                  de	jV                  de	jX                  de	jZ                  de	j\                  de	j^                  de	j`                  de	jb                  de	jd                  de	jP                  e	jf                  e	jh                  e	jj                  e	jl                  e	jn                  dd
      Z8 e  ejD                  d      i de	jF                  de	jH                  de	jJ                  de	jr                  d e	jt                  de	jL                  de	jR                  de	jV                  de	jX                  de	jZ                  de	j\                  d!e	jv                  de	j^                  de	j`                  de	jb                  d"e	jx                  de	jd                  e	jP                  e	jz                  e	j|                  e	jf                  e	jh                  e	j~                  e	j                  e	j                  e	jn                  d#	d      ZB G d$ d%eC      ZDefd&ZEd)d'ZFy# e$ r dZY w xY w)*    )divisionN)_utils_validators)
Sequenceurljoinurlsplit	urldefragunquoteurlopen	str_types	int_types	iteritems	lru_cache)	ErrorTree)RefResolutionErrorSchemaErrorUnknownTypec                       fd}|S )a  
    Register the decorated validator for a ``version`` of the specification.

    Registered validators and their meta schemas will be considered when
    parsing ``$schema`` properties' URIs.

    Arguments:

        version (str):

            An identifier to use as the version's name

    Returns:

        callable: a class decorator to decorate the validator with the version

    c                 b    | t         <   d| j                  v r| t        | j                  d   <   | S )Nid)
validatorsMETA_SCHEMAmeta_schemas)clsversions    (lib/third_party/jsonschema/validators.py
_validateszvalidates.<locals>._validates.   s1    !
7COO#36L/0
     )r   r   s   ` r   	validatesr       s    &
 r   c                     5t         t        t        t        d       t        j
                  t        t        d G  fddt              }|9 t        |      |      }|j                         j                  dd      dz   |_        |S )N)arraybooleanintegernullnumberobjectstringc                       e Zd Z eW       Z eW       Z eW        Z	 d	dZed        Z	d
dZ
ddZd Zd Zd
dZy)create.<locals>.ValidatorNc                     t        | j                        | _        | j                  j                  |       |t        j                  |      }|| _        || _        || _        y N)	dictDEFAULT_TYPES_typesupdateRefResolverfrom_schemaresolverformat_checkerschema)selfr5   typesr3   r4   s        r   __init__z"create.<locals>.Validator.__init__C   sT     t112DKKKu%&226:$DM"0D DKr   c                 x     | | j                         j                  |      D ]  }t        j                  |       y r,   )r   iter_errorsr   create_from)r   r5   errors      r   check_schemaz&create.<locals>.Validator.check_schemaP   s2    S__-99&A!--e44 Br   c              3   <  K   || j                   }|j                  d      }|r| j                  j                  |       	 |j                  d      }|d|fg}nt	        |      }|D ]r  \  }}| j
                  j                  |      }|$ || |||      xs d}	|	D ];  }
|
j                  ||||       |dk7  r|
j                  j                  |       |
 = t 	 |r| j                  j                          y y # |r| j                  j                          w w xY ww)Nr   $refr   )	validatorvalidator_valueinstancer5   )
r5   getr3   
push_scoper   
VALIDATORS_setschema_path
appendleft	pop_scope)r6   rB   _schemascoperefr   kvr@   errorsr<   s              r   r:   z%create.<locals>.Validator.iter_errorsU   s     ++KK&E((/.kk'*?#*C.!1J!*7!3J&DAq $ 3 3A 6I ( &tQ'BHbF!'

&',-%-#*	 #  <!--88;# "( '$ MM++- 5MM++- s   =D BC: D:DDc              3      K   | j                  ||      D ]@  }||j                  j                  |       ||j                  j                  |       | B y wr,   )r:   pathrH   rG   )r6   rB   r5   rQ   rG   r<   s         r   descendz!create.<locals>.Validator.descendx   sT     ))(F;#JJ))$/*%%00= <s   AAc                 4     | j                   |i |D ]  }| y r,   )r:   )r6   argskwargsr<   s       r   validatez"create.<locals>.Validator.validate   s#    )))4:6: ;r   c                    || j                   vrt        ||| j                        | j                   |   }t        |t              r2t        j                  |      }t        d |D              }|r	t        |vryt        ||      S )Nc              3   P   K   | ]  }t        |t        j                           y wr,   )
issubclassnumbersNumber).0pytypes     r   	<genexpr>z4create.<locals>.Validator.is_type.<locals>.<genexpr>   s       EL6Jvw~~6Ws   $&F)r/   r   r5   
isinstanceboolr   flattenany)r6   rB   typepytypes	is_numbers        r   is_typez!create.<locals>.Validator.is_type   s|    4;;&!$$++>>kk$'G (D) ..1  EL  	 W!4 h00r   c                 B    t        | j                  ||      d       }|d u S r,   )nextr:   )r6   rB   rJ   r<   s       r   is_validz"create.<locals>.Validator.is_valid   s%    ))(G<dCED= r   r   NNr,   )NN)__name__
__module____qualname__r-   rE   r   r.   r8   classmethodr=   r:   rR   rV   rf   ri   )default_typesmeta_schemar   s   r   	Validatorr*   >   s[    *%
;']+ CG	! 
	5 
	5!	.F			1	!r   rq     )listr`   r   rc   rZ   r[   r-   r   r'   r    titlereplacerk   )rp   r   r   ro   rq   s   `` ` r   createrw   6   s}    )$ZGNNt 
W! W!F W!r &Ig&y1	$]]_44S"=K	r   c                     t        | j                        }|j                  |       t        | j                  ||| j
                        S )N)rp   r   r   ro   )r-   rE   r0   rw   r   r.   )r@   r   r   all_validatorss       r   extendrz      sE    )../N*%))!--	 r   draft3r?   additionalItemsadditionalPropertiesdependenciesdisallowdivisibleByenumextendsformatitemsmaxItems	maxLengthmaximumminItems	minLengthminimum
multipleOf)patternpatternProperties
propertiesrc   uniqueItems)rp   r   r   draft4allOfanyOfmaxPropertiesminProperties)	r   notoneOfr   r   r   requiredrc   r   c                       e Zd ZdZ	 	 	 	 	 ddZed        Zd Zd Ze	d        Z
e	d        Zej                  d	        Zej                  d
        Zd Zd Zd Zd Zy)r1   a&  
    Resolve JSON References.

    Arguments:

        base_uri (str):

            The URI of the referring document

        referrer:

            The actual referring document

        store (dict):

            A mapping from URIs to documents to cache

        cache_remote (bool):

            Whether remote refs should be cached after first resolution

        handlers (dict):

            A mapping from URI schemes to functions that should be used
            to retrieve them

        urljoin_cache (functools.lru_cache):

            A cache that will be used for caching the results of joining
            the resolution scope to subscopes.

        remote_cache (functools.lru_cache):

            A cache that will be used for caching the results of
            resolved remote URLs.

    Nc                    | t        d      t              }| t        d      | j                        }|| _        || _        t        |      | _        |g| _        t        j                  d t        t              D              | _        | j                  j                  |       || j                  |<   || _        || _        y )Ni   c              3   >   K   | ]  \  }}||j                   f  y wr,   )r   )r\   r   r@   s      r   r^   z'RefResolver.__init__.<locals>.<genexpr>#  s'      $
!8I &&'!8s   )r   r   resolve_from_urlreferrercache_remoter-   handlers_scopes_stackr   URIDictr   r   storer0   _urljoin_cache_remote_cache)r6   base_urir   r   r   r   urljoin_cacheremote_caches           r   r8   zRefResolver.__init__  s      +IdOG4M*9T?4+@+@AL (X&Z^^ $
!*<!8$
 

 	

% '

8+)r   c                 <     | |j                  dd      |g|i |S )z
        Construct a resolver from a JSON schema object.

        Arguments:

            schema:

                the referring schema

        Returns:

            :class:`RefResolver`

        r   rs   )rC   )r   r5   rT   rU   s       r   r2   zRefResolver.from_schema-  s&    " 6::eS)6CDCFCCr   c                 n    | j                   j                  | j                  | j                  |             y r,   )r   appendr   resolution_scoper6   rK   s     r   rD   zRefResolver.push_scope@  s,    !! 5 5u=	
r   c                 j    	 | j                   j                          y # t        $ r t        d      w xY w)NznFailed to pop the scope from an empty stack. `pop_scope()` should only be called once for every `push_scope()`)r   pop
IndexErrorr   r6   s    r   rI   zRefResolver.pop_scopeE  s:    	""$ 	$! 	s    2c                      | j                   d   S )N)r   r   s    r   r   zRefResolver.resolution_scopeO  s    !!"%%r   c                 6    t        | j                        \  }}|S r,   )r	   r   )r6   uri_s      r   r   zRefResolver.base_uriS  s    4001Q
r   c              #      K   | j                  |       	 d  | j                          y # | j                          w xY wwr,   )rD   rI   r   s     r   in_scopezRefResolver.in_scopeX  s.     	NNDNNs   ?* ?<?c              #      K   | j                  |      \  }}| j                  |       	 | | j                          y# | j                          w xY ww)z
        Context manager which resolves a JSON ``ref`` and enters the
        resolution scope of this ref.

        Arguments:

            ref (str):

                The reference to resolve

        N)resolverD   rI   )r6   rL   urlresolveds       r   	resolvingzRefResolver.resolving`  sD      S)X	NNNDNNs   &A> AAAc                 `    | j                  | j                  |      }|| j                  |      fS r,   )r   r   r   )r6   rL   r   s      r   r   zRefResolver.resolveu  s0    !!$"7"7=D&&s+++r   c                     t        |      \  }}	 | j                  |   }| j                  ||      S # t        $ r2 	 | j                  |      }n# t        $ r}t        |      d }~ww xY wY Lw xY wr,   )r	   r   KeyErrorresolve_remote	Exceptionr   resolve_fragment)r6   r   fragmentdocumentexcs        r   r   zRefResolver.resolve_from_urly  s|    !#X	.zz#H $$Xx88  	....s3 .(--. 	.s2   1 	A,AA,	A&A!!A&&A,+A,c                 Z   |j                  d      }|rt        |      j                  d      ng }|D ]F  }|j                  dd      j                  dd      }t	        |t
              r	 t        |      }	 ||   }H |S # t        $ r Y w xY w# t        t        f$ r t        d|z        w xY w)z
        Resolve a ``fragment`` within the referenced ``document``.

        Arguments:

            document:

                The referrant document

            fragment (str):

                a URI fragment to resolve within it

        /z~1z~0~zUnresolvable JSON pointer: %r)lstripr
   splitrv   r_   r   int
ValueError	TypeErrorLookupErrorr   )r6   r   r   partsparts        r   r   zRefResolver.resolve_fragment  s      ??4(19!''-rD<<t,44UDAD(H-t9D#D>    "  {+ (3h> s   (A=4B=	B	B	B*c                 <   t        |      j                  }|| j                  v r | j                  |   |      }n|dv rt        rt	        t        j
                  dd      gt        t        j
                  j                        r$t        j                  |      j                         }n[t        j                  |      j                  }n;t        j                  t        |      j                         j                  d            }| j                  r|| j                  |<   |S )a  
        Resolve a remote ``uri``.

        If called directly, does not check the store first, but after
        retrieving the document at the specified URI it will be saved in
        the store if :attr:`cache_remote` is True.

        .. note::

            If the requests_ library is present, ``jsonschema`` will use it to
            request the remote ``uri``, so that the correct encoding is
            detected and used.

            If it isn't, or if the scheme of the ``uri`` is not ``http`` or
            ``https``, UTF-8 is assumed.

        Arguments:

            uri (str):

                The URI to resolve

        Returns:

            The retrieved document

        .. _requests: http://pypi.python.org/pypi/requests/

        )httphttpsjsonNzutf-8)r   schemer   requestsgetattrResponsecallabler   rC   loadsr   readdecoder   r   )r6   r   r   results       r   r   zRefResolver.resolve_remote  s    > #%%T]]"*T]]6*3/F))H%%vt4@ ))../!c*//1!c*// ZZ 1 1 3 : :7 CDF$DJJsOr   )r   Tr   NN)rk   rl   rm   __doc__r8   rn   r2   rD   rI   propertyr   r   
contextlibcontextmanagerr   r   r   r   r   r   r   r   r   r1   r1      s    $T *< D D$

 & &      (,
9#J4r   r1   c                 j    |t         u rt        }t        j                  | j                  dd      |      S )Nz$schemars   )_unsetDraft4Validatorr   rC   )r5   defaults     r   validator_forr     s-    &!FJJz37AAr   c                 v    |t        |      }|j                  |        ||g|i |j                  |        y)av  
    Validate an instance under the given schema.

        >>> validate([2, 3, 4], {"maxItems": 2})
        Traceback (most recent call last):
            ...
        ValidationError: [2, 3, 4] is too long

    :func:`validate` will first verify that the provided schema is itself
    valid, since not doing so can lead to less obvious error messages and fail
    in less obvious or consistent ways. If you know you have a valid schema
    already or don't care, you might prefer using the
    :meth:`~IValidator.validate` method directly on a specific validator
    (e.g. :meth:`Draft4Validator.validate`).


    Arguments:

        instance:

            The instance to validate

        schema:

            The schema to validate with

        cls (:class:`IValidator`):

            The class that will be used to validate the instance.

    If the ``cls`` argument is not provided, two things will happen in
    accordance with the specification. First, if the schema has a
    :validator:`$schema` property containing a known meta-schema [#]_ then the
    proper validator will be used.  The specification recommends that all
    schemas contain :validator:`$schema` properties for this reason. If no
    :validator:`$schema` property is found, the default validator class is
    :class:`Draft4Validator`.

    Any other provided positional and keyword arguments will be passed on when
    instantiating the ``cls``.

    Raises:

        :exc:`ValidationError` if the instance is invalid

        :exc:`SchemaError` if the schema itself is invalid

    .. rubric:: Footnotes
    .. [#] known by a validator registered with :func:`validates`
    N)r   r=   rV   )rB   r5   r   rT   rU   s        r   rV   rV     s?    f {F#V   ))(3r   rj   r,   )G
__future__r   r   r   rZ   r   ImportError
jsonschemar   r   jsonschema.compatr   r   r   r	   r
   r   r   r   r   r   jsonschema.exceptionsr   r   r   r   Unsetr   r   r   r   r    rw   rz   load_schemarL   r|   r}   r~   disallow_draft3r   r   extends_draft3r   r   r   r   r   r   r   r   r   r   properties_draft3type_draft3r   Draft3ValidatorallOf_draft4anyOf_draft4maxProperties_draft4minProperties_draft4
not_draft4oneOf_draft4properties_draft4required_draft4type_draft4r   r'   r1   r   rV   r   r   r   <module>r      s       +   , N N 

v~~6eP """8,K77 	 !A!A 	11	
 	[00 	.. 	!! 	K.. 	;%% 	+## 	[)) 	k++ 	K'' 	[)) 	k++  	K''!" 	{--#$  '');;"44((#//-0 5: """8,K77 	 !A!A 	+**	
 	+** 	11 	!! 	;%% 	+## 	[)) 	k++ 	+:: 	K'' 	[)) 	k++  	+::!" 	K''#$ #--&&**'');;"44 00((#//58 =Dv& vr #) B64}  Hs   L L'&L'