Revision 35ad9d81 ncclient/content/hello.py
b/ncclient/content/hello.py | ||
---|---|---|
17 | 17 |
|
18 | 18 |
logging.getLogger('ncclient.content.hello') |
19 | 19 |
|
20 |
from . import NETCONF_NS |
|
21 |
from .util import qualify as _ |
|
20 | 22 |
from ..capability import Capabilities |
21 | 23 |
|
22 |
ns = 'urn:ietf:params:xml:ns:netconf:base:1.0' |
|
23 |
|
|
24 | 24 |
def make(capabilities): |
25 |
return '<hello xmlns="%s">%s</hello>' % (ns, capabilities)
|
|
25 |
return '<hello xmlns="%s">%s</hello>' % (NETCONF_NS, capabilities)
|
|
26 | 26 |
|
27 | 27 |
def parse(raw): |
28 | 28 |
id, capabilities = 0, Capabilities() |
29 |
hello = ElementTree.fromstring(raw) |
|
30 |
for child in hello.getchildren(): |
|
31 |
if child.tag == '{%s}session-id' % ns: |
|
32 |
id = child.text |
|
33 |
elif child.tag == '{%s}capabilities' % ns: |
|
34 |
for cap in child.getiterator('{%s}capability' % ns): |
|
35 |
capabilities.add(cap.text) |
|
29 |
root = ElementTree.fromstring(raw) |
|
30 |
if root.tag == _('hello'): |
|
31 |
for child in hello.getchildren(): |
|
32 |
if child.tag == _('session-id'): |
|
33 |
id = int(child.text) |
|
34 |
elif child.tag == _('capabilities'): |
|
35 |
for cap in child.getiterator(_('capability')): |
|
36 |
capabilities.add(cap.text) |
|
36 | 37 |
return id, capabilities |
37 |
|
|
38 |
#class HelloParser: |
|
39 |
# |
|
40 |
# 'Fast parsing with expat' |
|
41 |
# |
|
42 |
# capability, sid = range(2) |
|
43 |
# |
|
44 |
# def __init__(self, raw): |
|
45 |
# self._sid = None |
|
46 |
# self._capabilities = Capabilities() |
|
47 |
# p = xml.parsers.expat.ParserCreate() |
|
48 |
# p.StartElementHandler = self._start_element |
|
49 |
# p.EndElementHandler = self._end_element |
|
50 |
# p.CharacterDataHandler = self._char_data |
|
51 |
# self._expect = None |
|
52 |
# p.parse(raw, True) |
|
53 |
# |
|
54 |
# def _start_element(self, name, attrs): |
|
55 |
# if name == 'capability': |
|
56 |
# self._expect = HelloParser.capability |
|
57 |
# elif name == 'session-id': |
|
58 |
# self._expect = HelloParser.sid |
|
59 |
# |
|
60 |
# def _end_element(self, name): |
|
61 |
# self._expect = None |
|
62 |
# |
|
63 |
# def _char_data(self, data): |
|
64 |
# if self._expect == HelloParser.capability: |
|
65 |
# self._capabilities.add(data) |
|
66 |
# elif self._expect == HelloParser.sid: |
|
67 |
# self._sid = int(data) |
|
68 |
# |
|
69 |
# @property |
|
70 |
# def sid(self): return self._sid |
|
71 |
# |
|
72 |
# @property |
|
73 |
# def capabilities(self): return self._capabilities |
Also available in: Unified diff