Statistics
| Branch: | Tag: | Revision:

root / ncclient / operations / retrieve.py @ 216bb34c

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
    """Adds attributes for the *<data>* element to :class:`RPCReply`, which
24
    pertains to the :class:`Get` and :class:`GetConfig` operations."""
25

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

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

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

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

    
52
    #: Same as :attr:`data_ele`
53
    data = data_ele
54

    
55

    
56
class Get(RPC):
57

    
58
    "The *<get>* RPC"
59

    
60
    SPEC = {'tag': 'get', 'subtree': []}
61

    
62
    REPLY_CLS = GetReply
63

    
64
    def request(self, filter=None):
65
        """
66
        :arg filter: optional; see :ref:`filter`
67

68
        :seealso: :ref:`return`
69
        """
70
        spec = Get.SPEC.copy()
71
        if filter is not None:
72
            spec['subtree'].append(util.build_filter(filter))
73
        return self._request(spec)
74

    
75

    
76
class GetConfig(RPC):
77

    
78
    "The *<get-config>* RPC"
79

    
80
    SPEC = {'tag': 'get-config', 'subtree': []}
81

    
82
    REPLY_CLS = GetReply
83

    
84
    def request(self, source, filter=None):
85
        """
86
        :arg source: See :ref:`source_target`
87

88
        :arg filter: optional; see :ref:`filter`
89

90
        :seealso: :ref:`return`
91
        """
92
        spec = GetConfig.SPEC.copy()
93
        spec['subtree'].append(util.store_or_url('source', source, self._assert))
94
        if filter is not None:
95
            spec['subtree'].append(util.build_filter(filter))
96
        return self._request(spec)