示例#1
0
文件: test_xml.py 项目: zhuhj89/spyne
    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(ServiceBase):
            @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 = StringIO()
        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']
示例#2
0
文件: test_xml.py 项目: skumyol/spyne
    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']
示例#3
0
def get_object_as_yaml(o, cls, ignore_wrappers=False, complex_as=dict,
                                            encoding='utf8', polymorphic=False):
    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 ''.join(ctx.out_string)
示例#4
0
def yaml_loads(s, cls, protocol=YamlDocument, ignore_wrappers=False, **kwargs):
    if s is None:
        return None
    if s == '':
        return None
    prot = protocol(ignore_wrappers=ignore_wrappers, **kwargs)
    ctx = FakeContext(in_string=[s])
    prot.create_in_document(ctx)
    return prot._doc_to_object(cls, ctx.in_document, validator=prot.validator)
示例#5
0
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)
示例#6
0
文件: dictdoc.py 项目: pxiol/spyne
def get_object_as_json(o,
                       cls,
                       ignore_wrappers=True,
                       complex_as=list,
                       encoding='utf8'):
    prot = JsonDocument(ignore_wrappers=ignore_wrappers, complex_as=complex_as)
    ctx = FakeContext(out_document=[prot._object_to_doc(cls, o)])
    prot.create_out_string(ctx, encoding)
    return ''.join(ctx.out_string)
示例#7
0
def get_object_as_json(o, cls=None, ignore_wrappers=True, complex_as=list,
                                            encoding='utf8', polymorphic=False):
    if cls is None:
        cls = o.__class__

    prot = JsonDocument(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)
示例#8
0
def get_object_as_msgpack(o, cls=None, ignore_wrappers=False, complex_as=dict,
                                            encoding='utf8', 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, encoding)
    return ''.join(ctx.out_string)
示例#9
0
def yaml_loads(s, cls, protocol=YamlDocument, ignore_wrappers=False, **kwargs):
    prot = protocol(ignore_wrappers=ignore_wrappers, **kwargs)
    ctx = FakeContext(in_string=[s])
    prot.create_in_document(ctx)
    return prot._doc_to_object(cls, ctx.in_document)
示例#10
0
def json_loads(s, cls, protocol=JsonDocument, encoding=None, **kwargs):
    prot = protocol(**kwargs)
    ctx = FakeContext(in_string=[s])
    prot.create_in_document(ctx)
    return prot._doc_to_object(cls, ctx.in_document)