The netconf.util Module

netconf.util.elm(tag, attrib=None, **extra)

Create an lxml.etree.Element using qname to obtain the tag.

This is a replacement for calling lxml.etree.Element directly that supports prefixed tags.

Parameters:
  • tag – A possibly prefixed tag.
  • attrib – Attributes for the element.
  • extra – extra parameters see lxml.etree.Element.
Returns:

lxml.etree.Element.

netconf.util.filter_containment_iter(fcontain_elm, dest_node, containment_nodes, leaf_elms, append_to)

Given a containment filter node (or None) verify that all leaf elements either match, have corresponding selection nodes (empty) or are not present.

If all leaf criteria are met then the iterator will return a triple of (new_filter_node, new_dest_node, new_data). new_filter_node corresponds to the matched containment node which is returned in new_dest_node, and new_data will be an element corresponding to the passed in dest_node.

These should be processed by calling filter_containment_iter again.

Additionally the correct leaf data will be added to dest_node, and dest_node will be appended to append_to if append_to is not None.

This implements RFC6241 section 6.2.5

netconf.util.filter_leaf_allows(filter_elm, xpath, value)

Check the value at the xpath specified leaf matches the value.

  • If filter_elm is None then allow.
  • If there is no xpath element then allow if there are no other children.
  • XXX what about xpath that has embedded predicates! perhaps what we want to call this is a normal path not an xpath.
netconf.util.filter_leaf_allows_add(filter_elm, tag, data, value)
netconf.util.filter_leaf_values(fcontain_elm, dest_node, leaf_elms, append_to)

Given a containment element (or None) verify that all leaf elements in leaf_elms either match, have corresponding selection nodes (empty) or are not present.

Additionally the correct leaf data will be added to dest_node, and dest_node will be appended to append_to if append_to is not None.

The return value with be True, False, or a possibly empty set of selection/containment nodes The only failing value is False, if True is returned then the caller should include all containment sibling nodes, otherwise the caller should process the list of containment/selection nodes.

netconf.util.filter_list_iter(filter_list, key_xpath, keys)

Return key, elm pairs that are allowed by keys using the values found using the given key_xpath

netconf.util.filter_node_match(filter_node, match_elm)

Given a filter node element and a nodename and attribute dictionary return true if the filter element matches the elmname, attributes and value (if not None).

The filter element can use a wildcard namespace or a specific namespace the attributes can be missing from the filter node but otherwise must match and the value is only checked for a match if it is not None.

netconf.util.filter_node_match_no_value(filter_node, match_elm)
netconf.util.filter_results(rpc, data, filter_or_none, debug=False)

Check for a user filter and prune the result data accordingly.

Parameters:
  • rpc – An RPC message element.
  • data – The data to filter.
  • filter_or_none (lxml.Element) – Filter element or None.
netconf.util.filter_tag_match(filter_tag, elm_tag)
netconf.util.filter_to_xpath(felm)

Convert a filter sub-tree to an xpath expression.

Parameters:felm – A subtree-filter XML sub-tree.
Returns str:An xpath expression equivalent to the sub-tree.
netconf.util.is_selection_node(felm)
netconf.util.leaf(tag, value, attrib=None, **extra)

Create an lxml.etree.Element leaf node using qname to obtain the tag.

This is a replacement for calling lxml.etree.Element directly that supports prefixed tags.

Parameters:
  • tag – A possibly prefixed tag.
  • value – Value for text of element.
  • attrib – Attributes for the element.
  • extra – extra parameters see lxml.etree.Element.
Returns:

lxml.etree.Element.

netconf.util.leaf_elm(tag, value, attrib=None, **extra)

Create an lxml.etree.Element leaf node using qname to obtain the tag.

This is a replacement for calling lxml.etree.Element directly that supports prefixed tags.

Parameters:
  • tag – A possibly prefixed tag.
  • value – Value for text of element.
  • attrib – Attributes for the element.
  • extra – extra parameters see lxml.etree.Element.
Returns:

lxml.etree.Element.

netconf.util.qname(tag)

Return a qualified tag name (i.e. {namespace}localtag)

Handles prefix notation by looking up in global dictionary.

Parameters:tag – A possibly prefixed tag.
Returns:Fully qualified tag name (lxml.etree.QName).
netconf.util.subelm(pelm, tag, attrib=None, **extra)

Create an child lxml.etree.Element using qname to obtain the tag.

This is a replacement for calling lxml.etree.SubElement directly that supports prefixed tags.

Parameters:
  • pelm – The parent element.
  • tag – A possibly prefixed tag.
  • attrib – Attributes for the element.
  • extra – extra parameters see lxml.etree.SubElement.
Returns:

lxml.etree.Element.

netconf.util.xpath_filter_result(data, xpath)

Filter a result given an xpath expression.

Parameters:
  • data – The nc:data result element.
  • xpath – The xpath expression string.
Returns:

New nc:data result element pruned by the xpath expression.

>>> xml = '''
... <data>
...   <devs>
...     <dev>
...       <name>dev1</name>
...       <slots>1</slots>
...     </dev>
...     <dev>
...       <name>dev2</name>
...       <slots>2</slots>
...     </dev>
...     <dev>
...       <name>dev3</name>
...       <slots>3</slots>
...     </dev>
...   </devs>
... </data>
... '''
>>> data = etree.fromstring(xml.replace(' ', '').replace('\n', ''))
>>> result = xpath_filter_result(data, "/devs/dev")
>>> etree.tounicode(result)
'<data><devs><dev><name>dev1</name><slots>1</slots></dev><dev><name>dev2</name><slots>2</slots></dev><dev><name>dev3</name><slots>3</slots></dev></devs></data>'
>>> result = xpath_filter_result(data, "/devs/dev[name='dev1']")
>>> etree.tounicode(result)
'<data><devs><dev><name>dev1</name><slots>1</slots></dev></devs></data>'
>>> result = xpath_filter_result(data, "/devs/dev[name='dev2']")
>>> etree.tounicode(result)
'<data><devs><dev><name>dev2</name><slots>2</slots></dev></devs></data>'
>>> result = xpath_filter_result(data, "/devs/dev[name='dev2'] | /devs/dev[name='dev1']")
>>> etree.tounicode(result)
'<data><devs><dev><name>dev1</name><slots>1</slots></dev><dev><name>dev2</name><slots>2</slots></dev></devs></data>'
>>> result = xpath_filter_result(data, "/devs/dev[name='dev1'] | /devs/dev[name='dev2']")
>>> etree.tounicode(result)
'<data><devs><dev><name>dev1</name><slots>1</slots></dev><dev><name>dev2</name><slots>2</slots></dev></devs></data>'
>>> result = xpath_filter_result(data, "/devs/dev[name='dev1'] | /devs/dev[slots='2']")
>>> etree.tounicode(result)
'<data><devs><dev><name>dev1</name><slots>1</slots></dev><dev><name>dev2</name><slots>2</slots></dev></devs></data>'