Statistics
| Branch: | Tag: | Revision:

root / ncclient / operations / reply.py @ 3e022b7b

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

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

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

    
67

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

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