ncdiff.ModelDiff class

class ncdiff.ModelDiff(model1, model2)[source]

Bases: object

Abstraction of differences between two Model instances. It supports str() which returns a string illustrating the differences between model1 and model2.

model1

First Model instance.

Type:Model
model2

Second Model instance.

Type:Model
tree

The model difference tree as an Element object.

Type:Element
added

A string presentation of added nodes from model1 to model2.

Type:str
deleted

A string presentation of deleted nodes from model1 to model2.

Type:str
modified

A string presentation of modified nodes from model1 to model2.

Type:str
width

This is used to facilitate pretty print of a model. Dictionary keys are nodes in the model tree, and values are indents.

Type:dict

__init__ instantiates a Model instance.

emit_tree(tree)

High-level api: Emit a string presentation of the model.

Parameters:tree (Element) – The model.
Returns:A string presentation of the model that is very similar to the output of ‘pyang -f tree’
Return type:str
get_width(element)

High-level api: Calculate how much indent is needed for a node.

Parameters:element (Element) – A node in model tree.
Returns:Start position from the left margin.
Return type:int
compare(xpath)[source]

High-level api: Return a string presentation of comparison between the node in model1 and model2.

Parameters:xpath (str) – XPATH to locate a node.
Returns:A string presentation of comparison.
Return type:str
emit_children(tree, type='other')[source]

High-level api: Emit a string presentation of a part of the model.

Parameters:
  • tree (Element) – The model.
  • type (str) – Type of model content required. Its value can be ‘other’, ‘rpc’, or ‘notification’.
Returns:

A string presentation of the model that is very similar to the output of ‘pyang -f tree’.

Return type:

str

get_name_str(element)[source]

High-level api: Produce a string that represents the name of a node.

Parameters:element (Element) – A node in model tree.
Returns:A string that represents the name of a node.
Return type:str
get_diff_str(element, length)[source]

High-level api: Produce a string that indicates the difference between two models.

Parameters:
  • element (Element) – A node in model tree.
  • length (int) – String length that has been consumed.
Returns:

A string that indicates the difference between two models.

Return type:

str

static compare_nodes(node1, node2, ret)[source]

High-level api: Compare node1 and node2 and put the result in ret.

Parameters:
  • node1 (Element) – A node in a model tree.
  • node2 (Element) – A node in another model tree.
  • ret (Element) – A node in self.tree.
Returns:

Nothing returns.

Return type:

None

static copy_subtree(ret, element, msg)[source]

High-level api: Copy element as a subtree and put it as a child of ret.

Parameters:
  • element (Element) – A node in a model tree.
  • msg (str) – Message to be added.
  • ret (Element) – A node in self.tree.
Returns:

Nothing returns.

Return type:

None

static copy_node(ret, element, msg)[source]

High-level api: Copy element as a node without its children and put it as a child of ret.

Parameters:
  • element (Element) – A node in a model tree.
  • msg (str) – Message to be added.
  • ret (Element) – A node in self.tree.
Returns:

Nothing returns.

Return type:

None

static process_attrib(element, msg)[source]

High-level api: Delete four attributes from an ElementTree node if they exist: operation, insert, etc. Then a new attribute ‘diff’ is added.

Parameters:
  • element (Element) – A node needs to be looked at.
  • msg (str) – Message to be added in attribute ‘diff’.
Returns:

Argument ‘element’ is returned after processing.

Return type:

Element

static get_peer(tag, node)[source]

High-level api: Find all children under the node with the tag.

Parameters:
  • tag (str) – A tag in {namespace}tagname notaion.
  • node (Element) – A node to be looked at.
Returns:

None if not found. An Element object when found.

Return type:

Element or None

static node_equal(node1, node2)[source]

High-level api: Evaluate whether two nodes are equal.

Parameters:
  • node1 (Element) – A node in a model tree.
  • node2 (Element) – A node in another model tree.
Returns:

True if node1 and node2 are equal.

Return type:

bool

static node_less(node1, node2)[source]

Low-level api: Return True if all descendants of node1 exist in node2. Otherwise False. This is a recursive method.

Parameters:
  • node1 (Element) – A node in a model tree.
  • node2 (Element) – A node in another model tree.
Returns:

True if all descendants of node1 exist in node2, otherwise False.

Return type:

bool

static trim(parent, msg)[source]

Low-level api: Return True if parent has no child after trimming. The trimming to filter out one type of diff: added, deleted, or modified.

Parameters:
  • parent (Element) – A node in a model tree.
  • msg (str) – A type of diff: added, deleted, or modified.
Returns:

True if parent has no child after trimming.

Return type:

bool