Revision c35cebbf ncclient/transport/hello.py

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

Also available in: Unified diff