def test_many(self): class SomeComplexModel(ComplexModel): s = Unicode i = Integer v = [ SomeComplexModel(s='a', i=1), SomeComplexModel(s='b', i=2), SomeComplexModel(s='c', i=3), SomeComplexModel(s='d', i=4), SomeComplexModel(s='e', i=5), ] class SomeService(Service): @rpc(_returns=Array(SomeComplexModel)) def get(ctx): return v desc = SomeService.public_methods['get'] ctx = FakeContext(out_object=[v], descriptor=desc) ostr = ctx.out_stream = BytesIO() XmlDocument(Application([SomeService], __name__)) \ .serialize(ctx, XmlDocument.RESPONSE) elt = etree.fromstring(ostr.getvalue()) print(etree.tostring(elt, pretty_print=True)) assert elt.xpath('x:getResult/x:SomeComplexModel/x:i/text()', namespaces={'x': __name__}) == ['1', '2', '3', '4', '5'] assert elt.xpath('x:getResult/x:SomeComplexModel/x:s/text()', namespaces={'x': __name__}) == ['a', 'b', 'c', 'd', 'e']
def get_object_as_xml_cloth(inst, cls=None, no_namespace=False, encoding='utf8'): """Returns an ElementTree representation of a :class:`spyne.model.complex.ComplexModel` subclass. :param inst: The instance of the class to be serialized. :param cls: The class to be serialized. Optional. :param root_tag_name: The root tag string to use. Defaults to the output of ``value.__class__.get_type_name_ns()``. :param no_namespace: When true, namespace information is discarded. """ if cls is None: cls = inst.__class__ if cls.get_namespace() is None and no_namespace is None: no_namespace = True if no_namespace is None: no_namespace = False ostr = BytesIO() xml_cloth = XmlCloth(use_ns=(not no_namespace)) ctx = FakeContext() with etree.xmlfile(ostr, encoding=encoding) as xf: ctx.outprot_ctx.doctype_written = False ctx.protocol.prot_stack = tlist([], ProtocolMixin) tn = cls.get_type_name() ret = xml_cloth.subserialize(ctx, cls, inst, xf, tn) assert not isgenerator(ret) return ostr.getvalue()
def test_one(self): class SomeComplexModel(ComplexModel): s = Unicode i = Integer v = SomeComplexModel(s='a', i=1), class SomeService(ServiceBase): @rpc(_returns=SomeComplexModel) def get(ctx): return v desc = SomeService.public_methods['get'] ctx = FakeContext(out_object=v, descriptor=desc) ostr = ctx.out_stream = BytesIO() XmlDocument(Application([SomeService], __name__)) \ .serialize(ctx, XmlDocument.RESPONSE) elt = etree.fromstring(ostr.getvalue()) print(etree.tostring(elt, pretty_print=True)) assert elt.xpath('x:getResult/x:i/text()', namespaces={'x': __name__}) == ['1'] assert elt.xpath('x:getResult/x:s/text()', namespaces={'x': __name__}) == ['a']
def json_loads(s, cls, protocol=JsonDocument, **kwargs): if s is None: return None if s == '': return None prot = protocol(**kwargs) ctx = FakeContext(in_string=[s]) prot.create_in_document(ctx) return prot._doc_to_object(cls, ctx.in_document, validator=prot.validator)
def get_object_as_msgpack(o, cls=None, ignore_wrappers=False, complex_as=dict, polymorphic=False): if cls is None: cls = o.__class__ prot = MessagePackDocument(ignore_wrappers=ignore_wrappers, complex_as=complex_as, polymorphic=polymorphic) ctx = FakeContext(out_document=[prot._object_to_doc(cls,o)]) prot.create_out_string(ctx) return b''.join(ctx.out_string)
def get_object_as_yaml(o, cls=None, ignore_wrappers=False, complex_as=dict, encoding='utf8', polymorphic=False): if cls is None: cls = o.__class__ prot = YamlDocument(ignore_wrappers=ignore_wrappers, complex_as=complex_as, polymorphic=polymorphic) ctx = FakeContext(out_document=[prot._object_to_doc(cls,o)]) prot.create_out_string(ctx, encoding) return b''.join(ctx.out_string)
def get_object_as_json(o, cls=None, ignore_wrappers=True, complex_as=list, encoding='utf8', polymorphic=False, indent=None, **kwargs): if cls is None: cls = o.__class__ prot = JsonDocument(ignore_wrappers=ignore_wrappers, complex_as=complex_as, polymorphic=polymorphic, indent=indent, **kwargs) ctx = FakeContext(out_document=[prot._object_to_doc(cls, o)]) prot.create_out_string(ctx, encoding) return b''.join(ctx.out_string)
def yaml_loads(s, cls, protocol=YamlDocument, ignore_wrappers=False, **kwargs): if s is None: return None if s == '' or s == b'': return None prot = protocol(ignore_wrappers=ignore_wrappers, **kwargs) ctx = FakeContext(in_string=[s]) prot.create_in_document(ctx) retval = prot._doc_to_object(None, cls, ctx.in_document, validator=prot.validator) return retval
def get_xml_as_object_polymorphic(elt, cls): """Returns a native :class:`spyne.model.complex.ComplexModel` child from an ElementTree representation of the same class. :param elt: The xml document to be deserialized. :param cls: The class the xml document represents. """ tns = cls.get_namespace() if tns is None: raise ValueError("Please set a namespace for %r" % (cls, )) class _DummyService(ServiceBase): @srpc(cls) def f(_): pass app = Application([_DummyService], tns=tns, in_protocol=XmlDocument(polymorphic=True)) unregister_application(app) return app.in_protocol.from_element(FakeContext(app=app), cls, elt)
def test_xml_encoding(self): ctx = FakeContext(out_document=E.rain(u"yağmur")) XmlDocument(encoding='iso-8859-9').create_out_string(ctx) s = b''.join(ctx.out_string) assert u"ğ".encode('iso-8859-9') in s