Revision 9667bcb2 ncclient/operations/lock.py

b/ncclient/operations/lock.py
14 14

  
15 15
'Locking-related NETCONF operations'
16 16

  
17
from copy import deepcopy
17
from ncclient.xml_ import *
18 18

  
19 19
from rpc import RPC
20 20

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

  
24 25
class Lock(RPC):
25 26

  
26 27
    "*<lock>* RPC"
27

  
28
    SPEC = {
29
        'tag': 'lock',
30
        'subtree': {
31
            'tag': 'target',
32
            'subtree': {'tag': None }
33
        }
34
    }
35

  
36
    #REPLY_CLS = LockReply
37

  
28
    
38 29
    def request(self, target):
39 30
        """
40 31
        :arg target: see :ref:`source_target`
......
42 33

  
43 34
        :rtype: :ref:`return`
44 35
        """
45
        spec = deepcopy(Lock.SPEC)
46
        spec['subtree']['subtree']['tag'] = target
47
        return self._request(spec)
36
        node = new_ele("lock")
37
        sub_ele(sub_ele(node, "target"), "running")
38
        return self._request(node)
48 39

  
49 40

  
50 41
class Unlock(RPC):
51 42

  
52 43
    "*<unlock>* RPC"
53

  
54
    SPEC = {
55
        'tag': 'unlock',
56
        'subtree': {
57
            'tag': 'target',
58
            'subtree': {'tag': None }
59
        }
60
    }
61

  
44
    
62 45
    def request(self, target):
63 46
        """
64 47
        :arg target: see :ref:`source_target`
......
66 49

  
67 50
        :rtype: :ref:`return`
68 51
        """
69
        spec = deepcopy(Unlock.SPEC)
70
        spec['subtree']['subtree']['tag'] = target
71
        return self._request(spec)
52
        node = new_ele("lock")
53
        sub_ele(sub_ele(node, "target"), "running")
54
        return self._request(node)
72 55

  
73 56

  
74 57
class LockContext:
......
87 70
        self.target = target
88 71

  
89 72
    def __enter__(self):
90
        reply = Lock(self.session).request(self.target)
91
        if not reply.ok: # an error locking should definitely always be raised
92
            raise reply.error
93
        else:
94
            return self
73
        Lock(self.session).request(self.target)
74
        return self
95 75

  
96 76
    def __exit__(self, *args):
97
        reply = Unlock(self.session).request(self.target)
98
        if not reply.ok:
99
            raise reply.error
77
        Unlock(self.session).request(self.target)
100 78
        return False

Also available in: Unified diff