python - Using XML ElementTree to create list of objects with atrributes -
i use python requests module xml teamcity rest api looks this:
<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>
i trying create list of "trigger" objects using class. ideally object have id, type, , list of properties attributes dictionaries of {name : value}. code far is:
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 = [] trigger in tree.iter('trigger'): triggertype = trigger.get('type') triggerid = trigger.get('id') triggername = str(triggerid) triggername = triggerlist(triggerid, triggertype) listoftriggers.append(triggername) triggerproperty in tree.iter('property'): propertyname = triggerproperty.get('name') propertyvalue = triggerproperty.get('value') propdict = {propertyname : propertyvalue} triggername.add_property(propdict)
this gives me list of objects every object has list of every property dictionary. output:
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'}]
i don't know how stop loop properties specific trigger. there way use elementtree properties specific trigger? there more efficient way create object?
not directly answering question, may reinventing wheel here, check lxml.objectify
package:
the main idea hide usage of xml behind normal python objects, referred data-binding. allows use xml if dealing normal python object hierarchy. accessing children of xml element deploys object attribute access. if there multiple children same name, slicing , indexing can used. python data types extracted xml content automatically , made available normal python operators.
Comments
Post a Comment