使用 Python SUDS 列出 SOAP 枚举的所有可能值
我正在将 SUDS 客户端连接到 SOAP 服务器,该服务器的 wsdl 包含许多枚举,如下所示:
</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>
在我的客户端中,我正在接收包含这些不同枚举类型的元素的序列。我的需要是,给定一个成员变量,我需要知道所有可能的枚举值。基本上我需要一个函数,它接受这些枚举之一的实例并返回一个字符串列表,其中所有可能的值。
当我有一个实例时,运行:
print type(foo.enumInstance)
我得到:
<class 'suds.sax.text.Text'>
我不确定如何从中获取实际的 simpleType 名称,然后从我自己解析 WSDL 中获取可能的值。
编辑:我发现了一种获取给定 simpleType 名称的枚举的方法,如下所示,因此我的问题缩小到查找给定变量的类型名称,假设 type(x) 返回 suds。 sax.text.Text代替真实姓名
for l in client.factory.create('FOOENUMERATION'):
print l[0]
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]
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您知道所需枚举的名称,则应该能够将 suds 为您提供的枚举对象视为字典,并使用该名称进行直接查找。例如,如果您的枚举类型名为
SOAPIPMode
,并且您希望在该枚举中使用名为STATIC_MANUAL
的枚举:结果值的类型为
suds.sax.text。文本
,其作用类似于字符串。您还可以像数组一样迭代枚举类型:
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 namedSTATIC_MANUAL
in that enumeration: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:
我已经找到了一种相当老套的方法来实现这一点,但希望有人仍然能为我提供更好的答案。由于某种原因,从服务器返回的对象具有
suds.sax.text
.Text 类型的枚举,但是使用工厂创建的对象具有与枚举相关的类型,因此这是可行的:然后我可以这样做:
即使 foo 是从服务器返回而不是由工厂创建的,也会获得 foo.enumInstance 的可能值的列表,因为我工厂创建了一个与传入的类型相同的新类。但是,我不能想象一下这个混乱是做到这一点的正确/最好的方法。
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:Then I can do:
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.
查看是否可以将 WSDL 输入到 Python 上的
ElementTree
组件中并使用它来获取枚举。See if you can feed in the WSDL into the
ElementTree
component on Python and use it to obtain the enumerations.