# HG changeset patch # User paulb # Date 1191703573 0 # Node ID c2860e2547ab396da4011ac9363347d77ab68f4d # Parent 832e661001e161be9cc6e8acd8ce8be83f749a1d [project @ 2007-10-06 20:46:10 by paulb] Introduced the ParameterValue class and removed equivalence between tuples and parameter names and values. diff -r 832e661001e1 -r c2860e2547ab libxml2dom/rpc.py --- a/libxml2dom/rpc.py Sat Oct 06 19:33:47 2007 +0000 +++ b/libxml2dom/rpc.py Sat Oct 06 20:46:13 2007 +0000 @@ -32,8 +32,7 @@ self.name = name def __eq__(self, other): - other_ns, other_name = other - return self.ns, to_localName(self.name) == other_ns, to_localName(other_name) + return self.ns, to_localName(self.name) == other.ns, to_localName(other.name) def __hash__(self): return hash(self.ns + to_localName(self.name)) @@ -41,40 +40,51 @@ def __repr__(self): return "ParameterName(%s, %s)" % (repr(self.ns), repr(self.name or None)) +class ParameterValue(object): + + "A method parameter value." + + def __init__(self, name, value): + self.name = name + self.value = value + + def convert(self, converters=None): + conv = default_converters + conv.update(converters or {}) + typename = self.name.ns + localName = to_localName(self.name.name) + if isinstance(self.value, list): + return [item.convert(converters) for item in self.value] + else: + functions = conv.get(typename, {}) + function = functions.get(localName) or functions.get(None, unicode) + return function(self.value) + + def _data(self): + return self.convert() + + data = property(_data) + + def __eq__(self, other): + return self.name == other.name and self.value == other.value + + def __hash__(self): + return hash(self.value) + + def __repr__(self): + return "ParameterValue(%s, %s)" % (repr(self.name), repr(self.value)) + # Sequence emulation. def __len__(self): - return 2 + return len(self.value) def __getitem__(self, i): - return (self.ns, self.name)[i] + return self.value[i] def to_localName(name): return (name or "").split(":")[-1] or None -def convert(parameters, converters=None): - - """ - Convert the 'parameters', returning a list of name, value items where the - names are the plain names for each parameter, and the values may be - converted from strings to other data types. - """ - - conv = default_converters - conv.update(converters or {}) - results = [] - for parameter_name, parameter_value in parameters: - typename, name = parameter_name - localName = to_localName(name) - if isinstance(parameter_value, list): - value = convert(parameter_value, converters) - else: - functions = conv.get(typename, {}) - function = functions.get(localName) or functions.get(None, unicode) - value = function(parameter_value) - results.append((localName, value)) - return results - # Utility functions. def boolean(s): diff -r 832e661001e1 -r c2860e2547ab libxml2dom/soap.py --- a/libxml2dom/soap.py Sat Oct 06 19:33:47 2007 +0000 +++ b/libxml2dom/soap.py Sat Oct 06 20:46:13 2007 +0000 @@ -32,7 +32,7 @@ from libxml2dom.macrolib import * from libxml2dom.macrolib import \ createDocument as Node_createDocument -from libxml2dom.rpc import ParameterName +from libxml2dom.rpc import ParameterName, ParameterValue # SOAP-related namespaces. @@ -225,13 +225,13 @@ def _parameters(self): return self.xpath("*") - def _rawParameterValues(self): + def _parameterValues(self): values = [] for parameter in self.parameters: values.append(self._get_value(parameter)) return values - def _setRawParameterValues(self, parameters): + def _setParameterValues(self, parameters): for node in self.parameters: self.removeChild(node) @@ -240,27 +240,23 @@ for parameter in parameters: self._add_value(self, parameter) - def _parameterValues(self): - return libxml2dom.rpc.convert(self.rawParameterValues, getattr(self.ownerDocument, "converters", None)) - # Internal methods. def _add_value(self, value, parameter): "Add to the 'value' element the given 'parameter'." - parameter_name, parameter_value = parameter - container = self.ownerDocument.createElementNS(*parameter_name) + container = self.ownerDocument.createElementNS(parameter.name.ns, parameter.name.name) value.appendChild(container) - if isinstance(parameter_value, (list, dict)): - if isinstance(parameter_value, dict): - items = parameter_value.items() + if isinstance(parameter.value, (list, dict)): + if isinstance(parameter.value, dict): + items = parameter.value.items() else: - items = parameter_value + items = parameter.value for item in items: self._add_value(container, item) else: - text = self.ownerDocument.createTextNode(unicode(parameter_value)) + text = self.ownerDocument.createTextNode(unicode(parameter.value)) container.appendChild(text) def _get_value(self, parameter): @@ -272,16 +268,15 @@ items = [] for element in elements: items.append(self._get_value(element)) - return ParameterName(parameter.namespaceURI, parameter.name), items + return ParameterValue(ParameterName(parameter.namespaceURI, parameter.name), items) else: - return ParameterName(parameter.namespaceURI, parameter.name), parameter.textContent.strip() + return ParameterValue(ParameterName(parameter.namespaceURI, parameter.name), parameter.textContent.strip()) methodName = property(_methodName) resultParameter = property(_resultParameter) resultParameterValue = property(_resultParameterValue) parameters = property(_parameters) - rawParameterValues = property(_rawParameterValues, _setRawParameterValues) - parameterValues = property(_parameterValues) + parameterValues = property(_parameterValues, _setParameterValues) class SOAPFaultElement(SOAPNode): diff -r 832e661001e1 -r c2860e2547ab libxml2dom/xmlrpc.py --- a/libxml2dom/xmlrpc.py Sat Oct 06 19:33:47 2007 +0000 +++ b/libxml2dom/xmlrpc.py Sat Oct 06 20:46:13 2007 +0000 @@ -32,7 +32,7 @@ from libxml2dom.macrolib import * from libxml2dom.macrolib import \ createDocument as Node_createDocument -from libxml2dom.rpc import ParameterName +from libxml2dom.rpc import ParameterName, ParameterValue class XMLRPCImplementation(libxml2dom.Implementation): @@ -172,7 +172,7 @@ def _parameters(self): return self.xpath("./params/param") - def _rawParameterValues(self): + def _parameterValues(self): values = self.xpath("./params/param/value") if values: items = [] @@ -182,7 +182,7 @@ else: return [] - def _setRawParameterValues(self, parameters): + def _setParameterValues(self, parameters): param_list = self.parameters params = (self.xpath("./params") or [None])[0] @@ -205,22 +205,17 @@ param.appendChild(value) self._add_value(value, parameter) - def _parameterValues(self): - return libxml2dom.rpc.convert(self.rawParameterValues) - # Internal methods. def _add_value(self, value, parameter): "Add to the 'value' element the given 'parameter'." - (typename, parameter_name), parameter_value = parameter - - if typename == "struct": - if isinstance(parameter_value, dict): - items = parameter_value.items() + if parameter.name.ns == "struct": + if isinstance(parameter.value, dict): + items = parameter.value.items() else: - items = parameter_value + items = parameter.value # Create a struct element and add the members. @@ -228,13 +223,12 @@ value.appendChild(struct) for item in items: - (item_typename, item_name), item_value = item member = struct.createMember() struct.appendChild(member) # Peek into the item to set up the name. - member.memberName = item_name + member.memberName = item.name # Add the item inside a new value element. @@ -242,7 +236,7 @@ member.appendChild(memberValue) self._add_value(memberValue, item) - elif typename == "array": + elif parameter.name.ns == "array": # Create an array element and add the members. @@ -251,7 +245,7 @@ data = array.createData() array.appendChild(data) - for item in parameter_value: + for item in parameter.value: # Add the item inside a new value element. @@ -260,9 +254,9 @@ self._add_value(data_value, item) else: - container = self.ownerDocument.createElement(typename) + container = self.ownerDocument.createElement(parameter.name.ns) value.appendChild(container) - container.value = unicode(parameter_value) + container.value = unicode(parameter.value) def _get_value(self, value, name=None): @@ -279,16 +273,16 @@ for member in value.container.members: items.append(self._get_value(member.value, member.memberName)) - return (ParameterName(value.type, name), items) + return ParameterValue(ParameterName(value.type, name), items) elif value.type == "array": items = [] for data_value in value.container.data.values: items.append(self._get_value(data_value)) - return (ParameterName(value.type, name), items) + return ParameterValue(ParameterName(value.type, name), items) else: - return (ParameterName(value.type, name), value.container.value) + return ParameterValue(ParameterName(value.type, name), value.container.value) # Node construction methods. @@ -305,8 +299,7 @@ methodNameElement = property(_methodNameElement) methodName = property(_methodName, _setMethodName) parameters = property(_parameters) - rawParameterValues = property(_rawParameterValues, _setRawParameterValues) - parameterValues = property(_parameterValues) + parameterValues = property(_parameterValues, _setParameterValues) class XMLRPCArrayElement(XMLRPCNode): diff -r 832e661001e1 -r c2860e2547ab tests/soap_test.py --- a/tests/soap_test.py Sat Oct 06 19:33:47 2007 +0000 +++ b/tests/soap_test.py Sat Oct 06 20:46:13 2007 +0000 @@ -2,6 +2,7 @@ # -*- coding: iso-8859-15 -*- import libxml2dom.soap +from libxml2dom.rpc import ParameterName, ParameterValue request = """ @@ -31,19 +32,19 @@ req = libxml2dom.soap.parseString(request) assert req.method.methodName == "chargeReservation" -assert req.method.rawParameterValues == [ - (("http://travelcompany.example.org/reservation", "reservation"), [ - (("http://travelcompany.example.org/reservation", "code"), "FT35ZBQ") +assert req.method.parameterValues == [ + ParameterValue(ParameterName("http://travelcompany.example.org/reservation", "reservation"), [ + ParameterValue(ParameterName("http://travelcompany.example.org/reservation", "code"), "FT35ZBQ") ]), - (("http://mycompany.example.com/financial", "creditCard"), [ - (("http://mycompany.example.com/employees", "name"), u"Åke Jógvan Øyvind"), - (("http://mycompany.example.com/financial", "number"), "123456789099999"), - (("http://mycompany.example.com/financial", "expiration"), "2005-02") + ParameterValue(ParameterName("http://mycompany.example.com/financial", "creditCard"), [ + ParameterValue(ParameterName("http://mycompany.example.com/employees", "name"), u"Åke Jógvan Øyvind"), + ParameterValue(ParameterName("http://mycompany.example.com/financial", "number"), "123456789099999"), + ParameterValue(ParameterName("http://mycompany.example.com/financial", "expiration"), "2005-02") ]) ] assert req.fault is None print "Method name:", req.method.methodName -print "Parameter values:", req.method.rawParameterValues +print "Parameter values:", req.method.parameterValues print "Fault:", req.fault response = """ @@ -68,13 +69,14 @@ resp = libxml2dom.soap.parseString(response) assert resp.method.methodName == "chargeReservationResponse" -assert resp.method.rawParameterValues == [ - (("http://travelcompany.example.org/", "code"), "FT35ZBQ"), - (("http://travelcompany.example.org/", "viewAt"), "http://travelcompany.example.org/reservations?code=FT35ZBQ") +assert resp.method.parameterValues == [ + ParameterValue(ParameterName("http://travelcompany.example.org/", "code"), "FT35ZBQ"), + ParameterValue(ParameterName("http://travelcompany.example.org/", "viewAt"), + "http://travelcompany.example.org/reservations?code=FT35ZBQ") ] assert resp.fault is None print "Method name:", resp.method.methodName -print "Parameter values:", resp.method.rawParameterValues +print "Parameter values:", resp.method.parameterValues print "Fault:", resp.fault response2 = """ @@ -102,15 +104,16 @@ resp2 = libxml2dom.soap.parseString(response2) assert resp2.method.methodName == "chargeReservationResponse" -assert resp2.method.rawParameterValues == [ - (("http://www.w3.org/2003/05/soap-rpc", "result"), "m:status"), - (("http://travelcompany.example.org/", "status"), "confirmed"), - (("http://travelcompany.example.org/", "code"), "FT35ZBQ"), - (("http://travelcompany.example.org/", "viewAt"), "http://travelcompany.example.org/reservations?code=FT35ZBQ") +assert resp2.method.parameterValues == [ + ParameterValue(ParameterName("http://www.w3.org/2003/05/soap-rpc", "result"), "m:status"), + ParameterValue(ParameterName("http://travelcompany.example.org/", "status"), "confirmed"), + ParameterValue(ParameterName("http://travelcompany.example.org/", "code"), "FT35ZBQ"), + ParameterValue(ParameterName("http://travelcompany.example.org/", "viewAt"), + "http://travelcompany.example.org/reservations?code=FT35ZBQ") ] assert resp2.fault is None print "Method name:", resp2.method.methodName -print "Parameter values:", resp2.method.rawParameterValues +print "Parameter values:", resp2.method.parameterValues print "Fault:", resp2.fault failed = """ diff -r 832e661001e1 -r c2860e2547ab tests/xmlrpc_test.py --- a/tests/xmlrpc_test.py Sat Oct 06 19:33:47 2007 +0000 +++ b/tests/xmlrpc_test.py Sat Oct 06 20:46:13 2007 +0000 @@ -1,6 +1,7 @@ #!/usr/bin/env python import libxml2dom.xmlrpc +from libxml2dom.rpc import ParameterName, ParameterValue # Some examples from the specification. @@ -16,10 +17,10 @@ req = libxml2dom.xmlrpc.parseString(request) assert req.method.methodName == "examples.getStateName" -assert req.method.rawParameterValues == [(("i4", ""), "41")] +assert req.method.parameterValues == [ParameterValue(ParameterName("i4", ""), "41")] assert req.fault is None print "Method name:", req.method.methodName -print "Parameter values:", req.method.rawParameterValues +print "Parameter values:", req.method.parameterValues print "Fault:", req.fault response = """ @@ -33,10 +34,10 @@ resp = libxml2dom.xmlrpc.parseString(response) assert resp.method.methodName is None -assert resp.method.rawParameterValues == [(("string", ""), "South Dakota")] +assert resp.method.parameterValues == [ParameterValue(ParameterName("string", ""), "South Dakota")] assert resp.fault is None print "Method name:", resp.method.methodName -print "Parameter values:", resp.method.rawParameterValues +print "Parameter values:", resp.method.parameterValues print "Fault:", resp.fault failed = """ @@ -59,11 +60,11 @@ f = libxml2dom.xmlrpc.parseString(failed) assert f.method.methodName is None -assert f.method.rawParameterValues == [] +assert f.method.parameterValues == [] assert f.fault.code == "4" assert f.fault.reason == "Too many parameters." print "Method name:", f.method.methodName -print "Parameter values:", f.method.rawParameterValues +print "Parameter values:", f.method.parameterValues print "Fault code:", f.fault.code # Python Package Index examples. @@ -96,15 +97,16 @@ s = libxml2dom.xmlrpc.parseString(search) assert s.method.methodName == "search" -assert s.method.rawParameterValues == [ - (("struct", None), [ - (("string", "name"), "libxml2dom"), (("string", "description"), "XML") +assert s.method.parameterValues == [ + ParameterValue(ParameterName("struct", None), [ + ParameterValue(ParameterName("string", "name"), "libxml2dom"), + ParameterValue(ParameterName("string", "description"), "XML") ]), - (("string", None), "and") + ParameterValue(ParameterName("string", None), "and") ] assert s.fault is None print "Method name:", s.method.methodName -print "Parameter values:", s.method.rawParameterValues +print "Parameter values:", s.method.parameterValues print "Fault:", s.fault # Nested structure examples. @@ -144,15 +146,18 @@ s2 = libxml2dom.xmlrpc.parseString(search2) assert s2.method.methodName == "search" -assert s2.method.rawParameterValues == [ - (("struct", None), [ - (("struct", "names"), [(("string", "name"), "libxml2dom"), (("string", "description"), "XML")]) +assert s2.method.parameterValues == [ + ParameterValue(ParameterName("struct", None), [ + ParameterValue(ParameterName("struct", "names"), [ + ParameterValue(ParameterName("string", "name"), "libxml2dom"), + ParameterValue(ParameterName("string", "description"), "XML") + ]) ]), - (("string", None), "and") + ParameterValue(ParameterName("string", None), "and") ] assert s2.fault is None print "Method name:", s2.method.methodName -print "Parameter values:", s2.method.rawParameterValues +print "Parameter values:", s2.method.parameterValues print "Fault:", s2.fault arrays = """ @@ -177,12 +182,15 @@ a = libxml2dom.xmlrpc.parseString(arrays) assert a.method.methodName is None -assert a.method.rawParameterValues == [ - (("array", None), [(("string", None), "libxml2dom"), (("string", None), "XSLTools")]) +assert a.method.parameterValues == [ + ParameterValue(ParameterName("array", None), [ + ParameterValue(ParameterName("string", None), "libxml2dom"), + ParameterValue(ParameterName("string", None), "XSLTools") + ]) ] assert a.fault is None print "Method name:", a.method.methodName -print "Parameter values:", a.method.rawParameterValues +print "Parameter values:", a.method.parameterValues print "Fault:", a.fault # vim: tabstop=4 expandtab shiftwidth=4