4168e1d99682262439e2ef59ace2b3c1a3a6ba6c
[ncclient] / ncclient / operations / util.py
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'
16
17 from ncclient import OperationError
18
19 from . import 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 store_or_url(store, url):
33     one_of(store, url)
34     node = {}
35     if store is not None:
36         node['tag'] = store
37     else:
38         node['tag'] = 'url'
39         node['text'] = url
40     return node
41
42 def build_filter(type, criteria):
43     filter = {
44         'tag': 'filter',
45         'attributes': {'type': type}
46     }
47     if type == 'xpath':
48         filter['attributes']['select'] = criteria
49     else:
50         filter['subtree'] = [criteria]
51     return filter