^ZD&dZddlmZdZdZdZdZ ddlmZe Z ddl mZmZdd lmZdd lZdd lZeZdd Zefd Zd ZGddeZGddeZGddeZdZdZy #e $re ZY[wxYw#e $r ddlmZmZYewxYw)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)teeNc>t|}|j|||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)docpointervalueinplaces -/usr/lib/python3/dist-packages/jsonpointer.py set_pointerr?s!,'"G ;;sE7 ++c<t|}|j||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 defaults rresolve_pointerrYs H'"G ??3 ((rcFt|\}}|D]}nt||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_s rpairwisers/ x=DAq   1:rc eZdZy)JsonPointerExceptionN)__name__ __module__ __qualname__rrrrsrrceZdZdZdZdZy) EndOfListz)Result of accessing element "-" of a listc||_yN)list_)selfr&s r__init__zEndOfList.__init__s  rcvdj|jjt|jS)Nz {cls}({lst}))clslst)format __class__rreprr&r's r__repr__zEndOfList.__repr__s2$$)@)@)-djj)9%; ;rN)rrr __doc__r(r0r!rrr#r#s3;rr#ceZdZdZej dZej dZdZdZ e fdZ e Z ddZ dZd Zd Zd Zed Zd ZdZedZy)r z;A JSON Pointer that can reference parts of an JSON documentz0|[1-9][0-9]*$z (~[^01]|~$)c6|jj|}|r(tdj|j |j d}|j ddk7r td|Dcgc] }t|}}||_ycc}w)NzFound invalid escape {}/rzlocation must starts with /) _RE_INVALID_ESCAPEsearchrr,groupsplitpopunescapeparts)r'r invalid_escaper<parts rr(zJsonPointer.__init__s0077@ &'@'G'G$$&(() ) c" 99Q<2 &'DE E,12D$22 3s:Bc|js|dfS|jddD]}|j||}||j||jdfS)z>Resolves ptr until the last step, returns (sub-doc, last-step)N)r<walkget_partr'r r>s rto_lastzJsonPointer.to_lasts^zz9 JJsO 'D))C&C 'DMM#tzz"~666rc|jD]} |j||}|S#t$r|tur|cYcSwxYw)zBResolves the pointer against doc and returns the referenced object)r<rAr_nothing)r'r rr>s rrzJsonPointer.resolvesTJJ #D #iiT* # ( #h&"N  #s'AAct|jdk(r|r td|S|stj|}|j |\}}|||<|S)zFResolve the pointer against the doc and replace the target with value.rzcannot set root in place)lenr<rcopydeepcopyrD)r'r r rparentr>s rr zJsonPointer.setsY tzz?a *+EFFL--$Cc*t  rct|tr|St|trD|dk(r|S|jj t |st d|zt|St|dr|St dt|z)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_INDEXmatchstrrinthasattrtyperCs rrBzJsonPointer.get_parts c7 #K X &s{ ''--c$i8*+ORV+VWWt9  S- (K'(Y[_`c[d(ef frc0|j||}t|dsJdt|t|tr|dk(r t |S ||S ||S#t $rtd|dwxYw#t$rtd|d|wxYw)z7 Walks one step in doc and returns the referenced part rNzinvalid document type rMzindex 'z' is out of boundszmember 'z' not found in ) rBrTrUrOrr# IndexErrorrKeyErrorrCs rrAzJsonPointer.walks}}S$'sM*U$s),UU* c8 $s{ ~% U4y  Tt9   U*D+STT U T&c'RS S TsAA:A7:Bc`|jdt|j|jk(S)- Returns True if self contains the given ptr N)r<rH)r'ptrs rcontainszJsonPointer.containss$zz/3syy>*cii77rc$|j|S)rZ)r\)r'items r __contains__zJsonPointer.__contains__s}}T""rc|jDcgc] }t|}}djd|DScc}w)zwReturns the string representation of the pointer >>> ptr = JsonPointer('/~0/0/~1').path == '/~0/0/~1' r5c3&K|] }d|z ywr4Nr!.0r>s r z#JsonPointer.path..)s4dsTz4)r<escapejoin)r'r>r<s rpathzJsonPointer.path"s9 +/**5$55ww4e4446s;cVt|tsy|j|jk(S)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)rOr r<)r'others r__eq__zJsonPointer.__eq__+s$%-zzU[[((rc>tt|jSr%)hashtupler<r/s r__hash__zJsonPointer.__hash__8sE$**%&&rc|Dcgc]}tt|}}|djd|D}|Scc}w)zConstructs a JsonPointer from a list of (unescaped) paths >>> JsonPointer.from_parts(['a', '~', '/', 0]).path == '/a/~0/~1/0' True r5c3&K|] }d|z ywrbr!rcs rrez)JsonPointer.from_parts..Cs7#*7rf)rgrRrh)r*r<r>r[s r from_partszJsonPointer.from_parts;sC055tD "55"''7778 6sANT)rrr r1recompilerPr6r(rDrFrgetr rBrAr\r_propertyrirlrp classmethodrsr!rrr r sE!bjj!12O#M2  7$,  C f4T28#55 )'rr cF|jddjddS)N~~0r4~1replacess rrgrgGs 99S$  ' 'T 22rcF|jddjddS)Nr}r4r|r{r~rs rr;r;Js 99T3  ' 'c 22rrt)r1 __future__r __author__ __version__ __website__ __license__ itertoolsrunicoderR ImportErrorzipcollections.abcrr collectionsrrurIobjectrFrrr Exceptionrr#r rgr;r!rrrsB>'0  B $  C.1  8,4+3%)P( 9 ;;`&`F33w D .--.s"A,A9,A65A69 B B