Revision cc9af1c3 ncclient/content.py

b/ncclient/content.py
16 16

  
17 17
from xml.etree import cElementTree as ET
18 18

  
19
iselement = ET.iselement
20
element2string = ET.tostring
21 19

  
22 20
### Namespace-related ###
23 21

  
......
57 55
            if found is not None:
58 56
                break
59 57
    return found
60
    
58

  
61 59

  
62 60
### Build XML using Python data structures ###
63 61

  
......
70 68
        "TODO: docstring"
71 69
        self._root = XMLConverter.build(spec)
72 70
    
73
    def to_string(self, encoding='utf-8'):
71
    def tostring(self, encoding='utf-8'):
74 72
        "TODO: docstring"
75 73
        xml = ET.tostring(self._root, encoding)
76 74
        # some etree versions don't include xml decl with utf-8
......
90 88
            return spec
91 89
        elif isinstance(spec, basestring):
92 90
            return ET.XML(spec)
93
        ## assume isinstance(spec, dict)
91
        # assume isinstance(spec, dict)
94 92
        if 'tag' in spec:
95 93
            ele = ET.Element(spec.get('tag'), spec.get('attributes', {}))
96 94
            ele.text = spec.get('text', '')
......
104 102
            return ele
105 103
        elif 'comment' in spec:
106 104
            return ET.Comment(spec.get('comment'))
105
        # TODO elif DOM rep
107 106
        else:
108 107
            raise ContentError('Invalid tree spec')
109 108
    
110 109
    @staticmethod
111
    def from_string(xml):
110
    def fromstring(xml):
112 111
        return XMLConverter.parse(ET.fromstring(xml))
113 112
    
114 113
    @staticmethod
......
120 119
            'tail': root.tail,
121 120
            'subtree': [ XMLConverter.parse(child) for child in root.getchildren() ]
122 121
        }
122

  
123
## utility functions
124

  
125
iselement = ET.iselement
126

  
127
def isdom(x): return True # TODO
128

  
129
def root_ensured(rep, tag):
130
    if isinstance(rep, basestring):
131
        rep = ET.XML(rep)
132
    err = False
133
    if ((iselement(rep) and (rep.tag not in (tag, qualify(tag))) or (isdom(x)))): 
134
        raise ArgumentError("Expected root element [%s] not found" % tag)
135
    else:
136
        return rep

Also available in: Unified diff