Revision 9f49600d
b/ncclient/content.py | ||
---|---|---|
26 | 26 |
return '{%s}%s' % (ns, tag) |
27 | 27 |
_ = qualify |
28 | 28 |
|
29 |
################################################################################ |
|
30 |
|
|
31 |
class Hello: |
|
32 |
|
|
33 |
NS = 'urn:ietf:params:xml:ns:netconf:base:1.0' |
|
34 |
|
|
35 |
@staticmethod |
|
36 |
def build(capabilities, encoding='utf-8'): |
|
37 |
hello = ElementTree.Element(_('hello', Hello.NS)) |
|
38 |
caps = ElementTree.Element('capabilities') |
|
39 |
for uri in capabilities: |
|
40 |
cap = ElementTree.Element('capability') |
|
41 |
cap.text = uri |
|
42 |
caps.append(cap) |
|
43 |
hello.append(caps) |
|
44 |
tree = ElementTree.ElementTree(hello) |
|
45 |
fp = StringIO() |
|
46 |
tree.write(fp, encoding) |
|
47 |
return fp.getvalue() |
|
48 |
|
|
49 |
@staticmethod |
|
50 |
def parse(raw): |
|
51 |
'Returns tuple of (session-id, ["capability_uri", ...])' |
|
52 |
id, capabilities = 0, [] |
|
53 |
root = ElementTree.fromstring(raw) |
|
54 |
if root.tag == _('hello', Hello.NS): |
|
55 |
for child in root.getchildren(): |
|
56 |
if child.tag == _('session-id', Hello.NS): |
|
57 |
id = int(child.text) |
|
58 |
elif child.tag == _('capabilities', Hello.NS): |
|
59 |
for cap in child.getiterator(_('capability', Hello.NS)): |
|
60 |
capabilities.append(cap.text) |
|
61 |
return id, capabilities |
|
62 |
|
|
63 |
################################################################################ |
|
29 | 64 |
|
30 | 65 |
class RootElementParser: |
31 | 66 |
|
... | ... | |
62 | 97 |
return None |
63 | 98 |
|
64 | 99 |
|
65 |
########### |
|
100 |
################################################################################
|
|
66 | 101 |
|
67 | 102 |
class XMLBuilder: |
68 | 103 |
|
69 | 104 |
@staticmethod |
70 |
def _element(node): |
|
71 |
element = ElementTree.Element( _(node.get('tag'), |
|
72 |
node.get('namespace', None)), |
|
73 |
node.get('attributes', {})) |
|
74 |
if node.has_key('children'): |
|
75 |
for child in node['children']: |
|
76 |
element.append(_make_element(child)) |
|
77 |
else: |
|
78 |
return element |
|
105 |
def _element(spec): |
|
106 |
element = ElementTree.Element(spec['tag'], spec.get('attrib', {})) |
|
107 |
for child in spec.get('children', []): |
|
108 |
element.append(XMLBuilder._element(child)) |
|
109 |
return element |
|
79 | 110 |
|
80 | 111 |
@staticmethod |
81 |
def _etree(tree_dict):
|
|
82 |
return ElementTree.ElementTree(XMLBuilder._element(tree_dict))
|
|
112 |
def _etree(spec):
|
|
113 |
return ElementTree.ElementTree(XMLBuilder._element(spec))
|
|
83 | 114 |
|
84 | 115 |
@staticmethod |
85 |
def to_xml(tree_dict, encoding='utf-8'):
|
|
116 |
def build(spec, encoding='utf-8'):
|
|
86 | 117 |
fp = StringIO() |
87 |
self._etree(tree_dict).write(fp, encoding)
|
|
118 |
XMLBuilder._etree(spec).write(fp, encoding)
|
|
88 | 119 |
return fp.get_value() |
89 |
|
|
90 |
|
|
91 |
### Hello exchange |
|
92 |
|
|
93 |
class Hello: |
|
94 |
|
|
95 |
NS = 'urn:ietf:params:xml:ns:netconf:base:1.0' |
|
96 |
|
|
97 |
@staticmethod |
|
98 |
def build(capabilities, encoding='utf-8'): |
|
99 |
hello = ElementTree.Element(_('hello', Hello.NS)) |
|
100 |
caps = ElementTree.Element('capabilities') |
|
101 |
for uri in capabilities: |
|
102 |
cap = ElementTree.Element('capability') |
|
103 |
cap.text = uri |
|
104 |
caps.append(cap) |
|
105 |
hello.append(caps) |
|
106 |
tree = ElementTree.ElementTree(hello) |
|
107 |
fp = StringIO() |
|
108 |
tree.write(fp, encoding) |
|
109 |
return fp.getvalue() |
|
110 |
|
|
111 |
@staticmethod |
|
112 |
def parse(raw): |
|
113 |
'Returns tuple of (session-id, ["capability_uri", ...])' |
|
114 |
id, capabilities = 0, [] |
|
115 |
root = ElementTree.fromstring(raw) |
|
116 |
if root.tag == _('hello', Hello.NS): |
|
117 |
for child in root.getchildren(): |
|
118 |
if child.tag == _('session-id', Hello.NS): |
|
119 |
id = int(child.text) |
|
120 |
elif child.tag == _('capabilities', Hello.NS): |
|
121 |
for cap in child.getiterator(_('capability', Hello.NS)): |
|
122 |
capabilities.append(cap.text) |
|
123 |
return id, capabilities |
Also available in: Unified diff