paulb@304 | 1 | #!/usr/bin/env python |
paulb@304 | 2 | |
paulb@304 | 3 | """ |
paulb@304 | 4 | XML-RPC support using libxml2dom. |
paulb@304 | 5 | |
paulb@304 | 6 | See: http://www.xmlrpc.com/spec |
paulb@304 | 7 | |
paulb@304 | 8 | Copyright (C) 2007 Paul Boddie <paul@boddie.org.uk> |
paulb@304 | 9 | |
paulb@304 | 10 | This program is free software; you can redistribute it and/or modify it under |
paulb@304 | 11 | the terms of the GNU Lesser General Public License as published by the Free |
paulb@304 | 12 | Software Foundation; either version 3 of the License, or (at your option) any |
paulb@304 | 13 | later version. |
paulb@304 | 14 | |
paulb@304 | 15 | This program is distributed in the hope that it will be useful, but WITHOUT |
paulb@304 | 16 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
paulb@304 | 17 | FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more |
paulb@304 | 18 | details. |
paulb@304 | 19 | |
paulb@304 | 20 | You should have received a copy of the GNU Lesser General Public License along |
paulb@304 | 21 | with this program. If not, see <http://www.gnu.org/licenses/>. |
paulb@304 | 22 | |
paulb@304 | 23 | -------- |
paulb@304 | 24 | |
paulb@304 | 25 | The sending and receiving of XML-RPC messages can be done using traditional HTTP |
paulb@304 | 26 | libraries. |
paulb@304 | 27 | |
paulb@304 | 28 | See tests/xmlrpc_test.py for more details. |
paulb@304 | 29 | """ |
paulb@304 | 30 | |
paulb@304 | 31 | import libxml2dom |
paulb@304 | 32 | from libxml2dom.macrolib import * |
paulb@304 | 33 | from libxml2dom.macrolib import \ |
paulb@304 | 34 | createDocument as Node_createDocument |
paulb@307 | 35 | import datetime |
paulb@304 | 36 | |
paulb@304 | 37 | class XMLRPCImplementation(libxml2dom.Implementation): |
paulb@304 | 38 | |
paulb@304 | 39 | "Contains an XML-RPC-specific implementation." |
paulb@304 | 40 | |
paulb@304 | 41 | # Wrapping of documents. |
paulb@304 | 42 | |
paulb@304 | 43 | def adoptDocument(self, node): |
paulb@304 | 44 | return XMLRPCDocument(node, self) |
paulb@304 | 45 | |
paulb@304 | 46 | # Factory functions. |
paulb@304 | 47 | |
paulb@304 | 48 | def get_node(self, _node, context_node): |
paulb@304 | 49 | |
paulb@304 | 50 | """ |
paulb@304 | 51 | Get a libxml2dom node for the given low-level '_node' and libxml2dom |
paulb@304 | 52 | 'context_node'. |
paulb@304 | 53 | """ |
paulb@304 | 54 | |
paulb@304 | 55 | if Node_nodeType(_node) == context_node.ELEMENT_NODE: |
paulb@304 | 56 | |
paulb@304 | 57 | # Make special elements. |
paulb@304 | 58 | |
paulb@304 | 59 | if Node_localName(_node) in ("methodCall", "methodResponse"): |
paulb@304 | 60 | return XMLRPCMethodElement(_node, self, context_node.ownerDocument) |
paulb@304 | 61 | elif Node_localName(_node) == "methodName": |
paulb@304 | 62 | return XMLRPCMethodNameElement(_node, self, context_node.ownerDocument) |
paulb@304 | 63 | elif Node_localName(_node) == "fault": |
paulb@304 | 64 | return XMLRPCFaultElement(_node, self, context_node.ownerDocument) |
paulb@304 | 65 | elif Node_localName(_node) == "string": |
paulb@304 | 66 | return XMLRPCStringElement(_node, self, context_node.ownerDocument) |
paulb@304 | 67 | elif Node_localName(_node) in ("int", "i4"): |
paulb@304 | 68 | return XMLRPCIntegerElement(_node, self, context_node.ownerDocument) |
paulb@304 | 69 | elif Node_localName(_node) == "boolean": |
paulb@304 | 70 | return XMLRPCBooleanElement(_node, self, context_node.ownerDocument) |
paulb@304 | 71 | elif Node_localName(_node) == "double": |
paulb@304 | 72 | return XMLRPCDoubleElement(_node, self, context_node.ownerDocument) |
paulb@304 | 73 | elif Node_localName(_node) == "dateTime.iso8601": |
paulb@304 | 74 | return XMLRPCDateTimeElement(_node, self, context_node.ownerDocument) |
paulb@304 | 75 | elif Node_localName(_node) == "base64": |
paulb@304 | 76 | return XMLRPCBase64Element(_node, self, context_node.ownerDocument) |
paulb@304 | 77 | elif Node_localName(_node) == "struct": |
paulb@304 | 78 | return XMLRPCStructElement(_node, self, context_node.ownerDocument) |
paulb@304 | 79 | elif Node_localName(_node) == "member": |
paulb@304 | 80 | return XMLRPCMemberElement(_node, self, context_node.ownerDocument) |
paulb@307 | 81 | elif Node_localName(_node) == "value": |
paulb@307 | 82 | return XMLRPCValueElement(_node, self, context_node.ownerDocument) |
paulb@309 | 83 | elif Node_localName(_node) == "name": |
paulb@309 | 84 | return XMLRPCNameElement(_node, self, context_node.ownerDocument) |
paulb@304 | 85 | |
paulb@304 | 86 | # Otherwise, make generic XML-RPC elements. |
paulb@304 | 87 | |
paulb@304 | 88 | return XMLRPCElement(_node, self, context_node.ownerDocument) |
paulb@304 | 89 | |
paulb@304 | 90 | else: |
paulb@304 | 91 | return libxml2dom.Implementation.get_node(self, _node, context_node) |
paulb@304 | 92 | |
paulb@304 | 93 | # Convenience functions. |
paulb@304 | 94 | |
paulb@304 | 95 | def createXMLRPCMessage(self, namespaceURI, localName): |
paulb@304 | 96 | |
paulb@304 | 97 | "Create a new XML-RPC message document (fragment)." |
paulb@304 | 98 | |
paulb@304 | 99 | return XMLRPCDocument(Node_createDocument(namespaceURI, localName, None), self).documentElement |
paulb@304 | 100 | |
paulb@307 | 101 | def createMethodCall(self): |
paulb@307 | 102 | return self.createXMLRPCMessage(None, "methodCall") |
paulb@307 | 103 | |
paulb@307 | 104 | def createMethodResponse(self): |
paulb@307 | 105 | return self.createXMLRPCMessage(None, "methodResponse") |
paulb@307 | 106 | |
paulb@304 | 107 | # Node classes. |
paulb@304 | 108 | |
paulb@304 | 109 | class XMLRPCNode(libxml2dom.Node): |
paulb@304 | 110 | |
paulb@304 | 111 | "Convenience modifications to nodes specific to libxml2dom.xmlrpc." |
paulb@304 | 112 | |
paulb@304 | 113 | pass |
paulb@304 | 114 | |
paulb@307 | 115 | class XMLRPCElement(XMLRPCNode): |
paulb@307 | 116 | |
paulb@307 | 117 | "An XML-RPC element." |
paulb@307 | 118 | |
paulb@307 | 119 | pass |
paulb@307 | 120 | |
paulb@304 | 121 | class XMLRPCDocument(libxml2dom._Document, XMLRPCNode): |
paulb@304 | 122 | |
paulb@304 | 123 | "An XML-RPC document fragment." |
paulb@304 | 124 | |
paulb@304 | 125 | def _method(self): |
paulb@309 | 126 | return (self.xpath("methodCall|methodResponse") or [None])[0] |
paulb@307 | 127 | |
paulb@307 | 128 | def _fault(self): |
paulb@307 | 129 | if self.method is not None: |
paulb@307 | 130 | return self.method.fault |
paulb@307 | 131 | else: |
paulb@307 | 132 | return None |
paulb@304 | 133 | |
paulb@309 | 134 | method = property(_method) |
paulb@309 | 135 | fault = property(_fault) |
paulb@309 | 136 | |
paulb@307 | 137 | # Node construction methods. |
paulb@307 | 138 | |
paulb@307 | 139 | def createMethodCall(self): |
paulb@307 | 140 | return self.ownerDocument.createElement("methodCall") |
paulb@307 | 141 | |
paulb@307 | 142 | def createMethodResponse(self): |
paulb@307 | 143 | return self.ownerDocument.createElement("methodResponse") |
paulb@307 | 144 | |
paulb@304 | 145 | class XMLRPCMethodElement(XMLRPCNode): |
paulb@304 | 146 | |
paulb@304 | 147 | "An XML-RPC method element." |
paulb@304 | 148 | |
paulb@304 | 149 | def _fault(self): |
paulb@307 | 150 | return (self.xpath("./fault") or [None])[0] |
paulb@304 | 151 | |
paulb@309 | 152 | def _methodNameElement(self): |
paulb@307 | 153 | return (self.xpath("./methodName") or [None])[0] |
paulb@307 | 154 | |
paulb@309 | 155 | def _methodName(self): |
paulb@309 | 156 | name = self.methodNameElement |
paulb@307 | 157 | if name is not None: |
paulb@307 | 158 | return name.value |
paulb@307 | 159 | else: |
paulb@307 | 160 | return None |
paulb@307 | 161 | |
paulb@309 | 162 | def _setMethodName(self, name): |
paulb@309 | 163 | if self.methodNameElement is None: |
paulb@307 | 164 | methodName = self.createMethodName() |
paulb@307 | 165 | self.appendChild(methodName) |
paulb@309 | 166 | self.methodNameElement.value = name |
paulb@304 | 167 | |
paulb@304 | 168 | def _parameters(self): |
paulb@307 | 169 | return self.xpath("./params/param") |
paulb@307 | 170 | |
paulb@309 | 171 | def _parameterValues(self): |
paulb@307 | 172 | values = self.xpath("./params/param/value") |
paulb@307 | 173 | if values: |
paulb@307 | 174 | items = [] |
paulb@307 | 175 | for value in values: |
paulb@309 | 176 | items.append(self._get_value(value)) |
paulb@307 | 177 | return items |
paulb@307 | 178 | else: |
paulb@307 | 179 | return [] |
paulb@304 | 180 | |
paulb@307 | 181 | def _setParameterValues(self, parameters): |
paulb@307 | 182 | param_list = self.parameters |
paulb@307 | 183 | params = (self.xpath("./params") or [None])[0] |
paulb@309 | 184 | |
paulb@309 | 185 | # Remove any previous parameters. |
paulb@309 | 186 | |
paulb@307 | 187 | if params: |
paulb@307 | 188 | if param_list: |
paulb@307 | 189 | for param in param_list: |
paulb@307 | 190 | params.removeChild(param) |
paulb@307 | 191 | else: |
paulb@307 | 192 | params = self.createParameters() |
paulb@307 | 193 | self.appendChild(params) |
paulb@307 | 194 | |
paulb@309 | 195 | # Add parameter values. |
paulb@309 | 196 | |
paulb@307 | 197 | for parameter in parameters: |
paulb@307 | 198 | param = self.ownerDocument.createElement("param") |
paulb@307 | 199 | params.appendChild(param) |
paulb@307 | 200 | value = self.ownerDocument.createElement("value") |
paulb@307 | 201 | param.appendChild(value) |
paulb@309 | 202 | self._add_value(value, parameter) |
paulb@307 | 203 | |
paulb@307 | 204 | # Internal methods. |
paulb@307 | 205 | |
paulb@309 | 206 | def _add_value(self, value, parameter): |
paulb@309 | 207 | typename, parameter = parameter |
paulb@309 | 208 | if typename == "struct": |
paulb@309 | 209 | if isinstance(parameter, dict): |
paulb@309 | 210 | items = parameter.items() |
paulb@309 | 211 | else: |
paulb@309 | 212 | items = parameter |
paulb@309 | 213 | struct = self.ownerDocument.createElement("struct") |
paulb@309 | 214 | for item_name, item_value in items: |
paulb@309 | 215 | member = struct.createMember() |
paulb@309 | 216 | struct.appendChild(member) |
paulb@309 | 217 | member.memberName = item_name |
paulb@309 | 218 | memberValue = member.createValue() |
paulb@309 | 219 | member.appendChild(memberValue) |
paulb@309 | 220 | self._add_value(memberValue, item_value) |
paulb@309 | 221 | value.appendChild(struct) |
paulb@309 | 222 | else: |
paulb@309 | 223 | container = self.ownerDocument.createElement(typename) |
paulb@309 | 224 | value.appendChild(container) |
paulb@309 | 225 | container.value = unicode(parameter) |
paulb@307 | 226 | |
paulb@309 | 227 | def _get_value(self, value): |
paulb@307 | 228 | if value.type == "struct": |
paulb@307 | 229 | items = [] |
paulb@307 | 230 | for member in value.container.members: |
paulb@309 | 231 | items.append((member.memberName, self._get_value(member.value))) |
paulb@309 | 232 | return (value.type, items) |
paulb@307 | 233 | else: |
paulb@309 | 234 | return (value.type, value.container.value) |
paulb@307 | 235 | |
paulb@307 | 236 | # Node construction methods. |
paulb@307 | 237 | |
paulb@307 | 238 | def createMethodName(self): |
paulb@307 | 239 | return self.ownerDocument.createElement("methodName") |
paulb@307 | 240 | |
paulb@307 | 241 | def createParameters(self): |
paulb@307 | 242 | return self.ownerDocument.createElement("params") |
paulb@307 | 243 | |
paulb@304 | 244 | def createFault(self): |
paulb@304 | 245 | return self.ownerDocument.createElement("fault") |
paulb@304 | 246 | |
paulb@304 | 247 | fault = property(_fault) |
paulb@309 | 248 | methodNameElement = property(_methodNameElement) |
paulb@309 | 249 | methodName = property(_methodName, _setMethodName) |
paulb@304 | 250 | parameters = property(_parameters) |
paulb@307 | 251 | parameterValues = property(_parameterValues, _setParameterValues) |
paulb@309 | 252 | |
paulb@309 | 253 | class XMLRPCStructElement(XMLRPCNode): |
paulb@309 | 254 | |
paulb@309 | 255 | "An XML-RPC structure element." |
paulb@309 | 256 | |
paulb@309 | 257 | def _members(self): |
paulb@309 | 258 | return self.xpath("./member") |
paulb@309 | 259 | |
paulb@309 | 260 | # Node construction methods. |
paulb@309 | 261 | |
paulb@309 | 262 | def createMember(self): |
paulb@309 | 263 | return self.ownerDocument.createElement("member") |
paulb@309 | 264 | |
paulb@309 | 265 | members = property(_members) |
paulb@309 | 266 | |
paulb@309 | 267 | class XMLRPCMemberElement(XMLRPCNode): |
paulb@309 | 268 | |
paulb@309 | 269 | "An XML-RPC structure member element." |
paulb@309 | 270 | |
paulb@309 | 271 | def _nameElement(self): |
paulb@309 | 272 | return (self.xpath("./name") or [None])[0] |
paulb@309 | 273 | |
paulb@309 | 274 | def _memberName(self): |
paulb@309 | 275 | if self.nameElement is not None: |
paulb@309 | 276 | return self.nameElement.value |
paulb@309 | 277 | else: |
paulb@309 | 278 | return None |
paulb@309 | 279 | |
paulb@309 | 280 | def _setMemberName(self, name): |
paulb@309 | 281 | if self.nameElement is None: |
paulb@309 | 282 | nameElement = self.createName() |
paulb@309 | 283 | self.appendChild(nameElement) |
paulb@309 | 284 | self.nameElement.value = name |
paulb@309 | 285 | |
paulb@309 | 286 | def _value(self): |
paulb@309 | 287 | return (self.xpath("./value") or [None])[0] |
paulb@309 | 288 | |
paulb@309 | 289 | # Node construction methods. |
paulb@309 | 290 | |
paulb@309 | 291 | def createName(self): |
paulb@309 | 292 | return self.ownerDocument.createElement("name") |
paulb@309 | 293 | |
paulb@309 | 294 | def createValue(self): |
paulb@309 | 295 | return self.ownerDocument.createElement("value") |
paulb@309 | 296 | |
paulb@309 | 297 | nameElement = property(_nameElement) |
paulb@309 | 298 | memberName = property(_memberName, _setMemberName) |
paulb@309 | 299 | value = property(_value) |
paulb@304 | 300 | |
paulb@304 | 301 | class XMLRPCStringElement(XMLRPCNode): |
paulb@304 | 302 | |
paulb@304 | 303 | "An XML-RPC string element." |
paulb@304 | 304 | |
paulb@304 | 305 | def _value(self): |
paulb@304 | 306 | return self.textContent.strip() |
paulb@304 | 307 | |
paulb@304 | 308 | def _setValue(self, value): |
paulb@304 | 309 | for node in self.childNodes: |
paulb@304 | 310 | self.removeChild(node) |
paulb@304 | 311 | text = self.ownerDocument.createTextNode(value) |
paulb@304 | 312 | self.appendChild(text) |
paulb@304 | 313 | |
paulb@304 | 314 | value = property(_value, _setValue) |
paulb@304 | 315 | |
paulb@309 | 316 | class XMLRPCNameElement(XMLRPCStringElement): |
paulb@309 | 317 | |
paulb@309 | 318 | "An XML-RPC name element." |
paulb@309 | 319 | |
paulb@309 | 320 | pass |
paulb@309 | 321 | |
paulb@307 | 322 | class XMLRPCValueElement(XMLRPCStringElement): |
paulb@307 | 323 | |
paulb@307 | 324 | "An XML-RPC value element." |
paulb@307 | 325 | |
paulb@307 | 326 | def _type(self): |
paulb@307 | 327 | elements = self.xpath("*") |
paulb@307 | 328 | if elements: |
paulb@307 | 329 | return elements[0].localName |
paulb@307 | 330 | else: |
paulb@307 | 331 | return "string" |
paulb@307 | 332 | |
paulb@307 | 333 | def _container(self): |
paulb@307 | 334 | return (self.xpath("*") or [self])[0] |
paulb@307 | 335 | |
paulb@307 | 336 | type = property(_type) |
paulb@307 | 337 | container = property(_container) |
paulb@307 | 338 | |
paulb@304 | 339 | class XMLRPCMethodNameElement(XMLRPCStringElement): |
paulb@304 | 340 | |
paulb@304 | 341 | "An XML-RPC method element." |
paulb@304 | 342 | |
paulb@304 | 343 | pass |
paulb@304 | 344 | |
paulb@304 | 345 | class XMLRPCIntegerElement(XMLRPCStringElement): |
paulb@304 | 346 | |
paulb@304 | 347 | "An XML-RPC integer element." |
paulb@304 | 348 | |
paulb@304 | 349 | pass |
paulb@304 | 350 | |
paulb@304 | 351 | class XMLRPCBooleanElement(XMLRPCStringElement): |
paulb@304 | 352 | |
paulb@304 | 353 | "An XML-RPC boolean element." |
paulb@304 | 354 | |
paulb@304 | 355 | pass |
paulb@304 | 356 | |
paulb@304 | 357 | class XMLRPCDoubleElement(XMLRPCStringElement): |
paulb@304 | 358 | |
paulb@304 | 359 | "An XML-RPC double floating point number element." |
paulb@304 | 360 | |
paulb@304 | 361 | pass |
paulb@304 | 362 | |
paulb@304 | 363 | class XMLRPCDateTimeElement(XMLRPCStringElement): |
paulb@304 | 364 | |
paulb@304 | 365 | "An XML-RPC date/time element." |
paulb@304 | 366 | |
paulb@304 | 367 | pass |
paulb@304 | 368 | |
paulb@304 | 369 | class XMLRPCBase64Element(XMLRPCStringElement): |
paulb@304 | 370 | |
paulb@304 | 371 | "An XML-RPC integer element." |
paulb@304 | 372 | |
paulb@304 | 373 | pass |
paulb@304 | 374 | |
paulb@304 | 375 | class XMLRPCFaultElement(XMLRPCNode): |
paulb@304 | 376 | |
paulb@304 | 377 | "An XML-RPC fault element." |
paulb@304 | 378 | |
paulb@304 | 379 | def _code(self): |
paulb@307 | 380 | code = self.xpath("./value/struct/member[./name/text() = 'faultCode']/value/int") |
paulb@307 | 381 | if code: |
paulb@307 | 382 | return code[0].value |
paulb@307 | 383 | else: |
paulb@307 | 384 | return None |
paulb@304 | 385 | |
paulb@304 | 386 | def _reason(self): |
paulb@307 | 387 | reason = self.xpath("./value/struct/member[./name/text() = 'faultString']/value/string") |
paulb@307 | 388 | if reason: |
paulb@307 | 389 | return reason[0].value |
paulb@307 | 390 | else: |
paulb@307 | 391 | return None |
paulb@304 | 392 | |
paulb@304 | 393 | code = property(_code) |
paulb@304 | 394 | reason = property(_reason) |
paulb@304 | 395 | |
paulb@304 | 396 | # Utility functions. |
paulb@304 | 397 | |
paulb@304 | 398 | createDocument = libxml2dom.createDocument |
paulb@304 | 399 | createDocumentType = libxml2dom.createDocumentType |
paulb@304 | 400 | |
paulb@304 | 401 | def createXMLRPCMessage(namespaceURI, localName): |
paulb@307 | 402 | return default_impl.createXMLRPCMessage(None, localName) |
paulb@307 | 403 | |
paulb@307 | 404 | def createMethodCall(): |
paulb@307 | 405 | return default_impl.createMethodCall() |
paulb@307 | 406 | |
paulb@307 | 407 | def createMethodResponse(): |
paulb@307 | 408 | return default_impl.createMethodResponse() |
paulb@304 | 409 | |
paulb@304 | 410 | def parse(stream_or_string, html=0, htmlencoding=None, unfinished=0, impl=None): |
paulb@304 | 411 | return libxml2dom.parse(stream_or_string, html=html, htmlencoding=htmlencoding, unfinished=unfinished, impl=(impl or default_impl)) |
paulb@304 | 412 | |
paulb@304 | 413 | def parseFile(filename, html=0, htmlencoding=None, unfinished=0, impl=None): |
paulb@304 | 414 | return libxml2dom.parseFile(filename, html=html, htmlencoding=htmlencoding, unfinished=unfinished, impl=(impl or default_impl)) |
paulb@304 | 415 | |
paulb@304 | 416 | def parseString(s, html=0, htmlencoding=None, unfinished=0, impl=None): |
paulb@304 | 417 | return libxml2dom.parseString(s, html=html, htmlencoding=htmlencoding, unfinished=unfinished, impl=(impl or default_impl)) |
paulb@304 | 418 | |
paulb@304 | 419 | def parseURI(uri, html=0, htmlencoding=None, unfinished=0, impl=None): |
paulb@304 | 420 | return libxml2dom.parseURI(uri, html=html, htmlencoding=htmlencoding, unfinished=unfinished, impl=(impl or default_impl)) |
paulb@304 | 421 | |
paulb@304 | 422 | # Single instance of the implementation. |
paulb@304 | 423 | |
paulb@304 | 424 | default_impl = XMLRPCImplementation() |
paulb@304 | 425 | |
paulb@304 | 426 | # vim: tabstop=4 expandtab shiftwidth=4 |