Statistics
| Branch: | Tag: | Revision:

root / ncclient / operations / lock.py @ 0cdb8b3c

History | View | Annotate | Download (1.8 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 rpc import RPC
18

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

    
37

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

    
56

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