root / ncclient / operations / retrieve.py @ ebf2bbc6
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 |
from rpc import RPC, RPCReply |
16 |
|
17 |
from ncclient import content |
18 |
|
19 |
import util |
20 |
|
21 |
class GetReply(RPCReply): |
22 |
|
23 |
'Adds data attribute'
|
24 |
|
25 |
# tested: no
|
26 |
# combed: yes
|
27 |
|
28 |
def _parsing_hook(self, root): |
29 |
self._data = None |
30 |
if not self._errors: |
31 |
self._data = content.namespaced_find(root, 'data') |
32 |
|
33 |
@property
|
34 |
def data_element(self): |
35 |
if not self._parsed: |
36 |
self.parse()
|
37 |
return self._data |
38 |
|
39 |
@property
|
40 |
def data_xml(self): |
41 |
return content.element2string(self.data_element) |
42 |
|
43 |
data = data_element |
44 |
|
45 |
class Get(RPC): |
46 |
|
47 |
# tested: no
|
48 |
# combed: yes
|
49 |
|
50 |
SPEC = { |
51 |
'tag': 'get', |
52 |
'subtree': []
|
53 |
} |
54 |
|
55 |
REPLY_CLS = GetReply |
56 |
|
57 |
def request(self, filter=None): |
58 |
spec = Get.SPEC.copy() |
59 |
if filter is not None: |
60 |
spec['subtree'].append(util.build_filter(filter))) |
61 |
return self._request(spec) |
62 |
|
63 |
class GetConfig(RPC): |
64 |
|
65 |
# tested: no
|
66 |
# combed: yes
|
67 |
|
68 |
SPEC = { |
69 |
'tag': 'get-config', |
70 |
'subtree': []
|
71 |
} |
72 |
|
73 |
REPLY_CLS = GetReply |
74 |
|
75 |
def request(self, source=None, source_url=None, filter=None): |
76 |
"""
|
77 |
`filter` has to be a tuple of (type, criteria)
|
78 |
The type may be one of 'xpath' or 'subtree'
|
79 |
The criteria may be an ElementTree.Element, an XML fragment, or tree specification
|
80 |
"""
|
81 |
spec = GetConfig.SPEC.copy() |
82 |
spec['subtree'].append({
|
83 |
'tag': 'source', |
84 |
'subtree': util.store_or_url(source, source_url)
|
85 |
}) |
86 |
if filter is not None: |
87 |
spec['subtree'].append(util.build_filter(filter)) |
88 |
return self._request(spec) |
89 |
|