Statistics
| Branch: | Tag: | Revision:

root / ncclient / operations / lock.py @ b660fcda

History | View | Annotate | Download (2 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.xml_ import *
18

    
19
from rpc import RPC
20

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

    
25
class Lock(RPC):
26

    
27
    "*<lock>* RPC"
28
    
29
    def request(self, target):
30
        """
31
        :arg target: see :ref:`source_target`
32
        :type target: string
33

34
        :rtype: :ref:`return`
35
        """
36
        node = new_ele("lock")
37
        sub_ele(sub_ele(node, "target"), "running")
38
        return self._request(node)
39

    
40

    
41
class Unlock(RPC):
42

    
43
    "*<unlock>* RPC"
44
    
45
    def request(self, target):
46
        """
47
        :arg target: see :ref:`source_target`
48
        :type target: string
49

50
        :rtype: :ref:`return`
51
        """
52
        node = new_ele("unlock")
53
        sub_ele(sub_ele(node, "target"), "running")
54
        return self._request(node)
55

    
56

    
57
class LockContext:
58

    
59
    """
60
    A context manager for the :class:`Lock` / :class:`Unlock` pair of RPC's.
61
    
62
    RPC errors are always raised as exceptions.
63
    
64
    Initialise with session instance (:class:`Session
65
    <ncclient.transport.Session>`) and lock target (:ref:`source_target`)
66
    """
67

    
68
    def __init__(self, session, target):
69
        self.session = session
70
        self.target = target
71

    
72
    def __enter__(self):
73
        Lock(self.session).request(self.target)
74
        return self
75

    
76
    def __exit__(self, *args):
77
        Unlock(self.session).request(self.target)
78
        return False