Re: [netmod] Open source filtering code?

Christian Hopps <> Sat, 24 February 2018 23:41 UTC

Return-Path: <>
Received: from localhost (localhost []) by (Postfix) with ESMTP id C6B0312D946; Sat, 24 Feb 2018 15:41:54 -0800 (PST)
X-Quarantine-ID: <RTD19QSz8EHQ>
X-Virus-Scanned: amavisd-new at
X-Amavis-Alert: BANNED, message contains text/plain,.exe
X-Spam-Flag: NO
X-Spam-Score: -1.909
X-Spam-Status: No, score=-1.909 tagged_above=-999 required=5 tests=[BAYES_00=-1.9, T_RP_MATCHES_RCVD=-0.01, URIBL_BLOCKED=0.001] autolearn=ham autolearn_force=no
Received: from ([]) by localhost ( []) (amavisd-new, port 10024) with ESMTP id RTD19QSz8EHQ; Sat, 24 Feb 2018 15:41:53 -0800 (PST)
Received: from ( []) by (Postfix) with ESMTP id 2B472126C22; Sat, 24 Feb 2018 15:41:53 -0800 (PST)
Received: from ( []) (using TLSv1.2 with cipher AES256-GCM-SHA384 (256/256 bits)) (Client did not present a certificate) by (Postfix) with ESMTPSA id 81C8F62A28; Sat, 24 Feb 2018 23:41:52 +0000 (UTC)
References: <>
User-agent: mu4e 1.0; emacs 25.3.1
From: Christian Hopps <>
To: "netconf\" <>
Cc: "netmod\" <>
In-reply-to: <>
Date: Sat, 24 Feb 2018 18:41:50 -0500
Message-ID: <>
MIME-Version: 1.0
Content-Type: text/plain; format=flowed
Archived-At: <>
Subject: Re: [netmod] Open source filtering code?
X-Mailman-Version: 2.1.22
Precedence: list
List-Id: NETMOD WG list <>
List-Unsubscribe: <>, <>
List-Archive: <>
List-Post: <>
List-Help: <>
List-Subscribe: <>, <>
X-List-Received-Date: Sat, 24 Feb 2018 23:41:55 -0000

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:
                # Recurse
                # Remove the mark
                del child.attrib['__filter_marked__']


    return data


Christian Hopps <> writes:

> I've developed a basic python based netconf server and client (, 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.