3 G\D&@s dZddlmZdZdZdZdZyddlmZe Z Wne k rLe ZYnXyddl mZmZWn$e k rddlmZmZYnXdd 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|}|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)docpointervalueinplacer h/private/var/folders/pf/wv4htv3x0qs2c2mp0dnn0kchsvlck3/T/pip-install-emcbgzcf/jsonpointer/jsonpointer.py set_pointer?srcCst|}|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 )rresolve)r r defaultr r rresolve_pointerYs$rcCs&t|\}}x|D]}PqWt||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 ||_dS)N)list_)selfrr r r__init__szEndOfList.__init__cCsdj|jjt|jdS)Nz {cls}({lst}))clslst)format __class__rreprr)rr r r__repr__s zEndOfList.__repr__N)rrr__doc__rr%r r r rrsrc@seZdZdZejdZejdZddZddZ 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|jj|}|r"tdj|j|jd}|jddkrBtddd|D}||_dS)NzFound invalid escape {}/rzlocation must starts with /cSsg|] }t|qSr )unescape).0partr r r sz(JsonPointer.__init__..)_RE_INVALID_ESCAPEsearchrr"groupsplitpopparts)rr Zinvalid_escaper2r r rrs   zJsonPointer.__init__cCsH|js|dfSx"|jddD]}|j||}qW||j||jdfS)z>Resolves ptr until the last step, returns (sub-doc, last-step)Nr4)r2walkget_part)rr r+r r rto_lasts zJsonPointer.to_lastc CsJxD|jD]:}y|j||}Wqtk r@|tkr8n|SYqXqW|S)zBResolves the pointer against doc and returns the referenced object)r2r5r_nothing)rr rr+r r rrs zJsonPointer.resolveTcCsFt|jdkr|rtd|S|s,tj|}|j|\}}|||<|S)zFResolve the pointer against the doc and replace the target with value.rzcannot set root in place)lenr2rcopydeepcopyr7)rr r r parentr+r r rrs zJsonPointer.setcCsjt|tr|St|trH|dkr$|S|jjt|s@td|t|St|drV|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_INDEXmatchstrrinthasattrtype)rr r+r r rr6s    zJsonPointer.get_partcCs|j||}t|ds(tdt|ft|trn|dkrBt|Sy||Stk rltd|fYnXy||St k rtd||fYnXdS)z7 Walks one step in doc and returns the referenced part r>zinvalid document type %sr=zindex '%s' is out of boundszmember '%s' not found in %sN) r6rDAssertionErrorrEr?rr IndexErrorrKeyError)rr r+r r rr5s  zJsonPointer.walkcCs|jdt|j|jkS)z- Returns True if self contains the given ptr N)r2r9)rptrr r rcontainsszJsonPointer.containscCs |j|S)z- Returns True if self contains the given ptr )rJ)ritemr r r __contains__szJsonPointer.__contains__cCs$dd|jD}djdd|DS)zwReturns the string representation of the pointer >>> ptr = JsonPointer('/~0/0/~1').path == '/~0/0/~1' cSsg|] }t|qSr )escape)r*r+r r rr,(sz$JsonPointer.path..r(css|]}d|VqdS)r'Nr )r*r+r r r )sz#JsonPointer.path..)r2join)rr2r 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)r?rr2)rotherr r r__eq__+s zJsonPointer.__eq__cCstt|jS)N)hashtupler2)rr r r__hash__8szJsonPointer.__hash__cCs*dd|D}|djdd|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 )rMrB)r*r+r r rr,Bsz*JsonPointer.from_parts..r(css|]}d|VqdS)r'Nr )r*r+r r rrNCsz)JsonPointer.from_parts..)rO)r r2rIr r r from_parts;szJsonPointer.from_partsN)T)rrrr&recompiler@r-rr7r8rgetrr6r5rJrLpropertyrPrRrU classmethodrVr r r rrs       rcCs|jddjddS)N~z~0r'z~1)replace)sr r rrMGsrMcCs|jddjddS)Nz~1r'z~0r\)r])r^r r rr)Jsr))T)r& __future__r __author__ __version__Z __website__ __license__ itertoolsrunicoderB ImportErrorzipcollections.abcrr collectionsrrWr:objectr8rrr ExceptionrrrrMr)r r r r!s6      ( $