Skip to content

Commit 0d9aa65

Browse files
committed
Refactor to use proper Enum
1 parent 8284d87 commit 0d9aa65

File tree

4 files changed

+17
-17
lines changed

4 files changed

+17
-17
lines changed

diffsync/diff.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -106,9 +106,9 @@ def order_children_default(cls, children: Mapping) -> Iterator["DiffElement"]:
106106
def summary(self) -> Mapping[Text, int]:
107107
"""Build a dict summary of this Diff and its child DiffElements."""
108108
summary = {
109-
DiffSyncActions.CREATE: 0,
110-
DiffSyncActions.UPDATE: 0,
111-
DiffSyncActions.DELETE: 0,
109+
DiffSyncActions.CREATE.value: 0,
110+
DiffSyncActions.UPDATE.value: 0,
111+
DiffSyncActions.DELETE.value: 0,
112112
"no-change": 0,
113113
}
114114
for child in self.get_children():
@@ -221,11 +221,11 @@ def __len__(self):
221221
return total
222222

223223
@property
224-
def action(self) -> Optional[Text]:
224+
def action(self) -> Optional[DiffSyncActions]:
225225
"""Action, if any, that should be taken to remediate the diffs described by this element.
226226
227227
Returns:
228-
str: DiffSyncActions ("create", "update", "delete", or None)
228+
DiffSyncActions ("create", "update", "delete", or None)
229229
"""
230230
if self.source_attrs is not None and self.dest_attrs is None:
231231
return DiffSyncActions.CREATE
@@ -329,13 +329,13 @@ def has_diffs(self, include_children: bool = True) -> bool:
329329
def summary(self) -> Mapping[Text, int]:
330330
"""Build a summary of this DiffElement and its children."""
331331
summary = {
332-
DiffSyncActions.CREATE: 0,
333-
DiffSyncActions.UPDATE: 0,
334-
DiffSyncActions.DELETE: 0,
332+
DiffSyncActions.CREATE.value: 0,
333+
DiffSyncActions.UPDATE.value: 0,
334+
DiffSyncActions.DELETE.value: 0,
335335
"no-change": 0,
336336
}
337337
if self.action:
338-
summary[self.action] += 1
338+
summary[self.action.value] += 1
339339
else:
340340
summary["no-change"] += 1
341341
child_summary = self.child_diff.summary()

diffsync/enum.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ class DiffSyncStatus(enum.Enum):
7575
ERROR = "error"
7676

7777

78-
class DiffSyncActions: # pylint: disable=too-few-public-methods
78+
class DiffSyncActions(enum.Enum):
7979
"""List of valid Action for DiffSyncModel."""
8080

8181
CREATE = "create"

diffsync/helpers.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,7 @@ def __init__( # pylint: disable=too-many-arguments
296296
# Local state maintained during synchronization
297297
self.logger: structlog.BoundLogger = self.base_logger
298298
self.model_class: Type["DiffSyncModel"]
299-
self.action: Optional[str] = None
299+
self.action: Optional[DiffSyncActions] = None
300300

301301
def incr_elements_processed(self, delta: int = 1):
302302
"""Increment self.elements_processed, then call self.callback if present."""
@@ -390,7 +390,7 @@ def sync_model(
390390
return (False, model)
391391

392392
try:
393-
self.logger.debug(f"Attempting model {self.action}")
393+
self.logger.debug(f"Attempting model {self.action.value}")
394394
if self.action == DiffSyncActions.CREATE:
395395
if model is not None:
396396
raise ObjectNotCreated(f"Failed to create {self.model_class.get_type()} {ids} - it already exists!")
@@ -404,13 +404,13 @@ def sync_model(
404404
raise ObjectNotDeleted(f"Failed to delete {self.model_class.get_type()} {ids} - not found!")
405405
model = model.delete()
406406
else:
407-
raise ObjectCrudException(f'Unknown action "{self.action}"!')
407+
raise ObjectCrudException(f'Unknown action "{self.action.value}"!')
408408

409409
if model is not None:
410410
status, message = model.get_status()
411411
else:
412412
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."
414414

415415
except ObjectCrudException as exception:
416416
status = DiffSyncStatus.ERROR
@@ -424,7 +424,7 @@ def sync_model(
424424

425425
return (True, model)
426426

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):
428428
"""Log the current sync status at the appropriate verbosity with appropriate context.
429429
430430
Helper method to `sync_diff_element`/`sync_model`.

tests/unit/test_diffsync.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -619,7 +619,7 @@ def check_sync_logs_against_diff(diffsync, diff, log, errors_permitted=False):
619619

620620
assert {
621621
("action", element.action),
622-
("event", f"Attempting model {element.action}"),
622+
("event", f"Attempting model {element.action.value}"),
623623
("level", "debug"),
624624
} <= begin_event.items()
625625
# attrs_diffs dict is unhashable so we can't include it in the above set comparison
@@ -632,7 +632,7 @@ def check_sync_logs_against_diff(diffsync, diff, log, errors_permitted=False):
632632
if complete_event["status"] == "success":
633633
assert {
634634
("action", element.action),
635-
("event", f"{element.action.title()}d successfully"),
635+
("event", f"{element.action.value.title()}d successfully"),
636636
("level", "info"),
637637
("status", "success"),
638638
} <= complete_event.items()

0 commit comments

Comments
 (0)