Statistics
| Branch: | Tag: | Revision:

root / ncclient / operations / lock.py @ 179b00d4

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 ncclient.rpc import RPC
18

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

    
36

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

    
54

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