o >](@sdZddlmZmZddlZddlZddlZddlZddlm Z ddl m Z m Z e ddgZe Ze ZGdd d eZGd d d eZGd d d eZejddGdddeZGdddeZGdddeZGdddeZGdddeZeefddZeZefddZdS)z2 Validation errors, and some surrounding helpers. ) defaultdictdequeN)_utils)PY3 iteritemsanyOfoneOfc seZdZedddeeeddf fdd ZddZddZer!eZnd d Ze d d Z e d dZ e ddZ ddZddZZS)_ErrorNc stt|||||||||| | ||_t||_|_t| |_|_t ||_ ||_ |_ ||_ ||_||_||_| |_|D]} || _q@dSN)superr __init__messagerpath relative_path schema_pathrelative_schema_pathlistcontextcause __cause__ validatorvalidator_valueinstanceschemaparent) selfrrrrrrrrrrerror __class__r 7/usr/lib/python3/dist-packages/jsonschema/exceptions.pyr s2    z_Error.__init__cCsd|jj|jfS)Nz<%s: %r>)r__name__rrr r r __repr__=sz_Error.__repr__c Cs|j|j|j|jf}tdd|Dr|jStj|jdd}tj|jdd}|jt d |j|j t t|jddt ||jt |jt |fS)Ncss|]}|tuVqdSr )_unset).0mr r r Dsz%_Error.__unicode__..Hwidthzl Failed validating %r in %s%s: %s On %s%s: %s )rrrranyrpprintpformattextwrapdedentrstrip!_word_for_schema_in_error_messagerformat_as_indexrrindent#_word_for_instance_in_error_messager)ressential_for_verbosepschema pinstancer r r __unicode__@s$   z_Error.__unicode__cCt|dSNzutf-8unicodeencoder"r r r __str__^z_Error.__str__cCs|di|S)Nr  _contents)clsotherr r r create_fromasz_Error.create_fromcC2|j}|dur |jSt|j}|t|j|Sr )rrr extendleftreversed absolute_pathrrrr r r rIe  z_Error.absolute_pathcCrFr )rrrrGrHabsolute_schema_pathrJr r r rLorKz_Error.absolute_schema_pathcKs0t|D]\}}t||turt|||qdSr )rgetattrr$setattr)rkwargskvr r r _setys  z _Error._setcsd}tfdd|DS)N) rrrrrrrrrrc3s|] }|t|fVqdSr )rM)r%attrr"r r r'sz#_Error._contents..)dict)rattrsr r"r rB~sz_Error._contents)r! __module__ __qualname__r$r r#r9rr? classmethodrEpropertyrIrLrRrB __classcell__r r rr r s0'    r c@eZdZdZdZdZdS)ValidationErrorz: An instance was invalid under a provided schema. rrNr!rVrW__doc__r2r5r r r r r\r\c@r[) SchemaErrorzB A schema was invalid under its corresponding metaschema. metaschemarNr]r r r r r`r_r`T)hashc@s eZdZdZeZddZdS)RefResolutionErrorz& A ref could not be resolved. cC t|jSr )str_causer"r r r r? zRefResolutionError.__str__N)r!rVrWr^rSibrfr?r r r r rcs rcc@4eZdZdZddZddZereZdSddZdS) UndefinedTypeCheckzN A type checker was asked to check a type it did not have registered. cCs ||_dSr type)rrlr r r r rgzUndefinedTypeCheck.__init__cCs d|jS)Nz'Type %r is unknown to this type checkerrkr"r r r r9rgzUndefinedTypeCheck.__unicode__cCr:r;r<r"r r r r?r@zUndefinedTypeCheck.__str__Nr!rVrWr^r r9rr?r r r r rjs rjc@ri) UnknownTypezP A validator was asked to validate an instance against an unknown type. cCs||_||_||_dSr )rlrr)rrlrrr r r r s zUnknownType.__init__cCsFtj|jdd}tj|jdd}td|jt |t |fS)Nr(r)z Unknown type %r for validator with schema: %s While checking instance: %s ) r-r.rrr/r0r1rlrr4)rr7r8r r r r9szUnknownType.__unicode__cCr:r;r<r"r r r r?r@zUnknownType.__str__Nrmr r r r rns  rncsBeZdZdZd fdd ZddZereZZSddZZS) FormatErrorz% Validating a format failed. Ncs(tt|||||_||_|_dSr )r ror rrr)rrrrr r r szFormatError.__init__cC|jSr )rr"r r r r9szFormatError.__unicode__cCs |jdSr;)rr>r"r r r r?s zFormatError.__str__r ) r!rVrWr^r r9rr?rZr r rr ros roc@sZeZdZdZeZdddZddZddZd d Z d d Z d dZ ddZ e ddZdS) ErrorTreezF ErrorTrees make it easier to check which validations failed. r cCsLi|_t|j|_|D]}|}|jD]}||}q||j|j<|j|_q dSr )errorsrrrBrrr _instance)rrrr containerelementr r r r s     zErrorTree.__init__cCs ||jvS)zC Check whether ``instance[index]`` has any errors. rArindexr r r __contains__ zErrorTree.__contains__cCs&|jtur||vr|j||j|S)a\ Retrieve the child tree one level down at the given ``index``. If the index is not in the instance that this tree corresponds to and is not known by this tree, whatever error would be raised by ``instance.__getitem__`` will be propagated (usually this is some subclass of `exceptions.LookupError`. )rsr$rBrvr r r __getitem__s  zErrorTree.__getitem__cCs||j|<dS)zB Add an error to the tree at the given ``index``. NrA)rrwvaluer r r __setitem__szErrorTree.__setitem__cCrd)zY Iterate (non-recursively) over the indices in the instance with errors. )iterrBr"r r r __iter__ryzErrorTree.__iter__cCrp)z, Return the `total_errors`. ) total_errorsr"r r r __len__szErrorTree.__len__cCsd|jjt|fS)Nz<%s (%s total errors)>)rr!lenr"r r r r#"szErrorTree.__repr__cCs&tddt|jD}t|j|S)zT The total number of errors in the entire tree, including children. css|] \}}t|VqdSr )r)r%_treer r r r'+sz)ErrorTree.total_errors..)sumrrBrrr)r child_errorsr r r r%szErrorTree.total_errorsN)r )r!rVrWr^r$rsr rxrzr|r~rr#rYrr r r r rqs  rqcsfdd}|S)aq Create a key function that can be used to sort errors by relevance. Arguments: weak (set): a collection of validator names to consider to be "weak". If there are two errors at the same level of the instance and one is in the set of weak validator names, the other error will take priority. By default, :validator:`anyOf` and :validator:`oneOf` are considered weak validators and will be superseded by other same-level validation errors. strong (set): a collection of validator names to consider to be "strong" cs |j}t|j |v|vfSr )rrr)rrstrongweakr r relevance?szby_relevance..relevancer )rrrr rr by_relevance/srcCsRt|}t|d}|durdStt|g||d}|jr't|j|d}|js|S)a Try to find an error that appears to be the best match among given errors. In general, errors that are higher up in the instance (i.e. for which `ValidationError.path` is shorter) are considered better matches, since they indicate "more" is wrong with the instance. If the resulting match is either :validator:`oneOf` or :validator:`anyOf`, the *opposite* assumption is made -- i.e. the deepest error is picked, since these validators only need to match once, and any other errors may not be relevant. Arguments: errors (collections.Iterable): the errors to select from. Do not provide a mixture of errors from different validation attempts (i.e. from different instances or schemas), since it won't produce sensical output. key (collections.Callable): the key to use when sorting errors. See `relevance` and transitively `by_relevance` for more details (the default is to sort with the defaults of that function). Changing the default is only useful if you want to change the function that rates errors but still want the error context descent done by this function. Returns: the best matching error, or ``None`` if the iterable was empty .. note:: This function is a heuristic. Its return value may change for a given set of inputs from version to version if better heuristics are added. N)key)r}nextmax itertoolschainrmin)rrrbestr r r best_matchHs& r) r^ collectionsrrrr-r/rS jsonschemarjsonschema.compatrr frozenset WEAK_MATCHESSTRONG_MATCHESUnsetr$ Exceptionr r\r`srcrjrnroobjectrqrrrr r r r s.  q  H