From d867bd92db2cac7da495af580a84161da77ecf32 Mon Sep 17 00:00:00 2001 From: Christophe de Vienne Date: Fri, 28 Oct 2011 17:45:50 +0200 Subject: [PATCH] binary is now a UserType, which simplify most of the protocols implementation --- wsme/protocols/restjson.py | 10 ---------- wsme/protocols/restxml.py | 15 --------------- wsme/types.py | 30 +++++++++++++++++++++++------- 3 files changed, 23 insertions(+), 32 deletions(-) diff --git a/wsme/protocols/restjson.py b/wsme/protocols/restjson.py index b9babff..e516a0c 100644 --- a/wsme/protocols/restjson.py +++ b/wsme/protocols/restjson.py @@ -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. diff --git a/wsme/protocols/restxml.py b/wsme/protocols/restxml.py index 57ca251..ec58098 100644 --- a/wsme/protocols/restxml.py +++ b/wsme/protocols/restxml.py @@ -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. diff --git a/wsme/types.py b/wsme/types.py index 7e5449d..2fe358f 100644 --- a/wsme/types.py +++ b/wsme/types.py @@ -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]