42
42
import json
43
43
import sys
44
44
45
+ from jsonpointer import JsonPointer , JsonPointerException
46
+
45
47
46
- _ST_ADD = 0
48
+ _ST_ADD = 0
47
49
_ST_REMOVE = 1
48
50
49
51
52
54
except ImportError :
53
55
from collections import MutableMapping , MutableSequence
54
56
55
- from jsonpointer import JsonPointer , JsonPointerException
56
-
57
57
# Will be parsed by setup.py to determine package metadata
58
58
__author__ = 'Stefan Kögl <[email protected] >'
59
59
__version__ = '1.16'
@@ -486,7 +486,7 @@ def apply(self, obj):
486
486
raise JsonPatchConflict ("can't replace outside of list" )
487
487
488
488
elif isinstance (subobj , MutableMapping ):
489
- if not part in subobj :
489
+ if part not in subobj :
490
490
msg = "can't replace non-existent object '{0}'" .format (part )
491
491
raise JsonPatchConflict (msg )
492
492
else :
@@ -649,10 +649,11 @@ def store_index(self, value, index, st):
649
649
try :
650
650
storage = self .index_storage [st ]
651
651
stored = storage .get (value )
652
- if stored == None :
652
+ if stored is None :
653
653
storage [value ] = [index ]
654
654
else :
655
655
storage [value ].append (index )
656
+
656
657
except TypeError :
657
658
self .index_storage2 [st ].append ((value , index ))
658
659
@@ -661,6 +662,7 @@ def take_index(self, value, st):
661
662
stored = self .index_storage [st ].get (value )
662
663
if stored :
663
664
return stored .pop ()
665
+
664
666
except TypeError :
665
667
storage = self .index_storage2 [st ]
666
668
for i in range (len (storage )- 1 , - 1 , - 1 ):
@@ -699,27 +701,28 @@ def execute(self):
699
701
while curr is not root :
700
702
if curr [1 ] is not root :
701
703
op_first , op_second = curr [2 ], curr [1 ][2 ]
702
- if ( #op_first.key == op_second.key and \
703
- op_first .location == op_second .location and \
704
+ if op_first .location == op_second .location and \
704
705
type (op_first ) == RemoveOperation and \
705
- type (op_second ) == AddOperation ) :
706
+ type (op_second ) == AddOperation :
706
707
yield ReplaceOperation ({
707
708
'op' : 'replace' ,
708
709
'path' : op_second .location ,
709
710
'value' : op_second .operation ['value' ],
710
711
}).operation
711
712
curr = curr [1 ][1 ]
712
713
continue
714
+
713
715
yield curr [2 ].operation
714
716
curr = curr [1 ]
715
717
716
718
def _item_added (self , path , key , item ):
717
719
index = self .take_index (item , _ST_REMOVE )
718
- if index != None :
720
+ if index is not None :
719
721
op = index [2 ]
720
722
if type (op .key ) == int :
721
723
for v in self .iter_from (index ):
722
724
op .key = v ._on_undo_remove (op .path , op .key )
725
+
723
726
self .remove (index )
724
727
if op .location != _path_join (path , key ):
725
728
new_op = MoveOperation ({
@@ -745,11 +748,12 @@ def _item_removed(self, path, key, item):
745
748
})
746
749
index = self .take_index (item , _ST_ADD )
747
750
new_index = self .insert (new_op )
748
- if index != None :
751
+ if index is not None :
749
752
op = index [2 ]
750
753
if type (op .key ) == int :
751
754
for v in self .iter_from (index ):
752
755
op .key = v ._on_undo_add (op .path , op .key )
756
+
753
757
self .remove (index )
754
758
if new_op .location != op .location :
755
759
new_op = MoveOperation ({
@@ -758,8 +762,10 @@ def _item_removed(self, path, key, item):
758
762
'path' : op .location ,
759
763
})
760
764
new_index [2 ] = new_op
765
+
761
766
else :
762
767
self .remove (new_index )
768
+
763
769
else :
764
770
self .store_index (item , new_index , _ST_REMOVE )
765
771
@@ -775,10 +781,13 @@ def _compare_dicts(self, path, src, dst):
775
781
dst_keys = set (dst .keys ())
776
782
added_keys = dst_keys - src_keys
777
783
removed_keys = src_keys - dst_keys
784
+
778
785
for key in removed_keys :
779
786
self ._item_removed (path , str (key ), src [key ])
787
+
780
788
for key in added_keys :
781
789
self ._item_added (path , str (key ), dst [key ])
790
+
782
791
for key in src_keys & dst_keys :
783
792
self ._compare_values (path , key , src [key ], dst [key ])
784
793
@@ -791,26 +800,34 @@ def _compare_lists(self, path, src, dst):
791
800
old , new = src [key ], dst [key ]
792
801
if old == new :
793
802
continue
803
+
794
804
self ._item_removed (path , key , old )
795
805
self ._item_added (path , key , new )
806
+
796
807
elif len_src > len_dst :
797
808
self ._item_removed (path , len_dst , src [key ])
809
+
798
810
else :
799
811
self ._item_added (path , key , dst [key ])
800
812
801
813
def _compare_values (self , path , key , src , dst ):
802
814
if src == dst :
803
815
return
816
+
804
817
elif isinstance (src , MutableMapping ) and \
805
818
isinstance (dst , MutableMapping ):
806
819
self ._compare_dicts (_path_join (path , key ), src , dst )
820
+
807
821
elif isinstance (src , MutableSequence ) and \
808
822
isinstance (dst , MutableSequence ):
809
823
self ._compare_lists (_path_join (path , key ), src , dst )
824
+
810
825
else :
811
826
self ._item_replaced (path , key , dst )
812
827
828
+
813
829
def _path_join (path , key ):
814
- if key != None :
815
- return path + '/' + str (key ).replace ('~' , '~0' ).replace ('/' , '~1' )
816
- return path
830
+ if key is None :
831
+ return path
832
+
833
+ return path + '/' + str (key ).replace ('~' , '~0' ).replace ('/' , '~1' )
0 commit comments