debug log received data as well
[ncclient] / ncclient / transport / session.py
index de1b8d9..570fd47 100644 (file)
@@ -15,7 +15,7 @@
 from Queue import Queue
 from threading import Thread, Lock, Event
 
-from ncclient import content
+from ncclient.xml_ import *
 from ncclient.capabilities import Capabilities
 
 from errors import TransportError
@@ -30,7 +30,7 @@ class Session(Thread):
     def __init__(self, capabilities):
         Thread.__init__(self)
         self.setDaemon(True)
-        self._listeners = set() # 3.0's weakset would be ideal
+        self._listeners = set()
         self._lock = Lock()
         self.setName('session')
         self._q = Queue()
@@ -43,26 +43,23 @@ class Session(Thread):
 
     def _dispatch_message(self, raw):
         try:
-            root = content.parse_root(raw)
+            root = parse_root(raw)
         except Exception as e:
             logger.error('error parsing dispatch message: %s' % e)
             return
         with self._lock:
             listeners = list(self._listeners)
         for l in listeners:
-            logger.debug('dispatching message to %r' % l)
-            try:
-                l.callback(root, raw)
-            except Exception as e:
-                logger.warning('[error] %r' % e)
-
+            logger.debug('dispatching message to %r: %s' % (l, raw))
+            l.callback(root, raw) # no try-except; fail loudly if you must!
+    
     def _dispatch_error(self, err):
         with self._lock:
             listeners = list(self._listeners)
         for l in listeners:
             logger.debug('dispatching error to %r' % l)
-            try:
-                l.errback(err)
+            try: # here we can be more considerate with catching exceptions
+                l.errback(err) 
             except Exception as e:
                 logger.warning('error dispatching to %r: %r' % (l, e))
 
@@ -168,11 +165,6 @@ class Session(Thread):
         been initialized it will be :const:`None`"""
         return self._id
 
-    @property
-    def can_pipeline(self):
-        "Whether this session supports pipelining"
-        return True
-
 
 class SessionListener(object):
 
@@ -211,7 +203,8 @@ class HelloHandler(SessionListener):
         self._error_cb = error_cb
 
     def callback(self, root, raw):
-        if content.unqualify(root[0]) == 'hello':
+        tag, attrs = root
+        if tag == qualify("hello"):
             try:
                 id, capabilities = HelloHandler.parse(raw)
             except Exception as e:
@@ -225,28 +218,22 @@ class HelloHandler(SessionListener):
     @staticmethod
     def build(capabilities):
         "Given a list of capability URI's returns <hello> message XML string"
-        spec = {
-            'tag': 'hello',
-            'attrib': {'xmlns': content.BASE_NS},
-            'subtree': [{
-                'tag': 'capabilities',
-                'subtree': # this is fun :-)
-                    [{'tag': 'capability', 'text': uri} for uri in capabilities]
-                }]
-            }
-        return content.dtree2xml(spec)
+        hello = new_ele("hello", xmlns=BASE_NS_1_0)
+        caps = sub_ele(hello, "capabilities")
+        def fun(uri): sub_ele(caps, "capability").text = uri
+        map(fun, capabilities)
+        return to_xml(hello)
 
     @staticmethod
     def parse(raw):
         "Returns tuple of (session-id (str), capabilities (Capabilities)"
         sid, capabilities = 0, []
-        root = content.xml2ele(raw)
+        root = to_ele(raw)
         for child in root.getchildren():
-            tag = content.unqualify(child.tag)
-            if tag == 'session-id':
+            if child.tag == qualify("session-id"):
                 sid = child.text
-            elif tag == 'capabilities':
+            elif child.tag == qualify("capabilities"):
                 for cap in child.getchildren():
-                    if content.unqualify(cap.tag) == 'capability':
+                    if cap.tag == qualify("capability"):
                         capabilities.append(cap.text)
         return sid, Capabilities(capabilities)