binary is now a UserType, which simplify most of the protocols implementation
This commit is contained in:
@@ -65,11 +65,6 @@ def datetime_tojson(datatype, value):
|
||||
return value.isoformat()
|
||||
|
||||
|
||||
@tojson.when_object(wsme.types.binary)
|
||||
def datetime_tojson(datatype, value):
|
||||
return base64.encodestring(value)
|
||||
|
||||
|
||||
@generic
|
||||
def fromjson(datatype, value):
|
||||
"""
|
||||
@@ -124,11 +119,6 @@ def time_fromjson(datatype, value):
|
||||
return datetime.datetime.strptime(value, '%Y-%m-%dT%H:%M:%S')
|
||||
|
||||
|
||||
@fromjson.when_object(wsme.types.binary)
|
||||
def binary_fromjson(datatype, value):
|
||||
return base64.decodestring(value)
|
||||
|
||||
|
||||
class RestJsonProtocol(RestProtocol):
|
||||
"""
|
||||
REST+Json protocol.
|
||||
|
||||
@@ -129,16 +129,6 @@ def datetime_toxml(datatype, key, value):
|
||||
return el
|
||||
|
||||
|
||||
@toxml.when_object(wsme.types.binary)
|
||||
def binary_toxml(datatype, key, value):
|
||||
el = et.Element(key)
|
||||
if value is None:
|
||||
el.set('nil', 'true')
|
||||
else:
|
||||
el.text = base64.encodestring(value)
|
||||
return el
|
||||
|
||||
|
||||
@fromxml.when_type(list)
|
||||
def array_fromxml(datatype, element):
|
||||
if element.get('nil') == 'true':
|
||||
@@ -166,11 +156,6 @@ def datetime_fromxml(datatype, element):
|
||||
return datetime.datetime.strptime(element.text, '%Y-%m-%dT%H:%M:%S')
|
||||
|
||||
|
||||
@fromxml.when_object(wsme.types.binary)
|
||||
def binary_fromxml(datatype, element):
|
||||
return base64.decodestring(element.text)
|
||||
|
||||
|
||||
class RestXmlProtocol(RestProtocol):
|
||||
"""
|
||||
REST+XML protocol.
|
||||
|
||||
@@ -1,10 +1,8 @@
|
||||
import base64
|
||||
import datetime
|
||||
import decimal
|
||||
import weakref
|
||||
import inspect
|
||||
|
||||
binary = object()
|
||||
|
||||
import weakref
|
||||
|
||||
class UserType(object):
|
||||
basetype = None
|
||||
@@ -18,6 +16,27 @@ class UserType(object):
|
||||
def frombasetype(self, value):
|
||||
return value
|
||||
|
||||
|
||||
def isusertype(class_):
|
||||
return isinstance(class_, UserType)
|
||||
|
||||
|
||||
class BinaryType(UserType):
|
||||
"""
|
||||
A user type that use base64 strings to carry binary data.
|
||||
"""
|
||||
basetype = str
|
||||
|
||||
def tobasetype(self, value):
|
||||
return base64.encodestring(value)
|
||||
|
||||
def frombasetype(self, value):
|
||||
return base64.decodestring(value)
|
||||
|
||||
#: The binary almost-native type
|
||||
binary = BinaryType()
|
||||
|
||||
|
||||
class Enum(UserType):
|
||||
"""
|
||||
A simple enumeration type. Can be based on any non-complex type.
|
||||
@@ -42,9 +61,6 @@ class Enum(UserType):
|
||||
def frombasetype(self, value):
|
||||
return value
|
||||
|
||||
def isusertype(class_):
|
||||
return isinstance(class_, UserType)
|
||||
|
||||
pod_types = [str, unicode, int, float, bool]
|
||||
dt_types = [datetime.date, datetime.time, datetime.datetime]
|
||||
extra_types = [binary, decimal.Decimal]
|
||||
|
||||
Reference in New Issue
Block a user