def dumps(params, methodname=None, methodresponse=None, encoding=None,
allow_none=0):
"""data [,options] -> marshalled data
Convert an argument tuple or a Fault instance to an XML-RPC
request (or response, if the methodresponse option is used).
In addition to the data object, the following options can be given
as keyword arguments:
methodname: the method name for a methodCall packet
methodresponse: true to create a methodResponse packet.
If this option is used with a tuple, the tuple must be
a singleton (i.e. it can contain only one element).
encoding: the packet encoding (default is UTF-8)
All 8-bit strings in the data structure are assumed to use the
packet encoding. Unicode strings are automatically converted,
where necessary.
"""
#=====================================================================================================
# Support for extensions namespace
def dump_None(m, value, write):
if not m.allow_none:
raise TypeError, "cannot marshal None unless allow_none is enabled"
write("")
#=====================================================================================================
assert isinstance(params, TupleType) or isinstance(params, Fault),\
"argument must be tuple or Fault instance"
if isinstance(params, Fault):
methodresponse = 1
elif methodresponse and isinstance(params, TupleType):
assert len(params) == 1, "response tuple must be a singleton"
if not encoding:
encoding = "utf-8"
#=====================================================================================================
# Support for the 'ex' namespace prefix
m = xmlrpclib.Marshaller(encoding, allow_none)
m.dispatch[NoneType] = lambda *args:dump_None(*args)
#=====================================================================================================
data = m.dumps(params)
if encoding != "utf-8":
xmlheader = "\n" % str(encoding)
else:
xmlheader = "\n" # utf-8 is default
# standard XML-RPC wrappings
if methodname:
# a method call
if not isinstance(methodname, str):
methodname = methodname.encode(encoding)
data = (
xmlheader,
#=====================================================================================================
# Support for extensions namespace
"\n"
#=====================================================================================================
"", methodname, "\n",
data,
"\n"
)
elif methodresponse:
# a method response, or a fault structure
data = (
xmlheader,
#=====================================================================================================
# Support for extensions namespace
"\n",
#=====================================================================================================
data,
"\n"
)
else:
return data # return as is
return StringType.join('', data)