4242import json
4343import sys
4444
45+ from jsonpointer import JsonPointer , JsonPointerException
46+
4547
46- _ST_ADD = 0
48+ _ST_ADD = 0
4749_ST_REMOVE = 1
4850
4951
5254except ImportError :
5355 from collections import MutableMapping , MutableSequence
5456
55- from jsonpointer import JsonPointer , JsonPointerException
56-
5757# Will be parsed by setup.py to determine package metadata
5858__author__ = 'Stefan Kögl <stefan@skoegl.net>'
5959__version__ = '1.16'
@@ -486,7 +486,7 @@ def apply(self, obj):
486486 raise JsonPatchConflict ("can't replace outside of list" )
487487
488488 elif isinstance (subobj , MutableMapping ):
489- if not part in subobj :
489+ if part not in subobj :
490490 msg = "can't replace non-existent object '{0}'" .format (part )
491491 raise JsonPatchConflict (msg )
492492 else :
@@ -649,10 +649,11 @@ def store_index(self, value, index, st):
649649 try :
650650 storage = self .index_storage [st ]
651651 stored = storage .get (value )
652- if stored == None :
652+ if stored is None :
653653 storage [value ] = [index ]
654654 else :
655655 storage [value ].append (index )
656+
656657 except TypeError :
657658 self .index_storage2 [st ].append ((value , index ))
658659
@@ -661,6 +662,7 @@ def take_index(self, value, st):
661662 stored = self .index_storage [st ].get (value )
662663 if stored :
663664 return stored .pop ()
665+
664666 except TypeError :
665667 storage = self .index_storage2 [st ]
666668 for i in range (len (storage )- 1 , - 1 , - 1 ):
@@ -699,27 +701,28 @@ def execute(self):
699701 while curr is not root :
700702 if curr [1 ] is not root :
701703 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 \
704705 type (op_first ) == RemoveOperation and \
705- type (op_second ) == AddOperation ) :
706+ type (op_second ) == AddOperation :
706707 yield ReplaceOperation ({
707708 'op' : 'replace' ,
708709 'path' : op_second .location ,
709710 'value' : op_second .operation ['value' ],
710711 }).operation
711712 curr = curr [1 ][1 ]
712713 continue
714+
713715 yield curr [2 ].operation
714716 curr = curr [1 ]
715717
716718 def _item_added (self , path , key , item ):
717719 index = self .take_index (item , _ST_REMOVE )
718- if index != None :
720+ if index is not None :
719721 op = index [2 ]
720722 if type (op .key ) == int :
721723 for v in self .iter_from (index ):
722724 op .key = v ._on_undo_remove (op .path , op .key )
725+
723726 self .remove (index )
724727 if op .location != _path_join (path , key ):
725728 new_op = MoveOperation ({
@@ -745,11 +748,12 @@ def _item_removed(self, path, key, item):
745748 })
746749 index = self .take_index (item , _ST_ADD )
747750 new_index = self .insert (new_op )
748- if index != None :
751+ if index is not None :
749752 op = index [2 ]
750753 if type (op .key ) == int :
751754 for v in self .iter_from (index ):
752755 op .key = v ._on_undo_add (op .path , op .key )
756+
753757 self .remove (index )
754758 if new_op .location != op .location :
755759 new_op = MoveOperation ({
@@ -758,8 +762,10 @@ def _item_removed(self, path, key, item):
758762 'path' : op .location ,
759763 })
760764 new_index [2 ] = new_op
765+
761766 else :
762767 self .remove (new_index )
768+
763769 else :
764770 self .store_index (item , new_index , _ST_REMOVE )
765771
@@ -775,10 +781,13 @@ def _compare_dicts(self, path, src, dst):
775781 dst_keys = set (dst .keys ())
776782 added_keys = dst_keys - src_keys
777783 removed_keys = src_keys - dst_keys
784+
778785 for key in removed_keys :
779786 self ._item_removed (path , str (key ), src [key ])
787+
780788 for key in added_keys :
781789 self ._item_added (path , str (key ), dst [key ])
790+
782791 for key in src_keys & dst_keys :
783792 self ._compare_values (path , key , src [key ], dst [key ])
784793
@@ -791,26 +800,34 @@ def _compare_lists(self, path, src, dst):
791800 old , new = src [key ], dst [key ]
792801 if old == new :
793802 continue
803+
794804 self ._item_removed (path , key , old )
795805 self ._item_added (path , key , new )
806+
796807 elif len_src > len_dst :
797808 self ._item_removed (path , len_dst , src [key ])
809+
798810 else :
799811 self ._item_added (path , key , dst [key ])
800812
801813 def _compare_values (self , path , key , src , dst ):
802814 if src == dst :
803815 return
816+
804817 elif isinstance (src , MutableMapping ) and \
805818 isinstance (dst , MutableMapping ):
806819 self ._compare_dicts (_path_join (path , key ), src , dst )
820+
807821 elif isinstance (src , MutableSequence ) and \
808822 isinstance (dst , MutableSequence ):
809823 self ._compare_lists (_path_join (path , key ), src , dst )
824+
810825 else :
811826 self ._item_replaced (path , key , dst )
812827
828+
813829def _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