root / ncclient / operations / lock.py @ a7cb58ce
History | View | Annotate | Download (1.7 kB)
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 rpc import RPC |
18 |
|
19 |
class Lock(RPC): |
20 |
|
21 |
SPEC = { |
22 |
'tag': 'lock', |
23 |
'subtree': {
|
24 |
'tag': 'target', |
25 |
'subtree': {'tag': None } |
26 |
} |
27 |
} |
28 |
|
29 |
def request(self, target, *args, **kwds): |
30 |
spec = Lock.SPEC.copy() |
31 |
spec['subtree']['subtree']['tag'] = target |
32 |
return self._request(spec, *args, **kwds) |
33 |
|
34 |
|
35 |
class Unlock(RPC): |
36 |
|
37 |
SPEC = { |
38 |
'tag': 'unlock', |
39 |
'subtree': {
|
40 |
'tag': 'target', |
41 |
'subtree': {'tag': None } |
42 |
} |
43 |
} |
44 |
|
45 |
def request(self, target, *args, **kwds): |
46 |
spec = Unlock.SPEC.copy() |
47 |
spec['subtree']['subtree']['tag'] = target |
48 |
return self._request(spec, *args, **kwds) |
49 |
|
50 |
|
51 |
class LockContext: |
52 |
|
53 |
def __init__(self, session, target): |
54 |
self.session = session
|
55 |
self.target = target
|
56 |
|
57 |
def __enter__(self): |
58 |
reply = Lock(self.session).request(self.target) |
59 |
if not reply.ok: |
60 |
raise reply.error
|
61 |
else:
|
62 |
return self |
63 |
|
64 |
def __exit__(self, *args): |
65 |
reply = Unlock(session).request(self.target)
|
66 |
if not reply.ok: |
67 |
raise reply.error
|
68 |
return False |