XEP-0009: Updated RPC value conversion code

Updated the XML-RPC value conversion to correctly apply namespaces, and
fixed an error uncovered by the tests in the XML -> Python conversion of
dateTime values.
This commit is contained in:
Correl Roush 2011-12-20 02:03:06 -05:00
parent 2b3d11a7a5
commit 6c58b8cc4b

View file

@ -42,46 +42,46 @@ def py2xml(*args):
def _py2xml(*args): def _py2xml(*args):
for x in args: for x in args:
val = ET.Element("value") val = ET.Element("{%s}value" % _namespace)
if x is None: if x is None:
nil = ET.Element("nil") nil = ET.Element("{%s}nil" % _namespace)
val.append(nil) val.append(nil)
elif type(x) is int: elif type(x) is int:
i4 = ET.Element("i4") i4 = ET.Element("{%s}i4" % _namespace)
i4.text = str(x) i4.text = str(x)
val.append(i4) val.append(i4)
elif type(x) is bool: elif type(x) is bool:
boolean = ET.Element("boolean") boolean = ET.Element("{%s}boolean" % _namespace)
boolean.text = str(int(x)) boolean.text = str(int(x))
val.append(boolean) val.append(boolean)
elif type(x) is str: elif type(x) is str:
string = ET.Element("string") string = ET.Element("{%s}string" % _namespace)
string.text = x string.text = x
val.append(string) val.append(string)
elif type(x) is float: elif type(x) is float:
double = ET.Element("double") double = ET.Element("{%s}double" % _namespace)
double.text = str(x) double.text = str(x)
val.append(double) val.append(double)
elif type(x) is rpcbase64: elif type(x) is rpcbase64:
b64 = ET.Element("base64") b64 = ET.Element("{%s}base64" % _namespace)
b64.text = x.encoded() b64.text = x.encoded()
val.append(b64) val.append(b64)
elif type(x) is rpctime: elif type(x) is rpctime:
iso = ET.Element("dateTime.iso8601") iso = ET.Element("{%s}dateTime.iso8601" % _namespace)
iso.text = str(x) iso.text = str(x)
val.append(iso) val.append(iso)
elif type(x) in (list, tuple): elif type(x) in (list, tuple):
array = ET.Element("array") array = ET.Element("{%s}array" % _namespace)
data = ET.Element("data") data = ET.Element("{%s}data" % _namespace)
for y in x: for y in x:
data.append(_py2xml(y)) data.append(_py2xml(y))
array.append(data) array.append(data)
val.append(array) val.append(array)
elif type(x) is dict: elif type(x) is dict:
struct = ET.Element("struct") struct = ET.Element("{%s}struct" % _namespace)
for y in x.keys(): for y in x.keys():
member = ET.Element("member") member = ET.Element("{%s}member" % _namespace)
name = ET.Element("name") name = ET.Element("{%s}name" % _namespace)
name.text = y name.text = y
member.append(name) member.append(name)
member.append(_py2xml(x[y])) member.append(_py2xml(x[y]))
@ -110,13 +110,13 @@ def _xml2py(value):
return value.find('{%s}string' % namespace).text return value.find('{%s}string' % namespace).text
if value.find('{%s}double' % namespace) is not None: if value.find('{%s}double' % namespace) is not None:
return float(value.find('{%s}double' % namespace).text) return float(value.find('{%s}double' % namespace).text)
if value.find('{%s}base64') is not None: if value.find('{%s}base64' % namespace) is not None:
return rpcbase64(value.find('base64' % namespace).text) return rpcbase64(value.find('{%s}base64' % namespace).text)
if value.find('{%s}Base64') is not None: if value.find('{%s}Base64' % namespace) is not None:
# Older versions of XEP-0009 used Base64 # Older versions of XEP-0009 used Base64
return rpcbase64(value.find('Base64' % namespace).text) return rpcbase64(value.find('{%s}Base64' % namespace).text)
if value.find('{%s}dateTime.iso8601') is not None: if value.find('{%s}dateTime.iso8601' % namespace) is not None:
return rpctime(value.find('{%s}dateTime.iso8601')) return rpctime(value.find('{%s}dateTime.iso8601' % namespace).text)
if value.find('{%s}struct' % namespace) is not None: if value.find('{%s}struct' % namespace) is not None:
struct = {} struct = {}
for member in value.find('{%s}struct' % namespace).findall('{%s}member' % namespace): for member in value.find('{%s}struct' % namespace).findall('{%s}member' % namespace):