Statistics
| Branch: | Tag: | Revision:

root / ncclient / operations / lock.py @ ce5fb329

History | View | Annotate | Download (2.3 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 copy import deepcopy
18

    
19
from rpc import RPC
20

    
21
class Lock(RPC):
22

    
23
    # TESTED
24

    
25
    "*<lock>* RPC"
26

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

    
35
    def request(self, target):
36
        """
37
        :arg target: see :ref:`source_target`
38
        :type target: string
39

40
        :rtype: :ref:`return`
41
        """
42
        spec = deepcopy(Lock.SPEC)
43
        spec['subtree']['subtree']['tag'] = target
44
        return self._request(spec)
45

    
46

    
47
class Unlock(RPC):
48

    
49
    # TESTED
50

    
51
    "*<unlock>* RPC"
52

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

    
61
    def request(self, target):
62
        """
63
        :arg target: see :ref:`source_target`
64
        :type target: string
65

66
        :rtype: :ref:`return`
67
        """
68
        spec = deepcopy(Unlock.SPEC)
69
        spec['subtree']['subtree']['tag'] = target
70
        return self._request(spec)
71

    
72

    
73
class LockContext:
74

    
75
    # TESTED
76

    
77
    """
78
    A context manager for the :class:`Lock` / :class:`Unlock` pair of RPC's.
79

80
    Initialise with session instance (:class:`Session
81
    <ncclient.transport.Session>`) and lock target (:ref:`source_target`)
82
    """
83

    
84
    def __init__(self, session, target):
85
        self.session = session
86
        self.target = target
87

    
88
    def __enter__(self):
89
        reply = Lock(self.session).request(self.target)
90
        if not reply.ok:
91
            raise reply.error
92
        else:
93
            return self
94

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