Statistics
| Branch: | Tag: | Revision:

root / ncclient / operations / lock.py @ 6e571704

History | View | Annotate | Download (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 ncclient.xml_ import *
18

    
19
from rpc import RaiseMode, RPC
20

    
21
# TODO: parse session-id from a lock-denied error, and raise a tailored exception?
22

    
23
class Lock(RPC):
24

    
25
    "`lock` RPC"
26
    
27
    def request(self, target):
28
        """Allows the client to lock the configuration system of a device.
29

30
        *target* is the name of the configuration datastore to lock
31
        """
32
        node = new_ele("lock")
33
        sub_ele(sub_ele(node, "target"), target)
34
        return self._request(node)
35

    
36

    
37
class Unlock(RPC):
38

    
39
    "`unlock` RPC"
40
    
41
    def request(self, target):
42
        """Release a configuration lock, previously obtained with the lock operation.
43

44
        *target* is the name of the configuration datastore to unlock
45
        """
46
        node = new_ele("unlock")
47
        sub_ele(sub_ele(node, "target"), target)
48
        return self._request(node)
49

    
50

    
51
class LockContext:
52

    
53
    """A context manager for the :class:`Lock` / :class:`Unlock` pair of RPC's.
54

55
    Any `rpc-error` will be raised as an exception.
56

57
    Initialise with (:class:`Session <ncclient.transport.Session>`) instance and lock target.
58
    """
59

    
60
    def __init__(self, session, target):
61
        self.session = session
62
        self.target = target
63

    
64
    def __enter__(self):
65
        Lock(self.session, raise_mode=RaiseMode.ERRORS).request(self.target)
66
        return self
67

    
68
    def __exit__(self, *args):
69
        Unlock(self.session, raise_mode=RaiseMode.ERRORS).request(self.target)
70
        return False