Revision 5858a82c ncclient/operations/rpc.py

b/ncclient/operations/rpc.py
16 16
from uuid import uuid1
17 17

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

  
20
class RPC:
21
        
22
class RPC(object):
23
    
22 24
    def __init__(self, session, async=False):
23 25
        self._session = session
24 26
        self._id = uuid1().urn
27
        self._listener = RPCReplyListener(session)
28
        self._listener.register(self._id, self)
25 29
        self._reply = RPCReply()
26 30
        self._reply_event = Event()
27 31
    
28 32
    def _build(self, op, encoding='utf-8'):
29
        if isinstance(op, basestring):
30
            return RPCBuilder.build_from_string(self._id, op, encoding)
33
        if isinstance(op, dict):
34
            return self.build_from_spec(self._id, op, encoding)
35
        elif isinstance(op, basestring): 
36
            return self.build_from_string(self._id, op, encoding)
31 37
        else:
32
            return RPCBuilder.build_from_spec(self._id, op, encoding)
38
            raise ValueError('Inappropriate value of tree spec.')
33 39
    
34 40
    def _request(self, op):
35
        self._reply = RPCReply()
36
        # get the listener instance for this session
37
        # <rpc-reply> with message id will reach response_cb
38
        self._listener.register(self._id, self)
39
        # only effective the first time, transport.session.Subject internally
40
        # uses a set type for listeners
41
        self._session.add_listener(self._listener)
42
        req = RPCBuilder.build(self._id, op)
41
        req = self._build(op)
43 42
        self._session.send(req)
44 43
        if reply_event is not None: # if we were provided an Event to use
45 44
            self._reply_event = reply_event
......
85 84
    def build_from_string(msgid, opstr, encoding='utf-8'):
86 85
        "TODO: docstring"
87 86
        decl = '<?xml version="1.0" encoding="%s"?>' % encoding
88
        doc = (u'''<rpc message-id="%s" xmlns="%s">%s</rpc>''' %
87
        doc = (u'<rpc message-id="%s" xmlns="%s">%s</rpc>' %
89 88
               (msgid, BASE_NS, opstr)).encode(encoding)
90
        return (decl + doc)
89
        return '%s%s' % (decl, doc)

Also available in: Unified diff