def edx(self, out_dir): # Copy the Pdf to the static directory static_dir = os.path.join(out_dir, 'static') if not os.path.exists(static_dir): os.makedirs(static_dir) # In order to get an unique filename inside edx, we have to prefix the project and group name target_filename = self.url_name()+'.pdf' target_path = os.path.join(static_dir,target_filename) shutil.copyfile(os.path.join(self.parent.path, self.path), target_path); html_dir = os.path.join(out_dir, 'html') if not os.path.exists(html_dir): os.makedirs(html_dir) html = Element('html', {'filename':self.url_name(), 'display_name':"Pdf"}); tree = ElementTree(html) tree.write(os.path.join(html_dir, "{0}.xml".format(self.url_name())) ) # We have to double %% because % is a placeholder for the argument html = '' if courseURL == None: logging.warning("courseURL is not specified. Therefore the inline pdf-viewer will be disabled.") else: html += ''' <object data="%(courseURL)s/asset/%(file)s" type="application/pdf" width="100%%" height="600pt"> ''' %{'courseURL':courseURL , 'file':target_filename} html += ''' <a href="/static/%(file)s">Download Pdf %(name)s</a> ''' % {'file':target_filename, 'name':os.path.basename(self.path)} html += "</object>" with codecs.open(os.path.join(html_dir, "{0}.html".format(self.url_name())), mode='w', encoding='utf-8') as f: f.write(html)
def write(self, filename, midi): midifile = Element('MidiFile') header = HeaderChunk() header.write(midifile, midi.header) i = 0 for cur in midi.tracks: node = SubElement(midifile, '_' + str(i)) #garantit l'ordre des datas temp = TrackChunk() temp.write(node, cur) i += 1 tree = ElementTree(midifile) tree.write(filename, encoding="utf-8") import xml.dom.minidom root = xml.dom.minidom.parse(filename) f = open(filename, "w") f.write(root.toprettyxml())
def merge(self): last_hr = 0 self.tacx_tcx = self._parse_file(self.tacx_tcx_file) self.hr_tcx = self._parse_file(self.hr_tcx_file) for tp in self.tacx_tcx.dom_trackpoints: timestamp = tp.find('role:Time', ns).text timestamp_key = timestamp[0:19] if timestamp_key in self.hr_tcx.TrackPoints.keys(): heartrate_from_other_file = self.hr_tcx.TrackPoints[timestamp_key].HeartRateBpm if heartrate_from_other_file is not None: hr_node = self._create_heartrate(heartrate_from_other_file) tp.append(hr_node) last_hr = heartrate_from_other_file else: hr_node = self._create_heartrate(last_hr) tp.append(hr_node) tree = ElementTree(self.tacx_tcx.root) tree.write(open(self.file_name, 'wb'), encoding="utf-8", xml_declaration=True) # Add UGLY! temporary fix for namespace "ns1-issue" f = open (self.file_name, "r") data = f.read() data = data.replace('ns1:TPX', 'TPX') data = data.replace('ns1:Speed', 'Speed') data = data.replace('ns1:Watts', 'Watts') f.close f = open(self.file_name, "w") f.write(data) f.close() return self.file_name
def edx(self, out_dir): # Copy the image to the static directory static_dir = os.path.join(out_dir, 'static') if not os.path.exists(static_dir): os.makedirs(static_dir) # In order to get an unique filename inside edx, we have to prefix the project and group name # We cannot use the filename, because it may contain characters, that have to be escaped. # Therefore we just add the extension, which is expected to contain [a-z][A-Z][0-9]. _, fileExtension = os.path.splitext(self.path) target_filename = self.url_name()+fileExtension target_path = os.path.join(static_dir,target_filename) shutil.copyfile(os.path.join(self.parent.path, self.path), target_path); html_dir = os.path.join(out_dir, 'html') if not os.path.exists(html_dir): os.makedirs(html_dir) html = Element('html', {'filename':self.url_name(), 'display_name':"Img"}); tree = ElementTree(html) tree.write(os.path.join(html_dir, "{0}.xml".format(self.url_name())) ) # We have to double %% because % is a placeholder for the argument html = '<img src="/static/%(file)s">' % {'file':target_filename} html += '''<br> <a href="/static/%(file)s">Download Image %(name)s</a> ''' % {'file':target_filename, 'name':os.path.basename(self.path)} with codecs.open(os.path.join(html_dir, "{0}.html".format(self.url_name())), mode='w', encoding='utf-8') as f: f.write(html)
def serialize(data, name='object'): content_elem = Element(name) _serialize(content_elem, data) tree = ElementTree(content_elem) f = StringIO() tree.write(f, 'UTF-8') return f.getvalue()
def extractImages(html): if html is None: return [], html tree = ElementTree() tree.parse(StringIO(html)) imagetags = tree.findall(".//img") images = [] for tag in imagetags: image = tag.get('src') path, name = os.path.split(image) if image not in images: images.append(image) tag.set('alt', name) tag.set('title', name) #index files for multipart storage index = {} for image in images: path, name = os.path.split(image) index[image] = '0x%08x' % binascii.crc32(name) #update html email image tags for tag in imagetags: image = tag.get('src') tag.set('src', "cid:%s" % index[image]) html = StringIO() tree.write(html) html.write("\n") return [index, html.getvalue()]
def main(): options, args = parse_args() rootElement = Element('packages') packages = {} print "Searching for packages.config files:" for dirpath, subdirs, filenames in walk('src'): for filename in filenames: if filename == 'packages.config': filepath = join(dirpath, filename) print " " + filepath et = parse(filepath) for packageElement in et.findall('package'): pkgId = packageElement.get('id') pkgVersion = packageElement.get('version') packages[pkgId, pkgVersion] = packageElement print print "Writing projectdata/packages.config:" rootElement.extend([value for (key, value) in sorted(packages.items())]) indent(rootElement) tree = ElementTree(rootElement) dump(tree) tree.write('projectdata/packages.config')
def write(pattern, f, settings=None): """Writes an svg file of the stitchblocks.""" root = Element(NAME_SVG) root.set(ATTR_VERSION, VALUE_SVG_VERSION) root.set(ATTR_XMLNS, VALUE_XMLNS) root.set(ATTR_XMLNS_LINK, VALUE_XLINK) root.set(ATTR_XMLNS_EV, VALUE_XMLNS_EV) extents = pattern.extents() width = extents[2] - extents[0] height = extents[3] - extents[1] root.set(ATTR_WIDTH, str(width)) root.set(ATTR_HEIGHT, str(height)) viewbox = \ str(extents[0]) + " " +\ str(extents[1]) + " " +\ str(width) + " " +\ str(height) root.set(ATTR_VIEWBOX, viewbox) for stitchblock in pattern.get_as_stitchblock(): block = stitchblock[0] thread = stitchblock[1] path = SubElement(root, NAME_PATH) data = "M" for stitch in block: x = stitch[0] y = stitch[1] data += " " + str(x) + "," + str(y) path.set(ATTR_DATA, data) path.set(ATTR_FILL, VALUE_NONE) path.set(ATTR_STROKE, thread.hex_color()) path.set(ATTR_STROKE_WIDTH, "3") tree = ElementTree(root) tree.write(f)
def _split_configuration(self, projectfile, temp_dir): num_pieces = multiprocessing.cpu_count() tree = ET(file=projectfile) num_files = len(tree.findall('./files/file')) splitfiles = [] files_per_job = int(math.ceil(float(num_files)/num_pieces)) for idx in xrange(num_pieces): tree = ET(file=projectfile) root = tree.getroot() start = idx*files_per_job end = start + files_per_job if end > num_files: end = None for elem in ('files', 'images', 'pages', 'file-name-disambiguation'): elem_root = root.find(elem) to_keep = elem_root.getchildren()[start:end] to_remove = [x for x in elem_root.getchildren() if not x in to_keep] for node in to_remove: elem_root.remove(node) out_file = os.path.join(temp_dir, "{0}-{1}.ScanTailor".format( os.path.splitext(os.path.basename( projectfile))[0], idx)) tree.write(out_file) splitfiles.append(out_file) return splitfiles
def tweak_build_xml(self): runjdwp_args = [ 'transport=dt_socket', 'server=y', 'address=8765', 'suspend=n', ] runjdwp_args = ','.join(runjdwp_args) jvm_debug_args = [ '-Xdebug', '-Xrunjdwp:%s' % (runjdwp_args, ), ] jvm_debug_args = ' '.join(jvm_debug_args) build_xml = self.get_build_xml() tree = ElementTree() tree.parse(build_xml) root = tree.getroot() targets = root.findall('target') for node in targets: if node.get('name') == 'run': java_node = node.find('java') SubElement(java_node, 'jvmarg', { 'line': jvm_debug_args, }) tree.write(build_xml)
def edx(self, out_dir): video_dir = os.path.join(out_dir, 'video') if not os.path.exists(video_dir): os.makedirs(video_dir) video = Element('video', {'youtube':'1.00:'+self.youtube_id, 'youtube_id_1_0':self.youtube_id}); tree = ElementTree(video) tree.write(os.path.join(video_dir, "{0}.xml".format(self.url_name())) )
def test_3x32mb_download_from_xml(self): '''Download three randomly-generated 32MB files from a GT server via an XML manifest''' uuid1 = self.data_upload_test(1024 * 1024 * 32) uuid2 = self.data_upload_test(1024 * 1024 * 32) uuid3 = self.data_upload_test(1024 * 1024 * 32) uuids = [uuid1, uuid2, uuid3] # build a XML result set result_set = Element('ResultSet') result_1 = SubElement(result_set, 'Result') analysis_data_uri_1 = SubElement(result_1, 'analysis_data_uri') analysis_data_uri_1.text = '%s/cghub/data/analysis/download/' \ % TestConfig.HUB_SERVER + str(uuid1) result_2 = SubElement(result_set, 'Result') analysis_data_uri_2 = SubElement(result_2, 'analysis_data_uri') analysis_data_uri_2.text = '%s/cghub/data/analysis/download/' \ % TestConfig.HUB_SERVER + str(uuid2) result_3 = SubElement(result_set, 'Result') analysis_data_uri_3 = SubElement(result_3, 'analysis_data_uri') analysis_data_uri_3.text = '%s/cghub/data/analysis/download/' \ % TestConfig.HUB_SERVER + str(uuid3) doc = ElementTree(result_set) f = NamedTemporaryFile(delete=False, suffix='.xml') doc.write(f) f.close() self.data_download_test_xml(f.name, uuids) os.remove(f.name)
def edx(self, out_dir): # Copy the Pdf to the static directory static_dir = os.path.join(out_dir, 'static') if not os.path.exists(static_dir): os.makedirs(static_dir) # In order to get an unique filename inside edx, we have to prefix the project and group name _, fileExtension = os.path.splitext(self.path) target_filename = self.url_name()+fileExtension target_path = os.path.join(static_dir,target_filename) shutil.copyfile(os.path.join(self.parent.path, self.path), target_path); html_dir = os.path.join(out_dir, 'html') if not os.path.exists(html_dir): os.makedirs(html_dir) html = Element('html', {'filename':self.url_name(), 'display_name':"File"}); tree = ElementTree(html) tree.write(os.path.join(html_dir, "{0}.xml".format(self.url_name())) ) (_ , filename) = os.path.split(self.path) html = ''' <a href="/static/%(file)s">Download %(filename)s</a> ''' % {'file':target_filename, 'filename':filename} with codecs.open(os.path.join(html_dir, "{0}.html".format(self.url_name())), mode='w', encoding='utf-8') as f: f.write(html)
def dump(self, stream): if self.prettyprint: self.indent(self.xml) document = ElementTree(self.xml) header='<?xml version="1.0" encoding="%s"?>'%self.encoding stream.write(header.encode(self.encoding)) document.write(stream, encoding=self.encoding)
def dump(self, stream): if self.prettyprint: self.indent(self.xml) document = ElementTree(self.xml) header = '<?xml version="1.0" encoding="%s"?>' % self.encoding stream.write(header.encode(self.encoding)) document.write(stream, encoding=self.encoding)
def _split_configuration(self, projectfile, temp_dir): num_pieces = multiprocessing.cpu_count() tree = ET(file=unicode(projectfile)) num_files = len(tree.findall('./files/file')) splitfiles = [] files_per_job = int(math.ceil(float(num_files)/num_pieces)) for idx in xrange(num_pieces): tree = ET(file=unicode(projectfile)) root = tree.getroot() start = idx*files_per_job end = start + files_per_job if end > num_files: end = None for elem in ('files', 'images', 'pages', 'file-name-disambiguation'): elem_root = root.find(elem) to_keep = elem_root.getchildren()[start:end] to_remove = [x for x in elem_root.getchildren() if not x in to_keep] for node in to_remove: elem_root.remove(node) out_file = temp_dir / "{0}-{1}.ScanTailor".format(projectfile.stem, idx) tree.write(unicode(out_file)) splitfiles.append(out_file) return splitfiles
def to_html(self, obj): root = Element('body') self.data_to_html(root, obj) tree = ElementTree(root) stream = StringIO() tree.write(stream, 'UTF-8') return "<html>" + stream.getvalue() + "</html>"
def close(self): if len(self.segment_values) == 0: return root = Element(NAME_SVG) root.set(ATTR_VERSION, VALUE_SVG_VERSION) root.set(ATTR_XMLNS, VALUE_XMLNS) root.set(ATTR_XMLNS_LINK, VALUE_XLINK) root.set(ATTR_XMLNS_EV, VALUE_XMLNS_EV) width = self.max_x - self.min_x height = self.max_y - self.min_y root.set(ATTR_WIDTH, str(width)) root.set(ATTR_HEIGHT, str(height)) viewbox = \ str(self.min_x) + " " + \ str(self.min_y) + " " + \ str(width) + " " + \ str(height) root.set(ATTR_VIEWBOX, viewbox) group = SubElement(root, NAME_GROUP) group.set(ATTR_FILL, VALUE_NONE) group.set(ATTR_STROKE, "#000") try: for segments in self.segment_values: path = SubElement(group, NAME_PATH) data = "M%i,%i %i,%i" % (segments[0], segments[1], segments[2], segments[3]) path.set(ATTR_DATA, data) except MemoryError: pass tree = ElementTree(root) tree.write(self.write_object) self.segment_values = []
def to_xml(self, obj): root = Element('response') self.data_to_xml(root, obj) tree = ElementTree(root) stream = StringIO() tree.write(stream, 'UTF-8') return '<?xml version="1.0"?>' + stream.getvalue()
def edx(self, out_dir): discussion_dir = os.path.join(out_dir, 'discussion') if not os.path.exists(discussion_dir): os.makedirs(discussion_dir) discussion = Element('discussion', {'discussion_id':self.url_name()}); tree = ElementTree(discussion) tree.write(os.path.join(discussion_dir, "{0}.xml".format(self.url_name())) )
def create_czmac_from_pythonfile(macro_filename, python_filename): """ Create a czmac file out of the given python file. Write the macro into the given 'macroname' file. """ # Read python content from given file with open(python_filename, 'rt') as python: python_content = python.read() # Create czmac xml structure script_node = Element("Script") script_node.set("Version", "1.0") language_node = Element("Language") language_node.text = "Python" script_node.append(language_node) text_node = Element("Text") text_node.text = python_content script_node.append(text_node) doc = ElementTree(script_node) # write czmac to given filename doc.write(macro_filename)
def create_czmac(macro_filename, macro_lines): # write a czmac file with the given OAD macro lines # Prepare python content python_content = "" for line in macro_lines: python_content += line + "\n" # Create czmac xml structure script_node = Element("Script") script_node.set("Version", "1.0") language_node = Element("Language") language_node.text = "Python" script_node.append(language_node) text_node = Element("Text") text_node.text = python_content script_node.append(text_node) doc = ElementTree(script_node) # write czmac to given filename doc.write(macro_filename)
def main(): options, args = parse_args() rootElement = Element('packages') packages = {} print "Searching for packages.config files:" for dirpath, subdirs, filenames in walk('src'): for filename in filenames: if filename == 'packages.config': filepath = join(dirpath, filename) print " " + filepath et = parse(filepath) for packageElement in et.findall('package'): pkgId = packageElement.get('id') pkgVersion = packageElement.get('version') packages[pkgId, pkgVersion] = packageElement print print "Writing projectdata/packages.config:" rootElement.extend([value for (key,value) in sorted(packages.items())]) indent(rootElement) tree = ElementTree(rootElement) dump(tree) tree.write('projectdata/packages.config')
def serialize(data, name='object'): content_elem = Element(name) _serialize(content_elem, data) tree = ElementTree(content_elem) f = StringIO() tree.write(f, 'UTF-8') return f.getvalue()
def tweak_build_xml(self): runjdwp_args = [ 'transport=dt_socket', 'server=y', 'address=8765', 'suspend=n', ] runjdwp_args = ','.join(runjdwp_args) jvm_debug_args = [ '-Xdebug', '-Xrunjdwp:%s' % (runjdwp_args,), ] jvm_debug_args = ' '.join(jvm_debug_args) build_xml = self.get_build_xml() tree = ElementTree() tree.parse(build_xml) root = tree.getroot() targets = root.findall('target') for node in targets: if node.get('name') == 'run': java_node = node.find('java') SubElement(java_node, 'jvmarg', { 'line': jvm_debug_args, }) tree.write(build_xml)
def exportGEXF(self, fileName): rootNode = Element("gexf") rootNode.attrib['xmlns'] = "http://www.gexf.net/1.2draft" rootNode.attrib['version'] = "1.2" graphNode = Element("graph") graphNode.attrib['mode'] = "static" graphNode.attrib['defaultedgetype'] = "directed" rootNode.append(graphNode) graphNode.append(Node.getGexfAttributeNode()) graphNode.append(Vertex.getGexfAttributeNode()) NodesList = Element("nodes") for n in self.nodes: NodesList.append(n.exportToGexfNode()) graphNode.append(NodesList) EdgesList = Element("edges") for e in self.vertices: EdgesList.append(e.exportToGexfNode()) graphNode.append(EdgesList) doc = ElementTree(rootNode) doc.write(fileName, "utf8", '<?xml version="1.0" encoding="UTF-8"?>')
def build(): character_name = raw_input("What is the name of this character? -> ") tree = Element("conversation") stack = [] prompts = [] stack.append(tree) prompts.append("as a greeting? -> "); while (len(stack)>0): # the current node in the conversation conversation = stack.pop() prompt = prompts.pop() # add an ai_dialog section to the conversation dialog = raw_input("What does the character say " + prompt) if (len(dialog) == 0): continue ai_dialog = SubElement(conversation, "ai_dialog").text = dialog # determine how many dialog options the player has at this point how_many_options = int(raw_input('how many dialog options does the player have from "' + ai_dialog +'" -> ')) # if there are dialog options... if (how_many_options > 0): # create a branch for player dialog options player_dialog_options = SubElement(conversation, "player_dialog_options") for i in range(how_many_options): # create a new choice choice = SubElement(player_dialog_options, "choice") # convert the dialog option number into ordinal form ordinal = lambda n: "%d%s" % (n,"tsnrhtdd"[(n/10%10!=1)*(n%10<4)*n%10::4]) # add the player's reply reply = raw_input("what is the player's " + ordinal(i+1) + ' response to "' + ai_dialog +'" -> ') SubElement(choice, "reply").text = reply # add a new conversation as a child of the selected choice stack.append(SubElement(choice, "conversation")) prompts.append('to "' + reply +'" -> ') output = ElementTree(tree) # save the tree to an xml file in a local directory named after the character path = os.path.dirname(os.path.realpath(__file__))+"/character-dialogs/"+character_name+"_conversation.xml" output.write(path)
def write(self, file): """ write(file) -> None Write XML to filename of file object in 'file' """ et = ElementTree(self.root) et.write(file)
def serialize(data, name='object'): content_elem = Element('contenttype') content_elem.attrib['name'] = name _serialize(content_elem, data) tree = ElementTree(content_elem) f = StringIO() tree.write(f, 'UTF-8') return f.getvalue()
def write(self, fh): # Serialize graph G in GEXF to the open fh if self.prettyprint: self.indent(self.xml) document = ElementTree(self.xml) header = '<?xml version="1.0" encoding="%s"?>' % self.encoding fh.write(header.encode(self.encoding)) document.write(fh, encoding=self.encoding)
def serialize(data, name='object'): content_elem = Element('contenttype') content_elem.attrib['name'] = name _serialize(content_elem, data) tree = ElementTree(content_elem) f = StringIO() tree.write(f, 'UTF-8') return f.getvalue()
def write(self, fh): # Serialize graph G in GEXF to the open fh if self.prettyprint: self.indent(self.xml) document = ElementTree(self.xml) header='<?xml version="1.0" encoding="%s"?>'%self.encoding fh.write(header.encode(self.encoding)) document.write(fh, encoding=self.encoding)
def msgfdb2pepxml(in_file, out_file, modifications_file=None, mzxml=None, fasta=None): """ Convert tab-separated ms-gfdb output to pepXML * in_file -- output file of MS-GFDB * out_file -- pepXML file to write to * modifications_file -- modifications file of MS-GFDB (Mods.txt) * mzxml -- input mzXML file of MS-GFDB * fasta -- input fasta file of MS-GFDB with database of peptides """ if not out_file: out_filename = re.sub(r"\.msgfdb$", ".pep.xml", in_file.name) if out_filename == in_file.name: raise Exception( "Provide output file or input file with extension .msgfdb") out_file = open(out_filename, 'w') modifications = default_modifications num_mods = None if modifications_file: num_mods, modifications = read_modifications(modifications_file) msms_pipeline_analysis = create_msms_pipeline_analysis() tree = ElementTree(msms_pipeline_analysis) msms_run_summary = SubElement(msms_pipeline_analysis, 'msms_run_summary') base_name = remove_file_extention(os.path.abspath(in_file.name)) msms_run_summary.set('base_name', base_name) SubElement(msms_run_summary, 'sample_enzyme') search_summary = SubElement(msms_run_summary, 'search_summary') search_summary.set('search_engine', "X! Tandem (k-score)") if fasta: search_database = SubElement(search_summary, 'search_database') search_database.set('local_path', os.path.abspath(fasta.name)) apply_msgfdb(in_file, msms_run_summary, modifications, num_mods) set_hit_ranks(msms_run_summary) if mzxml: msrun = get_msrun(mzxml) ms_instrument = msrun.find('msInstrument') if ms_instrument is not None: for field in [ 'msManufacturer', 'msModel', 'msIonisation', 'msDetector' ]: param = ms_instrument.find(field) if param is not None: msms_run_summary.set(field, param.get('value')) set_retention_times(msrun, msms_run_summary) out = StringIO() tree.write(out, encoding='UTF-8', xml_declaration=True) out_file.write(out.getvalue().replace('>', '>\n')) in_file.close() out_file.close() if modifications_file: modifications_file.close() if mzxml: mzxml.close() if fasta: fasta.close()
def edx(self, out_dir): discussion_dir = os.path.join(out_dir, 'discussion') if not os.path.exists(discussion_dir): os.makedirs(discussion_dir) discussion = Element('discussion', {'discussion_id':self.url_name(), 'discussion_category': self.parent.project(), 'discussion_target': self.parent.group() }) tree = ElementTree(discussion) tree.write(os.path.join(discussion_dir, "{0}.xml".format(self.url_name())) )
def create_IATI_xml(iatidata, dir, o): #iatidata contains the activities #o contains the organisation data output = '' node = Element('iati-activities') node.set("version", "1.03") current_datetime = datetime.now().replace(microsecond=0).isoformat() node.set("generated-datetime",current_datetime) node.append(Comment('generated by CSV2IATI 2.3')) character_encoding = o.get("data-encoding-override") or o["data-encoding"] for activity in iatidata: #for each activity, create one <iati-activity> a = Element("iati-activity") a.set("xml:lang", o["lang"]) a.set("default-currency", o["default-currency"]) a.set("last-updated-datetime", current_datetime) node.append(a) ro = Element("reporting-org") ro.set("ref", o["reporting-org"]["ref"]) ro.set("type", o["reporting-org"]["type"]) ro.text = o["reporting-org"]["text"] a.append(ro) if "add-to-activities" in o["contact-info"]: contact_info = Element("contact-info") person_name = Element("person-name") person_name.text = o["contact-info"]["person-name"] telephone = Element("telephone") telephone.text = o["contact-info"]["telephone"] email = Element("email") email.text = o["contact-info"]["email"] address = Element("mailing-address") address.text = o["contact-info"]["address"] contact_info.append(person_name) contact_info.append(telephone) contact_info.append(email) contact_info.append(address) a.append(contact_info) for field in activity: #e.g. activity['activity-date'] for key, val in field.items(): #e.g. activity['activity-date']['fields'] if type(val) == dict: append_recursive(key,val,a) else: if val != '': a.set(key, val) doc = ElementTree(node) XMLfile = str(time.time()) + '.xml' XMLfilename = dir + '/' + XMLfile XMLabsfilename = UPLOAD_FILES_BASE + dir + '/' + XMLfile indent(node) doc.write(XMLabsfilename, encoding="utf-8", xml_declaration=True) XMLfilename_html = request.url_root + XMLfilename return XMLfilename_html
def write(self, file): """ write(file) -> None Write XML to filename of file object in 'file' """ if DEBUG >= 0: print "in write(self, file)" et = ElementTree(self.root) et.write(file)
def write(self, file): """ write(file) -> None Write XML to filename of file object in 'file' """ logger.log(9, 'write(file=%r)', file) et = ElementTree(self.root) et.write(file)
def write(self, file): """ write(file) -> None Write XML to filename of file object in 'file' """ logger.log( 9, 'write(file=%r)', file) et = ElementTree(self.root) et.write(file)
def edx(self, out_dir): html_dir = os.path.join(out_dir, 'html') if not os.path.exists(html_dir): os.makedirs(html_dir) html = Element('html', {'filename':self.url_name(), 'display_name':"HTML"}); tree = ElementTree(html) tree.write(os.path.join(html_dir, "{0}.xml".format(self.url_name())) ) #Copy the corresponding html-file shutil.copyfile(os.path.join(self.parent.path, self.path), os.path.join(html_dir, "{0}.html".format(self.url_name())) );
def write(self, file, encoding="utf-8"): """ write to file """ # ElementTree.write does not write the XML header # if the encoding is utf-8 if not hasattr(file, "write"): file = open(file, "w") print >>file, '<?xml version="1.0" encoding="utf-8"?>' ElementTree.write(self, file, "utf-8")
def write(self, file, encoding="utf-8"): """ write to file """ # ElementTree.write does not write the XML header # if the encoding is utf-8 if not hasattr(file, "write"): file = open(file, "w") print >> file, '<?xml version="1.0" encoding="utf-8"?>' ElementTree.write(self, file, "utf-8")
def serialize(data, name="object"): """ Serializes dictionary to xml. """ content_elem = Element("contenttype") content_elem.attrib["name"] = name _serialize(content_elem, data) tree = ElementTree(content_elem) f = StringIO() tree.write(f, "UTF-8") return f.getvalue()
def serialize(data, name="object"): """ Serializes dictionary to xml. """ content_elem = Element("contenttype") content_elem.attrib["name"] = name _serialize(content_elem, data) tree = ElementTree(content_elem) f = StringIO() tree.write(f, "UTF-8") return f.getvalue()
def edx(self, out_dir): # Path of the source directory relative to our working directory path_complete = os.path.join(self.parent.path, self.path) # Create a archive with the source inside the static directory static_dir = os.path.join(out_dir, 'static') if not os.path.exists(static_dir): os.makedirs(static_dir) # In order to get an unique filename inside edx, we have to prefix the project and group name target_filename = self.url_name()+'.tar.gz' target_path = os.path.join(static_dir, target_filename) tar = tarfile.open(target_path, "w:gz") tar.add(path_complete, arcname=os.path.basename(path_complete)) tar.close() html_dir = os.path.join(out_dir, 'html') if not os.path.exists(html_dir): os.makedirs(html_dir) html = Element('html', {'filename':self.url_name(), 'display_name':"Source"}); tree = ElementTree(html) tree.write(os.path.join(html_dir, "{0}.xml".format(self.url_name())) ) html = '''<h3>Source of %(path)s</h3> ''' % {'path':escape(self.path) } html += ''' <a href="/static/%(file)s">Download source as archive</a> ''' % {'file':target_filename} for dirname, dirnames, filenames in os.walk(os.path.join(self.parent.path, self.path)): # Process each file for filename in filenames: #ignore any non-python file if not filename.endswith('.py') or filename.startswith('.'): continue path_full = os.path.join(dirname, filename) # This path is relative to the group definition path_relative = path_full[len(self.parent.path):] html += '<h3>%(path)s</h3>\n' % {'path':escape(path_relative)} # It would be better to control the font-size by the theme html += '<script src="https://google-code-prettify.googlecode.com/svn/loader/run_prettify.js?skin=tomorrow"></script>' html += '<pre class="prettyprint python">' with codecs.open(path_full, mode='r', encoding='utf-8') as f: html += escape(f.read()) html += '</pre>' with codecs.open(os.path.join(html_dir, "{0}.html".format(self.url_name())), mode='w', encoding='utf-8') as f: f.write(html)
def write(self, file, pretty_print=False): """ write(file, pretty_print=False) -> None Write XML to filename of file object in 'file'. If pretty_print is True, the XML will contain whitespace to make it human-readable. """ if pretty_print: indent(self.root) et = ElementTree(self.root) et.write(file, self.encoding, xml_declaration=True)
def write(self, file, pretty_print=False): """ write(file, pretty_print=False) -> None Write XML to filename of file object in 'file'. If pretty_print is True, the XML will contain whitespace to make it human-readable. """ if pretty_print: indent(self.root) et = ElementTree(self.root) et.write(file, self.encoding, xml_declaration=True)
def write_gexf(self, filename, ): """ writes the graph in gexf format to file with no modifications to the object """ root, graph = self.create_base_element() self.add_nodes_element(self.nodes) self.add_edges_element(self.edges) tree = ElementTree(root) print "saved to: " + os.getcwd() + '/' + filename tree.write(os.getcwd() + '/' + filename)
def _write_kodi_nfo(information, path): """Write the provided information to movie.nfo.""" click.echo("Writing movie.nfo...") root = Element("movie") SubElement(root, "title").text = information.get("title") SubElement(root, "originaltitle").text = information.get("title") SubElement(root, "sorttitle").text = information.get("title") SubElement(root, "set").text = information.get("set") SubElement(root, "year").text = information.get("release_date")[:4] SubElement(root, "plot").text = information.get("plot") SubElement(root, "studio").text = information.get("studio") tree = ElementTree(root) tree.write(os.path.join(path, "movie.nfo"), encoding="UTF-8")
def edx(self, out_dir): chapter_dir = os.path.join(out_dir, 'chapter') if not os.path.exists(chapter_dir): os.makedirs(chapter_dir) chapter = Element('chapter', {'display_name':escape(self.project)}); for group in self.groups: e = SubElement(chapter, 'sequential') e.set('url_name', group.url_name()) tree = ElementTree(chapter) tree.write(os.path.join(chapter_dir, "{0}.xml".format(self.url_name())) ) for group in self.groups: group.edx(out_dir)
def msgfdb2pepxml(in_file, out_file, modifications_file=None, mzxml=None, fasta=None): """ Convert tab-separated ms-gfdb output to pepXML * in_file -- output file of MS-GFDB * out_file -- pepXML file to write to * modifications_file -- modifications file of MS-GFDB (Mods.txt) * mzxml -- input mzXML file of MS-GFDB * fasta -- input fasta file of MS-GFDB with database of peptides """ if not out_file: out_filename = re.sub(r"\.msgfdb$", ".pep.xml", in_file.name) if out_filename == in_file.name: raise Exception("Provide output file or input file with extension .msgfdb") out_file = open(out_filename, 'w') modifications = default_modifications num_mods = None if modifications_file: num_mods, modifications = read_modifications(modifications_file) msms_pipeline_analysis = create_msms_pipeline_analysis() tree = ElementTree(msms_pipeline_analysis) msms_run_summary = SubElement(msms_pipeline_analysis, 'msms_run_summary') base_name = remove_file_extention(os.path.abspath(in_file.name)) msms_run_summary.set('base_name', base_name) SubElement(msms_run_summary,'sample_enzyme') search_summary = SubElement(msms_run_summary, 'search_summary') search_summary.set('search_engine', "X! Tandem (k-score)") if fasta: search_database = SubElement(search_summary, 'search_database') search_database.set('local_path', os.path.abspath(fasta.name)) apply_msgfdb(in_file, msms_run_summary, modifications, num_mods) set_hit_ranks(msms_run_summary) if mzxml: msrun = get_msrun(mzxml) ms_instrument = msrun.find('msInstrument') if ms_instrument is not None: for field in ['msManufacturer', 'msModel', 'msIonisation', 'msDetector']: param = ms_instrument.find(field) if param is not None: msms_run_summary.set(field, param.get('value')) set_retention_times(msrun, msms_run_summary) out = StringIO() tree.write(out, encoding='UTF-8', xml_declaration=True) out_file.write(out.getvalue().replace('>', '>\n')) in_file.close() out_file.close() if modifications_file: modifications_file.close() if mzxml: mzxml.close() if fasta: fasta.close()
def write(self, data, filename): """Writes data to the xml file. Data is a Person instance.""" person_el = Element('person', format='0.7') height_el = SubElement(person_el, 'height') height_el.text = str(data.height) weight_el = SubElement(person_el, 'weight') measurements_el = SubElement(weight_el, 'measurements') plan_el = SubElement(weight_el, 'plan') for dataset in data.measurements: _add_dataset_to_element(dataset, measurements_el) for dataset in data.plan: _add_dataset_to_element(dataset, plan_el) user_tree = ElementTree(person_el) user_tree.write(filename, encoding='UTF-8')
class etree(ebase): def __init__( self, fname ): ebase.__init__( self ) self.et = ElementTree( file=fname ) def write( self, fname ): self.et.write(fname) def ensure_child( self, tag ): retval = self.et.find("./"+tag) if not retval is None: return elem( retval ) else: return elem( SubElement( self.et.getroot(), tag ) )
def localiseFontsConf (self, pathFonts, sysFontFolder) : '''Sets the <dir> and <cachdir> to be the directory in which the fonts.conf file exists. This helps to provide better seperation of our fonts from the host system.''' fileName = pathFonts + '/fonts.conf' scrName = sysFontFolder + '/fonts.conf' # First lets check to see if the fonts.conf file exists if os.path.isfile(fileName) == False : shutil.copy(scrName, fileName) # Import this module for this process only (May need to move it # if other processes ever need it) from xml.etree.cElementTree import ElementTree et = ElementTree(file = fileName) path = os.path.dirname(os.path.abspath(fileName)) for p in ('dir', 'cachedir') : et.find(p).text = path # Write out the new font.conf file if et.write(fileName, encoding = 'utf-8') == None : self._log_manager.log('INFO', 'Fonts have been localised', 'true') return
def write_data(data, handle): """Write Cobertura XML for coverage data""" line_rates = get_line_rates(data) log("Generating output...") root = Element("coverage") root.set("line-rate", str(line_rates["_"])) root.set("branch-rate", "0") root.set("complexity", "0") root.set("branches-covered", "0") root.set("branches-valid", "0") root.set("timestamp", "0") root.set("lines-covered", str(line_rates["_hits"])) root.set("lines-valid", str(line_rates["_lines"])) root.set("version", "0") sources = SubElement(root, "sources") source = SubElement(sources, "source") source.text = "c:" packages = SubElement(root, "packages") package = SubElement(packages, "package") package.set("name", "gammu.exe") package.set("line-rate", str(line_rates["_"])) package.set("branch-rate", "0") package.set("complexity", "0") classes = SubElement(package, "classes") for item in data: obj = SubElement(classes, "class") obj.set("name", item.rsplit("\\", 1)[1]) obj.set("filename", item) obj.set("line-rate", str(line_rates[item])) obj.set("branch-rate", "0") obj.set("complexity", "0") SubElement(obj, "methods") lines = SubElement(obj, "lines") for line in sorted(data[item], key=lambda x: int(x)): obj = SubElement(lines, "line") obj.set("number", line) obj.set("hits", str(data[item][line])) tree = ElementTree(root) handle.write(HEADER) tree.write(handle, xml_declaration=False)
def write_data(data, handle): """Write Cobertura XML for coverage data""" line_rates = get_line_rates(data) log('Generating output...') root = Element('coverage') root.set('line-rate', str(line_rates['_'])) root.set('branch-rate', '0') root.set('complexity', '0') root.set('branches-covered', '0') root.set('branches-valid', '0') root.set('timestamp', '0') root.set('lines-covered', str(line_rates['_hits'])) root.set('lines-valid', str(line_rates['_lines'])) root.set('version', '0') sources = SubElement(root, 'sources') source = SubElement(sources, 'source') source.text = 'c:' packages = SubElement(root, 'packages') package = SubElement(packages, 'package') package.set('name', 'gammu.exe') package.set('line-rate', str(line_rates['_'])) package.set('branch-rate', '0') package.set('complexity', '0') classes = SubElement(package, 'classes') for item in data: obj = SubElement(classes, 'class') obj.set('name', item.rsplit('\\', 1)[1]) obj.set('filename', item) obj.set('line-rate', str(line_rates[item])) obj.set('branch-rate', '0') obj.set('complexity', '0') SubElement(obj, 'methods') lines = SubElement(obj, 'lines') for line in sorted(data[item], key=lambda x: int(x)): obj = SubElement(lines, 'line') obj.set('number', line) obj.set('hits', str(data[item][line])) tree = ElementTree(root) handle.write(HEADER) tree.write(handle, xml_declaration=False)
def serializeXML(results): root = Element(name(u'sparql')) header(results, root) result_list(results, root) out = StringIO() tree = ElementTree(root) # xml declaration must be written by hand # http://www.nabble.com/Writing-XML-files-with-ElementTree-t3433325.html out.write('<?xml version="1.0" encoding="utf-8"?>') out.write('<?xml-stylesheet type="text/xsl" ' + \ 'href="/static/sparql-xml-to-html.xsl"?>') tree.write(out, encoding='utf-8') return out.getvalue()
def render_sitemap(config): root = Element('urlset', {'xmlns': 'http://www.sitemaps.org/schemas/sitemap/0.9'}) for page in config['pages']: if not 'path' in page: continue url_element = SubElement(root, 'url') loc_element = SubElement(url_element, 'loc') loc_element.text = config['url_prefix'] + page['path'] tree = ElementTree(root) tree.write(path.join(PUBLIC_FOLDER, 'sitemap.xml'), xml_declaration=True, encoding='utf-8')
def strip(inf, strip_method): parser = iterparse(inf, events=("start", "end")) path = [] for event, elem in parser: if event == "start": path.append(elem) else: path.pop() try: strip_method(elem, path[-1]) except IndexError: # parent becomes None because this is the root elem strip_method(elem, None) et = ElementTree(parser.root) et.write(stdout, "UTF-8")
def write(self, data, filename): """Creates a sportstracker weights file at filename containing the data; data is an AllDatasets instance.""" id_ = 1 weightlist_el = Element('weight-list') for dataset in data: weight_el = SubElement(weightlist_el, 'weight') id_el = SubElement(weight_el, 'id') id_el.text = str(id_) date_el = SubElement(weight_el, 'date') date_el.text = str(dataset.date) + 'T12:00:00' value_el = SubElement(weight_el, 'value') value_el.text = str(dataset.weight) comment_el = SubElement(weight_el, 'comment') comment_el.text = dataset.note id_ += 1 st_tree = ElementTree(weightlist_el) st_tree.write(filename, encoding='UTF-8')