Revision 9667bcb2 ncclient/transport/session.py

b/ncclient/transport/session.py
15 15
from Queue import Queue
16 16
from threading import Thread, Lock, Event
17 17

  
18
from ncclient import xml_
18
from ncclient.xml_ import *
19 19
from ncclient.capabilities import Capabilities
20 20

  
21 21
from errors import TransportError
......
30 30
    def __init__(self, capabilities):
31 31
        Thread.__init__(self)
32 32
        self.setDaemon(True)
33
        self._listeners = set() # 3.0's weakset would be ideal
33
        self._listeners = set()
34 34
        self._lock = Lock()
35 35
        self.setName('session')
36 36
        self._q = Queue()
......
43 43

  
44 44
    def _dispatch_message(self, raw):
45 45
        try:
46
            root = xml_.parse_root(raw)
46
            root = parse_root(raw)
47 47
        except Exception as e:
48 48
            logger.error('error parsing dispatch message: %s' % e)
49 49
            return
......
203 203
        self._error_cb = error_cb
204 204

  
205 205
    def callback(self, root, raw):
206
        if xml_.unqualify(root[0]) == 'hello':
206
        tag, attrs = root
207
        if tag == qualify("hello"):
207 208
            try:
208 209
                id, capabilities = HelloHandler.parse(raw)
209 210
            except Exception as e:
......
217 218
    @staticmethod
218 219
    def build(capabilities):
219 220
        "Given a list of capability URI's returns <hello> message XML string"
220
        spec = {
221
            'tag': 'hello',
222
            'attrib': {'xmlns': xml_.BASE_NS_1_0},
223
            'subtree': [{
224
                'tag': 'capabilities',
225
                'subtree': # this is fun :-)
226
                    [{'tag': 'capability', 'text': uri} for uri in capabilities]
227
                }]
228
            }
229
        return xml_.dtree2xml(spec)
221
        hello = new_ele("hello", xmlns=BASE_NS_1_0)
222
        caps = sub_ele(hello, "capabilities")
223
        def fun(uri): sub_ele(caps, "capability").text = uri
224
        map(fun, capabilities)
225
        return to_xml(hello)
230 226

  
231 227
    @staticmethod
232 228
    def parse(raw):
233 229
        "Returns tuple of (session-id (str), capabilities (Capabilities)"
234 230
        sid, capabilities = 0, []
235
        root = xml_.xml2ele(raw)
231
        root = to_ele(raw)
236 232
        for child in root.getchildren():
237
            tag = xml_.unqualify(child.tag)
238
            if tag == 'session-id':
233
            if child.tag == qualify("session-id"):
239 234
                sid = child.text
240
            elif tag == 'capabilities':
235
            elif child.tag == qualify("capabilities"):
241 236
                for cap in child.getchildren():
242
                    if xml_.unqualify(cap.tag) == 'capability':
237
                    if cap.tag == qualify("capability"):
243 238
                        capabilities.append(cap.text)
244 239
        return sid, Capabilities(capabilities)

Also available in: Unified diff