Revision c2a5b930 ncclient/operations/retrieve.py
b/ncclient/operations/retrieve.py | ||
---|---|---|
14 | 14 |
|
15 | 15 |
from ncclient.rpc import RPC, RPCReply |
16 | 16 |
|
17 |
from ncclient import content |
|
18 |
|
|
17 | 19 |
import util |
18 | 20 |
|
21 |
# NOTES |
|
22 |
# - consider class for helping define <filter> for Get/GetConfig?? |
|
23 |
|
|
19 | 24 |
class GetReply(RPCReply): |
20 | 25 |
|
26 |
'Adds data attribute' |
|
27 |
|
|
21 | 28 |
# tested: no |
29 |
# combed: yes |
|
22 | 30 |
|
23 |
def __init__(self, *args, **kwds): |
|
24 |
RPCReply.__init__(self, *args, **kwds) |
|
31 |
def _parsing_hook(self, root): |
|
25 | 32 |
self._data = None |
33 |
if not self._errors: |
|
34 |
self._data = content.namespaced_find(root, 'data') |
|
26 | 35 |
|
27 |
def parse(self): |
|
28 |
RPCReply.parse(self) |
|
29 |
if self.ok: |
|
30 |
self.root.find('data') |
|
36 |
@property |
|
37 |
def data_element(self): |
|
38 |
if not self._parsed: |
|
39 |
self.parse() |
|
40 |
return self._data |
|
31 | 41 |
|
32 | 42 |
@property |
33 |
def data(self): |
|
34 |
return ET.tostring(self._data) |
|
43 |
def data_xml(self): |
|
44 |
return content.element2string(self.data_element) |
|
45 |
|
|
46 |
data = data_element |
|
35 | 47 |
|
36 | 48 |
class Get(RPC): |
37 | 49 |
|
38 | 50 |
# tested: no |
51 |
# combed: yes |
|
39 | 52 |
|
40 | 53 |
SPEC = { |
41 | 54 |
'tag': 'get', |
... | ... | |
51 | 64 |
return self._request(spec) |
52 | 65 |
|
53 | 66 |
class GetConfig(RPC): |
67 |
|
|
68 |
# tested: no |
|
69 |
# combed: yes |
|
54 | 70 |
|
55 | 71 |
SPEC = { |
56 | 72 |
'tag': 'get-config', |
... | ... | |
60 | 76 |
REPLY_CLS = GetReply |
61 | 77 |
|
62 | 78 |
def request(self, source=None, source_url=None, filter=None): |
63 |
util.one_of(source, source_url) |
|
79 |
""" |
|
80 |
`filter` has to be a tuple of (type, criteria) |
|
81 |
The type may be one of 'xpath' or 'subtree' |
|
82 |
The criteria may be an ElementTree.Element, an XML fragment, or tree specification |
|
83 |
""" |
|
64 | 84 |
spec = GetConfig.SPEC.copy() |
65 |
subtree = spec['subtree'] |
|
66 |
subtree.append({'tag': 'source', 'subtree': util.store_or_url(source, source_url)}) |
|
85 |
spec['subtree'].append({ |
|
86 |
'tag': 'source', |
|
87 |
'subtree': util.store_or_url(source, source_url) |
|
88 |
}) |
|
67 | 89 |
if filter is not None: |
68 |
subtree.append(util.build_filter(*filter))
|
|
90 |
spec['subtree'].append(util.build_filter(*filter))
|
|
69 | 91 |
return self._request(spec) |
Also available in: Unified diff