3 F\M?%@sddlmZddlZddlZddlZy ddlZWnek rHdZYnXddlmZm Z ddl m Z m Z m Z mZmZmZmZmZmZmZddlmZddlmZmZmZejZiZejZddZfddfd d Z dd d Z!e ej"d e j#e j$e j%e j&e j'e j(e j)e j*e j+e j,e j-e j.e j/e j0e j1e j2e j(e j3e j4e j5e j6e j7dd dZ8e ej"de j#e j$e j%e j9e j:e j&e j)e j+e j,e j-e j.e j;e j/e j0e j1e je j3e j4e j?e j@e jAe j7dddZBGdddeCZDefddZEdddZFdS))divisionN)_utils _validators) Sequenceurljoinurlsplit urldefragunquoteurlopen str_types int_types iteritems lru_cache) ErrorTree)RefResolutionError SchemaError UnknownTypecsfdd}|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 cs$|t<d|jkr |t|jd<|S)Nid) validators META_SCHEMA meta_schemas)cls)versionq/private/var/folders/pf/wv4htv3x0qs2c2mp0dnn0kchsvlck3/T/pip-install-emcbgzcf/jsonschema/jsonschema/validators.py _validates.s zvalidates.._validatesr)rrr)rr validatess rcshdkr"ttttdtjttdGfdddt}|dk rdt ||}|j j ddd|_ |S)N)arraybooleanintegernullnumberobjectstringcsneZdZeZeZeZfddfddZeddZ dddZ ddd Z d d Z d d Z dddZdS)zcreate..ValidatorNcSs@t|j|_|jj||dkr*tj|}||_||_||_dS)N) dict DEFAULT_TYPES_typesupdate RefResolver from_schemaresolverformat_checkerschema)selfr,typesr*r+rrr__init__Cs   z"create..Validator.__init__cSs(x"||jj|D]}tj|qWdS)N)r iter_errorsrZ create_from)rr,errorrrr check_schemaPsz&create..Validator.check_schemac ss|dkr|j}|jd}|r(|jj|z|jd}|dk rHd|fg}nt|}xr|D]j\}}|jj|}|dkrtqV|||||pf} x8| D]0} | j||||d|dkr| jj|| VqWqVWWd|r|jj XdS)Nrz$ref) validatorZvalidator_valueinstancer,) r,getr* push_scoper VALIDATORSZ_set schema_path appendleft pop_scope) r-r4_schemascoperefrkvr3errorsr1rrrr0Us4       z%create..Validator.iter_errorscssHxB|j||D]2}|dk r&|jj||dk r:|jj||VqWdS)N)r0pathr9r8)r-r4r,rAr8r1rrrdescendxs   z!create..Validator.descendc_sx|j||D]}|qWdS)N)r0)r-argskwargsr1rrrvalidatesz"create..Validator.validatecSsb||jkrt|||j|j|}t|trXtj|}tdd|D}|rXt|krXdSt||S)Ncss|]}t|tjVqdS)N) issubclassnumbersNumber).0Zpytyperrr sz4create..Validator.is_type..F)r&rr, isinstanceboolrflattenany)r-r4typeZpytypesZ is_numberrrris_types     z!create..Validator.is_typecSst|j||d}|dkS)N)nextr0)r-r4r;r1rrris_validsz"create..Validator.is_valid)N)NN)N)__name__ __module__ __qualname__r$r7rr%r/ classmethodr2r0rBrErPrRr) default_types meta_schemarrr Validator>s  # rY ) listrLr rOrGrHr$r r"rtitlereplacerS)rXrrrWrYr)rWrXrrcreate6s Y r_cCs(t|j}|j|t|j|||jdS)N)rXrrrW)r$r7r'r_rr%)r3rrZall_validatorsrrrextends  r`Zdraft3)z$refadditionalItemsadditionalProperties dependenciesZdisallowZ divisibleByenumZextendsformatitemsmaxItems maxLengthmaximumminItems minLengthminimum multipleOfpatternpatternProperties propertiesrO uniqueItems)rXrrZdraft4)z$refrarbZallOfZanyOfrcrdrerfrgrhZ maxPropertiesrirjrkZ minPropertiesrlrmnotoneOfrnrorprequiredrOrqc@seZdZdZfdfddfddZeddZdd Zd d Ze d d Z e ddZ e j ddZe j ddZddZddZddZddZdS)r(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. TNcCs|dkrtdt}|dkr*td|j}||_||_t||_|g|_tj ddt t D|_ |j j |||j |<||_||_dS)Nicss|]\}}||jfVqdS)N)r)rIrr3rrrrJ$sz'RefResolver.__init__..)rrresolve_from_urlreferrer cache_remoter$handlers _scopes_stackrURIDictr rstorer'_urljoin_cache _remote_cache)r-base_urirvr{rwrxZ urljoin_cacheZ remote_cacherrrr/s     zRefResolver.__init__cOs||jdd|f||S)z Construct a resolver from a JSON schema object. Arguments: schema: the referring schema Returns: :class:`RefResolver` rr[)r5)rr,rCrDrrrr)-szRefResolver.from_schemacCs|jj|j|j|dS)N)ryappendr|resolution_scope)r-r<rrrr6@szRefResolver.push_scopec Cs0y|jjWntk r*tdYnXdS)NznFailed to pop the scope from an empty stack. `pop_scope()` should only be called once for every `push_scope()`)rypop IndexErrorr)r-rrrr:Es zRefResolver.pop_scopecCs |jdS)N)ry)r-rrrrOszRefResolver.resolution_scopecCst|j\}}|S)N)rr)r-uri_rrrr~SszRefResolver.base_uric cs$|j|z dVWd|jXdS)N)r6r:)r-r<rrrin_scopeXs  zRefResolver.in_scopec cs2|j|\}}|j|z |VWd|jXdS)z Context manager which resolves a JSON ``ref`` and enters the resolution scope of this ref. Arguments: ref (str): The reference to resolve N)resolver6r:)r-r=urlZresolvedrrr resolving`s   zRefResolver.resolvingcCs|j|j|}||j|fS)N)r|rr})r-r=rrrrruszRefResolver.resolvecCsvt|\}}y|j|}WnNtk rhy|j|}Wn*tk rb}zt|WYdd}~XnXYnX|j||S)N)rr{KeyErrorresolve_remote Exceptionrresolve_fragment)r-rfragmentdocumentexcrrrruys  zRefResolver.resolve_from_urlcCs|jd}|rt|jdng}x~|D]v}|jddjdd}t|trjy t|}Wntk rhYnXy ||}Wq&tt fk rt d|Yq&Xq&W|S)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~zUnresolvable JSON pointer: %r) lstripr splitr^rKrint ValueError TypeError LookupErrorr)r-rrpartspartrrrrs     zRefResolver.resolve_fragmentcCst|j}||jkr$|j||}n`|dkrltrlttjdddk rlttjjr^tj|j}qtj|j}ntj 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/ httphttpsjsonNzutf-8)rr)rschemerxrequestsgetattrResponsecallablerr5loadsr readdecoderwr{)r-rrresultrrrrs    zRefResolver.resolve_remote)rSrTrU__doc__r/rVr)r6r:propertyrr~ contextlibcontextmanagerrrrrurrrrrrr(s"%     %r(cCs |tkr t}tj|jdd|S)Nz$schemar[)_unsetDraft4Validatorrr5)r,defaultrrr validator_forsrcOs4|dkrt|}|j|||f||j|dS)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)rr2rE)r4r,rrCrDrrrrEs3 rE)N)N)G __future__rrrrGr ImportErrorZ jsonschemarrZjsonschema.compatrrrrr r r r r rZjsonschema.exceptionsrrrrZUnsetrrrzrrr_r`Z load_schemar=rarbrcZdisallow_draft3rmrdZextends_draft3rerfrgrhrirjrkrlrnroZproperties_draft3Z type_draft3rqZDraft3ValidatorZ allOf_draft4Z anyOf_draft4ZmaxProperties_draft4ZminProperties_draft4Z not_draft4Z oneOf_draft4Zproperties_draft4Zrequired_draft4Z type_draft4rr"r(rrErrrrs   0 h z