Statistics
| Branch: | Tag: | Revision:

root / ncclient / operations / util.py @ ebf2bbc6

History | View | Annotate | Download (1.9 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 import OperationError
18
from ncclient.content import qualify as _
19
from ncclient.content import root_ensured
20

    
21
from errors import MissingCapabilityError
22

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

    
34
def store_or_url(store, url, capcheck=None):
35
    one_of(store, url)
36
    node = {}
37
    if store is not None:
38
        node['tag'] = store
39
    else:
40
        if capcheck is not None:
41
            capcheck(':url') # hmm.. schema check? deem overkill for now
42
        node['tag'] = 'url'
43
        node['text'] = url
44
    return node
45

    
46
def build_filter(spec, capcheck=None):
47
    type = None
48
    if isinstance(spec, tuple):
49
        type, criteria = tuple
50
        rep = {
51
            'tag': 'filter',
52
            'attributes': {'type': type},
53
            'subtree': criteria
54
       }
55
    else:
56
        rep = root_ensure(spec, 'filter', 'type')
57
        try:
58
            type = rep['type']
59
        except KeyError:
60
            type = ele[qualify('type'))
61
    if type == 'xpath' and capcheck_func is not None:
62
        capcheck_func(':xpath')
63
    return rep
64