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 | |
paul@342 | 8 | Copyright (C) 2007, 2008 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@315 | 35 | import datetime |
paulb@304 | 36 | |
paulb@304 | 37 | # Node classes. |
paulb@304 | 38 | |
paulb@304 | 39 | class XMLRPCNode(libxml2dom.Node): |
paulb@304 | 40 | |
paulb@304 | 41 | "Convenience modifications to nodes specific to libxml2dom.xmlrpc." |
paulb@304 | 42 | |
paul@342 | 43 | def add_or_replace_element(self, new_element): |
paul@342 | 44 | elements = self.xpath("*") |
paul@342 | 45 | if elements: |
paul@342 | 46 | self.replaceChild(new_element, elements[0]) |
paul@342 | 47 | else: |
paul@342 | 48 | self.appendChild(new_element) |
paulb@304 | 49 | |
paulb@307 | 50 | class XMLRPCElement(XMLRPCNode): |
paulb@307 | 51 | |
paulb@307 | 52 | "An XML-RPC element." |
paulb@307 | 53 | |
paulb@307 | 54 | pass |
paulb@307 | 55 | |
paulb@304 | 56 | class XMLRPCDocument(libxml2dom._Document, XMLRPCNode): |
paulb@304 | 57 | |
paulb@304 | 58 | "An XML-RPC document fragment." |
paulb@304 | 59 | |
paulb@304 | 60 | def _method(self): |
paulb@309 | 61 | return (self.xpath("methodCall|methodResponse") or [None])[0] |
paulb@307 | 62 | |
paulb@307 | 63 | def _fault(self): |
paulb@307 | 64 | if self.method is not None: |
paulb@307 | 65 | return self.method.fault |
paulb@307 | 66 | else: |
paulb@307 | 67 | return None |
paulb@304 | 68 | |
paulb@309 | 69 | method = property(_method) |
paulb@309 | 70 | fault = property(_fault) |
paulb@309 | 71 | |
paulb@307 | 72 | # Node construction methods. |
paulb@307 | 73 | |
paul@342 | 74 | def createMethodCall(self, insert=0): |
paul@342 | 75 | e = self.ownerDocument.createElement("methodCall") |
paul@342 | 76 | if insert: |
paul@342 | 77 | self.add_or_replace_element(e) |
paul@342 | 78 | return e |
paulb@307 | 79 | |
paul@342 | 80 | def createMethodResponse(self, insert=0): |
paul@342 | 81 | e = self.ownerDocument.createElement("methodResponse") |
paul@342 | 82 | if insert: |
paul@342 | 83 | self.add_or_replace_element(e) |
paul@342 | 84 | return e |
paulb@307 | 85 | |
paulb@304 | 86 | class XMLRPCMethodElement(XMLRPCNode): |
paulb@304 | 87 | |
paulb@304 | 88 | "An XML-RPC method element." |
paulb@304 | 89 | |
paulb@304 | 90 | def _fault(self): |
paulb@307 | 91 | return (self.xpath("./fault") or [None])[0] |
paulb@304 | 92 | |
paulb@309 | 93 | def _methodNameElement(self): |
paulb@307 | 94 | return (self.xpath("./methodName") or [None])[0] |
paulb@307 | 95 | |
paulb@309 | 96 | def _methodName(self): |
paulb@309 | 97 | name = self.methodNameElement |
paulb@307 | 98 | if name is not None: |
paulb@307 | 99 | return name.value |
paulb@307 | 100 | else: |
paulb@307 | 101 | return None |
paulb@307 | 102 | |
paulb@309 | 103 | def _setMethodName(self, name): |
paulb@309 | 104 | if self.methodNameElement is None: |
paulb@307 | 105 | methodName = self.createMethodName() |
paulb@307 | 106 | self.appendChild(methodName) |
paulb@309 | 107 | self.methodNameElement.value = name |
paulb@304 | 108 | |
paulb@314 | 109 | def _parameterValues(self): |
paulb@315 | 110 | return [value.container.contents for value in self.xpath("./params/param/value")] |
paulb@307 | 111 | |
paulb@307 | 112 | # Node construction methods. |
paulb@307 | 113 | |
paul@342 | 114 | def createMethodName(self, insert=0): |
paul@342 | 115 | e = self.ownerDocument.createElement("methodName") |
paul@342 | 116 | if insert: |
paul@342 | 117 | self.add_or_replace_element(e) |
paul@342 | 118 | return e |
paulb@307 | 119 | |
paul@342 | 120 | def createParameters(self, insert=0): |
paul@342 | 121 | e = self.ownerDocument.createElement("params") |
paul@342 | 122 | if insert: |
paul@342 | 123 | self.add_or_replace_element(e) |
paul@342 | 124 | return e |
paulb@307 | 125 | |
paul@342 | 126 | def createFault(self, insert=0): |
paul@342 | 127 | e = self.ownerDocument.createElement("fault") |
paul@342 | 128 | if insert: |
paul@342 | 129 | self.add_or_replace_element(e) |
paul@342 | 130 | return e |
paulb@304 | 131 | |
paulb@304 | 132 | fault = property(_fault) |
paulb@309 | 133 | methodNameElement = property(_methodNameElement) |
paulb@309 | 134 | methodName = property(_methodName, _setMethodName) |
paulb@315 | 135 | parameterValues = property(_parameterValues) |
paulb@312 | 136 | |
paul@342 | 137 | class XMLRPCParametersElement(XMLRPCNode): |
paul@342 | 138 | |
paul@342 | 139 | "An XML-RPC parameters/params element." |
paul@342 | 140 | |
paul@342 | 141 | # Node construction methods. |
paul@342 | 142 | |
paul@342 | 143 | def createParameter(self, insert=0): |
paul@342 | 144 | e = self.ownerDocument.createElement("param") |
paul@342 | 145 | if insert: |
paul@342 | 146 | self.appendChild(e) |
paul@342 | 147 | return e |
paul@342 | 148 | |
paul@342 | 149 | class XMLRPCParameterElement(XMLRPCNode): |
paul@342 | 150 | |
paul@342 | 151 | "An XML-RPC parameter/param element." |
paul@342 | 152 | |
paul@342 | 153 | # Node construction methods. |
paul@342 | 154 | |
paul@342 | 155 | def createValue(self, typename=None, insert=0): |
paul@342 | 156 | value = self.ownerDocument.createElement("value") |
paul@342 | 157 | if typename is not None: |
paul@342 | 158 | contents = self.ownerDocument.createElement(typename) |
paul@342 | 159 | value.appendChild(contents) |
paul@342 | 160 | if insert: |
paul@342 | 161 | self.add_or_replace_element(value) |
paul@342 | 162 | return value |
paul@342 | 163 | |
paulb@312 | 164 | class XMLRPCArrayElement(XMLRPCNode): |
paulb@312 | 165 | |
paulb@312 | 166 | "An XML-RPC array element." |
paulb@312 | 167 | |
paulb@312 | 168 | def _data(self): |
paulb@312 | 169 | return (self.xpath("./data") or [None])[0] |
paulb@312 | 170 | |
paulb@315 | 171 | def _contents(self): |
paulb@315 | 172 | return self |
paulb@315 | 173 | |
paulb@315 | 174 | # Sequence emulation. |
paulb@315 | 175 | |
paulb@315 | 176 | def __len__(self): |
paulb@315 | 177 | if self.data: |
paulb@315 | 178 | return len(self.data) |
paulb@315 | 179 | else: |
paulb@315 | 180 | return 0 |
paulb@315 | 181 | |
paulb@315 | 182 | def __getitem__(self, i): |
paulb@315 | 183 | if self.data: |
paulb@315 | 184 | return self.data[i] |
paulb@315 | 185 | else: |
paulb@315 | 186 | raise IndexError, i |
paulb@315 | 187 | |
paulb@315 | 188 | def __eq__(self, other): |
paulb@315 | 189 | for i, j in map(None, self, other): |
paulb@315 | 190 | if i != j: |
paulb@315 | 191 | return False |
paulb@315 | 192 | return True |
paulb@315 | 193 | |
paulb@312 | 194 | # Node construction methods. |
paulb@312 | 195 | |
paul@342 | 196 | def createData(self, insert=0): |
paul@342 | 197 | e = self.ownerDocument.createElement("data") |
paul@342 | 198 | if insert: |
paul@342 | 199 | self.add_or_replace_element(e) |
paul@342 | 200 | return e |
paulb@312 | 201 | |
paulb@312 | 202 | data = property(_data) |
paulb@315 | 203 | contents = property(_contents) |
paulb@309 | 204 | |
paulb@309 | 205 | class XMLRPCStructElement(XMLRPCNode): |
paulb@309 | 206 | |
paulb@309 | 207 | "An XML-RPC structure element." |
paulb@309 | 208 | |
paulb@309 | 209 | def _members(self): |
paulb@309 | 210 | return self.xpath("./member") |
paulb@309 | 211 | |
paulb@315 | 212 | def _contents(self): |
paulb@315 | 213 | return self |
paulb@315 | 214 | |
paulb@315 | 215 | # Sequence emulation. |
paulb@315 | 216 | |
paulb@315 | 217 | def __len__(self): |
paulb@315 | 218 | return len(self.members) |
paulb@315 | 219 | |
paulb@315 | 220 | def __getitem__(self, i): |
paulb@315 | 221 | return self.members[i] |
paulb@315 | 222 | |
paulb@315 | 223 | def __eq__(self, other): |
paulb@315 | 224 | for i, j in map(None, self, other): |
paulb@315 | 225 | if i != j: |
paulb@315 | 226 | return False |
paulb@315 | 227 | return True |
paulb@315 | 228 | |
paulb@309 | 229 | # Node construction methods. |
paulb@309 | 230 | |
paul@342 | 231 | def createMember(self, insert=0): |
paul@342 | 232 | e = self.ownerDocument.createElement("member") |
paul@342 | 233 | if insert: |
paul@342 | 234 | self.add_or_replace_element(e) |
paul@342 | 235 | return e |
paulb@309 | 236 | |
paulb@309 | 237 | members = property(_members) |
paulb@315 | 238 | contents = property(_contents) |
paulb@309 | 239 | |
paulb@312 | 240 | class XMLRPCDataElement(XMLRPCNode): |
paulb@312 | 241 | |
paulb@312 | 242 | "An XML-RPC array data element." |
paulb@312 | 243 | |
paulb@312 | 244 | def _values(self): |
paulb@312 | 245 | return self.xpath("./value") |
paulb@312 | 246 | |
paulb@315 | 247 | # Sequence emulation. |
paulb@315 | 248 | |
paulb@315 | 249 | def __len__(self): |
paulb@315 | 250 | return len(self.values) |
paulb@315 | 251 | |
paulb@315 | 252 | def __getitem__(self, i): |
paulb@315 | 253 | return self.values[i].container.contents |
paulb@312 | 254 | |
paulb@312 | 255 | # Node construction methods. |
paulb@312 | 256 | |
paul@342 | 257 | def createValue(self, insert=0): |
paul@342 | 258 | e = self.ownerDocument.createElement("value") |
paul@342 | 259 | if insert: |
paul@342 | 260 | self.add_or_replace_element(e) |
paul@342 | 261 | return e |
paulb@312 | 262 | |
paulb@315 | 263 | values = property(_values) |
paulb@315 | 264 | |
paulb@309 | 265 | class XMLRPCMemberElement(XMLRPCNode): |
paulb@309 | 266 | |
paulb@309 | 267 | "An XML-RPC structure member element." |
paulb@309 | 268 | |
paulb@312 | 269 | def _value(self): |
paulb@312 | 270 | return (self.xpath("./value") or [None])[0] |
paulb@312 | 271 | |
paulb@309 | 272 | def _nameElement(self): |
paulb@309 | 273 | return (self.xpath("./name") or [None])[0] |
paulb@309 | 274 | |
paulb@309 | 275 | def _memberName(self): |
paulb@309 | 276 | if self.nameElement is not None: |
paulb@309 | 277 | return self.nameElement.value |
paulb@309 | 278 | else: |
paulb@309 | 279 | return None |
paulb@309 | 280 | |
paulb@309 | 281 | def _setMemberName(self, name): |
paulb@309 | 282 | if self.nameElement is None: |
paulb@309 | 283 | nameElement = self.createName() |
paulb@309 | 284 | self.appendChild(nameElement) |
paulb@309 | 285 | self.nameElement.value = name |
paulb@309 | 286 | |
paulb@315 | 287 | def _contents(self): |
paulb@315 | 288 | return self |
paulb@315 | 289 | |
paulb@315 | 290 | # Item (name, value) emulation. |
paulb@315 | 291 | |
paulb@315 | 292 | def __len__(self): |
paulb@315 | 293 | return 2 |
paulb@315 | 294 | |
paulb@315 | 295 | def __getitem__(self, i): |
paulb@315 | 296 | return (self.memberName, self.value.container.contents)[i] |
paulb@315 | 297 | |
paulb@315 | 298 | def __eq__(self, other): |
paulb@315 | 299 | return self[0] == other[0] and self[1] == other[1] |
paulb@315 | 300 | |
paulb@309 | 301 | # Node construction methods. |
paulb@309 | 302 | |
paul@342 | 303 | def createName(self, insert=0): |
paul@342 | 304 | e = self.ownerDocument.createElement("name") |
paul@342 | 305 | if insert: |
paul@342 | 306 | self.add_or_replace_element(e) |
paul@342 | 307 | return e |
paulb@309 | 308 | |
paul@342 | 309 | def createValue(self, insert=0): |
paul@342 | 310 | e = self.ownerDocument.createElement("value") |
paul@342 | 311 | if insert: |
paul@342 | 312 | self.add_or_replace_element(e) |
paul@342 | 313 | return e |
paulb@309 | 314 | |
paulb@312 | 315 | value = property(_value) |
paulb@309 | 316 | nameElement = property(_nameElement) |
paulb@309 | 317 | memberName = property(_memberName, _setMemberName) |
paulb@315 | 318 | contents = property(_contents) |
paulb@304 | 319 | |
paulb@304 | 320 | class XMLRPCStringElement(XMLRPCNode): |
paulb@304 | 321 | |
paulb@304 | 322 | "An XML-RPC string element." |
paulb@304 | 323 | |
paulb@315 | 324 | typename = "string" |
paulb@315 | 325 | |
paulb@304 | 326 | def _value(self): |
paulb@304 | 327 | return self.textContent.strip() |
paulb@304 | 328 | |
paulb@304 | 329 | def _setValue(self, value): |
paulb@304 | 330 | for node in self.childNodes: |
paulb@304 | 331 | self.removeChild(node) |
paulb@304 | 332 | text = self.ownerDocument.createTextNode(value) |
paulb@304 | 333 | self.appendChild(text) |
paulb@304 | 334 | |
paulb@315 | 335 | def _contents(self): |
paulb@315 | 336 | return convert(self.typename, self.value) |
paulb@315 | 337 | |
paulb@315 | 338 | def __eq__(self, other): |
paulb@315 | 339 | if hasattr(other, "contents"): |
paulb@315 | 340 | return self.contents == other.contents |
paulb@315 | 341 | else: |
paulb@315 | 342 | return self.contents == other |
paulb@315 | 343 | |
paulb@304 | 344 | value = property(_value, _setValue) |
paulb@315 | 345 | contents = property(_contents) |
paulb@304 | 346 | |
paulb@309 | 347 | class XMLRPCNameElement(XMLRPCStringElement): |
paulb@309 | 348 | |
paulb@309 | 349 | "An XML-RPC name element." |
paulb@309 | 350 | |
paulb@309 | 351 | pass |
paulb@309 | 352 | |
paulb@307 | 353 | class XMLRPCValueElement(XMLRPCStringElement): |
paulb@307 | 354 | |
paulb@307 | 355 | "An XML-RPC value element." |
paulb@307 | 356 | |
paulb@307 | 357 | def _type(self): |
paulb@307 | 358 | elements = self.xpath("*") |
paulb@307 | 359 | if elements: |
paulb@307 | 360 | return elements[0].localName |
paulb@307 | 361 | else: |
paulb@307 | 362 | return "string" |
paulb@307 | 363 | |
paul@342 | 364 | def _setType(self, typename): |
paul@342 | 365 | new_contents = self.ownerDocument.createElement(typename) |
paul@342 | 366 | self.add_or_replace_element(new_contents) |
paul@342 | 367 | |
paulb@307 | 368 | def _container(self): |
paulb@307 | 369 | return (self.xpath("*") or [self])[0] |
paulb@307 | 370 | |
paul@342 | 371 | type = property(_type, _setType) |
paulb@307 | 372 | container = property(_container) |
paulb@307 | 373 | |
paulb@304 | 374 | class XMLRPCMethodNameElement(XMLRPCStringElement): |
paulb@304 | 375 | |
paulb@304 | 376 | "An XML-RPC method element." |
paulb@304 | 377 | |
paulb@304 | 378 | pass |
paulb@304 | 379 | |
paulb@304 | 380 | class XMLRPCIntegerElement(XMLRPCStringElement): |
paulb@304 | 381 | |
paulb@304 | 382 | "An XML-RPC integer element." |
paulb@304 | 383 | |
paulb@315 | 384 | typename = "int" |
paulb@304 | 385 | |
paulb@304 | 386 | class XMLRPCBooleanElement(XMLRPCStringElement): |
paulb@304 | 387 | |
paulb@304 | 388 | "An XML-RPC boolean element." |
paulb@304 | 389 | |
paulb@315 | 390 | typename = "boolean" |
paulb@304 | 391 | |
paulb@304 | 392 | class XMLRPCDoubleElement(XMLRPCStringElement): |
paulb@304 | 393 | |
paulb@304 | 394 | "An XML-RPC double floating point number element." |
paulb@304 | 395 | |
paulb@315 | 396 | typename = "double" |
paulb@304 | 397 | |
paulb@304 | 398 | class XMLRPCDateTimeElement(XMLRPCStringElement): |
paulb@304 | 399 | |
paulb@304 | 400 | "An XML-RPC date/time element." |
paulb@304 | 401 | |
paulb@315 | 402 | typename = "datetime" |
paulb@304 | 403 | |
paulb@304 | 404 | class XMLRPCBase64Element(XMLRPCStringElement): |
paulb@304 | 405 | |
paulb@304 | 406 | "An XML-RPC integer element." |
paulb@304 | 407 | |
paulb@315 | 408 | typename = "base64" |
paulb@304 | 409 | |
paulb@304 | 410 | class XMLRPCFaultElement(XMLRPCNode): |
paulb@304 | 411 | |
paulb@304 | 412 | "An XML-RPC fault element." |
paulb@304 | 413 | |
paulb@304 | 414 | def _code(self): |
paulb@307 | 415 | code = self.xpath("./value/struct/member[./name/text() = 'faultCode']/value/int") |
paulb@307 | 416 | if code: |
paulb@307 | 417 | return code[0].value |
paulb@307 | 418 | else: |
paulb@307 | 419 | return None |
paulb@304 | 420 | |
paulb@304 | 421 | def _reason(self): |
paulb@307 | 422 | reason = self.xpath("./value/struct/member[./name/text() = 'faultString']/value/string") |
paulb@307 | 423 | if reason: |
paulb@307 | 424 | return reason[0].value |
paulb@307 | 425 | else: |
paulb@307 | 426 | return None |
paulb@304 | 427 | |
paulb@304 | 428 | code = property(_code) |
paulb@304 | 429 | reason = property(_reason) |
paulb@304 | 430 | |
paulb@315 | 431 | # Conversion functions. |
paulb@315 | 432 | |
paulb@315 | 433 | def convert(typename, value): |
paulb@315 | 434 | return default_converters[typename](value) |
paulb@315 | 435 | |
paulb@315 | 436 | def boolean(s): |
paulb@315 | 437 | if s.lower() == "true": |
paulb@315 | 438 | return True |
paulb@315 | 439 | elif s.lower() == "false": |
paulb@315 | 440 | return False |
paulb@315 | 441 | else: |
paulb@315 | 442 | raise ValueError, "String value %s not convertable to boolean." % repr(s) |
paulb@315 | 443 | |
paulb@315 | 444 | def iso8601(s): |
paulb@315 | 445 | year, month, day, hour, minute, second = map(int, (s[:4], s[4:6], s[6:8], s[9:11], s[12:14], s[15:17])) |
paulb@315 | 446 | return datetime.datetime(year, month, day, hour, minute, second) |
paulb@315 | 447 | |
paulb@315 | 448 | default_converters = { |
paulb@315 | 449 | "string" : unicode, |
paulb@315 | 450 | "int" : int, |
paulb@315 | 451 | "i4" : int, |
paulb@315 | 452 | "double" : float, |
paulb@315 | 453 | "boolean" : boolean, |
paulb@315 | 454 | "dateTime.iso8601" : iso8601, |
paulb@315 | 455 | "base64" : str |
paulb@315 | 456 | } |
paulb@315 | 457 | |
paul@342 | 458 | # Implementation-related functionality. |
paul@342 | 459 | |
paul@342 | 460 | class XMLRPCImplementation(libxml2dom.Implementation): |
paul@342 | 461 | |
paul@342 | 462 | "Contains an XML-RPC-specific implementation." |
paul@342 | 463 | |
paul@342 | 464 | # Mapping of element names to wrappers. |
paul@342 | 465 | |
paul@342 | 466 | _class_for_name = { |
paul@342 | 467 | "methodCall" : XMLRPCMethodElement, |
paul@342 | 468 | "methodResponse" : XMLRPCMethodElement, |
paul@342 | 469 | "methodName" : XMLRPCMethodNameElement, |
paul@342 | 470 | "params" : XMLRPCParametersElement, |
paul@342 | 471 | "param" : XMLRPCParameterElement, |
paul@342 | 472 | "fault" : XMLRPCFaultElement, |
paul@342 | 473 | "string" : XMLRPCStringElement, |
paul@342 | 474 | "int" : XMLRPCIntegerElement, |
paul@342 | 475 | "i4" : XMLRPCIntegerElement, |
paul@342 | 476 | "boolean" : XMLRPCBooleanElement, |
paul@342 | 477 | "double" : XMLRPCDoubleElement, |
paul@342 | 478 | "dateTime.iso8601" : XMLRPCDateTimeElement, |
paul@342 | 479 | "base64" : XMLRPCBase64Element, |
paul@342 | 480 | "struct" : XMLRPCStructElement, |
paul@342 | 481 | "member" : XMLRPCMemberElement, |
paul@342 | 482 | "value" : XMLRPCValueElement, |
paul@342 | 483 | "name" : XMLRPCNameElement, |
paul@342 | 484 | "array" : XMLRPCArrayElement, |
paul@342 | 485 | "data" : XMLRPCDataElement |
paul@342 | 486 | } |
paul@342 | 487 | |
paul@342 | 488 | # Wrapping of documents. |
paul@342 | 489 | |
paul@342 | 490 | def adoptDocument(self, node): |
paul@342 | 491 | return XMLRPCDocument(node, self) |
paul@342 | 492 | |
paul@342 | 493 | # Factory functions. |
paul@342 | 494 | |
paul@342 | 495 | def get_node(self, _node, context_node): |
paul@342 | 496 | |
paul@342 | 497 | """ |
paul@342 | 498 | Get a libxml2dom node for the given low-level '_node' and libxml2dom |
paul@342 | 499 | 'context_node'. |
paul@342 | 500 | """ |
paul@342 | 501 | |
paul@342 | 502 | if Node_nodeType(_node) == context_node.ELEMENT_NODE: |
paul@342 | 503 | |
paul@342 | 504 | # Make special objects for certain elements. |
paul@342 | 505 | # Otherwise, make generic XML-RPC elements. |
paul@342 | 506 | |
paul@342 | 507 | cls = self._class_for_name.get(Node_localName(_node)) or XMLRPCElement |
paul@342 | 508 | return cls(_node, self, context_node.ownerDocument) |
paul@342 | 509 | |
paul@342 | 510 | else: |
paul@342 | 511 | return libxml2dom.Implementation.get_node(self, _node, context_node) |
paul@342 | 512 | |
paul@342 | 513 | # Convenience functions. |
paul@342 | 514 | |
paul@342 | 515 | def createXMLRPCMessage(self, namespaceURI, localName): |
paul@342 | 516 | |
paul@342 | 517 | "Create a new XML-RPC message document (fragment)." |
paul@342 | 518 | |
paul@342 | 519 | return XMLRPCDocument(Node_createDocument(namespaceURI, localName, None), self).documentElement |
paul@342 | 520 | |
paul@342 | 521 | def createMethodCall(self, name=None): |
paul@342 | 522 | |
paul@342 | 523 | """ |
paul@342 | 524 | Create and return a message fragment for a method call having the given |
paul@342 | 525 | 'name'. |
paul@342 | 526 | """ |
paul@342 | 527 | |
paul@342 | 528 | message = self.createXMLRPCMessage(None, "methodCall") |
paul@342 | 529 | if name is not None: |
paul@342 | 530 | message.methodName = name |
paul@342 | 531 | return message |
paul@342 | 532 | |
paul@342 | 533 | def createMethodResponse(self): |
paul@342 | 534 | |
paul@342 | 535 | "Create and return a message fragment for a method response." |
paul@342 | 536 | |
paul@342 | 537 | return self.createXMLRPCMessage(None, "methodResponse") |
paul@342 | 538 | |
paulb@304 | 539 | # Utility functions. |
paulb@304 | 540 | |
paulb@304 | 541 | createDocument = libxml2dom.createDocument |
paulb@304 | 542 | createDocumentType = libxml2dom.createDocumentType |
paulb@304 | 543 | |
paulb@304 | 544 | def createXMLRPCMessage(namespaceURI, localName): |
paulb@307 | 545 | return default_impl.createXMLRPCMessage(None, localName) |
paulb@307 | 546 | |
paul@342 | 547 | def createMethodCall(name=None): |
paul@342 | 548 | return default_impl.createMethodCall(name) |
paulb@307 | 549 | |
paulb@307 | 550 | def createMethodResponse(): |
paulb@307 | 551 | return default_impl.createMethodResponse() |
paulb@304 | 552 | |
paulb@304 | 553 | def parse(stream_or_string, html=0, htmlencoding=None, unfinished=0, impl=None): |
paulb@304 | 554 | return libxml2dom.parse(stream_or_string, html=html, htmlencoding=htmlencoding, unfinished=unfinished, impl=(impl or default_impl)) |
paulb@304 | 555 | |
paulb@304 | 556 | def parseFile(filename, html=0, htmlencoding=None, unfinished=0, impl=None): |
paulb@304 | 557 | return libxml2dom.parseFile(filename, html=html, htmlencoding=htmlencoding, unfinished=unfinished, impl=(impl or default_impl)) |
paulb@304 | 558 | |
paulb@304 | 559 | def parseString(s, html=0, htmlencoding=None, unfinished=0, impl=None): |
paulb@304 | 560 | return libxml2dom.parseString(s, html=html, htmlencoding=htmlencoding, unfinished=unfinished, impl=(impl or default_impl)) |
paulb@304 | 561 | |
paulb@304 | 562 | def parseURI(uri, html=0, htmlencoding=None, unfinished=0, impl=None): |
paulb@304 | 563 | return libxml2dom.parseURI(uri, html=html, htmlencoding=htmlencoding, unfinished=unfinished, impl=(impl or default_impl)) |
paulb@304 | 564 | |
paulb@304 | 565 | # Single instance of the implementation. |
paulb@304 | 566 | |
paulb@304 | 567 | default_impl = XMLRPCImplementation() |
paulb@304 | 568 | |
paulb@304 | 569 | # vim: tabstop=4 expandtab shiftwidth=4 |