M`pdZddlmZddlZddlZddlZddlZddlZ ddlm Z n #e $re Z YnwxYwddl m Z mZdZdZ ddlmZmZn#e $r ddlmZmZeZYnwxYwdZd Zd Zd Zejd kreefZGd deZGddeZGddeZ Gddee!Z"dZ#ej$ej%e#Z&de fdZ'e fdZ(Gdde)Z*Gdde*Z+Gdde*Z,Gd d!e*Z-Gd"d#e*Z.Gd$d%e*Z/Gd&d'e*Z0Gd(d)e)Z1Gd*d+e)Z2d,Z3dS)-z Apply JSON-Patches (RFC 6902) )unicode_literalsN)MappingProxyType) JsonPointerJsonPointerException)MutableMappingMutableSequenceu Stefan Kögl z1.32z0https://github.com/stefankoegl/python-json-patchzModified BSD License)rceZdZdZdS)JsonPatchExceptionzBase Json Patch exceptionN__name__ __module__ __qualname____doc__+/usr/lib/python3/dist-packages/jsonpatch.pyr r J####rr ceZdZdZdS)InvalidJsonPatchz, Raised if an invalid JSON Patch is created Nr rrrrrNs6666rrceZdZdZdS)JsonPatchConflicta Raised if patch could not be applied due to conflict situation such as: - attempt to add object key when it already exists; - attempt to operate with nonexistence object key; - attempt to insert value to array at position beyond its size; - etc. Nr rrrrrRsrrceZdZdZdS)JsonPatchTestFailedz A Test operation failed Nr rrrrrrrctjt}|D] \}}|||!t d|DS)z'Convert duplicate keys values to lists.c3ZK|]&\}}|t|dkr|dn|fV'dS)rrN)len).0keyvaluess r zmultidict..fsY C 3v;;!++fQii8r) collections defaultdictlistappenddictitems) ordered_pairsmdictr values r multidictr,_sz  #D ) )E#!! U c %    !;;==  r)object_pairs_hookFct|trt||}nt||}|||S)aApply list of patches to specified json document. :param doc: Document object. :type doc: dict :param patch: JSON patch as list of dicts or raw JSON-encoded string. :type patch: list or str :param in_place: While :const:`True` patch will modify target document. By default patch will be applied to document copy. :type in_place: bool :param pointer_cls: JSON pointer class to use. :type pointer_cls: Type[JsonPointer] :return: Patched document object. :rtype: dict >>> doc = {'foo': 'bar'} >>> patch = [{'op': 'add', 'path': '/baz', 'value': 'qux'}] >>> other = apply_patch(doc, patch) >>> doc is not other True >>> other == {'foo': 'bar', 'baz': 'qux'} True >>> patch = [{'op': 'add', 'path': '/baz', 'value': 'qux'}] >>> apply_patch(doc, patch, in_place=True) == {'foo': 'bar', 'baz': 'qux'} True >>> doc == other True  pointer_cls) isinstance basestring JsonPatch from_stringapply)docpatchin_placer0s r apply_patchr9rsWB%$$:%%e%EE%[999 ;;sH % %%rc<t|||S)a!Generates patch by comparing two document objects. Actually is a proxy to :meth:`JsonPatch.from_diff` method. :param src: Data source document object. :type src: dict :param dst: Data source document object. :type dst: dict :param pointer_cls: JSON pointer class to use. :type pointer_cls: Type[JsonPointer] >>> src = {'foo': 'bar', 'numbers': [1, 3, 4, 8]} >>> dst = {'baz': 'qux', 'numbers': [1, 4, 7]} >>> patch = make_patch(src, dst) >>> new = patch.apply(src) >>> new == dst True r/)r3 from_diff)srcdstr0s r make_patchr>s*   sC[  A AArceZdZdZefdZdZdZdZdZ e dZ e dZ e j d Z d S) PatchOperationz'A single operation inside a JSON Patch.c~||_|dstdt|d|jr |dj|_|d|_nO|d|_ ||j|_n!#t$r}tdd}~wwxYw||_dS)Npathz#Operation must have a 'path' memberzInvalid 'path') r0 __contains__rr1rBlocationpointer TypeError operation)selfrGr0exs r__init__zPatchOperation.__init__s&%%f-- J"#HII I i')9 : : 9%f-2DM$V,DLL%f-DM 9#// >>  9 9 9&'7888 9#s5B B3B..B3c td)zGAbstract method that applies a patch operation to the specified object.z%should implement the patch operation.)NotImplementedError)rHobjs rr5zPatchOperation.applys!"IJJJrchtt|jSN)hash frozensetrGr(rHs r__hash__zPatchOperation.__hash__s&Idn224455666rcPt|tsdS|j|jkSNF)r1r@rGrHothers r__eq__zPatchOperation.__eq__s(%00 5~00rc||k SrOrrVs r__ne__zPatchOperation.__ne__5=!!rcPd|jjddS)N/)joinrEpartsrRs rrBzPatchOperation.paths"xx *3B3/000rc t|jjdS#t$r|jjdcYSwxYw)Nr^)intrEr` ValueErrorrRs rr zPatchOperation.keysP *t|)"-.. . * * *<%b) ) ) ) *s!AAc~t||jjd<|jj|_|j|jd<dS)Nr^rB)strrEr`rBrDrG)rHr+s rr zPatchOperation.keys7!$U 2 ) !%vrN)rrrrrrJr5rSrXrZpropertyrBr setterrrrr@r@s11.9####$KKK777111 """11X1**X*  Z//Z///rr@c$eZdZdZdZdZdZdS)RemoveOperationz/Removes an object property or an array element.c|j|\}} ||=n=#ttf$r)}d|}t |d}~wwxYw|S)Nz(can't remove a non-existent object '{0}')rEto_lastKeyError IndexErrorformatr)rHrMsubobjpartrImsgs rr5zRemoveOperation.applysu|++C00  )t *% ) ) )<CCDIIC#C(( ( ) s#A$AAc^|j|kr!|j|kr|xjdz c_n|dz}|SNrrBr rHrBr s r_on_undo_removezRemoveOperation._on_undo_removes9 9  x3A q rc^|j|kr!|j|kr|xjdzc_n|dz}|Srsrtrus r _on_undo_addzRemoveOperation._on_undo_add9 9  x#~~A q rNrrrrr5rvrxrrrririsG99rric$eZdZdZdZdZdZdS) AddOperationz,Adds an object property or an array element.cr |jd}n!#t$r}tdd}~wwxYw|j|\}}t |t r[|dkr||n|t|ks|dkrtd| ||nyt |tr ||}n_|||<nY|/td t|td |j||S)Nr+/The operation does not contain a 'value' member-rzcan't insert outside of listinvalid document type {0}2unable to fully resolve json pointer {0}, part {1})rGrlrrErkr1r r&rrinsertrrFrntyperD)rHrMr+rIrorps rr5zAddOperation.apply s[ CN7+EE C C C"ACC C C|++C00  fo . . zs{{ e$$$$F ##taxx'(FGGG dE****  / / z|$t | ; B B4<< P PQQQ'(\(c(cdhdqsw(x(xyyy   .).c^|j|kr!|j|kr|xjdz c_n|dz }|Srsrtrus rrvzAddOperation._on_undo_remove)ryrc^|j|kr!|j|kr|xjdzc_n|dz }|Srsrtrus rrxzAddOperation._on_undo_add1ryrNrzrrrr|r|sH66@rr|c$eZdZdZdZdZdZdS)ReplaceOperationz?Replaces an object property or an array element by a new value.c |jd}n!#t$r}tdd}~wwxYw|j|\}}||S|dkrtdt |t r)|t|ks|dkrtdnt |tr)||vr$d |}t|nY|/td t|td |j ||||<|S) Nr+r~rz7'path' with '-' can't be applied to 'replace' operationrzcan't replace outside of listz)can't replace a non-existent object '{0}'rr)rGrlrrErkr1r rrrrnrFrrD)rHrMr+rIrorprqs rr5zReplaceOperation.apply=sp CN7+EE C C C"ACC C C|++C00  <L 3;;"#\]] ] fo . . zs6{{""dQhh'(GHHH'/ / / z6!!AHHNN',,,"| ; B B4<< P PQQQ'(\(c(cdhdqsw(x(xyyyt  rc|SrOrrus rrvz ReplaceOperation._on_undo_remove] rc|SrOrrus rrxzReplaceOperation._on_undo_add`rrNrzrrrrr:sHII@rrcpeZdZdZdZedZedZejdZdZ dZ dS) MoveOperationz?Moves an object property or an array element to a new location.c t|jd|jr|jd}n ||jd}n!#t$r}t dd}~wwxYw||\}} ||}n5#tt f$r!}tt|d}~wwxYw|j |kr|St|tr)|j |rtdtd|jdd|j |}td|j|d|j |}|S) Nfrom.The operation does not contain a 'from' memberz*Cannot move values into their own childrenremoveoprBr/addrrBr+)r1rGr0rlrrkrmrrerErcontainsrir5r|rDrHrMfrom_ptrrIrorpr+s rr5zMoveOperation.applygs B$.0$2BCC D>&1++DN6,BCC B B B"@BB B B '',,  -4LEE*% - - -#CGG,, , - <8 # #J fn - - R %%h// R#$PQQ QN6*  ')))*/s M  ' )))*/s  s0AA A/A**A/ BC%CCc||jd}d|jddS)Nrr]r^)r0rGr_r`rHrs r from_pathzMoveOperation.from_paths9##DN6$:;;xxss+,,,rc||jd} t|jdS#t$r|jdcYSwxYwNrr^)r0rGrbr`rFrs rfrom_keyzMoveOperation.from_keysd##DN6$:;; &x~b)** * & & &>"% % % % &s<AAc||jd}t||jd<|j|jd<dSr)r0rGrer`rB)rHr+rs rrzMoveOperation.from_keysB##DN6$:;; ZZr!)vrc|j|kr!|j|kr|xjdz c_n|dz}|j|kr!|j|kr|xjdz c_n|dz }|SrsrrrBr rus rrvzMoveOperation._on_undo_removesn >T ! !}## " q 9  x#~~A q rc|j|kr!|j|kr|xjdzc_n|dz}|j|kr!|j|kr|xjdzc_n|dz }|Srsrrus rrxzMoveOperation._on_undo_addsn >T ! !}s"" " q 9  x#~~A q rN) rrrrr5rfrrrgrvrxrrrrrdsII###J--X-&&X&_//_/        rrceZdZdZdZdS) TestOperationz!Test value by specified location.c  |j|\}}||}n|j||}n.#t$r!}t t |d}~wwxYw |jd}n!#t$r}tdd}~wwxYw||krAd}t | |t||t||S)Nr+r~z0{0} ({1}) is not equal to tested value {2} ({3})) rErkwalkrrrerGrlrrnr)rHrMrorpvalrIr+rqs rr5zTestOperation.applys /<//44LFD|l''55# / / /%c"gg.. . / CN7+EE C C C"ACC C C %<<DC%cjjd3ii16U 'E'EFF F s/=A A+ A&&A+/ A== BBBNrrrrr5rrrrrs)++rrceZdZdZdZdS) CopyOperationzA Copies an object property or an array element to a new location c ||jd}n!#t$r}tdd}~wwxYw||\}} t j||}n5#ttf$r!}tt|d}~wwxYwtd|j |d|j |}|S)Nrrrrr/) r0rGrlrrkcopydeepcopyrmrrer|rDr5rs rr5zCopyOperation.applys B''v(>??HH B B B"@BB B B '',,  -M&,//EE*% - - -#CGG,, , -M  ' )))*/s  s* # A<AA88B* B%%B*Nrrrrrrs)KKrrc eZdZeejZeeZe e e e e eedZ efdZdZdZeZdZdZdZdZed efd Zed d efd Zdd ZedZddZ dZ!d S)r3)rrreplacemovetestrc`||_||_|jD]}||dSrO)r7r0_get_operation)rHr7r0rs rrJzJsonPatch.__init__(sE & * $ $B    # # # # $ $rc*|S)zstr(self) -> self.to_string()) to_stringrRs r__str__zJsonPatch.__str__3s~~rc*t|jSrO)boolr7rRs r__bool__zJsonPatch.__bool__7DJrc*t|jSrO)iterr7rRs r__iter__zJsonPatch.__iter__<rrcDtt|jSrO)rPtuple_opsrRs rrSzJsonPatch.__hash__?sE$)$$%%%rcPt|tsdS|j|jkSrU)r1r3rrVs rrXzJsonPatch.__eq__Bs(%++ 5yEJ&&rc||k SrOrrVs rrZzJsonPatch.__ne__Gr[rNcD|p|j}||}|||S)aCreates JsonPatch instance from string source. :param patch_str: JSON patch as raw string. :type patch_str: str :param loads: A function of one argument that loads a serialized JSON string. :type loads: function :param pointer_cls: JSON pointer class to use. :type pointer_cls: Type[JsonPointer] :return: :class:`JsonPatch` instance. r/) json_loader)cls patch_strloadsr0rr7s rr4zJsonPatch.from_stringJs4 .s  I&&s5k2222rTc|p|j}t||||}|dd||t|}|||S)aCCreates JsonPatch instance based on comparison of two document objects. Json patch would be created for `src` argument against `dst` one. :param src: Data source document object. :type src: dict :param dst: Data source document object. :type dst: dict :param dumps: A function of one argument that produces a serialized JSON string. :type dumps: function :param pointer_cls: JSON pointer class to use. :type pointer_cls: Type[JsonPointer] :return: :class:`JsonPatch` instance. >>> src = {'foo': 'bar', 'numbers': [1, 3, 4, 8]} >>> dst = {'baz': 'qux', 'numbers': [1, 4, 7]} >>> patch = JsonPatch.from_diff(src, dst) >>> new = patch.apply(src) >>> new == dst True r/N) json_dumper DiffBuilder_compare_valuesr%execute) rr<r= optimizationdumpsr0rbuilderopss rr;zJsonPatch.from_diff^sl>.s c3 MMMD#s3337??$$%%s3K0000rc4|p|j}||jS)z!Returns patch set as JSON string.)rr7)rHrrs rrzJsonPatch.to_strings!/t/ {4:&&&rcPtt|j|jSrO)rmaprr7rRs rrzJsonPatch._opssS,dj99:::rFcp|stj|}|jD]}||}|S)a5Applies the patch to a given object. :param obj: Document object. :type obj: dict :param in_place: Tweaks the way how patch would be applied - directly to specified `obj` or to its copy. :type in_place: bool :return: Modified `obj`. )rrrr5)rHrMr8rGs rr5zJsonPatch.applysD %-$$C ' 'I//#&&CC rcd|vrtd|d}t|tstd||jvr"td||j|}|||jS)Nrz&Operation does not contain 'op' memberzOperation must be a stringzUnknown operation {0!r}r/)rr1r2 operationsrnr0)rHrGrrs rrzJsonPatch._get_operations y "#KLL L t_"j)) A"#?@@ @ T_ $ $"#<#C#CB#G#GHH Hob!s9$*:;;;;rrO)F)"rrr staticmethodjsonrr _jsonloadsrrrir|rrrrrrrJrr __nonzero__rrSrXrZ classmethodr4r;rrfrr5rrrrr3r3s,tz**K,z**K!!!# ##J,Z+6 $ $ $ $      K   &&&''' """*.K333[3&(,D#"1"1"1["1H'''' ;;X;* < < < <  > > > > > ?s*AA,B  B c||t|f} |j||}|r|SdS#t$rf|j|}t t|dz ddD]3}||d|kr||dccYS4YdSwxYw)Nrr^r)rrrpoprFrranger)rHr+rrrris r take_indexzDiffBuilder.take_indexsDKK(  -'+// ::F $zz||# $ $ - - -)"-G3w<<>2r22 - -1:a=I--";;q>>!,,,,,,. - - - -s5A A)B;6B;:B;cJ|j}|d}|||gx|d<|d<|dS)Nrrr)rHrrlasts rrzDiffBuilder.inserts4{Aw!4,,Q$q'Awrc4|\}}}||d<||d<g|dd<dS)Nrrr)rHr link_prev link_next_s rrzDiffBuilder.removes1"' 9a !  ! aaarc#`K|j}|d}||ur|dV|d}||udSdSNrr)rHstartrcurrs r iter_fromzDiffBuilder.iter_fromsP{Qx$q'MMM7D$rc#`K|j}|d}||ur|dV|d}||udSdSrr)rHrrs rrzDiffBuilder.__iter__sP{Aw$q'MMM7D$rc#K|j}|d}||ur|d|ur|d|dd}}|j|jkrpt|tkrXt|tkr@t d|j|jdd|jjV|dd}|djV|d}||udSdS)Nrrrr+rr/)rrDrrir|rrGr0)rHrrop_first op_seconds rrzDiffBuilder.executes {Aw$Awd""&*1gtAwqz)$ (:::X/99Y<77*' ) 2!*!4W!=,,$(#3 5556? ???  71:Dq'# # # #7D$rc||t}||d}t|jtkrUt|tkr=||D]'}||j|j|_(|||j t||krDtd|j t||d|j }| |dSdStdt|||d|j }| |}|||t dS)NrrrrrBr/rr)r _ST_REMOVErr rbrrvrBrrD _path_joinrr0rr|r_ST_ADD) rHrBr itemrrvnew_op new_indexs r _item_addedzDiffBuilder._item_addedssj11  qBBF||s""tCyyC'7'7..@@A..rw??BFF KK   {js3333& K&tS11(( $/ 111  F##### 43""4--## + ---F  F++I   T9g 6 6 6 6 6rctdt||d|j}||t}||}||d}|j|jd}t|tkr=| |D]'} | |j |j|_(|||j|jkr+t#d|j|jd|j}||d<dS||dS|||t&dS)Nrrr/rrrr)rirr0rrrrErkrrr%rrxrBr rrDrrr) rHrBr r r rr r added_itemr s r _item_removedzDiffBuilder._item_removedsq tS))" " ')))g..KK''  qB ++DL99!> 6 6CW}}s8SXS#::^44 5sN33 5'' 4(=(=sCHHHH_555"3885'' 4(=(=sCHHHH&&tS#666$$T344447""""4#c(;;;;  sCH5555- 6 6rct|tr * * 03// 0    4 5 5sC @ @ @ @ @ _ - - 0300 0    4 5 5sC @ @ @ @ @ZZ__ 3 / / F   c3 / / / / /rN)rrrrrrrJrrrrrrrr rrrrrrrrrrs/3z{%%%% ? ? ? - - -  (7772:::B*** @ @ @666800000rrc||S|dzt|ddddzS)Nr]~z~0z~1)rerrts rrrsA { #:C((d33;;CFF FFr)4r __future__rr#r functoolsrsystypesr ImportErrorr' jsonpointerrrrrcollections.abcrr unicodere __author__ __version__ __website__ __license__ version_infobytesr2 Exceptionr rrAssertionErrorrr,partialrrr9r>objectr@rir|rrrrr3rrrrrr:s B&%''''''  &&&&&&&:9999999   ?????????;;;;;;;; CCC 0  @ $ vJ$$$$$$$$77777)777*$$$$$,n$$$    Y tzY G G G &+ %&%&%&%&P&1BBBB03/3/3/3/3/V3/3/3/ln<11111>111h'''''~'''TSSSSSNSSSlN6N2@<@<@<@<@<@<@<@