Statistics
| Branch: | Tag: | Revision:

root / ncclient / operations / reply.py @ 541247ba

History | View | Annotate | Download (3.8 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 xml.etree import cElementTree as ET
16

    
17
from ncclient.content import multiqualify as _
18
from ncclient.content import unqualify as __
19

    
20
import logging
21
logger = logging.getLogger('ncclient.operations.reply')
22

    
23
class RPCReply:
24
    
25
    def __init__(self, raw):
26
        self._raw = raw
27
        self._parsed = False
28
        self._errors = []
29
    
30
    def __repr__(self):
31
        return self._raw
32
    
33
    def parse(self):
34
        if self._parsed: return
35
        root = ET.fromstring(self._raw) # <rpc-reply> element
36
        
37
        # per rfc 4741 an <ok/> tag is sent when there are no errors or warnings
38
        oktags = _('ok')
39
        for oktag in oktags:
40
            if root.find(oktag) is not None:
41
                logger.debug('parsed [%s]' % oktag)
42
                self._parsed = True
43
                return
44
        
45
        # create RPCError objects from <rpc-error> elements
46
        errtags = _('rpc-error')
47
        for errtag in errtags:
48
            for err in root.getiterator(errtag): # a particular <rpc-error>
49
                logger.debug('parsed [%s]' % errtag)
50
                d = {}
51
                for err_detail in err.getchildren(): # <error-type> etc..
52
                    d[__(err_detail.tag)] = err_detail.text
53
                self._errors.append(RPCError(d))
54
            if self._errors:
55
                break
56
        self._parsed = True
57
    
58
    @property
59
    def raw(self):
60
        return self._raw
61
    
62
    @property
63
    def ok(self):
64
        if not self._parsed: self.parse()
65
        return not bool(self._errors) # empty list = false
66
    
67
    @property
68
    def errors(self):
69
        'List of RPCError objects. Will be empty if no <rpc-error> elements in reply.'
70
        if not self._parsed: self.parse()
71
        return self._errors
72

    
73

    
74
class RPCError(Exception): # raise it if you like
75
    
76
    def __init__(self, err_dict):
77
        self._dict = err_dict
78
        if self.message is not None:
79
            Exception.__init__(self, self.message)
80
        else:
81
            Exception.__init__(self)
82
    
83
    @property
84
    def raw(self):
85
        return self._element.tostring()
86
    
87
    @property
88
    def type(self):
89
        return self.get('error-type', None)
90
    
91
    @property
92
    def severity(self):
93
        return self.get('error-severity', None)
94
    
95
    @property
96
    def tag(self):
97
        return self.get('error-tag', None)
98
    
99
    @property
100
    def path(self):
101
        return self.get('error-path', None)
102
    
103
    @property
104
    def message(self):
105
        return self.get('error-message', None)
106
    
107
    @property
108
    def info(self):
109
        return self.get('error-info', None)
110

    
111
    ## dictionary interface
112
    
113
    __getitem__ = lambda self, key: self._dict.__getitem__(key)
114
    
115
    __iter__ = lambda self: self._dict.__iter__()
116
    
117
    __contains__ = lambda self, key: self._dict.__contains__(key)
118
    
119
    keys = lambda self: self._dict.keys()
120
    
121
    get = lambda self, key, default: self._dict.get(key, default)
122
        
123
    iteritems = lambda self: self._dict.iteritems()
124
    
125
    iterkeys = lambda self: self._dict.iterkeys()
126
    
127
    itervalues = lambda self: self._dict.itervalues()
128
    
129
    values = lambda self: self._dict.values()
130
    
131
    items = lambda self: self._dict.items()
132
    
133
    __repr__ = lambda self: repr(self._dict)