Still interested in any answers to this query; however, I thought I would 
followup..

A somewhat sub-optimal but working solution using lxml for pruning results 
turned out to be pretty simple after all. :)

`data` is the data lxlm.Element containing the get[-config] result, and `xpath` 
is an xpath string.

def xpath_filter_result(data, xpath):

   # First get a copy we can safely modify.
   data = copy.deepcopy(data)
   results = data.xpath(xpath, namespaces=NSMAP)

   # Mark the tree up
   for result in results:
       # Mark all children
       for elm in result.iterdescendants():
           elm.attrib['__filter_marked__'] = ""
       # Mark this element and all parents
       while result is not data:
           result.attrib['__filter_marked__'] = ""
           result = result.getparent()

   def prunedecendants(elm):
       for child in elm.getchildren():
           if '__filter_marked__' not in child.attrib:
               elm.remove(child)
           else:
               # Recurse
               prunedecendants(child)
               # Remove the mark
               del child.attrib['__filter_marked__']

   prunedecendants(data)

   return data

Thanks,
Chris.


Christian Hopps <[email protected]> writes:

I've developed a basic python based netconf server and client 
(https://github.com/choppsv1/netconf), but it currently lacks any decent 
filtering capability. I was wondering can anyone point me at any open source 
(or way to use open source) that implements netconf/yang subtree/xpath 
filtering?

For this project something simple that takes a result and prunes it down would 
probably work OK.

FWIW I looked at lxml and xpath thinking that I could translate subtree filters 
to xpath first and then use xpath; however, lxml xpath is not really designed 
to support returning results from the root of the tree. Failing to find 
anything else I'll probably try and make that work anyway.

Thanks,
Chris.

_______________________________________________
netmod mailing list
[email protected]
https://www.ietf.org/mailman/listinfo/netmod

Reply via email to