@@ -526,14 +526,14 @@ def load_from_dict(self, data: Dict):
526
526
# Synchronization between DiffSync instances
527
527
# ------------------------------------------------------------------------------
528
528
529
- def sync_from (
529
+ def sync_from ( # pylint: disable=too-many-arguments
530
530
self ,
531
531
source : "DiffSync" ,
532
532
diff_class : Type [Diff ] = Diff ,
533
533
flags : DiffSyncFlags = DiffSyncFlags .NONE ,
534
534
callback : Optional [Callable [[Text , int , int ], None ]] = None ,
535
535
diff : Optional [Diff ] = None ,
536
- ): # pylint: disable=too-many-arguments :
536
+ ) -> Diff :
537
537
"""Synchronize data from the given source DiffSync object into the current DiffSync object.
538
538
539
539
Args:
@@ -543,6 +543,10 @@ def sync_from(
543
543
callback (function): Function with parameters (stage, current, total), to be called at intervals as the
544
544
calculation of the diff and subsequent sync proceed.
545
545
diff (Diff): An existing diff to be used rather than generating a completely new diff.
546
+ Returns:
547
+ Diff: Diff between origin object and source
548
+ Raises:
549
+ DiffClassMismatch: The provided diff's class does not match the diff_class
546
550
"""
547
551
if diff_class and diff :
548
552
if not isinstance (diff , diff_class ):
@@ -558,14 +562,16 @@ def sync_from(
558
562
if result :
559
563
self .sync_complete (source , diff , flags , syncer .base_logger )
560
564
561
- def sync_to (
565
+ return diff
566
+
567
+ def sync_to ( # pylint: disable=too-many-arguments
562
568
self ,
563
569
target : "DiffSync" ,
564
570
diff_class : Type [Diff ] = Diff ,
565
571
flags : DiffSyncFlags = DiffSyncFlags .NONE ,
566
572
callback : Optional [Callable [[Text , int , int ], None ]] = None ,
567
573
diff : Optional [Diff ] = None ,
568
- ): # pylint: disable=too-many-arguments
574
+ ) -> Diff :
569
575
"""Synchronize data from the current DiffSync object into the given target DiffSync object.
570
576
571
577
Args:
@@ -575,15 +581,19 @@ def sync_to(
575
581
callback (function): Function with parameters (stage, current, total), to be called at intervals as the
576
582
calculation of the diff and subsequent sync proceed.
577
583
diff (Diff): An existing diff that will be used when determining what needs to be synced.
584
+ Returns:
585
+ Diff: Diff between origin object and target
586
+ Raises:
587
+ DiffClassMismatch: The provided diff's class does not match the diff_class
578
588
"""
579
- target .sync_from (self , diff_class = diff_class , flags = flags , callback = callback , diff = diff )
589
+ return target .sync_from (self , diff_class = diff_class , flags = flags , callback = callback , diff = diff )
580
590
581
591
def sync_complete (
582
592
self ,
583
593
source : "DiffSync" ,
584
594
diff : Diff ,
585
595
flags : DiffSyncFlags = DiffSyncFlags .NONE ,
586
- logger : structlog .BoundLogger = None ,
596
+ logger : Optional [ structlog .BoundLogger ] = None ,
587
597
):
588
598
"""Callback triggered after a `sync_from` operation has completed and updated the model data of this instance.
589
599
@@ -776,7 +786,7 @@ def remove(self, obj: DiffSyncModel, remove_children: bool = False):
776
786
return self .store .remove (obj = obj , remove_children = remove_children )
777
787
778
788
def get_or_instantiate (
779
- self , model : Type [DiffSyncModel ], ids : Dict , attrs : Dict = None
789
+ self , model : Type [DiffSyncModel ], ids : Dict , attrs : Optional [ Dict ] = None
780
790
) -> Tuple [DiffSyncModel , bool ]:
781
791
"""Attempt to get the object with provided identifiers or instantiate it with provided identifiers and attrs.
782
792
@@ -790,19 +800,41 @@ def get_or_instantiate(
790
800
"""
791
801
return self .store .get_or_instantiate (model = model , ids = ids , attrs = attrs )
792
802
803
+ def get_or_add_model_instance (self , obj : DiffSyncModel ) -> Tuple [DiffSyncModel , bool ]:
804
+ """Attempt to get the object with provided obj identifiers or instantiate obj.
805
+
806
+ Args:
807
+ obj: An obj of the DiffSyncModel to get or add.
808
+
809
+ Returns:
810
+ Provides the existing or new object and whether it was created or not.
811
+ """
812
+ return self .store .get_or_add_model_instance (obj = obj )
813
+
793
814
def update_or_instantiate (self , model : Type [DiffSyncModel ], ids : Dict , attrs : Dict ) -> Tuple [DiffSyncModel , bool ]:
794
815
"""Attempt to update an existing object with provided ids/attrs or instantiate it with provided identifiers and attrs.
795
816
796
817
Args:
797
- model (DiffSyncModel): The DiffSyncModel to get or create.
798
- ids (Dict): Identifiers for the DiffSyncModel to get or create with.
818
+ model (DiffSyncModel): The DiffSyncModel to update or create.
819
+ ids (Dict): Identifiers for the DiffSyncModel to update or create with.
799
820
attrs (Dict): Attributes when creating/updating an object if it doesn't exist. Pass in empty dict, if no specific attrs.
800
821
801
822
Returns:
802
823
Tuple[DiffSyncModel, bool]: Provides the existing or new object and whether it was created or not.
803
824
"""
804
825
return self .store .update_or_instantiate (model = model , ids = ids , attrs = attrs )
805
826
827
+ def update_or_add_model_instance (self , obj : DiffSyncModel ) -> Tuple [DiffSyncModel , bool ]:
828
+ """Attempt to update an existing object with provided obj ids/attrs or instantiate obj.
829
+
830
+ Args:
831
+ instance: An instance of the DiffSyncModel to update or create.
832
+
833
+ Returns:
834
+ Provides the existing or new object and whether it was created or not.
835
+ """
836
+ return self .store .update_or_add_model_instance (obj = obj )
837
+
806
838
def count (self , model : Union [Text , "DiffSyncModel" , Type ["DiffSyncModel" ], None ] = None ):
807
839
"""Count how many objects of one model type exist in the backend store.
808
840
0 commit comments