Revision 19e7c7f6 ncclient/operations/lock.py

b/ncclient/operations/lock.py
12 12
# See the License for the specific language governing permissions and
13 13
# limitations under the License.
14 14

  
15
'Locking-related NETCONF operations'
15
"Locking-related NETCONF operations"
16 16

  
17 17
from ncclient.xml_ import *
18 18

  
19
from rpc import RPC
19
from rpc import RaiseMode, RPC
20 20

  
21
# TODO:
22
# should have some way to parse session-id from a lock-denied error, and raise
23
# a tailored exception
21
# TODO: parse session-id from a lock-denied error, and raise a tailored exception?
24 22

  
25 23
class Lock(RPC):
26 24

  
27
    "*<lock>* RPC"
25
    "`lock` RPC"
28 26
    
29 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
        """
30 32
        node = new_ele("lock")
31 33
        sub_ele(sub_ele(node, "target"), target)
32 34
        return self._request(node)
......
34 36

  
35 37
class Unlock(RPC):
36 38

  
37
    "*<unlock>* RPC"
39
    "`unlock` RPC"
38 40
    
39 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
        """
40 46
        node = new_ele("unlock")
41 47
        sub_ele(sub_ele(node, "target"), target)
42 48
        return self._request(node)
......
44 50

  
45 51
class LockContext:
46 52

  
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.
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.
54 58
    """
55 59

  
56 60
    def __init__(self, session, target):
......
58 62
        self.target = target
59 63

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

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

Also available in: Unified diff