问题
I'm connecting with a SUDS client to a SOAP Server whose wsdl contains many enumerations like the following:
</simpleType>
<simpleType name="FOOENUMERATION">
<restriction base="xsd:string">
<enumeration value="ALPHA"><!-- enum const = 0 -->
<enumeration value="BETA"/><!-- enum const = 1 -->
<enumeration value="GAMMA"/><!-- enum const = 2 -->
<enumeration value="DELTA"/><!-- enum const = 3 -->
</restriction>
</simpleType>
In my client I am receiving sequences which contain elements of these various enumeration types. My need is that given a member variable, I need to know all possible enumeration values. Basically I need a function which takes an instance of one of these enums and returns a list of strings which are all the possible values.
When I have an instance, running:
print type(foo.enumInstance)
I get:
<class 'suds.sax.text.Text'>
I'm not sure how to get the actual simpleType name from this, and then get the possible values from that short of parsing the WSDL myself.
Edit: I've discovered a way to get the enumerations given the simpleType name, as below, so my problem narrows down to findingthe type name for a given variable, given that type(x) returns suds.sax.text.Text instead of the real name
for l in client.factory.create('FOOENUMERATION'):
print l[0]
回答1:
If you know the name of the enum you want, you should be able to treat the enumeration object suds gives you like a dictionary, and do a direct lookup with that name. For example, if your enumeration type is called SOAPIPMode
and you want the enum named STATIC_MANUAL
in that enumeration:
soapIPMode = client.factory.create('SOAPIPMode')
staticManual = soapIPMode['STATIC_MANUAL']
The resulting value is of type suds.sax.text.Text
which acts like a string.
You can also iterate over the enumeration type as if it were an array:
for i in range(len(soapIPMode):
process(soapIPMode[i])
回答2:
I have figured out a rather hacky way to pull this off, but hopefully someone still has a better answer for me. For some reason objects returned from the server have enums with the suds.sax.text
.Text type, but those created with the factory have types related to the enum, so this works:
def printEnums(obj,field):
a=client.factory.create(str(getattr(client.factory.create( str(obj.__class__).replace('suds.sudsobject.','')),field).__class__).replace('suds.sudsobject.',''))
for i in a:
print i[0]
Then I can do:
printEnums(foo,'enumInstance')
and even if foo was returned from the server and not created by a factory get a listing of the possible values for foo.enumInstance, since I factory create a new class of the same type as the one passed in. Still, I can't imagine that this mess is the correct/best way to do this.
回答3:
See if you can feed in the WSDL into the ElementTree
component on Python and use it to obtain the enumerations.
来源:https://stackoverflow.com/questions/3027307/listing-all-possible-values-for-soap-enumeration-with-python-suds