[Soap-Python] Spyne: ability to change default results name from FooResult to WhateverXyz ?
Patricio Stegmann
kpoman at gmail.com
Thu May 14 20:51:48 CEST 2015
Burak,
"""
import os, sys, time
from spyne.model.primitive import String, Integer, Boolean, DateTime, Float
from spyne.server import wsgi
from spyne.model import ByteArray
from spyne.model.complex import Array, ComplexModel, XmlAttribute
from spyne.model.enum import Enum
from spyne import *
from spyne.util.xml import *
from lxml import etree
class FingerImageType(ComplexModel):
#__namespace__ = 'tns'
Version = String
NumberOfDistinctPositions = Integer
RepresentationList = String
class Template(ComplexModel):
Position = String
Data = String
class TemplateListType(ComplexModel):
#__namespace__ = 'tns'
TemplateType = XmlAttribute(String)
templates = Array(Template, wrapped=False)#max_occurs='unbounded',
sub_name='Template')
__type_name__ = 'TemplateList'
class RecuperaConteudoType(ComplexModel):
id = String
class RecuperaConteudoResponseType(ComplexModel):
#__namespace__ = 'tns'l
FingerImagem = FingerImageType
templates = Array(TemplateListType, sub_name='Templates')
print etree.tostring(
get_object_as_xml(RecuperaConteudoResponseType(
FingerImagem=FingerImageType(),
templates = [
TemplateListType(
TemplateType='ANSI',
templates=[
Template(Position='one', Data='data-one'),
Template(Position='two', Data='data-two')
]
),
TemplateListType(
TemplateType='XYT_NIST',
templates=[
Template(Position='three', Data='data-three'),
Template(Position='two', Data='data-four')
]
)
]
)
),
pretty_print=True
)
"""
I am almost there: this prints the following:
<RecuperaConteudoResponseType>
<Templates>
<TemplateList TemplateType="ANSI">
<templates>
<Template>
<Position>one</Position>
<Data>data-one</Data>
</Template>
<Template>
<Position>two</Position>
<Data>data-two</Data>
</Template>
</templates>
</TemplateList>
<TemplateList TemplateType="XYT_NIST">
<templates>
<Template>
<Position>three</Position>
<Data>data-three</Data>
</Template>
<Template>
<Position>two</Position>
<Data>data-four</Data>
</Template>
</templates>
</TemplateList>
</Templates>
<FingerImagem/>
</RecuperaConteudoResponseType>
The problem is that I want to get rid of <templates> container, I want
TemplateList to have, inside, directly, Template blocks !
On 30/04/2015 05:26, Burak Arslan wrote:
> Hello,
>
> On 04/30/15 03:11, Patricio Stegmann wrote:
>> Burak,
>> There is the message, but inside is the result which has its own
>> name. It is automatically generated with Result suffix, but my ws
>> consumer didnt want that. He wanted the name of it's spec on it. I
>> added that keyword and slightly changed some spyne lines.
>> I had lot of issues using multiple namespaces, in the lxml output
>> generation, which I managed to solve using git version, and now it
>> seems to work ok.
>
> I'm always interested in ways people patch Spyne. If you think it's
> worthy, please let me know.
>
>> I have just a simple question. I need to be able to generate
>> something like this:
>>
>> <MyResponse>
>> <Items>
>> <ItemList ofType="b">
>> <Item>blabla</Item>
>> <Item>bleble</Item>
>> <Item>blibli</Item>
>> </ItemList>
>> <ItemList ofType="c">
>> <Item>clacla</Item>
>> <Item>clecle</Item>
>> <Item>clicli</Item>
>> </ItemList>
>> </Items>
>> </MyResponse>
>
> from spyne import *
> from spyne.util.xml import *
> from lxml import etree
>
> class ItemList(ComplexModel):
> of_type = XmlAttribute(Unicode(sub_name='ofType'))
> items = Unicode(max_occurs='unbounded', sub_name='Item')
> # 2.12 will support this, which is imo nicer to look at:
> items = Array(Unicode, wrapped=False, sub_name='Item')
>
> class MyResponse(ComplexModel):
> items = Array(ItemList, sub_name="Items")
>
> print etree.tostring(get_object_as_xml(MyResponse(
> items=[
> ItemList(of_type="b", items=[
> "blabla", "bleble", 'blibli',
> ]),
> ItemList(of_type="c", items=[
> "clacla", "clecle", 'clicli',
> ]),
> ]
> )), pretty_print=True)
>
>
> The sub_name additions here are to prevent you from breaking the PEP8
> rules.
>
> hth,
>
> burak
>
>
---
This email has been checked for viruses by Avast antivirus software.
http://www.avast.com
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/soap/attachments/20150514/3259522f/attachment.html>
More information about the Soap
mailing list