rename content to xml_
[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 import xml_
18 from copy import deepcopy
19
20 import util
21
22 class GetReply(RPCReply):
23
24     # TESTED
25
26     """Adds attributes for the *<data>* element to :class:`RPCReply`, which
27     pertains to the :class:`Get` and :class:`GetConfig` operations."""
28
29     def _parsing_hook(self, root):
30         self._data = None
31         if not self._errors:
32             self._data = xml_.find(root, 'data',
33                                       nslist=[xml_.BASE_NS,
34                                               xml_.CISCO_BS])
35
36     @property
37     def data_ele(self):
38         "*<data>* element as an :class:`~xml.etree.ElementTree.Element`"
39         if not self._parsed:
40             self.parse()
41         return self._data
42
43     @property
44     def data_xml(self):
45         "*<data>* element as an XML string"
46         if not self._parsed:
47             self.parse()
48         return xml_.ele2xml(self._data)
49
50     @property
51     def data_dtree(self):
52         "*<data>* element in :ref:`dtree`"
53         return xml_.ele2dtree(self._data)
54
55     #: Same as :attr:`data_ele`
56     data = data_ele
57
58
59 class Get(RPC):
60
61     # TESTED
62
63     "The *<get>* RPC"
64
65     SPEC = {'tag': 'get', 'subtree': []}
66
67     REPLY_CLS = GetReply
68
69     def request(self, filter=None):
70         """
71         :arg filter: optional; see :ref:`filter`
72
73         :seealso: :ref:`return`
74         """
75         spec = deepcopy(Get.SPEC)
76         if filter is not None:
77             spec['subtree'].append(util.build_filter(filter))
78         return self._request(spec)
79
80
81 class GetConfig(RPC):
82
83     # TESTED
84
85     "The *<get-config>* RPC"
86
87     SPEC = {'tag': 'get-config', 'subtree': []}
88
89     REPLY_CLS = GetReply
90
91     def request(self, source, filter=None):
92         """
93         :arg source: See :ref:`source_target`
94
95         :arg filter: optional; see :ref:`filter`
96
97         :seealso: :ref:`return`
98         """
99         spec = deepcopy(GetConfig.SPEC)
100         spec['subtree'].append(util.store_or_url('source', source, self._assert))
101         if filter is not None:
102             spec['subtree'].append(util.build_filter(filter))
103         return self._request(spec)