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__()

Initialize a new, empty Diff object.

add(element: diffsync.diff.DiffElement)

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()

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() Mapping[str, Mapping[str, Mapping]]

Build a dictionary representation of this Diff.

get_children() Iterator[diffsync.diff.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()

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

Return type

bool

classmethod order_children_default(children: Mapping) Iterator[diffsync.diff.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)

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

summary() Mapping[str, int]

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

class diffsync.diff.DiffElement(obj_type: str, name: str, keys: typing.Mapping, 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.Mapping, 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: Optional[str]

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

Returns

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

Return type

str

add_attrs(source: Optional[Mapping] = None, dest: Optional[Mapping] = None)

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

add_child(element: diffsync.diff.DiffElement)

Attach a child object of type DiffElement.

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

Parameters

element – DiffElement

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

Build a dictionary representation of this DiffElement and its children.

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

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

Returns

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

Return type

dict

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[diffsync.diff.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)

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

summary() Mapping[str, int]

Build a summary of this DiffElement and its children.