Revision 179b00d4 ncclient/operations/lock.py

b/ncclient/operations/lock.py
1
# Copyright 2009 Shikhar Bhushan
1
# Copyright 2h009 Shikhar Bhushan
2 2
#
3 3
# Licensed under the Apache License, Version 2.0 (the "License");
4 4
# you may not use this file except in compliance with the License.
......
16 16

  
17 17
from ncclient.rpc import RPC
18 18

  
19
import util
20

  
21 19
class Lock(RPC):
22 20
    
21
    # tested: no
22
    
23 23
    SPEC = {
24 24
        'tag': 'lock',
25
        'children': {
25
        'subtree': {
26 26
            'tag': 'target',
27
            'children': {'tag': None }
27
            'subtree': {'tag': None }
28 28
        }
29 29
    }
30 30
    
31
    def request(self, target='running'):
32
        if target=='candidate':
33
            self._assert(':candidate')
31
    def request(self, target):
34 32
        spec = Lock.SPEC.copy()
35
        spec['children']['children']['tag'] = target
33
        spec['subtree']['subtree']['tag'] = target
36 34
        return self._request(spec)
37 35

  
38 36

  
39 37
class Unlock(RPC):
40 38
    
39
    # tested: no
40
    
41 41
    SPEC = {
42 42
        'tag': 'unlock',
43
        'children': {
43
        'subtree': {
44 44
            'tag': 'target',
45
            'children': {'tag': None }
45
            'subtree': {'tag': None }
46 46
        }
47 47
    }
48 48
    
49
    def request(self, target='running'):
50
        if target=='candidate':
51
            self._assert(':candidate')
49
    def request(self, target):
52 50
        spec = Unlock.SPEC.copy()
53
        spec['children']['children']['tag'] = target
51
        spec['subtree']['subtree']['tag'] = target
54 52
        return self._request(spec)
55 53

  
56 54

  
57 55
class LockContext:
58
        
59
    def __init__(self, session, target='running'):
56
    
57
    # tested: no
58
    
59
    def __init__(self, session, target):
60 60
        self.session = session
61 61
        self.target = target
62
        
62
    
63 63
    def __enter__(self):
64
        Lock(self.session).request(self.target)
65
        return self
64
        reply = Lock(self.session).request(self.target)
65
        if not reply.ok:
66
            raise reply.error
67
        else:
68
            return self
66 69
    
67
    def __exit__(self, t, v, tb):
68
        Unlock(self.session).request(self.target)
70
    def __exit__(self, *args):
71
        reply = Unlock(session).request(self.target)
72
        if not reply.ok:
73
            raise reply.error
69 74
        return False

Also available in: Unified diff