Statistics
| Branch: | Tag: | Revision:

root / ncclient / operations / util.py @ 9667bcb2

History | View | Annotate | Download (2.2 kB)

1
# Copyright 2009 Shikhar Bhushan
2
#
3
# Licensed under the Apache License, Version 2.0 (the "License");
4
# you may not use this file except in compliance with the License.
5
# You may obtain a copy of the License at
6
#
7
#    http://www.apache.org/licenses/LICENSE-2.0
8
#
9
# Unless required by applicable law or agreed to in writing, software
10
# distributed under the License is distributed on an "AS IS" BASIS,
11
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
# See the License for the specific language governing permissions and
13
# limitations under the License.
14

    
15
'Boilerplate ugliness'
16

    
17
from ncclient.xml_ import *
18

    
19
from errors import OperationError, MissingCapabilityError
20

    
21
def one_of(*args):
22
    "Verifies that only one of the arguments is not None"
23
    for i, arg in enumerate(args):
24
        if arg is not None:
25
            for argh in args[i+1:]:
26
                if argh is not None:
27
                    raise OperationError("Too many parameters")
28
            else:
29
                return
30
    raise OperationError("Insufficient parameters")
31

    
32
def datastore_or_url(wha, loc, capcheck=None):
33
    node = new_ele(wha)
34
    if "://" in loc: # e.g. http://, file://, ftp://
35
        if capcheck is not None:
36
            capcheck(":url") # url schema check at some point!
37
            sub_ele(node, "url").text = loc
38
    else:
39
        #if loc == 'candidate':
40
        #    capcheck(':candidate')
41
        #elif loc == 'startup':
42
        #    capcheck(':startup')
43
        #elif loc == 'running' and wha == 'target':
44
        #    capcheck(':writable-running')
45
        sub_ele(node, loc)
46
    return node
47

    
48
def build_filter(spec, capcheck=None):
49
    type = None
50
    if isinstance(spec, tuple):
51
        type, criteria = spec
52
        rep = new_ele("filter", type=type)
53
        if type == "xpath":
54
            rep.attrib["select"] = criteria
55
        elif type == "subtree":
56
            rep.append(to_ele(criteria))
57
        else:
58
            raise OperationError("Invalid filter type")
59
    else:
60
        rep = validated_element(spec, ("filter", qualify("filter")),
61
                                        attrs=("type",))
62
        # TODO set type var here, check if select attr present in case of xpath..
63
    if type == "xpath" and capcheck is not None:
64
        capcheck(":xpath")
65
    return rep