Revision dfdcbb0c ncclient/content/builders.py

b/ncclient/content/builders.py
17 17
from common import BASE_NS
18 18
from common import qualify as _
19 19

  
20
try:
21
    register_namespace = ET.register_namespace
22
except AttributeError:
23
    def register_namespace(prefix, uri):
24
        from xml.etree import ElementTree
25
        # cElementTree uses ElementTree's _namespace_map
26
        ElementTree._namespace_map[uri] = prefix
27

  
28
register_namespace('netconf', BASE_NS)
29

  
20 30
class TreeBuilder:
21 31
    '''Build an ElementTree.Element instance from an XML tree specification
22 32
    based on nested dictionaries.
......
26 36
        self._root = TreeBuilder.build(spec)
27 37
        
28 38
    def to_string(self, encoding='utf-8'):
29
        return ET.tostring(self._root, encoding)
39
        # etree does not include the <?xml..?> processing instruction
40
        # if encoding is utf-8. this is a problem with cisco routers.
41
        prepend = '<?xml version="1.0" encoding="UTF-8"?>' if encoding=='utf-8' else ''
42
        return prepend + ET.tostring(self._root, encoding)
30 43
    
31 44
    @property
32 45
    def tree(self):
......
38 51
        if spec.has_key('tag'):
39 52
            ele = ET.Element(spec.get('tag'), spec.get('attributes', {}))
40 53
            ele.text = spec.get('text', '')
41
            for child in spec.get('children', []):
54
            children = spec.get('children', [])
55
            if isinstance(children, dict):
56
                children = [children]
57
            for child in children:
42 58
                ele.append(TreeBuilder.build(child))
43 59
            return ele
44 60
        elif spec.has_key('comment'):
......
55 71
        spec = {
56 72
            'tag': _('hello', BASE_NS),
57 73
            'children': [{
58
                        'tag': 'capabilities',
74
                        'tag': u'capabilities',
59 75
                        'children': children
60 76
                        }]
61 77
            }
62 78
        return TreeBuilder(spec).to_string(encoding)
63 79

  
64

  
65 80
class RPCBuilder:
66 81
    
67 82
    @staticmethod
......
73 88
    
74 89
    @staticmethod
75 90
    def build_from_spec(msgid, opspec, encoding='utf-8'):
76
        if isinstance(opspec, dict):
77
            opspec = [opspec]
78 91
        spec = {
79 92
            'tag': _('rpc', BASE_NS),
80 93
            'attributes': {'message-id': msgid},

Also available in: Unified diff