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 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 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 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 call(self): # define namespaces used soapEnv = "http://schemas.xmlsoap.org/soap/envelope/" # DOM document impl = DOMImplementation() domdoc = impl.createDocument(None, None, None) # SOAP envelope namespace seObj = domdoc.createElementNS(soapEnv, "SOAP-ENV:Envelope") seObj.setAttributeNS(soapEnv, "xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance") seObj.setAttributeNS(soapEnv, "xmlns:xsd", "http://www.w3.org/2001/XMLSchema") seObj.setAttributeNS(soapEnv, "xmlns:SOAP-ENV", "http://schemas.xmlsoap.org/soap/envelope/") # add it to the root domdoc.appendChild(seObj) # header header = domdoc.createElement("SOAP-ENV:Header") # TODO: insert correct path of element for key in self.headers.keys(): v = domdoc.createElement(key) vv = domdoc.createTextNode(self.headers[key]) v.appendChild(vv) header.appendChild(v) seObj.appendChild(header) # body body = domdoc.createElement("SOAP-ENV:Body") # TODO: insert data to body request seObj.appendChild(body) # dump request to string soapStr = domdoc.toxml() ##PrettyPrint(domdoc, soapStr) # construct the header and post ws = httplib.HTTP(self.host, self.port) ws.putrequest("POST", self.path) ws.putheader("Host", self.host) ws.putheader("User-Agent", "iVista SOAP Client") ws.putheader("Content-type", "text/xml; charset=\"UTF-8\"") ws.putheader("Content-length", "%d" % len(soapStr)) ws.putheader("SOAPAction", "\"%s\"" % self.action) ws.endheaders() ws.send(soapStr) # get the response statuscode, statusmessage, header = ws.getreply() if (statuscode != 200): raise Exception(statusmessage) res = ws.getfile().read() self.result = nodeToDic(parseString(res))
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 writeXmlFromJson(dataSetUri, dataSetName, outputDirectory, splitElement=None, usePostfix=1): postfix="" rows=99 start = [int] start[0] = 0 count = 100 domImplementation = DOMImplementation() domain = str(dataSetUri.split("//")[-1].split("/")[0].split('?')[0]) try: while(count > (start[0])): count = process(rows, start, dataSetUri, dataSetName, outputDirectory, domain, splitElement, usePostfix) print("count returned: ", count) print("start returned: ", start[0]) except KeyboardInterrupt: print("Interrupted - ", sys.exc_info()[0]) raise except: print("Exception - ", sys.exc_info()[0]) #traceback.print_exc(file=sys.stdout) raise
def createSvgResponse(svgDoc, extraData=None): imp = DOMImplementation() xmlDoc = imp.createDocument(None, 'svgResponse', '') root = xmlDoc.documentElement #create the node where the svg data should be stored tempNode = xmlDoc.createElement('svgData') tempNode.appendChild(xmlDoc.createCDATASection(svgDoc)) root.appendChild(tempNode) #add extra data if extraData is not None: extraInfoNode = xmlDoc.createElement('extraInfo') if hasattr(extraData, 'documentElement'): extraInfoNode.appendChild(extraData.documentElement) else: extraInfoNode.appendChild(extraData) root.appendChild(extraInfoNode) return xmlDoc.toxml()
def __init__(self, description, outfile, graph, generation_log=config_generation_log, *args, **kwargs): self.__logger = logging.getLogger(self.__module__) BaseGraphOutputter.__init__(self, description=description, outfile=outfile, graph=graph, generation_log=generation_log, *args, **kwargs) GraphicalGraphOutputter.__init__(self, *args, **kwargs) self._supports_grouping = False # TODO self.__dom = DOMImplementation().createDocument(GraphMLConstants.GRAPHML_NS, "graphml", None) self.__edge_outputter = GraphMLEdgeOutputter(self.__dom, self._edge_label_decorators(), self._edge_tooltip_decorators(), self._graph()) self.__node_outputter = GraphMLNodeOutputter(self.__dom, self._node_label_decorators(), self._node_tooltip_decorators(), self._graph())
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()
def createXmlGridIdNode(gridId): imp = DOMImplementation() xmlDoc = imp.createDocument(None, 'usid', None) root = xmlDoc.documentElement root.appendChild(xmlDoc.createTextNode(gridId)) return root
def as_root(self): x = DOMImplementation() self.xml_doc = x.createDocument("", self.name, None) self.node = self.xml_doc.documentElement self.level = 0
def writeXmlFromJson(dataSetUri, outFileName): postfix = "" rows = 99 start = 0 count = 100 domImplementation = DOMImplementation() obj_xml_rootDocument = Document() root = obj_xml_rootDocument.createElement("root") obj_xml_rootDocument.appendChild(root) try: print("About to create file " + outFileName) obj_StreamReaderWriter = codecs.open(outFileName, 'w+', 'utf-8') print("obj_StreamReaderWriter: " + obj_StreamReaderWriter.__class__.__name__) while (count > (rows + start - 100)): postfix = str.format("&rows=" + str(rows) + "&start=" + str(start)) try: obj_addinfourl = urllib2.urlopen(dataSetUri + postfix, timeout=5) except exceptions.KeyboardInterrupt: print "Interrupted - ", sys.exc_info()[0] raise except: print("Exception %s when opening %s" % (sys.exc_info()[0], dataSetUri + postfix)) return print("Retrieved content at " + dataSetUri + postfix) assert (obj_addinfourl is not None) obj_json_str = (obj_addinfourl.read()) assert (obj_json_str is not None) obj_dict = json.loads(obj_json_str) elem = obj_xml_rootDocument.createElement("datasets") parse_element(obj_xml_rootDocument, elem, obj_dict) root.appendChild(elem) #print(obj_xml_rootDocument.toprettyxml()) countElementList = elem.getElementsByTagName("count") if (len(countElementList) == 1): assert (len(countElementList[0].childNodes[0].data) > 0) count = int(countElementList[0].childNodes[0].data) print("Remaining: " + str(count - (rows + start))) print("Remaining: " + str(count - (rows + start))) start += 100 print("Rows+Start-100: " + str((rows + start - 100))) #obj_StreamReaderWriter.write(obj_xml_Document.toprettyxml(encoding='utf-8', indent=' ')) except exceptions.KeyboardInterrupt: print "Interrupted - ", sys.exc_info()[0] raise except: print "Exception - ", sys.exc_info()[0] traceback.print_exc(file=sys.stdout) obj_StreamReaderWriter.write(obj_xml_rootDocument.toprettyxml()) print("Output written to %s" % outFileName) if obj_StreamReaderWriter is not None: obj_StreamReaderWriter.close()
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()
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
def __init__(self, fname, log, lang): self.fname = fname self.lang = lang self.zfile = zipfile.ZipFile(fname, 'r', zipfile.ZIP_DEFLATED, True) log('opened', fname) ## opf to use opf_file = self.xget(self.xml('META-INF/container.xml'), ['<container', '<rootfiles', '<rootfile'], 'full-path') log('opf is', opf_file) opf_dir = os.path.dirname(opf_file) ## toc item toc_item = self.xget(self.xml(opf_file), ['<package', '<spine'], 'toc') log('toc item is', toc_item) ## toc file if toc_item: toc_file = self.xget( self.xml(opf_file), ['<package', '<manifest', '<item', '#' + toc_item], 'href') log('toc file is', toc_file) else: log('No TOC', '') toc_file = None ## nav file nav_file = self.xget( self.xml(opf_file), ['<package', '<manifest', '<item', '=properties=nav'], 'href') log('nav file is', nav_file) ## content items txt_items = self.xgetall(self.xml(opf_file), ['<package', '<spine', '<itemref'], "idref") log('txt items is', *txt_items) ## content files txt_files = [ self.xget(self.xml(opf_file), ['<package', '<manifest', '<item', '#' + i], 'href') for i in txt_items ] log('txt files is', *txt_files) self.content = {} self.curpage = 0 self.pages = [] ### Taking the order of files from OPF ( rather than from NAV ) for tf in txt_files: if tf == nav_file: continue rf = os.path.relpath(tf, os.path.dirname(nav_file)) log("Processing ", tf, "( ", rf, ')') with self.zfile.open(opf_dir + "/" + tf) as fd: orig = fd.read() modi = re.sub(r'~!~([\sa-zA-z0-9_+-]*)~@~', lambda x: self.to_pagenumber(rf, x), orig) if orig != modi: self.content[opf_dir + "/" + tf] = modi ### Now update the nav. navdom = self.xml(opf_dir + "/" + nav_file) ## <nav epub:type="page-list"> ele_nav = navdom.createElement('nav') navdom.getElementsByTagName('html')[0].getElementsByTagName( 'body')[0].appendChild(ele_nav) ele_nav.setAttribute('epub:type', 'page-list') ## h1 ele_h1 = navdom.createElement('h1') ele_nav.appendChild(ele_h1) ele_h1.appendChild(navdom.createTextNode('Ink Print Page List')) ## ol ele_ol = navdom.createElement('ol') ele_nav.appendChild(ele_ol) ## li a href = for lp in self.pages: ele_li = navdom.createElement('li') ele_ol.appendChild(ele_li) ele_a = navdom.createElement('a') ele_li.appendChild(ele_a) ele_a.setAttribute('href', lp[0]) ele_a.appendChild(navdom.createTextNode(lp[1])) ## make sure nav_file is proper html. doctype = DOMImplementation().createDocumentType(qualifiedName='html', publicId='', systemId='') navdom.insertBefore(doctype, navdom.documentElement) html = navdom.documentElement html.setAttribute('xmlns', "http://www.w3.org/1999/xhtml") html.setAttribute('xmlns:epub', "http://www.idpf.org/2007/ops") html.setAttribute('xml:lang', self.lang) html.setAttribute('lang', self.lang) self.content[opf_dir + "/" + nav_file] = navdom.toprettyxml(encoding='utf-8')
def __init__(self): object.__init__(self) self.dom = DOMImplementation() self.doctype = self.dom.createDocumentType('xmltable', 0, '') self.doc = Document() self.doc.appendChild(self.doctype)
#! /usr/bin/env python # -*- 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__)
class GraphMLGraphOutputter(BaseGraphOutputter, GraphicalGraphOutputter): # TODO do not inherit from BaseGraphOutputter! def __init__(self, description, outfile, graph, generation_log=config_generation_log, *args, **kwargs): self.__logger = logging.getLogger(self.__module__) BaseGraphOutputter.__init__(self, description=description, outfile=outfile, graph=graph, generation_log=generation_log, *args, **kwargs) GraphicalGraphOutputter.__init__(self, *args, **kwargs) self._supports_grouping = False # TODO self.__dom = DOMImplementation().createDocument(GraphMLConstants.GRAPHML_NS, "graphml", None) self.__edge_outputter = GraphMLEdgeOutputter(self.__dom, self._edge_label_decorators(), self._edge_tooltip_decorators(), self._graph()) self.__node_outputter = GraphMLNodeOutputter(self.__dom, self._node_label_decorators(), self._node_tooltip_decorators(), self._graph()) @staticmethod def usual_extension(): return '.graphml' def _process_edge(self, edge, xml_node): self.__edge_outputter.render_edge(edge, xml_node) # def _output_group(self, group_name, processed_nodes): # self.__logger.debug("Outputting group %s" % (group_name)) # print >>self.file(), "subgraph cluster_%s {" % (GraphMLFormatterHelper.sanitize_node_name(group_name),) # print >>self.file(), ("colorscheme=\"pastel28\"; bgcolor=\"%i\"; label=\"%s\"" # % (hash(group_name) % 8 + 1, group_name)) # for node in processed_nodes: # print >>self.file(), "%s;" % (GraphMLFormatterHelper.sanitize_node_name(node),) # print >>self.file(), "}" def _process_node(self, node, xml_node): self.__node_outputter.process_node(node, xml_node) def _output_edges(self, xml_node): for edge in sorted(self._graph().edges()): self._process_edge(edge, xml_node) def _output_nodes(self, xml_node): for node in self._graph().nodes_raw(): if node != None: self._process_node(node, xml_node) else: self.__logger.debug("node == None in _output_nodes") def _output_head(self, xml_node): # <key id="d0" for="node" yfiles.type="nodegraphics"/> # <key id="d1" for="edge" yfiles.type="edgegraphics"/> key1 = self.__dom.createElementNS(GraphMLConstants.GRAPHML_NS, "key") key1.setAttribute("id", MyGraphMLConstants.NODE_GRAPHICS_KEY) key1.setAttribute("for", "node") key1.setAttribute("yfiles.type", "nodegraphics") xml_node.appendChild(key1) key2 = self.__dom.createElementNS(GraphMLConstants.GRAPHML_NS, "key") key2.setAttribute("id", MyGraphMLConstants.EDGE_GRAPHICS_KEY) key2.setAttribute("for", "edge") key2.setAttribute("yfiles.type", "edgegraphics") xml_node.appendChild(key2) # description = self.description() # print >>self.file(), ("label=\"%s (graph date %s)\\n%s\\n%s\\n%s\";" % # (description if description else "no graph description", # datetime.now().strftime("%Y-%m-%d %H:%M"), # "\\n".join(deco.decorate_graph(self._graph()) for deco in self._decorator_config().get_graph_decorators()), # ", ".join(chain.from_iterable(((deco.description() for deco in self._node_label_decorators()), # ("%s (T)" % deco.description() for deco in self._node_tooltip_decorators()) # ))), # ", ".join(chain.from_iterable(((deco.description() for deco in self._edge_label_decorators()), # ("%s (T)" % deco.description() for deco in self._edge_tooltip_decorators()) # ))), # )) def _output_tail(self): self.__dom.writexml(self.file(), newl="\n") def output_all(self): self.register_log() self.__logger.debug("Outputting graph %s using node grouper %s" % (self._graph(), self._node_grouper())) root_node = self.__dom.getElementsByTagName("graphml")[0] root_node.setAttribute("xmlns", GraphMLConstants.GRAPHML_NS) # TODO why is this necessary? root_node.setAttribute("xmlns:y", GraphMLConstants.YED_NS) # TODO why is this necessary? self._output_head(root_node) #self._output_groups() # TODO output nodes by groups self._attach_decorators() graph_node = self.__dom.createElementNS(GraphMLConstants.GRAPHML_NS, "graph") graph_node.setAttribute("id", "G") graph_node.setAttribute("edgedefault", "directed") root_node.appendChild(graph_node) try: self._output_nodes(graph_node) self._output_edges(graph_node) finally: self._detach_decorators() self._output_tail()
@author: Taizo ITO <*****@*****.**> @copyright: """ import os import re import libvirt import libvirtmod import logging import glob from StringIO import StringIO from xml.dom.minidom import DOMImplementation implementation = DOMImplementation() import karesansui import karesansui.lib.locale from karesansui.lib.utils import get_xml_parse as XMLParse from karesansui.lib.utils import get_xml_xpath as XMLXpath from karesansui.lib.utils import get_nums_xml_xpath as XMLXpathNum from karesansui.lib.utils import get_inspect_stack, preprint_r, r_chgrp, r_chmod from karesansui.lib.const import VIRT_XML_CONFIG_DIR, VIRT_SNAPSHOT_DIR, KARESANSUI_GROUP from karesansui.lib.file.configfile import ConfigFile from karesansui.lib.virt.config import ConfigParam from karesansui.lib.virt.virt import KaresansuiVirtException, \ KaresansuiVirtConnection