Statistics
| Branch: | Tag: | Revision:

root / ncclient / operations / reply.py @ 41e2ed46

History | View | Annotate | Download (3.7 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
        root = ET.fromstring(self._raw) # <rpc-reply> element
35
        
36
        # per rfc 4741 an <ok/> tag is sent when there are no errors or warnings
37
        oktags = _('ok')
38
        for oktag in oktags:
39
            if root.find(oktag) is not None:
40
                logger.debug('found %s' % oktag)
41
                self._parsed = True
42
                return
43
        
44
        # create RPCError objects from <rpc-error> elements
45
        errtags = _('rpc-error')
46
        for errtag in errtags:
47
            for err in root.getiterator(errtag): # a particular <rpc-error>
48
                d = {}
49
                for err_detail in err.getchildren(): # <error-type> etc..
50
                    d[__(err_detail)] = err_detail.text
51
                self._errors.append(RPCError(d))
52
            if self._errors:
53
                break
54
        self._parsed = True
55
    
56
    @property
57
    def raw(self):
58
        return self._raw
59
    
60
    @property
61
    def ok(self):
62
        if not self._parsed: self.parse()
63
        return not bool(self._errors) # empty list = false
64
    
65
    @property
66
    def errors(self):
67
        'List of RPCError objects. Will be empty if no <rpc-error> elements in reply.'
68
        if not self._parsed: self.parse()
69
        return self._errors
70

    
71

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

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