Statistics
| Branch: | Tag: | Revision:

root / ncclient / operations / util.py @ 188649fa

History | View | Annotate | Download (2 kB)

1 c2a5b930 Shikhar Bhushan
# Copyright 2009 Shikhar Bhushan
2 c2a5b930 Shikhar Bhushan
#
3 c2a5b930 Shikhar Bhushan
# Licensed under the Apache License, Version 2.0 (the "License");
4 c2a5b930 Shikhar Bhushan
# you may not use this file except in compliance with the License.
5 c2a5b930 Shikhar Bhushan
# You may obtain a copy of the License at
6 c2a5b930 Shikhar Bhushan
#
7 c2a5b930 Shikhar Bhushan
#    http://www.apache.org/licenses/LICENSE-2.0
8 c2a5b930 Shikhar Bhushan
#
9 c2a5b930 Shikhar Bhushan
# Unless required by applicable law or agreed to in writing, software
10 c2a5b930 Shikhar Bhushan
# distributed under the License is distributed on an "AS IS" BASIS,
11 c2a5b930 Shikhar Bhushan
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 c2a5b930 Shikhar Bhushan
# See the License for the specific language governing permissions and
13 c2a5b930 Shikhar Bhushan
# limitations under the License.
14 d6688264 Shikhar Bhushan
15 cc9af1c3 Shikhar Bhushan
'Boilerplate ugliness'
16 d6688264 Shikhar Bhushan
17 d771dffc Shikhar Bhushan
from ncclient import content
18 d6688264 Shikhar Bhushan
19 d771dffc Shikhar Bhushan
from errors import OperationError, MissingCapabilityError
20 d6688264 Shikhar Bhushan
21 179b00d4 Shikhar Bhushan
def one_of(*args):
22 179b00d4 Shikhar Bhushan
    'Verifies that only one of the arguments is not None'
23 d6688264 Shikhar Bhushan
    for i, arg in enumerate(args):
24 d6688264 Shikhar Bhushan
        if arg is not None:
25 d6688264 Shikhar Bhushan
            for argh in args[i+1:]:
26 d6688264 Shikhar Bhushan
                if argh is not None:
27 d6688264 Shikhar Bhushan
                    raise OperationError('Too many parameters')
28 d6688264 Shikhar Bhushan
            else:
29 d6688264 Shikhar Bhushan
                return
30 d6688264 Shikhar Bhushan
    raise OperationError('Insufficient parameters')
31 d6688264 Shikhar Bhushan
32 d771dffc Shikhar Bhushan
def store_or_url(wha, loc, capcheck=None):
33 d771dffc Shikhar Bhushan
    node = { 'tag': wha, 'subtree': {} }
34 d771dffc Shikhar Bhushan
    if '://' in loc: # e.g. http://, file://, ftp://
35 ebf2bbc6 Shikhar Bhushan
        if capcheck is not None:
36 d771dffc Shikhar Bhushan
            capcheck(':url') # url schema check at some point!
37 d771dffc Shikhar Bhushan
        node['subtree']['tag'] = 'url'
38 d771dffc Shikhar Bhushan
        node['subtree']['text'] = loc
39 d771dffc Shikhar Bhushan
    else:
40 d771dffc Shikhar Bhushan
        node['subtree']['tag'] = loc
41 d6688264 Shikhar Bhushan
    return node
42 a6c00291 Shikhar Bhushan
43 ebf2bbc6 Shikhar Bhushan
def build_filter(spec, capcheck=None):
44 ebf2bbc6 Shikhar Bhushan
    type = None
45 ebf2bbc6 Shikhar Bhushan
    if isinstance(spec, tuple):
46 408abf6d Shikhar Bhushan
        type, criteria = spec
47 188649fa Shikhar Bhushan
        rep = {'tag': 'filter', 'attrib': {'type': type}}
48 188649fa Shikhar Bhushan
        if type == 'xpath':
49 188649fa Shikhar Bhushan
            rep['attrib']['select'] = criteria
50 188649fa Shikhar Bhushan
        elif type == 'subtree':
51 188649fa Shikhar Bhushan
            rep['subtree'] = criteria
52 188649fa Shikhar Bhushan
        else:
53 188649fa Shikhar Bhushan
            raise OperationError("Invalid filter type")
54 ebf2bbc6 Shikhar Bhushan
    else:
55 a7cb58ce Shikhar Bhushan
        rep = content.validated_element(spec, ['filter', content.qualify('filter')],
56 a7cb58ce Shikhar Bhushan
                                        attrs=[('type', content.qualify('type'))])
57 d771dffc Shikhar Bhushan
    if type == 'xpath' and capcheck is not None:
58 d771dffc Shikhar Bhushan
        capcheck(':xpath')
59 ebf2bbc6 Shikhar Bhushan
    return rep