diffsync.diff

Diff and DiffElement classes for DiffSync.

class diffsync.diff.Diff

Bases: object

Diff Object, designed to store multiple DiffElement object and organize them in a group.

__init__() None

Initialize a new, empty Diff object.

add(element: DiffElement) None

Add a new DiffElement to the changeset of this Diff.

Raises:

ObjectAlreadyExists – if an element of the same type and same name is already stored.

children

DefaultDict for storing DiffElement objects.

self.children[group][unique_id] == DiffElement(…)

complete() None

Method to call when this Diff has been fully populated with data and is “complete”.

The default implementation does nothing, but a subclass could use this, for example, to save the completed Diff to a file or database record.

dict() Dict[str, Dict[str, Dict]]

Build a dictionary representation of this Diff.

get_children() Iterator[DiffElement]

Iterate over all child elements in all groups in self.children.

For each group of children, check if an order method is defined, Otherwise use the default method.

groups() List[str]

Get the list of all group keys in self.children.

has_diffs() bool

Indicate if at least one of the child elements contains some diff.

Returns:

True if at least one child element contains some diff

classmethod order_children_default(children: Dict[str, DiffElement]) Iterator[DiffElement]

Default method to an Iterator for children.

Since children is already an OrderedDefaultDict, this method is not doing anything special.

str(indent: int = 0) str

Build a detailed string representation of this Diff and its child DiffElements.

summary() Dict[str, int]

Build a dict summary of this Diff and its child DiffElements.

class diffsync.diff.DiffElement(obj_type: str, name: str, keys: ~typing.Dict, source_name: str = 'source', dest_name: str = 'dest', diff_class: ~typing.Type[~diffsync.diff.Diff] = <class 'diffsync.diff.Diff'>)

Bases: object

DiffElement object, designed to represent a single item/object that may or may not have any diffs.

__init__(obj_type: str, name: str, keys: ~typing.Dict, source_name: str = 'source', dest_name: str = 'dest', diff_class: ~typing.Type[~diffsync.diff.Diff] = <class 'diffsync.diff.Diff'>)

Instantiate a DiffElement.

Parameters:
  • obj_type – Name of the object type being described, as in DiffSyncModel.get_type().

  • name – Human-readable name of the object being described, as in DiffSyncModel.get_shortname(). This name must be unique within the context of the Diff that is the direct parent of this DiffElement.

  • keys – Primary keys and values uniquely describing this object, as in DiffSyncModel.get_identifiers().

  • source_name – Name of the source DiffSync object

  • dest_name – Name of the destination DiffSync object

  • diff_class – Diff or subclass thereof to use to calculate the diffs to use for synchronization

property action: str | None

Action, if any, that should be taken to remediate the diffs described by this element.

Returns:

“create”, “update”, “delete”, or None)

add_attrs(source: Dict | None = None, dest: Dict | None = None) None

Set additional attributes of a source and/or destination item that may result in diffs.

add_child(element: DiffElement) None

Attach a child object of type DiffElement.

Childs are saved in a Diff object and are organized by type and name.

dict() Dict[str, Dict[str, Any]]

Build a dictionary representation of this DiffElement and its children.

get_attrs_diffs() Dict[str, Dict[str, Any]]

Get the dict of actual attribute diffs between source_attrs and dest_attrs.

Returns:

{key1: <value>, key2: …}, “+”: {key1: <value>, key2: …}}`, where the “-” or “+” dicts may be absent.

Return type:

Dictionary of the form `{“-”

get_attrs_keys() Iterable[str]

Get the list of shared attrs between source and dest, or the attrs of source or dest if only one is present.

  • If source_attrs is not set, return the keys of dest_attrs

  • If dest_attrs is not set, return the keys of source_attrs

  • If both are defined, return the intersection of both keys

get_children() Iterator[DiffElement]

Iterate over all child DiffElements of this one.

has_diffs(include_children: bool = True) bool

Check whether this element (or optionally any of its children) has some diffs.

Parameters:

include_children – If True, recursively check children for diffs as well.

str(indent: int = 0) str

Build a detailed string representation of this DiffElement and its children.

summary() Dict[str, int]

Build a summary of this DiffElement and its children.