Statistics
| Branch: | Tag: | Revision:

root / ncclient / operations / reply.py @ 40a92771

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

    
78

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

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