Revision 3e022b7b ncclient/operations/rpc.py

b/ncclient/operations/rpc.py
15 15
from threading import Event, Lock
16 16
from uuid import uuid1
17 17

  
18
from . import logger
19 18
from ncclient.content import TreeBuilder, BASE_NS
20
from reply import RPCReply, RPCReplyListener
19
from ncclient.glue import Listener
20

  
21
from . import logger
22
from reply import RPCReply
23

  
21 24

  
22 25
class RPC(object):
23 26
    
......
26 29
        self._id = uuid1().urn
27 30
        self._listener = RPCReplyListener(session)
28 31
        self._listener.register(self._id, self)
29
        self._reply = RPCReply()
32
        self._reply = None
30 33
        self._reply_event = Event()
31 34
    
32 35
    def _build(self, op, encoding='utf-8'):
33 36
        if isinstance(op, dict):
34 37
            return self.build_from_spec(self._id, op, encoding)
35
        elif isinstance(op, basestring): 
36
            return self.build_from_string(self._id, op, encoding)
37 38
        else:
38
            raise ValueError('Inappropriate value of tree spec.')
39
            return self.build_from_string(self._id, op, encoding)
39 40
    
40 41
    def _request(self, op):
41 42
        req = self._build(op)
42 43
        self._session.send(req)
43
        if reply_event is not None: # if we were provided an Event to use
44
            self._reply_event = reply_event
45
        else: # otherwise, block till response received and return it
46
            self._reply_event = Event()
44
        if async:
47 45
            self._reply_event.wait()
48 46
            self._reply.parse()
49
        return self._reply
47
            return self._reply
50 48
    
51
    def request(self, *args, **kwds):
52
        raise NotImplementedError
49
    def deliver(self, raw):
50
        self._reply = RPCReply(raw)
51
        self._reply_event.set()
53 52
    
54 53
    @property
55 54
    def has_reply(self):
56
        try:
57
            return self._reply_event.isSet()
58
        except TypeError: # reply_event is None
59
            return False
55
        return self._reply_event.isSet()
60 56
    
61 57
    @property
62 58
    def reply(self):
......
70 66
    def session(self):
71 67
        return self._session
72 68
    
69
    @property
70
    def reply_event(self):
71
        return self._reply_event
72
    
73 73
    @staticmethod
74 74
    def build_from_spec(msgid, opspec, encoding='utf-8'):
75 75
        "TODO: docstring"
......
87 87
        doc = (u'<rpc message-id="%s" xmlns="%s">%s</rpc>' %
88 88
               (msgid, BASE_NS, opstr)).encode(encoding)
89 89
        return '%s%s' % (decl, doc)
90

  
91

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

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

Also available in: Unified diff