Statistics
| Branch: | Tag: | Revision:

root / ncclient / operations / retrieve.py @ dd225c7a

History | View | Annotate | Download (2.5 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 xml_
18
from copy import deepcopy
19

    
20
import util
21

    
22
class GetReply(RPCReply):
23

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

    
27
    def _parsing_hook(self, root):
28
        self._data = None
29
        if not self._errors:
30
            self._data = xml_.find(root, 'data', nslist=xml_.NSLIST)
31

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

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

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

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

    
54

    
55
class Get(RPC):
56

    
57
    "The *<get>* RPC"
58

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

    
61
    REPLY_CLS = GetReply
62

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

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

    
74

    
75
class GetConfig(RPC):
76

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

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

    
81
    REPLY_CLS = GetReply
82

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

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

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