class XmlDoc(object): def __init__(self): object.__init__(self) self.dom = DOMImplementation() self.doctype = self.dom.createDocumentType('xmltable', 0, '') self.doc = Document() self.doc.appendChild(self.doctype) def insert(self, parent, tag, text=None): if text: element = TextElement(tag, text) else: element = Element(tag) parent.appendChild(element) return element def write(self, path=None): if not path: fobj = strfile() else: fobj = file(path, 'w') PrettyPrint(self.doc, fobj) if not path: fobj.seek(0) return fobj.read()
class XmlDoc(object): def __init__(self): object.__init__(self) self.dom = DOMImplementation() self.doctype = self.dom.createDocumentType('xmltable', 0, '') self.doc = Document() self.doc.appendChild(self.doctype) def insert(self, parent, tag, text=None): if text: element = TextElement(tag, text) else: element = Element(tag) parent.appendChild(element) return element def write(self, path=None): if not path: fobj = strfile() else: fobj = file(path, 'w') PrettyPrint(self.doc, fobj) if not path: fobj.seek(0) return fobj.read()
def run(arg): xmldoc = minidom.parse(arg) remove_blanks(xmldoc) xmldoc.normalize() vle_project = xmldoc.getElementsByTagName('vle_project') experiment = vle_project[0].getElementsByTagName('experiment') conditions = vle_project[0].getElementsByTagName('conditions') if not vle_project.length > 0 or not experiment.length > 0: print(' -> Not a VLE VPZ file?') return if not experiment[0].hasAttribute('begin') or \ not experiment[0].hasAttribute('duration'): print(' -> Already a VLE 1.2 file?') return # get begin and duration tags data and remove them from the experiment # element begintag = experiment[0].getAttribute('begin') durationtag = experiment[0].getAttribute('duration') experiment[0].removeAttribute('begin') experiment[0].removeAttribute('duration') # build the new simulation_engine' condition. newcondition = xmldoc.createElement('condition') newcondition.setAttribute('name', 'simulation_engine') conditions[0].appendChild(newcondition) # build the condition's port begin. port = xmldoc.createElement('port') port.setAttribute('name', 'begin'); newcondition.appendChild(port) double = xmldoc.createElement('double') textnode = xmldoc.createTextNode(begintag) double.appendChild(textnode) port.appendChild(double) # build the conditions's port duration. port = xmldoc.createElement('port') port.setAttribute('name', 'duration'); newcondition.appendChild(port) double = xmldoc.createElement('double') textnode = xmldoc.createTextNode(durationtag) double.appendChild(textnode) port.appendChild(double) imp = DOMImplementation() doctype = imp.createDocumentType( qualifiedName='vle_project', publicId='-//VLE TEAM//DTD Strict//EN', systemId='https://www.vle-project.org/vle-1.2.0.dtd') xmldoc.replaceChild(doctype, xmldoc.doctype) with file(arg, 'w') as result: result.write(xmldoc.toprettyxml(indent = ' '))
def run(arg): xmldoc = minidom.parse(arg) remove_blanks(xmldoc) xmldoc.normalize() vle_project = xmldoc.getElementsByTagName('vle_project') experiment = vle_project[0].getElementsByTagName('experiment') conditions = vle_project[0].getElementsByTagName('conditions') if not vle_project.length > 0 or not experiment.length > 0: print(' -> Not a VLE VPZ file?') return if not experiment[0].hasAttribute('begin') or \ not experiment[0].hasAttribute('duration'): print(' -> Already a VLE 1.2 file?') return # get begin and duration tags data and remove them from the experiment # element begintag = experiment[0].getAttribute('begin') durationtag = experiment[0].getAttribute('duration') experiment[0].removeAttribute('begin') experiment[0].removeAttribute('duration') # build the new simulation_engine' condition. newcondition = xmldoc.createElement('condition') newcondition.setAttribute('name', 'simulation_engine') conditions[0].appendChild(newcondition) # build the condition's port begin. port = xmldoc.createElement('port') port.setAttribute('name', 'begin'); newcondition.appendChild(port) double = xmldoc.createElement('double') textnode = xmldoc.createTextNode(begintag) double.appendChild(textnode) port.appendChild(double) # build the conditions's port duration. port = xmldoc.createElement('port') port.setAttribute('name', 'duration'); newcondition.appendChild(port) double = xmldoc.createElement('double') textnode = xmldoc.createTextNode(durationtag) double.appendChild(textnode) port.appendChild(double) imp = DOMImplementation() doctype = imp.createDocumentType( qualifiedName='vle_project', publicId='-//VLE TEAM//DTD Strict//EN', systemId='http://www.vle-project.org/vle-1.2.0.dtd') xmldoc.replaceChild(doctype, xmldoc.doctype) with file(arg, 'w') as result: result.write(xmldoc.toprettyxml(indent = ' '))
def exportToIOSFormat(assy, fileName): """ Writes the IOS file @param assy: the NE1 assy. @type assy: L{assembly} @param: IOS output file in XML @type: string """ if fileName == '': print "No file selected to export" return d = DOMImplementation() #create doctype doctype = DOMImplementation.createDocumentType(d, 'ios', None, None) #create empty DOM Document and get root element doc = DOMImplementation.createDocument(d, EMPTY_NAMESPACE, 'ios', doctype) elemDoc = doc.documentElement elemDoc.setAttributeNS( XMLNS_NAMESPACE, "xmlns:ios", "http://www.parabon.com/namespaces/inSeqioOptimizationSpecification") elemDoc.setAttributeNS(XMLNS_NAMESPACE, "xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance") createTokenLibrary(doc, elemDoc) createMappingLibrary(doc, elemDoc) compInfoDict = createStrands(doc, elemDoc, assy) createConstraints(doc, elemDoc, assy, compInfoDict) #print doc to file f = open(fileName, 'w') PrettyPrint(doc, f) f.close() # don't know how to set the IOS prefix, so processing text to # include that f = open(fileName, 'r') allLines = f.readlines() allLines[ 1] = "<ios:IOS xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns:ios='http://www.parabon.com/namespaces/inSeqioOptimizationSpecification'>\n" allLines[len(allLines) - 1] = "</ios:IOS>\n" f.close() #write the document all over to reflect the changes f = open(fileName, 'w') f.writelines(allLines) f.close() return
def exportToIOSFormat(assy, fileName): """ Writes the IOS file @param assy: the NE1 assy. @type assy: L{assembly} @param: IOS output file in XML @type: string """ if fileName == '': print "No file selected to export" return d = DOMImplementation() #create doctype doctype = DOMImplementation.createDocumentType(d,'ios', None, None) #create empty DOM Document and get root element doc = DOMImplementation.createDocument(d, EMPTY_NAMESPACE,'ios', doctype) elemDoc = doc.documentElement elemDoc.setAttributeNS(XMLNS_NAMESPACE, "xmlns:ios", "http://www.parabon.com/namespaces/inSeqioOptimizationSpecification") elemDoc.setAttributeNS(XMLNS_NAMESPACE, "xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance") createTokenLibrary(doc, elemDoc) createMappingLibrary(doc,elemDoc) compInfoDict = createStrands(doc, elemDoc, assy) createConstraints(doc, elemDoc, assy, compInfoDict) #print doc to file f = open(fileName,'w') PrettyPrint(doc,f) f.close() # don't know how to set the IOS prefix, so processing text to # include that f = open(fileName,'r') allLines=f.readlines() allLines[1] = "<ios:IOS xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns:ios='http://www.parabon.com/namespaces/inSeqioOptimizationSpecification'>\n" allLines[len(allLines)-1] = "</ios:IOS>\n" f.close() #write the document all over to reflect the changes f = open(fileName,'w') f.writelines(allLines) f.close() return
def dict_to_xml(self,dic): if not isinstance(dic,dict): return None dom_impl = DOMImplementation() if self.doctype: doctype = dom_impl.createDocumentType(self.doctype[0],self.doctype[1],self.doctype[2]) else: doctype = None if dic: if len(dic) > 1: dic = {'Root':dic} root_name = list(dic.keys())[0] doc = dom_impl.createDocument(None,root_name,doctype) root = doc.lastChild for ele in self.to_xml(doc,dic[root_name]): root.appendChild(ele) #@todo: toprettyxml will change textnode return doc.toxml()
class MockRacksXML(object): def __init__(self): """ <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE rackmap SYSTEM "/usr/share/slurm-web/restapi/schema/dtd/racks.dtd"> <rackmap> <nodetypes/> <racks posx="0" posy="0" width="10" depth="10"/> </rackmap> """ self.imp = DOMImplementation() self.doctype = self.imp.createDocumentType( qualifiedName='rackmap', publicId='', systemId='/usr/share/slurm-web/restapi/schema/dtd/racks.dtd', ) self.doc = self.imp.createDocument(None, 'rackmap', self.doctype) #self.root = self.doc.createElement('rackmap') #self.doc.appendChild(self.root) self.root = self.doc.documentElement self.nodetypes = self.doc.createElement('nodetypes') self.root.appendChild(self.nodetypes) self.racks = self.doc.createElement('racks') self.racks.setAttribute('posx', '0') self.racks.setAttribute('posy', '0') self.racks.setAttribute('width', '10') self.racks.setAttribute('depth', '10') self.root.appendChild(self.racks) def add_nodetype(self, name, model, height, width): """ <nodetype id="m32x4321" model="Vendor A 32 x4321" height="1" width="0.5" /> """ nodetype = self.doc.createElement('nodetype') nodetype.setAttribute("id", name) nodetype.setAttribute("model", model) nodetype.setAttribute("height", height) nodetype.setAttribute("width", width) self.nodetypes.appendChild(nodetype) def add_racksrow(self, posx): """ <racksrow posx="0"/> Return the <racksrow/> element. """ racksrow = self.doc.createElement('racksrow') racksrow.setAttribute('posx', posx) self.racks.appendChild(racksrow) return racksrow def add_rack(self, row, name, posy): """ <rack id="A1" posy="0"> <nodes/> </rack> Return <nodes/> element. """ rack = self.doc.createElement('rack') rack.setAttribute('id', name) rack.setAttribute('posy', posy) row.appendChild(rack) nodes = self.doc.createElement('nodes') rack.appendChild(nodes) return nodes def add_nodeset(self, nodes, nodelist, nodetype, posx, posy, draw=None): """ <nodeset id="cn[505-518]" type="m32x4321" posx="0" posy="2" draw="down" /> """ nodeset = self.doc.createElement('nodeset') nodeset.setAttribute('id', nodelist) nodeset.setAttribute('type', nodetype) nodeset.setAttribute('posx', posx) nodeset.setAttribute('posy', posy) if draw is not None: nodeset.setAttribute('draw', draw) nodes.appendChild(nodeset)
def WriteBusNodeLink(bus_info,bus_xml): imp = DOMImplementation() dt = imp.createDocumentType(qualifiedName='network', publicId='', systemId='http://www.matsim.org/files/dtd/network_v2.dtd') newxml_domtree = imp.createDocument(None, 'network', dt) newxml_network = newxml_domtree.documentElement newxml_domtree.appendChild(newxml_network) newxml_nodes = newxml_domtree.createElement('nodes') newxml_network.appendChild(newxml_nodes) newxml_links = newxml_domtree.createElement('links') newxml_network.appendChild(newxml_links) #stop is one node actually. In order to adapt to the format of matsim,we divided it into two nodes 10 meters apart, # similar to bus platforms #stop is the input stop,node is the virtual platform node. A stop has two nodes accordingly. for busline_dict in bus_info: bus_name=list(busline_dict.keys())[0] line_info = list(busline_dict.values())[0] for stop_num in range(len(line_info)-1): x1,y1=WGS84toMercator(float(line_info[stop_num]['x']),float(line_info[stop_num]['y'])) x2, y2 = WGS84toMercator(float(line_info[stop_num+1]['x']), float(line_info[stop_num+1]['y'])) link_length=GeoDistance(float(line_info[stop_num]['x']),float(line_info[stop_num]['y']), float(line_info[stop_num + 1]['x']), float(line_info[stop_num + 1]['y'])) ratio = 10 / link_length # stop_from SetNode(newxml_domtree, newxml_nodes, bus_name + str(line_info[stop_num]['id']) + '_A', x1 - ratio * (x2 - x1), y1 - ratio * (y2 - y1), True, False) # stop_to SetNode(newxml_domtree, newxml_nodes, bus_name + str(line_info[stop_num]['id']) + '_B', x1 + ratio * (x2 - x1), y1 + ratio * (y2 - y1), True, False) if stop_num==len(line_info)-2: SetNode(newxml_domtree, newxml_nodes, bus_name+ str(line_info[stop_num+1]['id']) + '_A', x2 - ratio * (x2 - x1), y2 - ratio * (y2 - y1), True,False) # SetNode(newxml_domtree, newxml_nodes, new_id[1], new_lon[1], new_lat[1],False) SetNode(newxml_domtree, newxml_nodes, bus_name + str(line_info[stop_num+1]['id']) + '_B', x2 + ratio * (x2 - x1), y2 + ratio * (y2 - y1), True,True) nodelist = newxml_domtree.getElementsByTagName('node') for node_num in range(len(nodelist) - 1): from_node = nodelist[node_num] to_node = nodelist[node_num + 1] link_id = '' if from_node.childNodes[0].data == ' ': continue lon1, lat1 = MercatortoWGS84(float(from_node.attributes['x'].value), float(from_node.attributes['y'].value)) lon2, lat2 = MercatortoWGS84(float(to_node.attributes['x'].value), float(to_node.attributes['y'].value)) link_length = GeoDistance(lon1, lat1, lon2, lat2) if from_node.attributes['id'].value[-1] == 'A': link_id = from_node.attributes['id'].value[:-2] elif from_node.attributes['id'].value[-1] == 'B': link_id = 'from_' + from_node.attributes['id'].value + '_to_' + to_node.attributes['id'].value else: print('link name error!!!') SetLink(newxml_domtree, newxml_links, link_id, from_node.attributes['id'].value, to_node.attributes['id'].value, link_length, ) SetLink(newxml_domtree, newxml_links, link_id + '_r', to_node.attributes['id'].value, from_node.attributes['id'].value, link_length) with open(bus_xml, 'w') as f: # 缩进 - 换行 - 编码 newxml_domtree.writexml(f,indent='\t', addindent='\t',newl='\n',encoding='utf-8')
def WriteTransitSchedule(stop_list_all,schedule_xml,transit_list_all): imp_pt = DOMImplementation() depart_id=0 dt_pt = imp_pt.createDocumentType(qualifiedName='transitSchedule', publicId='', systemId='http://www.matsim.org/files/dtd/transitSchedule_v2.dtd') ptsch_domtree = imp_pt.createDocument(None, 'transitSchedule', dt_pt) ptsch_transitschedule = ptsch_domtree.documentElement ptsch_domtree.appendChild(ptsch_transitschedule) ptsch_transitStops = ptsch_domtree.createElement('transitStops') ptsch_transitschedule.appendChild(ptsch_transitStops) vehicle_id_list_all=[] ##generate transitStops for line_dict in stop_list_all: bus_label=stop_list_all.index(line_dict) label=list(line_dict.keys())[0] # feasible_subway_line_label.append(label) stop_list=list(line_dict.values())[0] ##generate transitline transitline_info = [] for item in transit_list_all: if list(item.values())[0][0]==label: transitline_info=list(item.values())[0] # print(feasible_subway_line_name) simplename=transitline_info[0] transit_strat_time = transitline_info[1] transit_end_time = transitline_info[2] interval=transitline_info[3] if ' ' in simplename: num = simplename.index(' ') simple_name = simplename[:num - 1] else: simple_name=simplename #transitline ptsch_transitLine = ptsch_domtree.createElement('transitLine') ptsch_transitLine.setAttribute('id',simple_name) ptsch_transitschedule.appendChild(ptsch_transitLine) #transitroute ptsch_transitRoute = ptsch_domtree.createElement('transitRoute') ptsch_transitRoute.setAttribute('id', simple_name+'1') ptsch_transitLine.appendChild(ptsch_transitRoute) #reverse ptsch_transitRoute_r = ptsch_domtree.createElement('transitRoute') ptsch_transitRoute_r.setAttribute('id', simple_name + '2') ptsch_transitLine.appendChild(ptsch_transitRoute_r) #transitmode ptsch_transportMode = ptsch_domtree.createElement('transportMode') text = ptsch_domtree.createTextNode('pt') ptsch_transportMode.appendChild(text) ptsch_transitRoute.appendChild(ptsch_transportMode) # reverse ptsch_transportMode_r = ptsch_domtree.createElement('transportMode') text_r = ptsch_domtree.createTextNode('pt') ptsch_transportMode_r.appendChild(text_r) ptsch_transitRoute_r.appendChild(ptsch_transportMode_r) #routeprofile ptsch_routeProfile = ptsch_domtree.createElement('routeProfile') #stop和stopfacility for stop in stop_list: stop_id='stop_'+simplename+stop['id'] stop_linkRefId=simplename+stop['id'] lon,lat=WGS84toMercator(float(stop['x']),float(stop['y'])) stopFacility=ptsch_domtree.createElement('stopFacility') stopFacility.setAttribute('id',stop_id) stopFacility.setAttribute('x', str(lon)) stopFacility.setAttribute('y', str(lat)) stopFacility.setAttribute('linkRefId', stop_linkRefId) ptsch_transitStops.appendChild(stopFacility) #reverse stopfacility stopFacility_r = ptsch_domtree.createElement('stopFacility') stopFacility_r.setAttribute('id', stop_id+'_r') stopFacility_r.setAttribute('x', str(lon+5))#随便设置的地点 stopFacility_r.setAttribute('y', str(lat+5)) stopFacility_r.setAttribute('linkRefId', stop_linkRefId+'_r') ptsch_transitStops.appendChild(stopFacility_r) #正向stop stop_node = ptsch_domtree.createElement('stop') stop_node.setAttribute('refId',stop_id) offset=stop_list.index(stop)*120+30 offsettime=datetime.timedelta(seconds=offset) if len(str(offsettime))==8: set_offset=str(offsettime) else: set_offset = '0'+str(offsettime) if stop_list.index(stop)==0: stop_node.setAttribute('departureOffset',set_offset) elif stop_list.index(stop)==len(stop_list)-1: stop_node.setAttribute('arrivalOffset', set_offset) else: stop_node.setAttribute('departureOffset', set_offset) stop_node.setAttribute('arrivalOffset', set_offset) stop_node.setAttribute('awaitDeparture', 'false') ptsch_routeProfile.appendChild(stop_node) ptsch_transitRoute.appendChild(ptsch_routeProfile) # reverse ptsch_routeProfile_r = ptsch_domtree.createElement('routeProfile') re_list=list(reversed(stop_list)) for stop_r in re_list: stop_id_r = 'stop_' + simplename+stop_r['id']+'_r' # stop_linkRefId_r = stop_r['id'] stop_node_r = ptsch_domtree.createElement('stop') stop_node_r.setAttribute('refId', stop_id_r) offset = re_list.index(stop_r) * 120 + 30 offsettime = datetime.timedelta(seconds=offset) if len(str(offsettime)) == 8: set_offset = str(offsettime) else: set_offset = '0' + str(offsettime) if re_list.index(stop_r) == 0: stop_node_r.setAttribute('departureOffset', set_offset) elif re_list.index(stop_r) == len(re_list) - 1: stop_node_r.setAttribute('arrivalOffset', set_offset) else: stop_node_r.setAttribute('departureOffset', set_offset) stop_node_r.setAttribute('arrivalOffset', set_offset) stop_node_r.setAttribute('awaitDeparture', 'false') ptsch_routeProfile_r.appendChild(stop_node_r) ptsch_transitRoute_r.appendChild(ptsch_routeProfile_r) #route ptsch_route = ptsch_domtree.createElement('route') #link # linklist=subway_tree.getElementsByTagName('link') for stop_num in range(len(stop_list)-1): from_stop=stop_list[stop_num] to_stop=stop_list[stop_num+1] from_stop_id=from_stop['id'] to_stop_id = to_stop['id'] link=ptsch_domtree.createElement('link') link.setAttribute('refId',simplename+from_stop_id) ptsch_route.appendChild(link) link1 = ptsch_domtree.createElement('link') link1.setAttribute('refId', 'from_'+simplename+from_stop_id+'_B_to_'+simplename+to_stop_id+'_A') ptsch_route.appendChild(link1) if stop_num==len(stop_list)-2: link2 = ptsch_domtree.createElement('link') link2.setAttribute('refId',simplename+to_stop_id) ptsch_route.appendChild(link2) ptsch_transitRoute.appendChild(ptsch_route) #reverse link ptsch_route_r = ptsch_domtree.createElement('route') for stop_num_r in range(len(re_list)-1): from_stop_r=re_list[stop_num_r] to_stop_r=re_list[stop_num_r+1] from_stop_id_r=from_stop_r['id'] to_stop_id_r = to_stop_r['id'] link_r=ptsch_domtree.createElement('link') link_r.setAttribute('refId',simplename+from_stop_id_r+'_r') ptsch_route_r.appendChild(link_r) link1_r = ptsch_domtree.createElement('link') link1_r.setAttribute('refId', 'from_'+simplename+to_stop_id_r+'_B_to_'+simplename+from_stop_id_r+'_A_r') ptsch_route_r.appendChild(link1_r) if stop_num_r==len(re_list)-2: link2_r = ptsch_domtree.createElement('link') link2_r.setAttribute('refId',simplename+to_stop_id_r+'_r') ptsch_route_r.appendChild(link2_r) ptsch_transitRoute_r.appendChild(ptsch_route_r) vehicle_id_list_one=[] #departures ptsch_departures = ptsch_domtree.createElement('departures') start_depart_time=StrToSecond(transit_strat_time) end_depart_time=StrToSecond(transit_end_time) for item in range(int(24*60/(60/interval))): a=int(start_depart_time+interval*item) if a>end_depart_time: break tt=datetime.timedelta(seconds=a) if len(str(tt))==8: set_tt=str(tt) else: set_tt = '0'+str(tt) ptsch_departure=ptsch_domtree.createElement('departure') ptsch_departure.setAttribute('id',str(depart_id))#防止不同line车辆id一样 depart_id=depart_id+1 ptsch_departure.setAttribute('departureTime', set_tt) aa=bus_label*1000+item%25 if aa<10: aa='0'+str(aa) else: aa=str(aa) ptsch_departure.setAttribute('vehicleRefId', aa) ptsch_departures.appendChild(ptsch_departure) if item<25: vehicle_id_list_one.append(bus_label*1000+(item%25)) ptsch_transitRoute.appendChild(ptsch_departures) # reverse departures ptsch_departures_r = ptsch_domtree.createElement('departures') for item_r in range(int(24*60/(60/interval))): a=int(start_depart_time+interval*item_r) if a>end_depart_time: break tt=datetime.timedelta(seconds=a) if len(str(tt))==8: set_tt=str(tt) else: set_tt = '0'+str(tt) ptsch_departure_r=ptsch_domtree.createElement('departure') ptsch_departure_r.setAttribute('id',str(depart_id))#防止车辆id一样 depart_id=depart_id+1 ptsch_departure_r.setAttribute('departureTime', set_tt) aa_r=bus_label*1000+25+item_r%25 ptsch_departure_r.setAttribute('vehicleRefId', str(aa_r)) ptsch_departures_r.appendChild(ptsch_departure_r) if item_r<25: vehicle_id_list_one.append(aa_r) ptsch_transitRoute_r.appendChild(ptsch_departures_r) vehicle_id_list_all.append({label:vehicle_id_list_one}) with open(schedule_xml, 'w') as f: # 缩进 - 换行 - 编码 ptsch_domtree.writexml(f, indent='\t', addindent='\t', newl='\n', encoding='utf-8') return vehicle_id_list_all
# -*- coding: utf-8 -*- __author__ = "Duong Dang" __version__ = "0.1" import logging, sys, re import urllib2, datetime import pytz from BeautifulSoup import BeautifulSoup from xml.dom.minidom import Document from xml.dom.minidom import DOMImplementation imp = DOMImplementation() doctype = imp.createDocumentType( qualifiedName='tv', publicId='', systemId="http://www.kazer.org/xmltv.dtd", ) logger = logging.getLogger("vtv4.py") hanoitz = pytz.timezone('Etc/GMT-7') paristz = pytz.timezone('Europe/Paris') fmt = '%Y%m%d%H%M %z' def main(): import optparse parser = optparse.OptionParser( usage='\n\t%prog [options]', version='%%prog %s' % __version__) parser.add_option("-v", "--verbose",
def generate_cobertura_xml(jscoverage_report, project_name, source_path): imp = DOMImplementation() doctype = imp.createDocumentType( qualifiedName='coverage', publicId='', systemId='http://cobertura.sourceforge.net/xml/coverage-04.dtd', ) doc = imp.createDocument(None, 'coverage', doctype) #print doc.toxml() #exit() #coverage_element = doc.createElementNS(None, "coverage") #doc.appendChild(coverage_element) coverage_element = doc.getElementsByTagName("coverage")[0] sources_element = doc.createElementNS(None, "sources") coverage_element.appendChild(sources_element) source_element = doc.createElementNS(None, "source") sources_element.appendChild(source_element) text = doc.createTextNode(source_path) source_element.appendChild(text) packages_element = doc.createElementNS(None, "packages") coverage_element.appendChild(packages_element) package_element = doc.createElementNS(None, "package") package_element.setAttributeNS(None, "name", project_name) package_element.setAttributeNS(None, "branch-rate", "0") package_element.setAttributeNS(None, "complexity", "0.0") packages_element.appendChild(package_element) classes_element = doc.createElementNS(None, "classes") package_element.appendChild(classes_element) total_statements = 0 total_hits = 0 for script in jscoverage_report: statement_lines = 0 covered_lines = 0 line_number = 0 class_element = doc.createElementNS(None, "class") class_element.setAttributeNS(None, "filename", script) class_element.setAttributeNS(None, "name", script) methods_element = doc.createElementNS(None, "methods") class_element.appendChild(methods_element) lines_element = doc.createElementNS(None, "lines") class_element.appendChild(lines_element) for line in jscoverage_report[script]["coverage"]: if line is not None: statement_lines = statement_lines + 1 total_statements = total_statements + 1 if line > 0: covered_lines = covered_lines + 1 total_hits = total_hits + 1 if line >= 0: line_element = doc.createElementNS(None, "line") lines_element.appendChild(line_element) line_element.setAttributeNS(None, "number", str(line_number)) line_element.setAttributeNS(None, "branch", "false") line_element.setAttributeNS(None, "hits", str(line)) line_number = line_number + 1 class_element.setAttributeNS(None, "branch-rate", "0") class_element.setAttributeNS(None, "complexity", "0.0") class_element.setAttributeNS(None, "line-rate", str(round((float(covered_lines)/float(statement_lines)),2))) classes_element.appendChild(class_element) package_element.setAttributeNS(None, "line-rate", str(round((float(total_hits)/float(total_statements)),2))) return doc.toprettyxml()
def generate_cobertura_xml(jscoverage_report, project_name, source_path): imp = DOMImplementation() doctype = imp.createDocumentType( qualifiedName='coverage', publicId='', systemId='http://cobertura.sourceforge.net/xml/coverage-04.dtd', ) doc = imp.createDocument(None, 'coverage', doctype) #print doc.toxml() #exit() #coverage_element = doc.createElementNS(None, "coverage") #doc.appendChild(coverage_element) coverage_element = doc.getElementsByTagName("coverage")[0] sources_element = doc.createElementNS(None, "sources") coverage_element.appendChild(sources_element) source_element = doc.createElementNS(None, "source") sources_element.appendChild(source_element) text = doc.createTextNode(source_path) source_element.appendChild(text) packages_element = doc.createElementNS(None, "packages") coverage_element.appendChild(packages_element) package_element = doc.createElementNS(None, "package") package_element.setAttributeNS(None, "name", project_name) package_element.setAttributeNS(None, "branch-rate", "0") package_element.setAttributeNS(None, "complexity", "0.0") packages_element.appendChild(package_element) classes_element = doc.createElementNS(None, "classes") package_element.appendChild(classes_element) total_statements = 0 total_hits = 0 for script in jscoverage_report: statement_lines = 0 covered_lines = 0 line_number = 0 class_element = doc.createElementNS(None, "class") class_element.setAttributeNS(None, "filename", script) class_element.setAttributeNS(None, "name", script) methods_element = doc.createElementNS(None, "methods") class_element.appendChild(methods_element) lines_element = doc.createElementNS(None, "lines") class_element.appendChild(lines_element) for line in jscoverage_report[script]["coverage"]: if line is not None: statement_lines = statement_lines + 1 total_statements = total_statements + 1 if line > 0: covered_lines = covered_lines + 1 total_hits = total_hits + 1 if line >= 0: line_element = doc.createElementNS(None, "line") lines_element.appendChild(line_element) line_element.setAttributeNS(None, "number", str(line_number)) line_element.setAttributeNS(None, "branch", "false") line_element.setAttributeNS(None, "hits", str(line)) line_number = line_number + 1 class_element.setAttributeNS(None, "branch-rate", "0") class_element.setAttributeNS(None, "complexity", "0.0") class_element.setAttributeNS( None, "line-rate", str(round((float(covered_lines) / float(statement_lines)), 2))) classes_element.appendChild(class_element) package_element.setAttributeNS( None, "line-rate", str(round((float(total_hits) / float(total_statements)), 2))) return doc.toprettyxml()