more doc updates
[ncclient] / ncclient / operations / lock.py
index aa454d0..13f5bdb 100644 (file)
 # See the License for the specific language governing permissions and
 # limitations under the License.
 
-'Locking-related NETCONF operations'
+"Locking-related NETCONF operations"
 
-from rpc import RPC
+from ncclient.xml_ import *
 
-class Lock(RPC):
+from rpc import RaiseMode, RPC
 
-    "*<lock>* RPC"
+# TODO: parse session-id from a lock-denied error, and raise a tailored exception?
 
-    SPEC = {
-        'tag': 'lock',
-        'subtree': {
-            'tag': 'target',
-            'subtree': {'tag': None }
-        }
-    }
+class Lock(RPC):
 
+    "`lock` RPC"
+    
     def request(self, target):
-        """
-        :arg target: see :ref:`source_target`
-        :type target: string
+        """Allows the client to lock the configuration system of a device.
 
-        :rtype: :ref:`return`
+        *target* is the name of the configuration datastore to lock
         """
-        spec = Lock.SPEC.copy()
-        spec['subtree']['subtree']['tag'] = target
-        return self._request(spec)
+        node = new_ele("lock")
+        sub_ele(sub_ele(node, "target"), target)
+        return self._request(node)
 
 
 class Unlock(RPC):
 
-    "*<unlock>* RPC"
-
-    SPEC = {
-        'tag': 'unlock',
-        'subtree': {
-            'tag': 'target',
-            'subtree': {'tag': None }
-        }
-    }
-
+    "`unlock` RPC"
+    
     def request(self, target):
-        """
-        :arg target: see :ref:`source_target`
-        :type target: string
+        """Release a configuration lock, previously obtained with the lock operation.
 
-        :rtype: :ref:`return`
+        *target* is the name of the configuration datastore to unlock
         """
-        spec = Unlock.SPEC.copy()
-        spec['subtree']['subtree']['tag'] = target
-        return self._request(spec)
+        node = new_ele("unlock")
+        sub_ele(sub_ele(node, "target"), target)
+        return self._request(node)
 
 
 class LockContext:
 
-    """
-    A context manager for the :class:`Lock` / :class:`Unlock` pair of RPC's.
+    """A context manager for the :class:`Lock` / :class:`Unlock` pair of RPC's.
+
+    Any `rpc-error` will be raised as an exception.
 
-    Initialise with session instance (:class:`Session
-    <ncclient.transport.Session>`) and lock target (:ref:`source_target`)
+    Initialise with (:class:`Session <ncclient.transport.Session>`) instance and lock target.
     """
 
     def __init__(self, session, target):
@@ -78,14 +62,9 @@ class LockContext:
         self.target = target
 
     def __enter__(self):
-        reply = Lock(self.session).request(self.target)
-        if not reply.ok:
-            raise reply.error
-        else:
-            return self
+        Lock(self.session, raise_mode=RaiseMode.ERRORS).request(self.target)
+        return self
 
     def __exit__(self, *args):
-        reply = Unlock(session).request(self.target)
-        if not reply.ok:
-            raise reply.error
+        Unlock(self.session, raise_mode=RaiseMode.ERRORS).request(self.target)
         return False