root / ncclient / operations / lock.py @ dd8b8dd7
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 ncclient.xml_ import * |
18 |
|
19 |
from rpc import RPC |
20 |
|
21 |
# TODO:
|
22 |
# should have some way to parse session-id from a lock-denied error, and raise
|
23 |
# a tailored exception
|
24 |
|
25 |
class Lock(RPC): |
26 |
|
27 |
"*<lock>* RPC"
|
28 |
|
29 |
def request(self, target): |
30 |
node = new_ele("lock")
|
31 |
sub_ele(sub_ele(node, "target"), "running") |
32 |
return self._request(node) |
33 |
|
34 |
|
35 |
class Unlock(RPC): |
36 |
|
37 |
"*<unlock>* RPC"
|
38 |
|
39 |
def request(self, target): |
40 |
node = new_ele("unlock")
|
41 |
sub_ele(sub_ele(node, "target"), "running") |
42 |
return self._request(node) |
43 |
|
44 |
|
45 |
class LockContext: |
46 |
|
47 |
"""
|
48 |
A context manager for the :class:`Lock` / :class:`Unlock` pair of RPC's.
|
49 |
|
50 |
RPC errors are always raised as exceptions.
|
51 |
|
52 |
Initialise with (:class:`Session <ncclient.transport.Session>`) instance
|
53 |
and lock target.
|
54 |
"""
|
55 |
|
56 |
def __init__(self, session, target): |
57 |
self.session = session
|
58 |
self.target = target
|
59 |
|
60 |
def __enter__(self): |
61 |
Lock(self.session).request(self.target) |
62 |
return self |
63 |
|
64 |
def __exit__(self, *args): |
65 |
Unlock(self.session).request(self.target) |
66 |
return False |