grthtrhthjhtyjytjytkergtrhtrjytjerhrfh4:24 29/09/2026a Ï^ZD&ã@sdZddlmZdZdZdZdZzddlmZe Z Wne yJe ZYn0zddl mZmZWn"e y‚ddlmZmZYn0dd lmZdd lZdd lZeƒZdd d „Zefdd„Zdd„ZGdd„deƒZGdd„deƒZGdd„deƒZdd„Zdd„Zd 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)ÚMappingÚSequence)ÚteeNTcCst|ƒ}| |||¡S)a¼Resolves 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 )Ú JsonPointerÚset)ÚdocÚpointerÚvalueÚinplace©r ú//usr/lib/python3.9/site-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 )rÚresolve)r r Údefaultr r rÚresolve_pointerYs$rcCs$t|ƒ\}}|D]}qqt||ƒ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)ÚiterableÚaÚbÚ_r r rÚpairwise�s rc@s eZdZdS)ÚJsonPointerExceptionN)Ú__name__Ú __module__Ú __qualname__r r r rr•src@s eZdZdZdd„Zdd„ZdS)Ú EndOfListz)Result of accessing element "-" of a listcCs ||_dS©N)Úlist_)Úselfrr r rÚ__init__œszEndOfList.__init__cCsdj|jjt|jƒd�S)Nz {cls}({lst}))ÚclsZlst)ÚformatÚ __class__rÚreprr©rr r rÚ__repr__Ÿs ÿzEndOfList.__repr__N)rrrÚ__doc__r r&r r r rr™src@s–eZdZdZe d¡Ze d¡Zdd„Zdd„Z e fdd „Z e Z dd d „Z d d„Zdd„Zdd„Zdd„Zedd„ƒZdd„Zdd„Zedd„ƒZdS)rz;A JSON Pointer that can reference parts of an JSON documentz0|[1-9][0-9]*$z (~[^01]|~$)cCsZ|j |¡}|r"td | ¡¡ƒ‚| d¡}| d¡dkrBtdƒ‚dd„|Dƒ}||_dS)NzFound invalid escape {}ú/rÚzlocation must starts with /cSsg|] }t|ƒ‘qSr )Úunescape©Ú.0Úpartr r rÚ ¸óz(JsonPointer.__init__..)Ú_RE_INVALID_ESCAPEÚsearchrr"ÚgroupÚsplitÚpopÚparts)rr Zinvalid_escaper5r r rr ¬s ÿ zJsonPointer.__init__cCsD|js|dfS|jdd…D]}| ||¡}q|| ||jd¡fS)z>Resolves ptr until the last step, returns (sub-doc, last-step)Néÿÿÿÿ)r5ÚwalkÚget_part©rr r-r r rÚto_last»s zJsonPointer.to_lastc CsL|jD]@}z| ||¡}WqtyD|tur4‚n |YSYq0q|S)zBResolves the pointer against doc and returns the referenced object)r5r7rÚ_nothing)rr rr-r r rrÆs  zJsonPointer.resolveTcCsFt|jƒdkr|rtdƒ‚|S|s,t |¡}| |¡\}}|||<|S)zFResolve the pointer against the doc and replace the target with value.rzcannot set root in place)Úlenr5rÚcopyÚdeepcopyr:)rr r r Úparentr-r r rr×s zJsonPointer.setcCsjt|tƒr|St|tƒrH|dkr$|S|j t|ƒ¡s@td|ƒ‚t|ƒSt|dƒrV|Stdt |ƒƒ‚dS)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__N) Ú isinstancerrÚ_RE_ARRAY_INDEXÚmatchÚstrrÚintÚhasattrÚtyper9r r rr8çs    ÿzJsonPointer.get_partcCs„| ||¡}t|tƒrR|dkr&t|ƒSz ||WStyPtd|fƒ‚Yn0z ||WSty~td||fƒ‚Yn0dS)z7 Walks one step in doc and returns the referenced part r@zindex '%s' is out of boundszmember '%s' not found in %sN)r8rBrrÚ IndexErrorrÚKeyErrorr9r r rr7s      zJsonPointer.walkcCs|jdt|jƒ…|jkS)ú- Returns True if self contains the given ptr N)r5r<)rÚptrr r rÚcontainsszJsonPointer.containscCs | |¡S)rK)rM)rÚitemr r rÚ __contains__szJsonPointer.__contains__cCs$dd„|jDƒ}d dd„|Dƒ¡S)zwReturns the string representation of the pointer >>> ptr = JsonPointer('/~0/0/~1').path == '/~0/0/~1' cSsg|] }t|ƒ‘qSr )Úescaper+r r rr.(r/z$JsonPointer.path..r)css|]}d|VqdS©r(Nr r+r r rÚ )r/z#JsonPointer.path..)r5Újoin)rr5r r rÚpath"szJsonPointer.pathcCst|tƒsdS|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)rBrr5)rÚotherr r rÚ__eq__+s zJsonPointer.__eq__cCstt|jƒƒSr)ÚhashÚtupler5r%r r rÚ__hash__8szJsonPointer.__hash__cCs*dd„|Dƒ}|d dd„|Dƒ¡ƒ}|S)zœConstructs a JsonPointer from a list of (unescaped) paths >>> JsonPointer.from_parts(['a', '~', '/', 0]).path == '/a/~0/~1/0' True cSsg|]}tt|ƒƒ‘qSr )rPrEr+r r rr.Br/z*JsonPointer.from_parts..r)css|]}d|VqdSrQr r+r r rrRCr/z)JsonPointer.from_parts..)rS)r!r5rLr r rÚ from_parts;szJsonPointer.from_partsN)T)rrrr'ÚreÚcompilerCr0r r:r;rÚgetrr8r7rMrOÚpropertyrTrVrYÚ classmethodrZr r r rr¤s$      rcCs| dd¡ dd¡S)Nú~ú~0r(ú~1©Úreplace©Úsr r rrPGsrPcCs| dd¡ dd¡S)Nrbr(rar`rcrer r rr*Jsr*)T)r'Z __future__rÚ __author__Ú __version__Z __website__Z __license__Ú itertoolsrZunicoderEÚ ImportErrorÚzipÚcollections.abcrrÚ collectionsrr[r=Úobjectr;rrrÚ ExceptionrrrrPr*r r r rÚ!s6        ( $