commit 4de03d63164babbe86975ae71dd5335bbcbaf817
Author: Shikhar Bhushan <shikhar@schmizz.net>
Date:   Tue May 12 01:58:40 2009 +0000

    git-svn-id: http://ncclient.googlecode.com/svn/trunk@110 6dbcf712-26ac-11de-a2f3-1373824ab735

diff --git a/ncclient/operations/rpc.py b/ncclient/operations/rpc.py
index 499902f..1e8bfd3 100644
--- a/ncclient/operations/rpc.py
+++ b/ncclient/operations/rpc.py
@@ -23,93 +23,6 @@ from errors import OperationError
 import logging
 logger = logging.getLogger('ncclient.rpc')
 
-class RPC(object):
-    
-    DEPENDS = []
-    REPLY_CLS = RPCReply
-    
-    def __init__(self, session, async=False, timeout=None):
-        if not session.can_pipeline:
-            raise UserWarning('Asynchronous mode not supported for this device/session')
-        self._session = session
-        try:
-            for cap in self.DEPENDS:
-                self._assert(cap)
-        except AttributeError:
-            pass        
-        self._async = async
-        self._timeout = timeout
-        # keeps things simple instead of having a class attr that has to be locked
-        self._id = uuid1().urn
-        self._listener = RPCReplyListener(session)
-        self._listener.register(self._id, self)
-        self._reply = None
-        self._reply_event = Event()
-    
-    def _build(self, opspec, encoding='utf-8'):
-        "TODO: docstring"
-        spec = {
-            'tag': content.qualify('rpc'),
-            'attributes': {'message-id': self._id},
-            'subtree': opspec
-            }
-        return content.dtree2xml(encoding)
-    
-    def _request(self, op):
-        req = self._build(op)
-        self._session.send(req)
-        if self._async:
-            return self._reply_event
-        else:
-            self._reply_event.wait(self._timeout)
-            if self._reply_event.isSet():
-                self._reply.parse()
-                return self._reply
-            else:
-                raise ReplyTimeoutError
-    
-    def request(self):
-        return self._request(self.SPEC)
-    
-    def _delivery_hook(self):
-        'For subclasses'
-        pass
-    
-    def _assert(self, capability):
-        if capability not in self._session.server_capabilities:
-            raise MissingCapabilityError('Server does not support [%s]' % cap)
-    
-    def deliver(self, raw):
-        self._reply = self.REPLY_CLS(raw)
-        self._delivery_hook()
-        self._reply_event.set()
-    
-    @property
-    def has_reply(self):
-        return self._reply_event.isSet()
-    
-    @property
-    def reply(self):
-        return self._reply
-    
-    @property
-    def id(self):
-        return self._id
-    
-    @property
-    def session(self):
-        return self._session
-    
-    @property
-    def reply_event(self):
-        return self._reply_event
-    
-    def set_async(self, bool): self._async = bool
-    async = property(fget=lambda self: self._async, fset=set_async)
-    
-    def set_timeout(self, timeout): self._timeout = timeout
-    timeout = property(fget=lambda self: self._timeout, fset=set_timeout)
-
 
 class RPCReply:
     
@@ -235,7 +148,7 @@ class RPCError(OperationError): # raise it if you like
     __repr__ = lambda self: repr(self._dict)
 
 
-class RPCReplyListener:
+class RPCReplyListener(object):
     
     # one instance per session
     def __new__(cls, session):
@@ -286,3 +199,92 @@ class RPCReplyListener:
     def errback(self, err):
         if self._errback is not None:
             self._errback(err)
+
+
+class RPC(object):
+    
+    DEPENDS = []
+    REPLY_CLS = RPCReply
+    
+    def __init__(self, session, async=False, timeout=None):
+        if not session.can_pipeline:
+            raise UserWarning('Asynchronous mode not supported for this device/session')
+        self._session = session
+        try:
+            for cap in self.DEPENDS:
+                self._assert(cap)
+        except AttributeError:
+            pass        
+        self._async = async
+        self._timeout = timeout
+        # keeps things simple instead of having a class attr that has to be locked
+        self._id = uuid1().urn
+        # RPCReplyListener itself makes sure there isn't more than one instance -- i.e. multiton
+        self._listener = RPCReplyListener(session)
+        self._listener.register(self._id, self)
+        self._reply = None
+        self._reply_event = Event()
+    
+    def _build(self, opspec, encoding='utf-8'):
+        "TODO: docstring"
+        spec = {
+            'tag': content.qualify('rpc'),
+            'attributes': {'message-id': self._id},
+            'subtree': opspec
+            }
+        return content.dtree2xml(encoding)
+    
+    def _request(self, op):
+        req = self._build(op)
+        self._session.send(req)
+        if self._async:
+            return self._reply_event
+        else:
+            self._reply_event.wait(self._timeout)
+            if self._reply_event.isSet():
+                self._reply.parse()
+                return self._reply
+            else:
+                raise ReplyTimeoutError
+    
+    def request(self):
+        return self._request(self.SPEC)
+    
+    def _delivery_hook(self):
+        'For subclasses'
+        pass
+    
+    def _assert(self, capability):
+        if capability not in self._session.server_capabilities:
+            raise MissingCapabilityError('Server does not support [%s]' % cap)
+    
+    def deliver(self, raw):
+        self._reply = self.REPLY_CLS(raw)
+        self._delivery_hook()
+        self._reply_event.set()
+    
+    @property
+    def has_reply(self):
+        return self._reply_event.isSet()
+    
+    @property
+    def reply(self):
+        return self._reply
+    
+    @property
+    def id(self):
+        return self._id
+    
+    @property
+    def session(self):
+        return self._session
+    
+    @property
+    def reply_event(self):
+        return self._reply_event
+    
+    def set_async(self, bool): self._async = bool
+    async = property(fget=lambda self: self._async, fset=set_async)
+    
+    def set_timeout(self, timeout): self._timeout = timeout
+    timeout = property(fget=lambda self: self._timeout, fset=set_timeout)
