Skip to content

Commit

Permalink
support for binary data types
Browse files Browse the repository at this point in the history
  • Loading branch information
baverman committed May 23, 2012
1 parent 6646977 commit ba627f5
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 0 deletions.
26 changes: 26 additions & 0 deletions dropthesoap/schema/xs.py
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -428,6 +428,32 @@ def to_python(value):
return value return value




class base64Binary(_FinalSimpleType):
namespace = namespace

@staticmethod
def from_python(value):
assert isinstance(value, str), 'Value should be a str not %s' % (type(value))
return value.encode('base64')

@staticmethod
def to_python(value):
return value.decode('base64')


class hexBinary(_FinalSimpleType):
namespace = namespace

@staticmethod
def from_python(value):
assert isinstance(value, str), 'Value should be a str not %s' % (type(value))
return value.encode('hex')

@staticmethod
def to_python(value):
return value.decode('hex')


class dateTime(_FinalSimpleType): class dateTime(_FinalSimpleType):
namespace = namespace namespace = namespace


Expand Down
19 changes: 19 additions & 0 deletions tests/test_schema.py
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -199,6 +199,25 @@ def test_boolean():
obj = schema.fromstring(tostring(request)) obj = schema.fromstring(tostring(request))
assert obj.foo == False assert obj.foo == False


def test_binary_data():
schema = xs.schema(Namespace('http://boo', 'boo'))(
xs.element('Request')(xs.cts(
xs.element('foo', xs.hexBinary),
xs.element('boo', xs.base64Binary)))
)

data = '\x01\x02\x03'
request = schema['Request'].instance(foo=data, boo=data)
assert validate(schema, request)

string_request = tostring(request)
assert '010203' in string_request
assert 'AQID' in string_request

obj = schema.fromstring(string_request)
assert obj.foo == data
assert obj.boo == data

def test_dict_instantiation_for_aliased_types(): def test_dict_instantiation_for_aliased_types():
schema = xs.schema(Namespace('http://boo', 'boo'))( schema = xs.schema(Namespace('http://boo', 'boo'))(
xs.complexType('Point')( xs.complexType('Point')(
Expand Down

0 comments on commit ba627f5

Please sign in to comment.