paulb@18 | 1 | #!/usr/bin/env python |
paulb@18 | 2 | |
paulb@18 | 3 | """ |
paulb@75 | 4 | DOM wrapper around libxml2, specifically the libxml2mod Python extension module. |
paulb@18 | 5 | """ |
paulb@18 | 6 | |
paulb@72 | 7 | __version__ = "0.2" |
paulb@27 | 8 | |
paulb@18 | 9 | import libxml2 |
paulb@72 | 10 | from libxml2dom.macrolib import * |
paulb@72 | 11 | from libxml2dom.macrolib import \ |
paulb@72 | 12 | createDocument as Node_createDocument, \ |
paulb@72 | 13 | parseString as Node_parseString, parseURI as Node_parseURI, \ |
paulb@72 | 14 | parseFile as Node_parseFile, \ |
paulb@72 | 15 | toString as Node_toString, toStream as Node_toStream, \ |
paulb@72 | 16 | toFile as Node_toFile |
paulb@75 | 17 | import weakref |
paulb@18 | 18 | |
paulb@18 | 19 | # NOTE: Consider a generator instead. |
paulb@18 | 20 | |
paulb@18 | 21 | class NamedNodeMap(object): |
paulb@18 | 22 | |
paulb@75 | 23 | """ |
paulb@75 | 24 | A wrapper around Node objects providing DOM and dictionary convenience |
paulb@75 | 25 | methods. |
paulb@75 | 26 | """ |
paulb@75 | 27 | |
paulb@18 | 28 | def __init__(self, node): |
paulb@18 | 29 | self.node = node |
paulb@18 | 30 | |
paulb@18 | 31 | def getNamedItem(self, name): |
paulb@72 | 32 | return self.node.getAttributeNode(name) |
paulb@18 | 33 | |
paulb@18 | 34 | def getNamedItemNS(self, ns, localName): |
paulb@72 | 35 | return self.node.getAttributeNodeNS(ns, localName) |
paulb@18 | 36 | |
paulb@18 | 37 | def setNamedItem(self, node): |
paulb@18 | 38 | self.node.setAttributeNode(node.name, node) |
paulb@18 | 39 | |
paulb@18 | 40 | def setNamedItemNS(self, node): |
paulb@18 | 41 | self.node.setAttributeNodeNS(node.namespaceURI, node.localName, node) |
paulb@18 | 42 | |
paulb@18 | 43 | def __getitem__(self, name): |
paulb@18 | 44 | return self.getNamedItem(name) |
paulb@18 | 45 | |
paulb@18 | 46 | def __setitem__(self, name, node): |
paulb@18 | 47 | if name == node.nodeName: |
paulb@18 | 48 | self.setNamedItem(node) |
paulb@18 | 49 | else: |
paulb@18 | 50 | raise KeyError, name |
paulb@18 | 51 | |
paulb@18 | 52 | def __delitem__(self, name): |
paulb@18 | 53 | # NOTE: To be implemented. |
paulb@18 | 54 | pass |
paulb@18 | 55 | |
paulb@18 | 56 | def values(self): |
paulb@75 | 57 | return [Node(_node, self.ownerDocument) for _node in Node_attributes(self.node.as_native_node()).values()] |
paulb@18 | 58 | |
paulb@18 | 59 | def keys(self): |
paulb@18 | 60 | return [(attr.namespaceURI, attr.localName) for attr in self.values()] |
paulb@18 | 61 | |
paulb@18 | 62 | def items(self): |
paulb@18 | 63 | return [((attr.namespaceURI, attr.localName), attr) for attr in self.values()] |
paulb@18 | 64 | |
paulb@18 | 65 | def __repr__(self): |
paulb@18 | 66 | return str(self) |
paulb@18 | 67 | |
paulb@18 | 68 | def __str__(self): |
paulb@18 | 69 | return "{%s}" % ",\n".join(["%s : %s" % (repr(key), repr(value)) for key, value in self.items()]) |
paulb@18 | 70 | |
paulb@18 | 71 | class Node(object): |
paulb@18 | 72 | |
paulb@75 | 73 | """ |
paulb@75 | 74 | A DOM-style wrapper around libxml2mod objects. |
paulb@75 | 75 | """ |
paulb@75 | 76 | |
paulb@81 | 77 | ATTRIBUTE_NODE = xml.dom.Node.ATTRIBUTE_NODE |
paulb@81 | 78 | COMMENT_NODE = xml.dom.Node.COMMENT_NODE |
paulb@81 | 79 | DOCUMENT_NODE = xml.dom.Node.DOCUMENT_NODE |
paulb@81 | 80 | DOCUMENT_TYPE_NODE = xml.dom.Node.DOCUMENT_TYPE_NODE |
paulb@81 | 81 | ELEMENT_NODE = xml.dom.Node.ELEMENT_NODE |
paulb@81 | 82 | ENTITY_NODE = xml.dom.Node.ENTITY_NODE |
paulb@81 | 83 | ENTITY_REFERENCE_NODE = xml.dom.Node.ENTITY_REFERENCE_NODE |
paulb@81 | 84 | NOTATION_NODE = xml.dom.Node.NOTATION_NODE |
paulb@81 | 85 | PROCESSING_INSTRUCTION_NODE = xml.dom.Node.PROCESSING_INSTRUCTION_NODE |
paulb@81 | 86 | TEXT_NODE = xml.dom.Node.TEXT_NODE |
paulb@81 | 87 | |
paulb@75 | 88 | def __init__(self, node, ownerDocument=None): |
paulb@18 | 89 | self._node = node |
paulb@75 | 90 | self.ownerDocument = ownerDocument |
paulb@18 | 91 | |
paulb@18 | 92 | def as_native_node(self): |
paulb@18 | 93 | return self._node |
paulb@18 | 94 | |
paulb@18 | 95 | def _nodeType(self): |
paulb@72 | 96 | return Node_nodeType(self._node) |
paulb@18 | 97 | |
paulb@18 | 98 | def _childNodes(self): |
paulb@18 | 99 | |
paulb@18 | 100 | # NOTE: Consider a generator instead. |
paulb@18 | 101 | |
paulb@75 | 102 | return [Node(_node, self.ownerDocument) for _node in Node_childNodes(self._node)] |
paulb@18 | 103 | |
paulb@18 | 104 | def _attributes(self): |
paulb@18 | 105 | return NamedNodeMap(self) |
paulb@18 | 106 | |
paulb@18 | 107 | def _namespaceURI(self): |
paulb@72 | 108 | return Node_namespaceURI(self._node) |
paulb@18 | 109 | |
paulb@18 | 110 | def _nodeValue(self): |
paulb@72 | 111 | return Node_nodeValue(self._node) |
paulb@18 | 112 | |
paulb@18 | 113 | def _prefix(self): |
paulb@72 | 114 | return Node_prefix(self._node) |
paulb@18 | 115 | |
paulb@18 | 116 | def _nodeName(self): |
paulb@72 | 117 | return Node_nodeName(self._node) |
paulb@18 | 118 | |
paulb@18 | 119 | def _tagName(self): |
paulb@72 | 120 | return Node_tagName(self._node) |
paulb@18 | 121 | |
paulb@18 | 122 | def _localName(self): |
paulb@72 | 123 | return Node_localName(self._node) |
paulb@18 | 124 | |
paulb@18 | 125 | def _parentNode(self): |
paulb@81 | 126 | return Node(Node_parentNode(self._node), self.ownerDocument) |
paulb@18 | 127 | |
paulb@24 | 128 | def _previousSibling(self): |
paulb@75 | 129 | return Node(Node_previousSibling(self._node), self.ownerDocument) |
paulb@24 | 130 | |
paulb@18 | 131 | def _nextSibling(self): |
paulb@75 | 132 | return Node(Node_nextSibling(self._node), self.ownerDocument) |
paulb@18 | 133 | |
paulb@18 | 134 | def hasAttributeNS(self, ns, localName): |
paulb@72 | 135 | return Node_hasAttributeNS(self._node, ns, localName) |
paulb@18 | 136 | |
paulb@18 | 137 | def hasAttribute(self, name): |
paulb@72 | 138 | return Node_hasAttribute(self._node, name) |
paulb@18 | 139 | |
paulb@18 | 140 | def getAttributeNS(self, ns, localName): |
paulb@72 | 141 | return Node_getAttributeNS(self._node, ns, localName) |
paulb@18 | 142 | |
paulb@18 | 143 | def getAttribute(self, name): |
paulb@72 | 144 | return Node_getAttribute(self._node, name) |
paulb@18 | 145 | |
paulb@18 | 146 | def getAttributeNodeNS(self, ns, localName): |
paulb@75 | 147 | return Node(Node_getAttributeNodeNS(self._node, ns, localName), self.ownerDocument) |
paulb@18 | 148 | |
paulb@18 | 149 | def getAttributeNode(self, localName): |
paulb@75 | 150 | return Node(Node_getAttributeNode(self._node, localName), self.ownerDocument) |
paulb@18 | 151 | |
paulb@18 | 152 | def setAttributeNS(self, ns, name, value): |
paulb@72 | 153 | Node_setAttributeNS(self._node, ns, name, value) |
paulb@18 | 154 | |
paulb@18 | 155 | def setAttribute(self, name, value): |
paulb@72 | 156 | Node_setAttribute(self._node, name, value) |
paulb@18 | 157 | |
paulb@18 | 158 | def setAttributeNodeNS(self, ns, name, node): |
paulb@72 | 159 | Node_setAttributeNodeNS(self._node, ns, name, node) |
paulb@18 | 160 | |
paulb@18 | 161 | def setAttributeNode(self, name, node): |
paulb@72 | 162 | Node_setAttributeNode(self._node, name, node) |
paulb@18 | 163 | |
paulb@18 | 164 | def createElementNS(self, ns, name): |
paulb@75 | 165 | return Node(Node_createElementNS(self._node, ns, name), self.ownerDocument) |
paulb@18 | 166 | |
paulb@18 | 167 | def createElement(self, name): |
paulb@75 | 168 | return Node(Node_createElement(self._node, name), self.ownerDocument) |
paulb@18 | 169 | |
paulb@18 | 170 | def createAttributeNS(self, ns, name): |
paulb@72 | 171 | # Returns a special temporary node. |
paulb@72 | 172 | return Node_createAttributeNS(self._node, ns, name) |
paulb@18 | 173 | |
paulb@18 | 174 | def createAttribute(self, name): |
paulb@72 | 175 | # Returns a special temporary node. |
paulb@72 | 176 | return Node_createAttribute(self._node, name) |
paulb@18 | 177 | |
paulb@18 | 178 | def createTextNode(self, value): |
paulb@75 | 179 | return Node(Node_createTextNode(self._node, value), self.ownerDocument) |
paulb@18 | 180 | |
paulb@34 | 181 | def createComment(self, value): |
paulb@75 | 182 | return Node(Node_createComment(self._node, value), self.ownerDocument) |
paulb@18 | 183 | |
paulb@18 | 184 | def importNode(self, node, deep): |
paulb@72 | 185 | if hasattr(node, "as_native_node"): |
paulb@75 | 186 | return Node(Node_importNode(self._node, node.as_native_node(), deep), self.ownerDocument) |
paulb@72 | 187 | else: |
paulb@75 | 188 | return Node(Node_importNode_DOM(self._node, node, deep), self.ownerDocument) |
paulb@18 | 189 | |
paulb@18 | 190 | def insertBefore(self, tmp, oldNode): |
paulb@72 | 191 | if hasattr(tmp, "as_native_node"): |
paulb@75 | 192 | return Node(Node_insertBefore(self._node, tmp.as_native_node(), oldNode.as_native_node()), self.ownerDocument) |
paulb@18 | 193 | else: |
paulb@75 | 194 | return Node(Node_insertBefore(self._node, tmp, oldNode.as_native_node()), self.ownerDocument) |
paulb@18 | 195 | |
paulb@18 | 196 | def replaceChild(self, tmp, oldNode): |
paulb@72 | 197 | if hasattr(tmp, "as_native_node"): |
paulb@75 | 198 | return Node(Node_replaceChild(self._node, tmp.as_native_node(), oldNode.as_native_node()), self.ownerDocument) |
paulb@18 | 199 | else: |
paulb@75 | 200 | return Node(Node_replaceChild(self._node, tmp, oldNode.as_native_node()), self.ownerDocument) |
paulb@18 | 201 | |
paulb@18 | 202 | def appendChild(self, tmp): |
paulb@72 | 203 | if hasattr(tmp, "as_native_node"): |
paulb@75 | 204 | return Node(Node_appendChild(self._node, tmp.as_native_node()), self.ownerDocument) |
paulb@18 | 205 | else: |
paulb@75 | 206 | return Node(Node_appendChild(self._node, tmp), self.ownerDocument) |
paulb@18 | 207 | |
paulb@18 | 208 | def removeChild(self, tmp): |
paulb@72 | 209 | if hasattr(tmp, "as_native_node"): |
paulb@72 | 210 | Node_removeChild(self._node, tmp.as_native_node()) |
paulb@72 | 211 | else: |
paulb@72 | 212 | Node_removeChild(self._node, tmp) |
paulb@18 | 213 | |
paulb@18 | 214 | childNodes = property(_childNodes) |
paulb@18 | 215 | value = data = nodeValue = property(_nodeValue) |
paulb@18 | 216 | name = nodeName = property(_nodeName) |
paulb@18 | 217 | tagName = property(_tagName) |
paulb@18 | 218 | namespaceURI = property(_namespaceURI) |
paulb@18 | 219 | prefix = property(_prefix) |
paulb@18 | 220 | localName = property(_localName) |
paulb@75 | 221 | ownerElement = parentNode = property(_parentNode) |
paulb@18 | 222 | nodeType = property(_nodeType) |
paulb@18 | 223 | attributes = property(_attributes) |
paulb@24 | 224 | previousSibling = property(_previousSibling) |
paulb@18 | 225 | nextSibling = property(_nextSibling) |
paulb@18 | 226 | |
paulb@72 | 227 | #def isSameNode(self, other): |
paulb@72 | 228 | # return self._node.nodePath() == other._node.nodePath() |
paulb@18 | 229 | |
paulb@72 | 230 | #def __eq__(self, other): |
paulb@72 | 231 | # return self._node.nodePath() == other._node.nodePath() |
paulb@18 | 232 | |
paulb@32 | 233 | # 4DOM extensions to the usual PyXML API. |
paulb@32 | 234 | # NOTE: To be finished. |
paulb@32 | 235 | |
paulb@32 | 236 | def xpath(self, expr, variables=None, namespaces=None): |
paulb@81 | 237 | result = Node_xpath(self._node, expr, variables, namespaces) |
paulb@81 | 238 | if hasattr(result, "len"): |
paulb@81 | 239 | return [Node(_node, self.ownerDocument) for _node in result] |
paulb@81 | 240 | else: |
paulb@81 | 241 | return result |
paulb@81 | 242 | |
paulb@81 | 243 | # Convenience methods. |
paulb@81 | 244 | |
paulb@81 | 245 | def toString(self, encoding=None): |
paulb@81 | 246 | return toString(self, encoding) |
paulb@81 | 247 | |
paulb@81 | 248 | def toStream(self, stream, encoding=None): |
paulb@81 | 249 | toStream(self, stream, encoding) |
paulb@81 | 250 | |
paulb@81 | 251 | def toFile(self, f, encoding=None): |
paulb@81 | 252 | toFile(self, f, encoding) |
paulb@75 | 253 | |
paulb@75 | 254 | # Document housekeeping mechanisms. |
paulb@75 | 255 | |
paulb@75 | 256 | class Document(Node): |
paulb@75 | 257 | |
paulb@75 | 258 | "A class providing document-level housekeeping." |
paulb@75 | 259 | |
paulb@75 | 260 | def __init__(self, node): |
paulb@75 | 261 | self._node = node |
paulb@75 | 262 | self.weakref_ownerDocument = weakref.ref(self) |
paulb@75 | 263 | |
paulb@75 | 264 | def _ownerDocument(self): |
paulb@75 | 265 | return self.weakref_ownerDocument() |
paulb@75 | 266 | |
paulb@75 | 267 | def __del__(self): |
paulb@75 | 268 | print "Freeing document", self._node |
paulb@75 | 269 | libxml2mod.xmlFreeDoc(self._node) |
paulb@75 | 270 | |
paulb@75 | 271 | ownerDocument = property(_ownerDocument) |
paulb@32 | 272 | |
paulb@18 | 273 | # Utility functions. |
paulb@18 | 274 | |
paulb@18 | 275 | def createDocumentType(localName, publicId, systemId): |
paulb@18 | 276 | return None |
paulb@18 | 277 | |
paulb@18 | 278 | def createDocument(namespaceURI, localName, doctype): |
paulb@75 | 279 | return Document(Node_createDocument(namespaceURI, localName, doctype)) |
paulb@18 | 280 | |
paulb@18 | 281 | def parse(stream_or_string): |
paulb@18 | 282 | if hasattr(stream_or_string, "read"): |
paulb@18 | 283 | stream = stream_or_string |
paulb@32 | 284 | return parseString(stream.read()) |
paulb@18 | 285 | else: |
paulb@32 | 286 | return parseFile(stream_or_string) |
paulb@32 | 287 | |
paulb@32 | 288 | def parseFile(s): |
paulb@75 | 289 | return Document(Node_parseFile(s)) |
paulb@18 | 290 | |
paulb@18 | 291 | def parseString(s): |
paulb@75 | 292 | return Document(Node_parseString(s)) |
paulb@18 | 293 | |
paulb@18 | 294 | def parseURI(uri): |
paulb@75 | 295 | return Document(Node_parseURI(uri)) |
paulb@18 | 296 | |
paulb@26 | 297 | def toString(node, encoding=None): |
paulb@72 | 298 | return Node_toString(node.as_native_node(), encoding) |
paulb@18 | 299 | |
paulb@72 | 300 | def toStream(node, stream, encoding=None): |
paulb@72 | 301 | Node_toStream(node.as_native_node(), stream, encoding) |
paulb@72 | 302 | |
paulb@72 | 303 | def toFile(node, f, encoding=None): |
paulb@72 | 304 | Node_toFile(node.as_native_node(), f, encoding) |
paulb@18 | 305 | |
paulb@81 | 306 | def adoptNodes(nodes): |
paulb@81 | 307 | if len(nodes) == 0: |
paulb@81 | 308 | return [] |
paulb@81 | 309 | doc = Document(libxml2mod.doc(nodes[0])) |
paulb@81 | 310 | results = [] |
paulb@81 | 311 | for node in nodes: |
paulb@81 | 312 | results.append(Node(node, doc)) |
paulb@81 | 313 | return results |
paulb@81 | 314 | |
paulb@18 | 315 | # vim: tabstop=4 expandtab shiftwidth=4 |