Statistics
| Branch: | Tag: | Revision:

root / ncclient / operations / reply.py @ 5858a82c

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
def parse():
16
    
17
    pass
18

    
19

    
20
class RPCReply:
21
    
22
    def __init__(self, event):
23
        self._raw = None
24
        self._errs = None
25
    
26
    def __str__(self):
27
        return self._raw
28
    
29
    def parse(self):
30
        if not self._parsed:
31
            ok = RPCReplyParser.parse(self._raw)
32
            for err in errs:
33
                self._errs.append(RPCError(*err))
34
            self._parsed = True
35
    
36
    @property
37
    def raw(self):
38
        return self._raw
39
    
40
    @property
41
    def parsed(self):
42
        return self._parsed
43
    
44
    @property
45
    def ok(self):
46
        return True if self._parsed and not self._errs else False
47
    
48
    @property
49
    def errors(self):
50
        return self._errs
51

    
52

    
53
class RPCError(Exception): # raise it if you like
54
    
55
    def __init__(self, raw, err_dict):
56
        self._raw = raw
57
        self._dict = err_dict
58
    
59
    def __str__(self):
60
        # TODO
61
        return self._raw
62
    
63
    def __dict__(self):
64
        return self._dict
65
    
66
    @property
67
    def raw(self):
68
        return self._raw
69
    
70
    @property
71
    def type(self):
72
        return self._dict.get('type', None)
73
    
74
    @property
75
    def severity(self):
76
        return self._dict.get('severity', None)
77
    
78
    @property
79
    def tag(self):
80
        return self._dict.get('tag', None)
81
    
82
    @property
83
    def path(self):
84
        return self._dict.get('path', None)
85
    
86
    @property
87
    def message(self):
88
        return self._dict.get('message', None)
89
    
90
    @property
91
    def info(self):
92
        return self._dict.get('info', None)
93

    
94

    
95
class RPCReplyListener(Listener):
96
    
97
    # TODO - determine if need locking
98
    
99
    # one instance per subject    
100
    def __new__(cls, subject):
101
        instance = subject.get_listener_instance(cls)
102
        if instance is None:
103
            instance = object.__new__(cls)
104
            instance._id2rpc = WeakValueDictionary()
105
            instance._errback = None
106
            subject.add_listener(instance)
107
        return instance
108
    
109
    def __str__(self):
110
        return 'RPCReplyListener'
111
    
112
    def set_errback(self, errback):
113
        self._errback = errback
114

    
115
    def register(self, msgid, rpc):
116
        self._id2rpc[msgid] = rpc
117
    
118
    def callback(self, root, raw):
119
        tag, attrs = root
120
        if __(tag) != 'rpc-reply':
121
            return
122
        for key in attrs:
123
            if __(key) == 'message-id':
124
                id = attrs[key]
125
                try:
126
                    rpc = self._id2rpc[id]
127
                    rpc.deliver(raw)
128
                except:
129
                    logger.warning('RPCReplyListener.callback: no RPC '
130
                                   + 'registered for message-id: [%s]' % id)
131
                break
132
        else:
133
            logger.warning('<rpc-reply> without message-id received: %s' % raw)
134
    
135
    def errback(self, err):
136
        logger.error('RPCReplyListener.errback: %r' % err)
137
        if self._errback is not None:
138
            self._errback(err)