commit 11d9e64201da5ef3ddac47619a6b3260f4ae4a1f
Author: Shikhar Bhushan <shikhar@schmizz.net>
Date:   Tue Apr 28 14:16:00 2009 +0000

    current state of ops, rpc --- really need to get this straightened out
    
    git-svn-id: http://ncclient.googlecode.com/svn/trunk@80 6dbcf712-26ac-11de-a2f3-1373824ab735

diff --git a/ncclient/operations/rpc.py b/ncclient/operations/rpc.py
index ea50f53..dc65b10 100644
--- a/ncclient/operations/rpc.py
+++ b/ncclient/operations/rpc.py
@@ -14,32 +14,24 @@
 
 from threading import Event, Lock
 from uuid import uuid1
-from weakref import WeakKeyDictionary
 
 from . import logger
-from listener import SessionListener
-from ncclient.content.builders import RPCBuilder
-from ncclient.content.parsers import RPCReplyParser
-
-_listeners = WeakKeyDictionary()
-_lock = Lock()
-
-def get_listener(session):
-    with self._lock:
-        return _listeners.setdefault(session, ReplyListener())
 
 class RPC:
         
-    def __init__(self, session):
+    def __init__(self, session, async=False):
         self._session = session
-        self._id = None
-        self._reply = None # RPCReply
-        self._reply_event = None
+        self._id = uuid1().urn
+        self._reply = RPCReply()
+        self._reply_event = Event()
     
-    @property
+    def _build(self, op, encoding='utf-8'):
+        if isinstance(op, basestring):
+            return RPCBuilder.build_from_string(self._id, op, encoding)
+        else:
+            return RPCBuilder.build_from_spec(self._id, op, encoding)
     
     def _request(self, op):
-        self._id = uuid1().urn
         self._reply = RPCReply()
         # get the listener instance for this session
         # <rpc-reply> with message id will reach response_cb
@@ -78,128 +70,21 @@ class RPC:
     @property
     def session(self):
         return self._session
-
-class RPCReply:
-    
-    def __init__(self, event):
-        self._delivery_event = event
-        self._raw = None
-        self._errs = None
-    
-    def __str__(self):
-        return self._raw
-    
-    def parse(self):
-        if not self._parsed:
-            errs = RPCReplyParser.parse(self._raw)
-            for raw, err_dict in errs:
-                self._errs.append(RPCError(raw, err_dict))
-            self._parsed = True
-    
-    def deliver(self, raw):
-        self._raw = raw
-        self._delivery_event.set()
-    
-    def received(self, timeout=None):
-        self._delivery_event.wait(timeout)
-        return True
-    
-    @property
-    def raw(self):
-        return self._raw
-    
-    @property
-    def parsed(self):
-        return self._parsed
-    
-    @property
-    def ok(self):
-        return True if self._parsed and not self._errs else False
-    
-    @property
-    def errors(self):
-        return self._errs
-
-class RPCError(Exception): # raise it if you like
-    
-    def __init__(self, raw, err_dict):
-        self._raw = raw
-        self._dict = err_dict
-
-    def __str__(self):
-        # TODO
-        return self._raw
-    
-    def __dict__(self):
-        return self._dict
-    
-    @property
-    def raw(self):
-        return self._raw
     
-    @property
-    def type(self):
-        return self._dict.get('type', None)
-    
-    @property
-    def severity(self):
-        return self._dict.get('severity', None)
-    
-    @property
-    def tag(self):
-        return self._dict.get('tag', None)
-    
-    @property
-    def path(self):
-        return self._dict.get('path', None)
-    
-    @property
-    def message(self):
-        return self._dict.get('message', None)
-    
-    @property
-    def info(self):
-        return self._dict.get('info', None)
-
-class Notification:
-    
-    pass
-
-
-
-from builder import TreeBuilder
-from common import BASE_NS
-from common import qualify as _
-
-################################################################################
-
-_ = qualify
-
-def build(msgid, op, encoding='utf-8'):
-    "TODO: docstring"
-    if isinstance(op, basestring):
-        return RPCBuilder.build_from_string(msgid, op, encoding)
-    else:
-        return RPCBuilder.build_from_spec(msgid, op, encoding)
-
-def build_from_spec(msgid, opspec, encoding='utf-8'):
-    "TODO: docstring"
-    spec = {
-        'tag': _('rpc', BASE_NS),
-        'attributes': {'message-id': msgid},
-        'children': opspec
-        }
-    return TreeBuilder(spec).to_string(encoding)
-
-def build_from_string(msgid, opstr, encoding='utf-8'):
-    "TODO: docstring"
-    decl = '<?xml version="1.0" encoding="%s"?>' % encoding
-    doc = (u'''<rpc message-id="%s" xmlns="%s">%s</rpc>''' %
-           (msgid, BASE_NS, opstr)).encode(encoding)
-    return (decl + doc)
-
-################################################################################
-
-# parsing stuff TODO
-
-
+    @staticmethod
+    def build_from_spec(msgid, opspec, encoding='utf-8'):
+        "TODO: docstring"
+        spec = {
+            'tag': _('rpc', BASE_NS),
+            'attributes': {'message-id': msgid},
+            'children': opspec
+            }
+        return TreeBuilder(spec).to_string(encoding)
+    
+    @staticmethod
+    def build_from_string(msgid, opstr, encoding='utf-8'):
+        "TODO: docstring"
+        decl = '<?xml version="1.0" encoding="%s"?>' % encoding
+        doc = (u'''<rpc message-id="%s" xmlns="%s">%s</rpc>''' %
+               (msgid, BASE_NS, opstr)).encode(encoding)
+        return (decl + doc)
