Completed the python 3 port

This commit is contained in:
Christophe de Vienne 2012-04-26 00:26:08 +02:00
parent f475504e98
commit 4aae5cc528
2 changed files with 30 additions and 14 deletions

View File

@ -99,6 +99,8 @@ def fromxml(datatype, element):
if sub is not None: if sub is not None:
setattr(obj, attrdef.key, fromxml(attrdef.datatype, sub)) setattr(obj, attrdef.key, fromxml(attrdef.datatype, sub))
return obj return obj
if datatype is wsme.types.bytes:
return element.text.encode('ascii')
return datatype(element.text) return datatype(element.text)
@ -119,8 +121,7 @@ def dict_toxml(datatype, key, value):
if value is None: if value is None:
el.set('nil', 'true') el.set('nil', 'true')
else: else:
key_type = datatype.keys()[0] key_type, value_type = list(datatype.items())[0]
value_type = datatype.values()[0]
for item in value.items(): for item in value.items():
key = toxml(key_type, 'key', item[0]) key = toxml(key_type, 'key', item[0])
value = toxml(value_type, 'value', item[1]) value = toxml(value_type, 'value', item[1])
@ -131,6 +132,16 @@ def dict_toxml(datatype, key, value):
return el return el
@toxml.when_object(wsme.types.bytes)
def bytes_toxml(datatype, key, value):
el = et.Element(key)
if value is None:
el.set('nil', 'true')
else:
el.text = value.decode('ascii')
return el
@toxml.when_object(bool) @toxml.when_object(bool)
def bool_toxml(datatype, key, value): def bool_toxml(datatype, key, value):
el = et.Element(key) el = et.Element(key)
@ -172,8 +183,7 @@ def array_fromxml(datatype, element):
def dict_fromxml(datatype, element): def dict_fromxml(datatype, element):
if element.get('nil') == 'true': if element.get('nil') == 'true':
return None return None
key_type = datatype.keys()[0] key_type, value_type = list(datatype.items())[0]
value_type = datatype.values()[0]
return dict(( return dict((
(fromxml(key_type, item.find('key')), (fromxml(key_type, item.find('key')),
fromxml(value_type, item.find('value'))) fromxml(value_type, item.find('value')))

View File

@ -31,8 +31,10 @@ def dumpxml(key, obj, datatype=None):
node.append(dumpxml('key', item[0], key_type)) node.append(dumpxml('key', item[0], key_type))
node.append(dumpxml('value', item[1], value_type)) node.append(dumpxml('value', item[1], value_type))
elif datatype == wsme.types.binary: elif datatype == wsme.types.binary:
el.text = base64.encodestring(obj) el.text = base64.encodestring(obj).decode('ascii')
elif isinstance(obj, six.string_types): elif isinstance(obj, wsme.types.bytes):
el.text = obj.decode('ascii')
elif isinstance(obj, wsme.types.text):
el.text = obj el.text = obj
elif type(obj) in (int, float, decimal.Decimal): elif type(obj) in (int, float, decimal.Decimal):
el.text = six.text_type(obj) el.text = six.text_type(obj)
@ -48,6 +50,7 @@ def dumpxml(key, obj, datatype=None):
elif type(obj) == dict: elif type(obj) == dict:
for name, value in obj.items(): for name, value in obj.items():
el.append(dumpxml(name, value)) el.append(dumpxml(name, value))
print(obj, datatype, et.tostring(el))
return el return el
@ -58,9 +61,10 @@ def loadxml(el, datatype):
if isinstance(datatype, list): if isinstance(datatype, list):
return [loadxml(item, datatype[0]) for item in el.findall('item')] return [loadxml(item, datatype[0]) for item in el.findall('item')]
elif isinstance(datatype, dict): elif isinstance(datatype, dict):
key_type, value_type = list(datatype.items())[0]
return dict(( return dict((
(loadxml(item.find('key'), datatype.keys()[0]), (loadxml(item.find('key'), key_type),
loadxml(item.find('value'), datatype.values()[0])) loadxml(item.find('value'), value_type))
for item in el.findall('item') for item in el.findall('item')
)) ))
elif len(el): elif len(el):
@ -75,7 +79,7 @@ def loadxml(el, datatype):
return d return d
else: else:
if datatype == wsme.types.binary: if datatype == wsme.types.binary:
return base64.decodestring(el.text) return base64.decodestring(el.text.encode('ascii'))
if isusertype(datatype): if isusertype(datatype):
datatype = datatype.basetype datatype = datatype.basetype
if datatype == datetime.date: if datatype == datetime.date:
@ -86,6 +90,8 @@ def loadxml(el, datatype):
return parse_isodatetime(el.text) return parse_isodatetime(el.text)
if datatype is None: if datatype is None:
return el.text return el.text
if datatype is wsme.types.bytes:
return el.text.encode('ascii')
return datatype(el.text) return datatype(el.text)
@ -125,23 +131,23 @@ class TestRestXML(wsme.tests.protocol.ProtocolTestCase):
def test_encode_sample_value(self): def test_encode_sample_value(self):
class MyType(object): class MyType(object):
aint = int aint = int
aunicode = unicode atext = wsme.types.text
register_type(MyType) register_type(MyType)
value = MyType() value = MyType()
value.aint = 5 value.aint = 5
value.aunicode = u('test') value.atext = u('test')
language, sample = self.root.protocols[0].encode_sample_value( language, sample = self.root.protocols[0].encode_sample_value(
MyType, value, True) MyType, value, True)
print (language, sample) print (language, sample)
assert language == 'xml' assert language == 'xml'
assert sample == """<value> assert sample == b("""<value>
<aint>5</aint> <aint>5</aint>
<aunicode>test</aunicode> <atext>test</atext>
</value>""" </value>""")
def test_nil_fromxml(self): def test_nil_fromxml(self):
for dt in ( for dt in (