1 # Copyright 2009 Shikhar Bhushan
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
7 # http://www.apache.org/licenses/LICENSE-2.0
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.
15 from threading import Event, Lock
16 from uuid import uuid1
17 from weakref import WeakValueDictionary
19 from ncclient.content import XMLConverter
20 from ncclient.content import qualify as _
21 from ncclient.content import unqualify as __
22 from ncclient.glue import Listener
24 from listener import RPCReplyListener
25 from reply import RPCReply
28 logger = logging.getLogger('ncclient.rpc')
35 def __init__(self, session, async=False, timeout=None):
36 if not session.can_pipeline:
37 raise UserWarning('Asynchronous mode not supported for this device/session')
38 self._session = session
40 for cap in self.DEPENDS:
42 except AttributeError:
45 self._timeout = timeout
46 self._id = uuid1().urn
47 self._listener = RPCReplyListener(session)
48 self._listener.register(self._id, self)
50 self._reply_event = Event()
52 def _build(self, opspec, encoding='utf-8'):
56 'attributes': {'message-id': self._id},
59 return XMLConverter(spec).to_string(encoding)
61 def _request(self, op):
63 self._session.send(req)
65 return self._reply_event
67 self._reply_event.wait(self._timeout)
68 if self._reply_event.isSet():
72 raise ReplyTimeoutError
75 return self._request(self.SPEC)
77 def _delivery_hook(self):
81 def _assert(self, capability):
82 if capability not in self._session.server_capabilities:
83 raise MissingCapabilityError('Server does not support [%s]' % cap)
85 def deliver(self, raw):
86 self._reply = self.REPLY_CLS(raw)
88 self._reply_event.set()
92 return self._reply_event.isSet()
107 def reply_event(self):
108 return self._reply_event
110 def set_async(self, bool): self._async = bool
111 async = property(fget=lambda self: self._async, fset=set_async)
113 def set_timeout(self, timeout): self._timeout = timeout
114 timeout = property(fget=lambda self: self._timeout, fset=set_timeout)