90a0f9c4bd7bfb8f6b650201010bdead59957078
[ncclient] / ncclient / operations / retrieve.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 from rpc import RPC, RPCReply
16
17 from ncclient.xml_ import *
18
19 import util
20
21 class GetReply(RPCReply):
22
23     """Adds attributes for the *data* element to `RPCReply`. This pertains to the `Get` and
24     `GetConfig` operations."""
25
26     def _parsing_hook(self, root):
27         self._data = None
28         if not self._errors:
29             self._data = root.find(qualify("data"))
30     
31     @property
32     def data_ele(self):
33         "*data* element as an `~xml.etree.ElementTree.Element`"
34         if not self._parsed:
35             self.parse()
36         return self._data
37
38     @property
39     def data_xml(self):
40         "*data* element as an XML string"
41         if not self._parsed:
42             self.parse()
43         return to_xml(self._data)
44     
45     data = data_ele
46     "Same as :attr:`data_ele`"
47
48
49 class Get(RPC):
50
51     "The *get* RPC."
52
53     REPLY_CLS = GetReply
54
55     def request(self, filter=None):
56         node = new_ele("get")
57         if filter is not None:
58             node.append(util.build_filter(filter))
59         return self._request(node)
60
61
62 class GetConfig(RPC):
63
64     "The *get-config* RPC."
65
66     REPLY_CLS = GetReply
67
68     def request(self, source, filter=None):
69         node = new_ele("get-config")
70         node.append(util.datastore_or_url("source", source, self._assert))
71         if filter is not None:
72             node.append(util.build_filter(filter))
73         return self._request(node)