ó µ‹URc@sÃddlmZdZdZdZdZddlZddlZddlZddl Z ddl Z ddl Z ej d#krŽe efZne jZd efd „ƒYZd efd „ƒYZd eefd„ƒYZd„Zd„Zeƒe _ed„Zd„Zdefd„ƒYZdefd„ƒYZdefd„ƒYZdefd„ƒYZ defd„ƒYZ!defd„ƒYZ"defd „ƒYZ#d!efd"„ƒYZ$dS($iÿÿÿÿ(tunicode_literalsu Stefan Kögl u1.2u0https://github.com/stefankoegl/python-json-patchuModified BSD LicenseNiitJsonPatchExceptioncBseZdZRS(uBase Json Patch exception(t__name__t __module__t__doc__(((s-/usr/lib/python2.7/site-packages/jsonpatch.pyR:stJsonPatchConflictcBseZdZRS(u Raised if patch could not be applied due to conflict situation such as: - attempt to add object key then it already exists; - attempt to operate with nonexistence object key; - attempt to insert value to array at position beyond of it size; - etc. (RRR(((s-/usr/lib/python2.7/site-packages/jsonpatch.pyR>stJsonPatchTestFailedcBseZdZRS(u A Test operation failed (RRR(((s-/usr/lib/python2.7/site-packages/jsonpatch.pyRFscCstjtƒ}x%|D]\}}||j|ƒqWx=|jƒD]/\}}t|ƒdkrD|d||>> doc = {'foo': 'bar'} >>> other = apply_patch(doc, [{'op': 'add', 'path': '/baz', 'value': 'qux'}]) >>> doc is not other True >>> other == {'foo': 'bar', 'baz': 'qux'} True >>> apply_patch(doc, [{'op': 'add', 'path': '/baz', 'value': 'qux'}], in_place=True) == {'foo': 'bar', 'baz': 'qux'} True >>> doc == other True (t isinstancet basestringt JsonPatcht from_stringtapply(tdoctpatchtin_place((s-/usr/lib/python2.7/site-packages/jsonpatch.pyt apply_patchms cCstj||ƒS(uÇGenerates patch by comparing of 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 >>> 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 (Rt from_diff(tsrctdst((s-/usr/lib/python2.7/site-packages/jsonpatch.pyt make_patchsRcBs•eZdZd„Zd„Zd„ZeZd„Zd„Zd„Z e d„ƒZ e d„ƒZ d „Z ed „ƒZed „Zd „ZRS( ugA JSON Patch is a list of Patch Operations. >>> patch = JsonPatch([ ... {'op': 'add', 'path': '/foo', 'value': 'bar'}, ... {'op': 'add', 'path': '/baz', 'value': [1, 2, 3]}, ... {'op': 'remove', 'path': '/baz/1'}, ... {'op': 'test', 'path': '/baz', 'value': [1, 3]}, ... {'op': 'replace', 'path': '/baz/0', 'value': 42}, ... {'op': 'remove', 'path': '/baz/1'}, ... ]) >>> doc = {} >>> result = patch.apply(doc) >>> expected = {'foo': 'bar', 'baz': [42]} >>> result == expected True JsonPatch object is iterable, so you could easily access to each patch statement in loop: >>> lpatch = list(patch) >>> expected = {'op': 'add', 'path': '/foo', 'value': 'bar'} >>> lpatch[0] == expected True >>> lpatch == patch.patch True Also JsonPatch could be converted directly to :class:`bool` if it contains any operation statements: >>> bool(patch) True >>> bool(JsonPatch([])) False This behavior is very handy with :func:`make_patch` to write more readable code: >>> old = {'foo': 'bar', 'numbers': [1, 3, 4, 8]} >>> new = {'baz': 'qux', 'numbers': [1, 4, 7]} >>> patch = make_patch(old, new) >>> if patch: ... # document have changed, do something useful ... patch.apply(old) #doctest: +ELLIPSIS {...} cCs@||_itd6td6td6td6td6td6|_dS(Nuremoveuaddureplaceumoveutestucopy(R#tRemoveOperationt AddOperationtReplaceOperationt MoveOperationt TestOperationt CopyOperationt operations(tselfR#((s-/usr/lib/python2.7/site-packages/jsonpatch.pyt__init__Ñs cCs |jƒS(ustr(self) -> self.to_string()(t to_string(R1((s-/usr/lib/python2.7/site-packages/jsonpatch.pyt__str__ÝscCs t|jƒS(N(tboolR#(R1((s-/usr/lib/python2.7/site-packages/jsonpatch.pyt__bool__áscCs t|jƒS(N(titerR#(R1((s-/usr/lib/python2.7/site-packages/jsonpatch.pyt__iter__æscCstt|jƒƒS(N(thashttuplet_ops(R1((s-/usr/lib/python2.7/site-packages/jsonpatch.pyt__hash__êscCs\t|tƒstStt|jƒƒtt|jƒƒko[tttj |j|jƒƒS(N( RRtFalseR R R;talltmaptoperatorteq(R1tother((s-/usr/lib/python2.7/site-packages/jsonpatch.pyt__eq__îs*cCstj|ƒ}||ƒS(u¸Creates JsonPatch instance from string source. :param patch_str: JSON patch as raw string. :type patch_str: str :return: :class:`JsonPatch` instance. (Rtloads(tclst patch_strR#((s-/usr/lib/python2.7/site-packages/jsonpatch.pyR ös csO‡‡fd†‰‡fd†‰‡fd†‰|tˆdg||ƒƒƒS(uOCreates JsonPatch instance based on comparing 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 :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 c3s½||krdSt|tƒrSt|tƒrSxˆˆ|||ƒD] }|VqAWnft|tƒr–t|tƒr–xEˆ|||ƒD] }|Vq„Wn#idd6dj|ƒd6|d6VdS(Nureplaceuopu/upathuvalue(RR R tjoin(tpathtvalueRBt operation(t compare_dictt compare_list(s-/usr/lib/python2.7/site-packages/jsonpatch.pytcompare_valuess   c3sÏxz|D]r}||krBidd6dj||gƒd6Vqn||g}x'ˆ|||||ƒD] }|VqjWqWxK|D]C}||kr„idd6dj||gƒd6||d6Vq„q„WdS(Nuremoveuopu/upathuadduvalue(RG(RHR'R(tkeytcurrentRJ(RM(s-/usr/lib/python2.7/site-packages/jsonpatch.pyRK#s  # !   c3s5t|ƒt|ƒ}}xZtt||ƒƒD]C}|t|ƒg}x'ˆ|||||ƒD] }|VqcWq/W||krÙx¬t||ƒD]@}|t|ƒg}idd6dj|ƒd6||d6Vq’WnX||kr1xItt||ƒƒD]/}idd6dj|t|ƒgƒd6VqûWndS(Nuadduopu/upathuvalueuremove(R trangetmintstrRGtreversed(RHR'R(tlsrctldsttidxRORJ(RM(s-/usr/lib/python2.7/site-packages/jsonpatch.pyRL/s!  . u(R (RER'R(((RKRLRMs-/usr/lib/python2.7/site-packages/jsonpatch.pyR&s  cCstj|jƒS(u!Returns patch set as JSON string.(RtdumpsR#(R1((s-/usr/lib/python2.7/site-packages/jsonpatch.pyR3?scCst|j|jƒS(N(R?t_get_operationR#(R1((s-/usr/lib/python2.7/site-packages/jsonpatch.pyR;CscCs?|stj|ƒ}nx |jD]}|j|ƒ}q"W|S(u/Applies the patch to given object. :param obj: Document object. :type obj: dict :param in_place: Tweaks way how patch would be applied - directly to specified `obj` or to his copy. :type in_place: bool :return: Modified `obj`. (tcopytdeepcopyR;R!(R1tobjR$RJ((s-/usr/lib/python2.7/site-packages/jsonpatch.pyR!Gs cCs|d|krtdƒ‚n|d}t|tƒsCtdƒ‚n||jkretd|ƒ‚n|j|}||ƒS(Nuopu&Operation does not contain 'op' memberuOperation must be a stringuUnknown operation '%s'(RRRR0(R1RJtopRE((s-/usr/lib/python2.7/site-packages/jsonpatch.pyRX\s   (RRRR2R4R6t __nonzero__R8R<RCt classmethodR R&R3tpropertyR;R=R!RX(((s-/usr/lib/python2.7/site-packages/jsonpatch.pyR£s-      =  tPatchOperationcBs2eZdZd„Zd„Zd„Zd„ZRS(u'A single operation inside a JSON Patch.cCs/|d|_tj|jƒ|_||_dS(Nupath(tlocationt jsonpointert JsonPointertpointerRJ(R1RJ((s-/usr/lib/python2.7/site-packages/jsonpatch.pyR2ps cCstdƒ‚dS(uAAbstract method that applies patch operation to specified object.u!should implement patch operation.N(tNotImplementedError(R1R[((s-/usr/lib/python2.7/site-packages/jsonpatch.pyR!uscCstt|jjƒƒƒS(N(R9t frozensetRJR (R1((s-/usr/lib/python2.7/site-packages/jsonpatch.pyR<zscCs#t|tƒstS|j|jkS(N(RR`R=RJ(R1RB((s-/usr/lib/python2.7/site-packages/jsonpatch.pyRC~s(RRRR2R!R<RC(((s-/usr/lib/python2.7/site-packages/jsonpatch.pyR`ms    R*cBseZdZd„ZRS(u/Removes an object property or an array element.cCsO|jj|ƒ\}}y ||=Wn%tk rJ}tt|ƒƒ‚nX|S(N(Rdtto_lastt IndexErrorRRR(R1R[tsubobjtparttex((s-/usr/lib/python2.7/site-packages/jsonpatch.pyR!ˆs  (RRRR!(((s-/usr/lib/python2.7/site-packages/jsonpatch.pyR*…sR+cBseZdZd„ZRS(u,Adds an object property or an array element.cCsû|jd}|jj|ƒ\}}t|tƒs[t|tƒs[tdttƒƒƒ‚t|tƒrÆ|dkr†|j |ƒq÷|t |ƒks¤|dkr³t dƒ‚q÷|j ||ƒn1t|tƒr÷|dkrê|}q÷|||!s:           " Ê "