a91fe659fa28dd724c4abe361e8ab411ef3d2c8b
[ncclient] / ncclient / rpc.py
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 import content
16
17 from threading import Event
18
19 from listener import RPCReplyListener
20
21 class RPC:
22     
23     current_id = {}
24     listeners = {}
25
26     def __init__(self, session=None, async=False):
27         self._session = None
28         self._async = None
29         self._reply = None
30         self._event = Event()
31     
32     def get_reply(self, timeout=2.0):
33         self._event.wait(timeout)
34         if self._event.isSet():
35             return self._reply
36     
37     def do(self, async=False):
38         self._async = async
39     
40     def _deliver(self, reply):
41         self._reply = reply
42         self._event.set()
43
44     @property
45     def has_reply(self):
46         return self._event.isSet()
47     
48     @property
49     def is_async(self):
50         return self._async
51     
52     @property
53     def listener(self):
54         if RPC.listeners[self._sid] is None:
55             RPC.listeners[self.sid] = listener.RPCReplyListener()
56         return RPC.listeners[self._sid]
57     
58     @property
59     def ok(self):
60         pass
61     
62     def _next_id(self):
63         RPC.current_id[self._session.id] = RPC.current_id.get(self._session.id, 0) + 1
64         return RPC.current_id[self._sid]
65     
66 class RPCReply:
67     
68     def __init__(self, id, raw):
69         self._id = id
70         self._raw = raw
71     
72     @property
73     def id(self):
74         return self._id
75     
76 class RPCError(NETCONFError):
77     pass