Statistics
| Branch: | Tag: | Revision:

root / ncclient / operations / retrieve.py @ 0b7d3b31

History | View | Annotate | Download (2.6 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
    # TESTED
24

    
25
    """Adds attributes for the *<data>* element to :class:`RPCReply`, which
26
    pertains to the :class:`Get` and :class:`GetConfig` operations."""
27

    
28
    def _parsing_hook(self, root):
29
        self._data = None
30
        if not self._errors:
31
            self._data = content.find(root, 'data',
32
                                      nslist=[content.BASE_NS,
33
                                              content.CISCO_BS])
34

    
35
    @property
36
    def data_ele(self):
37
        "*<data>* element as an :class:`~xml.etree.ElementTree.Element`"
38
        if not self._parsed:
39
            self.parse()
40
        return self._data
41

    
42
    @property
43
    def data_xml(self):
44
        "*<data>* element as an XML string"
45
        if not self._parsed:
46
            self.parse()
47
        return content.ele2xml(self._data)
48

    
49
    @property
50
    def data_dtree(self):
51
        "*<data>* element in :ref:`dtree`"
52
        return content.ele2dtree(self._data)
53

    
54
    #: Same as :attr:`data_ele`
55
    data = data_ele
56

    
57

    
58
class Get(RPC):
59

    
60
    # TESTED
61

    
62
    "The *<get>* RPC"
63

    
64
    SPEC = {'tag': 'get', 'subtree': []}
65

    
66
    REPLY_CLS = GetReply
67

    
68
    def request(self, filter=None):
69
        """
70
        :arg filter: optional; see :ref:`filter`
71

72
        :seealso: :ref:`return`
73
        """
74
        spec = Get.SPEC.copy()
75
        if filter is not None:
76
            spec['subtree'].append(util.build_filter(filter))
77
        return self._request(spec)
78

    
79

    
80
class GetConfig(RPC):
81

    
82
    # TESTED
83

    
84
    "The *<get-config>* RPC"
85

    
86
    SPEC = {'tag': 'get-config', 'subtree': []}
87

    
88
    REPLY_CLS = GetReply
89

    
90
    def request(self, source, filter=None):
91
        """
92
        :arg source: See :ref:`source_target`
93

94
        :arg filter: optional; see :ref:`filter`
95

96
        :seealso: :ref:`return`
97
        """
98
        spec = GetConfig.SPEC.copy()
99
        spec['subtree'].append(util.store_or_url('source', source, self._assert))
100
        if filter is not None:
101
            spec['subtree'].append(util.build_filter(filter))
102
        return self._request(spec)