root / ncclient / operations / lock.py @ 0b7d3b31
History | View | Annotate | Download (2.2 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 |
# TESTED
|
22 |
|
23 |
"*<lock>* RPC"
|
24 |
|
25 |
SPEC = { |
26 |
'tag': 'lock', |
27 |
'subtree': {
|
28 |
'tag': 'target', |
29 |
'subtree': {'tag': None } |
30 |
} |
31 |
} |
32 |
|
33 |
def request(self, target): |
34 |
"""
|
35 |
:arg target: see :ref:`source_target`
|
36 |
:type target: string
|
37 |
|
38 |
:rtype: :ref:`return`
|
39 |
"""
|
40 |
spec = Lock.SPEC.copy() |
41 |
spec['subtree']['subtree']['tag'] = target |
42 |
return self._request(spec) |
43 |
|
44 |
|
45 |
class Unlock(RPC): |
46 |
|
47 |
# TESTED
|
48 |
|
49 |
"*<unlock>* RPC"
|
50 |
|
51 |
SPEC = { |
52 |
'tag': 'unlock', |
53 |
'subtree': {
|
54 |
'tag': 'target', |
55 |
'subtree': {'tag': None } |
56 |
} |
57 |
} |
58 |
|
59 |
def request(self, target): |
60 |
"""
|
61 |
:arg target: see :ref:`source_target`
|
62 |
:type target: string
|
63 |
|
64 |
:rtype: :ref:`return`
|
65 |
"""
|
66 |
spec = Unlock.SPEC.copy() |
67 |
spec['subtree']['subtree']['tag'] = target |
68 |
return self._request(spec) |
69 |
|
70 |
|
71 |
class LockContext: |
72 |
|
73 |
# TESTED
|
74 |
|
75 |
"""
|
76 |
A context manager for the :class:`Lock` / :class:`Unlock` pair of RPC's.
|
77 |
|
78 |
Initialise with session instance (:class:`Session
|
79 |
<ncclient.transport.Session>`) and lock target (:ref:`source_target`)
|
80 |
"""
|
81 |
|
82 |
def __init__(self, session, target): |
83 |
self.session = session
|
84 |
self.target = target
|
85 |
|
86 |
def __enter__(self): |
87 |
reply = Lock(self.session).request(self.target) |
88 |
if not reply.ok: |
89 |
raise reply.error
|
90 |
else:
|
91 |
return self |
92 |
|
93 |
def __exit__(self, *args): |
94 |
reply = Unlock(self.session).request(self.target) |
95 |
if not reply.ok: |
96 |
raise reply.error
|
97 |
return False |