o ^ZD&@sdZddlmZdZdZdZdZz ddlmZe Z Wn e y%e ZYnwz ddl mZmZWne yAddlmZmZYnwdd lmZdd lZdd lZeZdd d ZefddZddZGdddeZGdddeZGdddeZddZddZd S)z7 Identify specific nodes in a JSON document (RFC 6901) )unicode_literalsu Stefan Kögl z2.0z2https://github.com/stefankoegl/python-json-pointerzModified BSD License)izip)MappingSequence)teeNTcCst|}||||S)aResolves pointer against doc and sets the value of the target within doc. With inplace set to true, doc is modified as long as pointer is not the root. >>> obj = {'foo': {'anArray': [ {'prop': 44}], 'another prop': {'baz': 'A string' }}} >>> set_pointer(obj, '/foo/anArray/0/prop', 55) == {'foo': {'another prop': {'baz': 'A string'}, 'anArray': [{'prop': 55}]}} True >>> set_pointer(obj, '/foo/yet another prop', 'added prop') == {'foo': {'another prop': {'baz': 'A string'}, 'yet another prop': 'added prop', 'anArray': [{'prop': 55}]}} True >>> obj = {'foo': {}} >>> set_pointer(obj, '/foo/a%20b', 'x') == {'foo': {'a%20b': 'x' }} True ) JsonPointerset)docpointervalueinplacer -/usr/lib/python3/dist-packages/jsonpointer.py set_pointer?srcCst|}|||S)aa Resolves pointer against doc and returns the referenced object >>> obj = {'foo': {'anArray': [ {'prop': 44}], 'another prop': {'baz': 'A string' }}, 'a%20b': 1, 'c d': 2} >>> resolve_pointer(obj, '') == obj True >>> resolve_pointer(obj, '/foo') == obj['foo'] True >>> resolve_pointer(obj, '/foo/another prop') == obj['foo']['another prop'] True >>> resolve_pointer(obj, '/foo/another prop/baz') == obj['foo']['another prop']['baz'] True >>> resolve_pointer(obj, '/foo/anArray/0') == obj['foo']['anArray'][0] True >>> resolve_pointer(obj, '/some/path', None) == None True >>> resolve_pointer(obj, '/a b', None) == None True >>> resolve_pointer(obj, '/a%20b') == 1 True >>> resolve_pointer(obj, '/c d') == 2 True >>> resolve_pointer(obj, '/c%20d', None) == None True )rresolve)r r defaultr r rresolve_pointerYs$ rcCs t|\}}|D]}t||S)z Transforms a list to a list of tuples of adjacent items s -> (s0,s1), (s1,s2), (s2, s3), ... >>> list(pairwise([])) [] >>> list(pairwise([1])) [] >>> list(pairwise([1, 2, 3, 4])) [(1, 2), (2, 3), (3, 4)] )rr)iterableab_r r rpairwises  rc@s eZdZdS)JsonPointerExceptionN)__name__ __module__ __qualname__r r r rrsrc@s eZdZdZddZddZdS) EndOfListz)Result of accessing element "-" of a listcCs ||_dSN)list_)selfrr r r__init__s zEndOfList.__init__cCsdj|jjt|jdS)Nz {cls}({lst}))clslst)format __class__rreprrrr r r__repr__s zEndOfList.__repr__N)rrr__doc__r r'r r r rrs rc@seZdZdZedZedZddZddZ e fdd Z e Z dd d Z d dZddZddZddZeddZddZddZeddZdS)rz;A JSON Pointer that can reference parts of an JSON documentz0|[1-9][0-9]*$z (~[^01]|~$)cCsZ|j|}|rtd||d}|ddkr!tddd|D}||_dS)NzFound invalid escape {}/rzlocation must starts with /cSg|]}t|qSr )unescape.0partr r r z(JsonPointer.__init__..)_RE_INVALID_ESCAPEsearchrr#groupsplitpopparts)rr invalid_escaper7r r rr s   zJsonPointer.__init__cCsD|js|dfS|jddD]}|||}q||||jdfS)z>Resolves ptr until the last step, returns (sub-doc, last-step)N)r7walkget_partrr r/r r rto_lasts zJsonPointer.to_lastc CsD|jD]}z|||}Wqty|tur|YSw|S)zBResolves the pointer against doc and returns the referenced object)r7r:r_nothing)rr rr/r r rrs   zJsonPointer.resolveTcCsFt|jdkr|r td|S|st|}||\}}|||<|S)zFResolve the pointer against the doc and replace the target with value.rzcannot set root in place)lenr7rcopydeepcopyr=)rr r r parentr/r r rrs zJsonPointer.setcCsft|tr|St|tr$|dkr|S|jt|s td|t|St|dr+|Stdt |)z)Returns the next step in the correct type-z"'%s' is not a valid sequence index __getitem__zXDocument '%s' does not support indexing, must be mapping/sequence or support __getitem__) isinstancerr_RE_ARRAY_INDEXmatchstrrinthasattrtyper<r r rr;s    zJsonPointer.get_partcCs|||}t|dsJdt|ft|tr5|dkr!t|Sz||WSty4td|fwz||WStyItd||fw)z7 Walks one step in doc and returns the referenced part rDzinvalid document type %srCzindex '%s' is out of boundszmember '%s' not found in %s) r;rJrKrErr IndexErrorrKeyErrorr<r r rr:s      zJsonPointer.walkcCs|jdt|j|jkS)- Returns True if self contains the given ptr N)r7r?)rptrr r rcontainsszJsonPointer.containscCs ||S)rN)rP)ritemr r r __contains__s zJsonPointer.__contains__cCs$dd|jD}ddd|DS)zwReturns the string representation of the pointer >>> ptr = JsonPointer('/~0/0/~1').path == '/~0/0/~1' cSr+r )escaper-r r rr0(r1z$JsonPointer.path..r*cs|]}d|VqdSr)Nr r-r r r )z#JsonPointer.path..)r7join)rr7r r rpath"szJsonPointer.pathcCst|tsdS|j|jkS)aCompares a pointer to another object Pointers can be compared by comparing their strings (or splitted strings), because no two different parts can point to the same structure in an object (eg no different number representations) F)rErr7)rotherr r r__eq__+s  zJsonPointer.__eq__cCstt|jSr)hashtupler7r&r r r__hash__8szJsonPointer.__hash__cCs*dd|D}|ddd|D}|S)zConstructs a JsonPointer from a list of (unescaped) paths >>> JsonPointer.from_parts(['a', '~', '/', 0]).path == '/a/~0/~1/0' True cSsg|]}tt|qSr )rSrHr-r r rr0Bsz*JsonPointer.from_parts..r*csrTrUr r-r r rrVCrWz)JsonPointer.from_parts..)rX)r!r7rOr r r from_parts;szJsonPointer.from_partsNT)rrrr(recompilerFr2r r=r>rgetrr;r:rPrRpropertyrYr[r^ classmethodr_r r r rrs&      rcC|ddddS)N~~0r)~1replacesr r rrSGrScCrf)Nrir)rhrgrjrlr r rr,Jrnr,r`)r( __future__r __author__ __version__ __website__ __license__ itertoolsrunicoderH ImportErrorzipcollections.abcrr collectionsrrar@objectr>rrr ExceptionrrrrSr,r r r rs<       ( $