我使用python请求模块从TeamCity rest api获取XML,如下所示:
<triggers count="10">
<trigger id="TRIGGER_1240" type="buildDependencyTrigger">
<properties count="2">
<property name="afterSuccessfulBuildOnly" value="true"/>
<property name="dependsOn" value="bt191"/>
</properties>
</trigger>
<trigger id="TRIGGER_1241" type="buildDependencyTrigger">
<properties count="2">
<property name="afterSuccessfulBuildOnly" value="true"/>
<property name="dependsOn" value="bt171"/>
</properties>
</trigger>
<trigger id="TRIGGER_1242" type="buildDependencyTrigger">
<properties count="2">
<property name="afterSuccessfulBuildOnly" value="true"/>
<property name="dependsOn" value="bt167"/>
</properties>
</trigger>
<trigger id="TRIGGER_1243" type="buildDependencyTrigger">
<properties count="2">
<property name="afterSuccessfulBuildOnly" value="true"/>
<property name="dependsOn" value="bt164"/>
</properties>
</trigger>
<trigger id="TRIGGER_1244" type="buildDependencyTrigger">
<properties count="2">
<property name="afterSuccessfulBuildOnly" value="true"/>
<property name="dependsOn" value="bt364"/>
</properties>
</trigger>
<trigger id="TRIGGER_736" type="buildDependencyTrigger">
<properties count="2">
<property name="afterSuccessfulBuildOnly" value="true"/>
<property name="dependsOn" value="Components_Ratchetdb"/>
</properties>
</trigger>
<trigger id="TRIGGER_149" type="buildDependencyTrigger">
<properties count="2">
<property name="afterSuccessfulBuildOnly" value="true"/>
<property name="dependsOn" value="Components_Filedb"/>
</properties>
</trigger>
<trigger id="TRIGGER_150" type="buildDependencyTrigger">
<properties count="2">
<property name="afterSuccessfulBuildOnly" value="true"/>
<property name="dependsOn" value="bt168"/>
</properties>
</trigger>
<trigger id="TRIGGER_1130" type="buildDependencyTrigger">
<properties count="2">
<property name="afterSuccessfulBuildOnly" value="true"/>
<property name="dependsOn" value="Components_Tbldb"/>
</properties>
</trigger>
<trigger id="vcsTrigger" type="vcsTrigger" inherited="true">
<properties count="3">
<property name="quietPeriod" value="60"/>
<property name="quietPeriodMode" value="USE_DEFAULT"/>
<property name="triggerRules" value="-:version.properties
-:comment=^Incremented:**
-:**/*-schema.sql"/>
</properties>
</trigger>
我正在尝试创建一个&#34;触发器&#34;使用类的对象。理想情况下,对象将具有id,type和属性列表属性作为{name:value}的字典。到目前为止我的代码是:
class triggerList:
def __init__(self, triggerId, triggerType):
self.id = triggerId
self.type = triggerType
self.properties = []
def add_property(self, buildProperty):
self.properties.append(buildProperty)
def getAllTriggers(buildId):
url = path + 'buildTypes/id:' + buildId + '/triggers'
r = requests.get(url, auth=auth)
tree = ElementTree.fromstring(r.content)
listOfTriggers = []
for trigger in tree.iter('trigger'):
triggerType = trigger.get('type')
triggerId = trigger.get('id')
triggerName = str(triggerId)
triggerName = triggerList(triggerId, triggerType)
listOfTriggers.append(triggerName)
for triggerProperty in tree.iter('property'):
propertyName = triggerProperty.get('name')
propertyValue = triggerProperty.get('value')
propDict = {propertyName : propertyValue}
triggerName.add_property(propDict)
这给了我一个对象列表,但每个对象都有一个每个属性字典的列表。这是输出:
a = listOfTriggers[1]
print a.id, a.type, a.properties
>>> TRIGGER_1241 buildDependencyTrigger [{'afterSuccessfulBuildOnly': 'true'}, {'dependsOn': 'bt191'}, {'afterSuccessfulBuildOnly': 'true'}, {'dependsOn': 'bt171'}, {'afterSuccessfulBuildOnly': 'true'}, {'dependsOn': 'bt167'}, {'afterSuccessfulBuildOnly': 'true'}, {'dependsOn': 'bt164'}, {'afterSuccessfulBuildOnly': 'true'}, {'dependsOn': 'bt364'}, {'afterSuccessfulBuildOnly': 'true'}, {'dependsOn': 'Components_Ratchetdb'}, {'afterSuccessfulBuildOnly': 'true'}, {'dependsOn': 'Components_Filedb'}, {'afterSuccessfulBuildOnly': 'true'}, {'dependsOn': 'bt168'}, {'afterSuccessfulBuildOnly': 'true'}, {'dependsOn': 'Components_Tbldb'}, {'quietPeriod': '60'}, {'quietPeriodMode': 'USE_DEFAULT'}, {'triggerRules': '-:version.properties\n-:comment=^Incremented:**\n-:**/*-schema.sql'}]
我不知道如何仅针对特定触发器的属性停止循环。有没有办法使用ElementTree只获取特定触发器的属性?有没有更有效的方法来创建这个对象?
答案 0 :(得分:0)
没有直接回答这个问题,但你可能在这里重新发明轮子,检查lxml.objectify
package:
主要思想是隐藏普通Python背后的XML用法 对象,有时也称为数据绑定。它允许你使用 XML就像处理普通的Python对象层次结构一样。 访问XML元素的子元素会部署对象属性 访问。如果有多个具有相同名称的子项,则切片和 索引可以使用。 Python数据类型是从XML内容中提取的 自动并使普通的Python操作员可以使用。
答案 1 :(得分:0)
简单的语法错误:
for triggerProperty in trigger.iter('property'):
propertyName = triggerProperty.get('name')
propertyValue = triggerProperty.get('value')
propDict = {propertyName : propertyValue}
triggerName.add_property(propDict)
我在整个树上迭代,而不是触发器。应该是:
对于触发器中的triggerProperty .iter('property'):