no need for weakvaluedict in RPCReplyListener, after all
[ncclient] / ncclient / operations / lock.py
1 # Copyright 2h009 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 'Locking-related NETCONF operations'
16
17 from copy import deepcopy
18
19 from rpc import RPC, RPCReply, RPCError
20
21 #class LockReply(RPCReply):
22 #
23 #    ERROR_CLS = LockDeniedError
24 #
25 #class LockDeniedError(RPCError):
26 #
27 #    def __new__(cls, err_dict):
28 #        if rpcerr['tag'] != 'lock-denied':
29 #            return RPCError(err_dict)
30 #        else:
31 #            return object.__new__(LockDeniedError)
32 #
33 #    def __init__(self, err_dict):
34 #        RPCError.__init__(self, err_dict)
35
36 class Lock(RPC):
37
38     # TESTED
39
40     "*<lock>* RPC"
41
42     SPEC = {
43         'tag': 'lock',
44         'subtree': {
45             'tag': 'target',
46             'subtree': {'tag': None }
47         }
48     }
49
50     #REPLY_CLS = LockReply
51
52     def request(self, target):
53         """
54         :arg target: see :ref:`source_target`
55         :type target: string
56
57         :rtype: :ref:`return`
58         """
59         spec = deepcopy(Lock.SPEC)
60         spec['subtree']['subtree']['tag'] = target
61         return self._request(spec)
62
63
64 class Unlock(RPC):
65
66     # TESTED
67
68     "*<unlock>* RPC"
69
70     SPEC = {
71         'tag': 'unlock',
72         'subtree': {
73             'tag': 'target',
74             'subtree': {'tag': None }
75         }
76     }
77
78     def request(self, target):
79         """
80         :arg target: see :ref:`source_target`
81         :type target: string
82
83         :rtype: :ref:`return`
84         """
85         spec = deepcopy(Unlock.SPEC)
86         spec['subtree']['subtree']['tag'] = target
87         return self._request(spec)
88
89
90 class LockContext:
91
92     # TESTED
93
94     """
95     A context manager for the :class:`Lock` / :class:`Unlock` pair of RPC's.
96
97     Initialise with session instance (:class:`Session
98     <ncclient.transport.Session>`) and lock target (:ref:`source_target`)
99     """
100
101     def __init__(self, session, target):
102         self.session = session
103         self.target = target
104
105     def __enter__(self):
106         reply = Lock(self.session).request(self.target)
107         if not reply.ok:
108             raise reply.error
109         else:
110             return self
111
112     def __exit__(self, *args):
113         reply = Unlock(self.session).request(self.target)
114         if not reply.ok:
115             raise reply.error
116         return False