Revision c35cebbf

b/ncclient/transport/hello.py
21 21
from ncclient.content import qualify as _
22 22
from ncclient.content import unqualify as __
23 23

  
24
def build(capabilities, encoding='utf-8'):
25
    "Given a list of capability URI's returns encoded <hello> message"
26
    spec = {
27
        'tag': _('hello', BASE_NS),
28
        'children': [{
29
            'tag': 'capabilities',
30
            'children': # this is fun :-)
31
                [{ 'tag': 'capability', 'text': uri} for uri in capabilities]
32
            }]
33
        }
34
    return TreeBuilder(spec).to_string(encoding)
35

  
36
def parse(raw):
37
    "Returns tuple of (session-id, ['capability_uri', ...])"
38
    sid, capabilities = 0, []
39
    root = ET.fromstring(raw)
40
    for child in root.getchildren():
41
        if __(child.tag) == 'session-id':
42
            sid = child.text
43
        elif __(child.tag) == 'capabilities':
44
            for cap in child.getiterator(_('capability', BASE_NS)):
45
                capabilities.append(cap.text)
46
            # cisco doesn't namespace hello message
47
            for cap in child.getiterator('capability'): 
48
                capabilities.append(cap.text)
49
    return sid, capabilities
50

  
51

  
52
class HelloListener(Listener):
24
class HelloHandler(Listener):
53 25
    
54 26
    def __init__(self, init_cb, error_cb):
55 27
        self._init_cb, self._error_cb = init_cb, error_cb
......
68 40
    
69 41
    def errback(self, err):
70 42
        self._error_cb(err)
43
    
44
    @staticmethod
45
    def build(capabilities, encoding='utf-8'):
46
        "Given a list of capability URI's returns encoded <hello> message"
47
        spec = {
48
            'tag': _('hello', BASE_NS),
49
            'children': [{
50
                'tag': 'capabilities',
51
                'children': # this is fun :-)
52
                    [{ 'tag': 'capability', 'text': uri} for uri in capabilities]
53
                }]
54
            }
55
        return TreeBuilder(spec).to_string(encoding)
56
    
57
    @staticmethod
58
    def parse(raw):
59
        "Returns tuple of ('session-id', ['capability_uri', ...])"
60
        sid, capabilities = 0, []
61
        root = ET.fromstring(raw)
62
        for child in root.getchildren():
63
            if __(child.tag) == 'session-id':
64
                sid = child.text
65
            elif __(child.tag) == 'capabilities':
66
                for cap in child.getiterator(_('capability', BASE_NS)):
67
                    capabilities.append(cap.text)
68
                # cisco doesn't namespace hello message
69
                for cap in child.getiterator('capability'): 
70
                    capabilities.append(cap.text)
71
        return sid, capabilities
b/ncclient/transport/session.py
16 16

  
17 17
from ncclient.capabilities import Capabilities, CAPABILITIES
18 18
from ncclient.glue import Subject
19
from ncclient.transport import logger
20 19

  
21
import hello
20
from . import logger
21
from hello import HelloHandler
22 22

  
23 23
class Session(Thread, Subject):
24 24
    
......
36 36
    
37 37
    def _post_connect(self):
38 38
        "TODO: docstring"
39
        self.send(hello.build(self._client_capabilities))
39
        self.send(HelloHandler.build(self._client_capabilities))
40 40
        error = None
41 41
        init_event = Event()
42 42
        # callbacks
......
46 46
        def err_cb(err):
47 47
            error = err
48 48
            init_event.set()
49
        listener = hello.HelloListener(ok_cb, err_cb)
49
        listener = HelloHandler(ok_cb, err_cb)
50 50
        self.add_listener(listener)
51 51
        # start the subclass' main loop
52 52
        self.start()
b/ncclient/transport/ssh.py
21 21
import paramiko
22 22

  
23 23
from . import logger
24
from errors import SSHError, SSHUnknownHostError, SSHAuthenticationError, SessionCloseError
24
from errors import AuthenticationError, SessionCloseError, SSHError, SSHUnknownHostError
25 25
from session import Session
26 26

  
27 27
BUF_SIZE = 4096

Also available in: Unified diff