Statistics
| Branch: | Tag: | Revision:

root / ncclient / operations / util.py @ a6c00291

History | View | Annotate | Download (1.1 kB)

1
#!/usr/bin/env python
2

    
3
'Boilerplate'
4

    
5
from ncclient import OperationError
6

    
7
from . import MissingCapabilityError
8

    
9
def one_of(self, *args):
10
    for i, arg in enumerate(args):
11
        if arg is not None:
12
            for argh in args[i+1:]:
13
                if argh is not None:
14
                    raise OperationError('Too many parameters')
15
            else:
16
                return
17
    raise OperationError('Insufficient parameters')
18

    
19

    
20
def assert_capability(key, capabilities):
21
    if key not in capabilities:
22
        raise MissingCapabilityError('[%s] capability is required for this operation' % key)
23

    
24

    
25
def store_or_url(store, url):
26
    one_of(store, url)
27
    node = {}
28
    if store is not None:
29
        node['tag'] = store
30
    else:
31
        node['tag'] = 'url'
32
        node['text'] = url
33
    return node
34

    
35
def build_filter(spec, type, criteria):
36
    filter = {
37
        'tag': 'filter',
38
        'attributes': {'type': type}
39
    }
40
    if type == 'subtree':
41
        filter['children'] = [criteria]
42
    elif type == 'xpath':
43
        filter['attributes']['select'] = criteria
44
    return filter