20
20
import structlog # type: ignore
21
21
22
22
from .diff import Diff , DiffElement
23
- from .enum import DiffSyncModelFlags , DiffSyncFlags , DiffSyncStatus
23
+ from .enum import DiffSyncModelFlags , DiffSyncFlags , DiffSyncStatus , DiffSyncActions
24
24
from .exceptions import ObjectNotFound , ObjectNotCreated , ObjectNotUpdated , ObjectNotDeleted , ObjectCrudException
25
25
from .utils import intersection , symmetric_difference
26
26
@@ -296,7 +296,7 @@ def __init__( # pylint: disable=too-many-arguments
296
296
# Local state maintained during synchronization
297
297
self .logger : structlog .BoundLogger = self .base_logger
298
298
self .model_class : Type ["DiffSyncModel" ]
299
- self .action : Optional [str ] = None
299
+ self .action : Optional [DiffSyncActions ] = None
300
300
301
301
def incr_elements_processed (self , delta : int = 1 ):
302
302
"""Increment self.elements_processed, then call self.callback if present."""
@@ -353,11 +353,11 @@ def sync_diff_element(self, element: DiffElement, parent_model: "DiffSyncModel"
353
353
self .logger .warning ("No object resulted from sync, will not process child objects." )
354
354
return changed
355
355
356
- if self .action == "create" :
356
+ if self .action == DiffSyncActions . CREATE :
357
357
if parent_model :
358
358
parent_model .add_child (model )
359
359
self .dst_diffsync .add (model )
360
- elif self .action == "delete" :
360
+ elif self .action == DiffSyncActions . DELETE :
361
361
if parent_model :
362
362
parent_model .remove_child (model )
363
363
if model .model_flags & DiffSyncModelFlags .SKIP_CHILDREN_ON_DELETE :
@@ -390,16 +390,16 @@ def sync_model(
390
390
return (False , model )
391
391
392
392
try :
393
- self .logger .debug (f"Attempting model { self .action } " )
394
- if self .action == "create" :
393
+ self .logger .debug (f"Attempting model { self .action . value } " )
394
+ if self .action == DiffSyncActions . CREATE :
395
395
if model is not None :
396
396
raise ObjectNotCreated (f"Failed to create { self .model_class .get_type ()} { ids } - it already exists!" )
397
397
model = self .model_class .create (diffsync = self .dst_diffsync , ids = ids , attrs = attrs )
398
- elif self .action == "update" :
398
+ elif self .action == DiffSyncActions . UPDATE :
399
399
if model is None :
400
400
raise ObjectNotUpdated (f"Failed to update { self .model_class .get_type ()} { ids } - not found!" )
401
401
model = model .update (attrs = attrs )
402
- elif self .action == "delete" :
402
+ elif self .action == DiffSyncActions . DELETE :
403
403
if model is None :
404
404
raise ObjectNotDeleted (f"Failed to delete { self .model_class .get_type ()} { ids } - not found!" )
405
405
model = model .delete ()
@@ -410,7 +410,7 @@ def sync_model(
410
410
status , message = model .get_status ()
411
411
else :
412
412
status = DiffSyncStatus .FAILURE
413
- message = f"{ self .model_class .get_type ()} { self .action } did not return the model object."
413
+ message = f"{ self .model_class .get_type ()} { self .action . value } did not return the model object."
414
414
415
415
except ObjectCrudException as exception :
416
416
status = DiffSyncStatus .ERROR
@@ -424,7 +424,7 @@ def sync_model(
424
424
425
425
return (True , model )
426
426
427
- def log_sync_status (self , action : Optional [str ], status : DiffSyncStatus , message : str ):
427
+ def log_sync_status (self , action : Optional [DiffSyncActions ], status : DiffSyncStatus , message : str ):
428
428
"""Log the current sync status at the appropriate verbosity with appropriate context.
429
429
430
430
Helper method to `sync_diff_element`/`sync_model`.
0 commit comments