root / ncclient / operations / rpc / reply.py @ cc9af1c3
History | View | Annotate | Download (4.4 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 |
import ncclient |
16 |
|
17 |
from xml.etree import cElementTree as ET |
18 |
|
19 |
from ncclient.content import namespaced_find |
20 |
from ncclient.content import unqualify as __ |
21 |
|
22 |
import logging |
23 |
logger = logging.getLogger('ncclient.rpc.reply')
|
24 |
|
25 |
class RPCReply: |
26 |
|
27 |
'NOTES: memory considerations?? storing both raw xml + ET.Element'
|
28 |
|
29 |
def __init__(self, raw): |
30 |
self._raw = raw
|
31 |
self._parsed = False |
32 |
self._root = None |
33 |
self._errors = []
|
34 |
|
35 |
def __repr__(self): |
36 |
return self._raw |
37 |
|
38 |
def parse(self): |
39 |
if self._parsed: return |
40 |
root = self._root = ET.fromstring(self._raw) # <rpc-reply> element |
41 |
# per rfc 4741 an <ok/> tag is sent when there are no errors or warnings
|
42 |
ok = namespaced_find(root, 'ok')
|
43 |
if ok is not None: |
44 |
logger.debug('parsed [%s]' % ok.tag)
|
45 |
else: # create RPCError objects from <rpc-error> elements |
46 |
error = namespaced_find(root, 'rpc-error')
|
47 |
if error is not None: |
48 |
logger.debug('parsed [%s]' % error.tag)
|
49 |
for err in root.getiterator(error.tag): |
50 |
# process a particular <rpc-error>
|
51 |
d = {} |
52 |
for err_detail in err.getchildren(): # <error-type> etc.. |
53 |
tag = __(err_detail.tag) |
54 |
d[tag] = (err_detail.text.strip() if tag != 'error-info' |
55 |
else ET.tostring(err_detail, 'utf-8')) |
56 |
self._errors.append(RPCError(d))
|
57 |
self._parsing_hook(root)
|
58 |
self._parsed = True |
59 |
|
60 |
def extract_subtree_xml(self): |
61 |
return ''.join([ET.tostring(ele) |
62 |
for ele in ET.fromstring(self.xml).getchildren()]) |
63 |
|
64 |
@property
|
65 |
def xml(self): |
66 |
'<rpc-reply> as returned'
|
67 |
return self._raw |
68 |
|
69 |
@property
|
70 |
def ok(self): |
71 |
if not self._parsed: self.parse() |
72 |
return not self._errors # empty list => false |
73 |
|
74 |
@property
|
75 |
def error(self): |
76 |
if not self._parsed: self.parse() |
77 |
if self._errors: |
78 |
return self._errors[0] |
79 |
else:
|
80 |
return None |
81 |
|
82 |
@property
|
83 |
def errors(self): |
84 |
'List of RPCError objects. Will be empty if no <rpc-error> elements in reply.'
|
85 |
if not self._parsed: self.parse() |
86 |
return self._errors |
87 |
|
88 |
|
89 |
class RPCError(ncclient.RPCError): # raise it if you like |
90 |
|
91 |
def __init__(self, err_dict): |
92 |
self._dict = err_dict
|
93 |
if self.message is not None: |
94 |
ncclient.RPCError.__init__(self, self.message) |
95 |
else:
|
96 |
ncclient.RPCError.__init__(self)
|
97 |
|
98 |
@property
|
99 |
def raw(self): |
100 |
return self._element.tostring() |
101 |
|
102 |
@property
|
103 |
def type(self): |
104 |
return self.get('error-type', None) |
105 |
|
106 |
@property
|
107 |
def severity(self): |
108 |
return self.get('error-severity', None) |
109 |
|
110 |
@property
|
111 |
def tag(self): |
112 |
return self.get('error-tag', None) |
113 |
|
114 |
@property
|
115 |
def path(self): |
116 |
return self.get('error-path', None) |
117 |
|
118 |
@property
|
119 |
def message(self): |
120 |
return self.get('error-message', None) |
121 |
|
122 |
@property
|
123 |
def info(self): |
124 |
return self.get('error-info', None) |
125 |
|
126 |
## dictionary interface
|
127 |
|
128 |
__getitem__ = lambda self, key: self._dict.__getitem__(key) |
129 |
|
130 |
__iter__ = lambda self: self._dict.__iter__() |
131 |
|
132 |
__contains__ = lambda self, key: self._dict.__contains__(key) |
133 |
|
134 |
keys = lambda self: self._dict.keys() |
135 |
|
136 |
get = lambda self, key, default: self._dict.get(key, default) |
137 |
|
138 |
iteritems = lambda self: self._dict.iteritems() |
139 |
|
140 |
iterkeys = lambda self: self._dict.iterkeys() |
141 |
|
142 |
itervalues = lambda self: self._dict.itervalues() |
143 |
|
144 |
values = lambda self: self._dict.values() |
145 |
|
146 |
items = lambda self: self._dict.items() |
147 |
|
148 |
__repr__ = lambda self: repr(self._dict) |