summaryrefslogtreecommitdiff
path: root/ncserver/base/util.py
blob: f69c65bdb1c048bda7d4f4ace02c1491fb074356 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
"""
NETCONF for APK Distributions server:
    Base utility functions.

Copyright © 2020 Adélie Software in the Public Benefit, Inc.

Released under the terms of the NCSA license.  See the LICENSE file included
with this source distribution for more information.

SPDX-License-Identifier: NCSA
"""


from netconf import error


def _(the_string: str) -> str:
    """Translate a user-facing string.

    Currently not implemented, but allows us to implement translation later."""
    return the_string


def ensure_leaf(rpc, node):
    """Ensure that the node is a leaf (contains no child nodes)."""
    if len(node) > 0:
        raise error.UnknownElementAppError(rpc, node[0])


def node_operation(node, def_op='merge') -> str:
    """Determine the operation desired for an XML node.

    :param node:
        The node.

    :param str def_op:
        The default operation if none is specified.  Defaults to 'merge'.

    :returns str:
        The operation desired.
    """

    nc10 = '{urn:ietf:params:xml:ns:netconf:base:1.0}operation'
    nc11 = '{urn:ietf:params:xml:ns:netconf:base:1.1}operation'
    return node.attrib.get(nc11, node.attrib.get(nc10, def_op))