dbfdg 3 1V$@sddlmZyddlmZmZWn$ek rDddlmZmZYnXdZdZdZ dZ y ddl m Z ddl mZeZWn$ek rddlm Z eZYnXdd l mZdd lZdd lZejd ZGd d d eZGdddeZeZefddZdddZGdddeZddZ d S))unicode_literals)MappingSequenceu Stefan Kögl z1.10z2https://github.com/stefankoegl/python-json-pointerzModified BSD License)unquote)izip)teeNz0|[1-9][0-9]*$c@s eZdZdS)JsonPointerExceptionN)__name__ __module__ __qualname__r r !/usr/lib/python3.6/jsonpointer.pyrBsrc@s eZdZdZddZddZdS) EndOfListz+ Result of accessing element "-" of a list cCs ||_dS)N)list_)selfrr r r __init__IszEndOfList.__init__cCsdj|jjt|jdS)Nz {cls}({lst}))clsZlst)format __class__r reprr)rr r r __repr__Ms zEndOfList.__repr__N)r r r __doc__rrr r r r rFsrcCst|}|j||S)an Resolves pointer against doc and returns the referenced object >>> obj = {"foo": {"anArray": [ {"prop": 44}], "another prop": {"baz": "A string" }}} >>> resolve_pointer(obj, '') == obj True >>> resolve_pointer(obj, '/foo') == obj['foo'] True >>> resolve_pointer(obj, '/foo/another%20prop') == obj['foo']['another prop'] True >>> resolve_pointer(obj, '/foo/another%20prop/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 ) JsonPointerresolve)docpointerdefaultr r r resolve_pointerUsrTcCst|}|j|||S)aV 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%20another%20prop', 'added prop') == {'foo': {'another prop': {'baz': 'A string'}, 'yet another prop': 'added prop', 'anArray': [{'prop': 55}]}} True )rset)rrvalueinplacer r r set_pointerrsr!c@seZdZdZddZddZefddZeZdd d Z d d Z d dZ ddZ ddZ eddZddZddZeddZdS)rz= A JSON Pointer that can reference parts of an JSON document cCsP|jd}|jddkr tdtt|}dd|D}dd|D}||_dS)N/rzlocation must starts with /cSsg|]}|jddqS)z~1r")replace).0partr r r sz(JsonPointer.__init__..cSsg|]}|jddqS)z~0~)r$)r%r&r r r r's)splitpoprmaprparts)rrr,r r r rs  zJsonPointer.__init__cCsH|js|dfSx"|jddD]}|j||}qW||j||jdfS)z@ Resolves ptr until the last step, returns (sub-doc, last-step) Nr.)r,walkget_part)rrr&r r r to_lasts zJsonPointer.to_lastc CsJxD|jD]:}y|j||}Wqtk r@|tkr8n|SYqXqW|S)zBResolves the pointer against doc and returns the referenced object)r,r/r_nothing)rrrr&r r r rs zJsonPointer.resolveTcCsFt|jdkr|rtd|S|s,tj|}|j|\}}|||<|S)zH Resolve the pointer against the doc and replace the target with value. rzcannot set root in place)lenr,rcopydeepcopyr1)rrrr parentr&r r r rs zJsonPointer.setcCsjt|tr|St|trH|dkr$|Stjt|s@td|ft|St|drV|Stdt |dS)z+ Returns the next step in the correct type -z'%s' is not a valid list index __getitem__zQDocument '%s' does not support indexing, must be dict/list or support __getitem__N) isinstancerrRE_ARRAY_INDEXmatchstrrinthasattrtype)rrr&r r r r0s   zJsonPointer.get_partc Cs|j||}t|trFy||Stk rBtd||fYqXnPt|tr|dkr`t|Sy||Stk rtd|fYqXn||SdS)z7 Walks one step in doc and returns the referenced part zmember '%s' not found in %sr7zindex '%s' is out of boundsN)r0r9rKeyErrorrrr IndexError)rrr&r r r r/s   zJsonPointer.walkcCs|jdt|j|jkS)z+Returns True if self contains the given ptrN)r,r3)rptrr r r containsszJsonPointer.containscCs |j|S)z+Returns True if self contains the given ptr)rC)ritemr r r __contains__szJsonPointer.__contains__cCs2dd|jD}dd|D}djdd|DS)zx Returns the string representation of the pointer >>> ptr = JsonPointer('/~0/0/~1').path == '/~0/0/~1' cSsg|]}|jddqS)r(z~0)r$)r%r&r r r r' sz$JsonPointer.path..cSsg|]}|jddqS)r"z~1)r$)r%r&r r r r' sr#css|]}d|VqdS)r"Nr )r%r&r r r sz#JsonPointer.path..)r,join)rr,r r r pathszJsonPointer.pathcCst|tsdS|j|jkS)z compares 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)r9rr,)rotherr r r __eq__ s zJsonPointer.__eq__cCstt|jS)N)hashtupler,)rr r r __hash__szJsonPointer.__hash__cCsFdd|D}dd|D}dd|D}|djdd|D}|S)z Constructs a JsonPointer from a list of (unescaped) paths >>> JsonPointer.from_parts(['a', '~', '/', 0]).path == '/a/~0/~1/0' True cSsg|] }t|qSr )r<)r%r&r r r r'$sz*JsonPointer.from_parts..cSsg|]}|jddqS)r(z~0)r$)r%r&r r r r'%scSsg|]}|jddqS)r"z~1)r$)r%r&r r r r'&sr#css|]}d|VqdS)r"Nr )r%r&r r r rF'sz)JsonPointer.from_parts..)rG)rr,rBr r r from_partss zJsonPointer.from_partsN)T)r r r rrr1r2rgetrr0r/rCrEpropertyrHrJrM classmethodrNr r r r rs     rcCs&t|\}}x|D]}PqWt||S)z 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 r pairwise,s  rV)T)!Z __future__rcollections.abcrr ImportError collections __author__ __version__Z __website__Z __license__Zurllibr itertoolsrZunicoder<Z urllib.parseziprrer4compiler: Exceptionrobjectrr2rr!rrVr r r r !s6          $