def setMentionXML(self): classMention = Element("classMention") classMention.set("id", self.getMentionID()) mentionClass = SubElement(classMention, "mentionClass") mentionClass.set("id", self.getMentionClass()) mentionClass.text = self.getText() self.MentionXML = classMention
def extend_filetree(root, path, perm, size, mtime, uid, gid, type): debug_log("extending " + str(root) + " with " + str(path)) if len(path) == 1: path = path[0] encoded_path = pathencode(path) file_element = next( (file for file in root.getchildren() if file.tag == encoded_path), None) if file_element is not None: debug_log("found " + path + "(" + encoded_path + ") in " + str(root)) return file_element = SubElement(root, encoded_path) debug_log("added " + path + "(" + encoded_path + ") to " + str(root)) file_element.set("perm", perm) file_element.set("size", -1) file_element.set("mtime", mtime) file_element.set("uid", uid) file_element.set("gid", gid) file_element.set("type", type) file_element.set("name", path) else: dir_path, path = path[0], path[1:] encoded_dir = pathencode(dir_path) dir_element = next( (file for file in root.getchildren() if file.tag == encoded_dir), None) if dir_element is not None: debug_log("adding " + dir_path + " to " + dir_element + "(" + encoded_dir + ") in " + str(root)) else: dir_element = SubElement(root, encoded_dir) debug_log("new " + dir_path + "(" + encoded_dir + ") in " + str(root)) extend_filetree(dir_element, path, perm, size, mtime, uid, gid, type)
def save_config(path, save_name, config_string): songbook_xml = parse(path) config_section = songbook_xml.find('configuration') # create the config section if we must if config_section is None: config_section = SubElement(songbook_xml.getroot(), 'configuration') # search for formatter config section named save_name formatter_node = None for node in config_section.findall('formatter'): if node.get('name').lower().strip() == save_name.lower().strip(): formatter_node = node break # we found our formatter node if formatter_node is None: # didn't find existing formatter_node formatter_node = SubElement(config_section, 'formatter') formatter_node.set('name', save_name.strip()) # now we have a formatter node formatter_node.text = config_string # now save back to file songbook_xml.write(path)
def write(self, root, header): node = SubElement(root, 'HeaderChunk') node.set("id", header.id) node.set("size", str(header.size)) node.set("format_type", str(header.format_type)) node.set("nbTracks", str(header.nbTracks)) node.set("timediv", str(header.timediv))
def xmlnode(root, name, text = '', **attrs): 'Simple way to attach an ElementTree node.' elem = SubElement(root, name) if root is not None else Element(name) if text: elem.text = text for k, v in attrs.iteritems(): elem.set(k, v) return elem
def xmlnode(root, name, text="", **attrs): "Simple way to attach an ElementTree node." elem = SubElement(root, name) if root is not None else Element(name) if text: elem.text = text for k, v in attrs.iteritems(): elem.set(k, v) return elem
def veto_xml(veto_root, freq, band, comment): vetoIdx = SubElement(veto_root, "veto_band") indent(vetoIdx, 2) vetoIdx.set('comment', 'notch (' + comment + ')') vetoIdx_band = SubElement(vetoIdx, "band") vetoIdx_band.text = str(band) indent(vetoIdx_band, 3) vetoIdx_band = SubElement(vetoIdx, "freq") vetoIdx_band.text = str(freq) indent(vetoIdx_band, 3) return vetoIdx
def save_project(project): root = Element("project") root.set("name", project.get_name()) files_node = SubElement(root, "files") for filename in project.get_files(): f_node = SubElement(files_node, "file") f_node.set("path", filename) with open(project.get_file(), "w") as f: f.write(utils.get_pretty_xml(root)) f.flush()
def asXml(self, parentnode, authed=False): from xml.etree.cElementTree import SubElement me = SubElement(parentnode, "channel") xmlpopulate(me, self.channel_obj) me.set("x_connecturl", self.connecturl) for sc in self.subchans: sc.asXml(me, authed) for pl in self.players: pl.asXml(me, authed)
def write(self, root, track): node = SubElement(root, 'TrackChunk') node.set("id", track.id) node.set("size", str(track.size)) i = 0 for data in track.data: type_event = TypeEventFactory.get(data.name) event_node = SubElement(node, '_'+str(i)) type_event.write(event_node, data) i += 1
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 create_svg_dom(pattern): 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) extends = pattern.bounds() width = extends[2] - extends[0] height = extends[3] - extends[1] root.set(ATTR_WIDTH, str(width)) root.set(ATTR_HEIGHT, str(height)) viewbox = \ str(extends[0]) + " " + \ str(extends[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") return ElementTree(root)
def create_xml_item(self, item): log.debug("{0} create xml item - {1}".format(self, item)) if item.id: log.debug( '{0} create xml item - {1} already exists, skipping'.format( self, item)) return item_id = item.get_id() if self.find_item_by_id(item_id): log.debug( '{0} create xml item - {1} already exists, skipping'.format( self, item)) return addon_id = item.addon_id xml_item = SubElement(self.xml_root_element.find('items'), 'item') xml_item.set('id', str(item_id)) xml_item.set('ctime', str(datetime.now())) if addon_id: xml_item.set('addon_id', str(addon_id)) name = SubElement(xml_item, 'name') name.text = item.name params = SubElement(xml_item, 'params') for key, value in item.params.iteritems(): params.set(str(key), str(value)) item.id = item_id return xml_item
def create_xml_item(self, item): log.logDebug("%s create xml item - %s"%(self,item)) log.debug("{0} create xml item - {1}".format(self, toString(item))) import time from datetime import datetime t = datetime.now() t1 = t.timetuple() uid = int(time.mktime(t1)) if item.id: item.id = uid #log.logDebug("%s create xml item - %s already exists, skipping"%(self,item)) #log.debug('{0} create xml item - {1} already exists, skipping'.format(self, toString(item))) #return item_id = item.get_id() if self.find_item_by_id(item_id): item_id = uid item.id = uid #log.logDebug("%s create xml item - %s already exists (2), skipping"%(self,item)) #log.debug('{0} create xml item - {1} already exists, skipping'.format(self, toString(item))) #return addon_id = item.addon_id xml_item = SubElement(self.xml_root_element.find('items'), 'item') xml_item.set('id', toUnicode(item_id)) xml_item.set('ctime', str(datetime.now())) if addon_id: xml_item.set('addon_id', toUnicode(addon_id)) name = SubElement(xml_item, 'name') name.text = toUnicode(item.name) params = SubElement(xml_item, 'params') for key, value in item.params.iteritems(): params.set(toUnicode(key), toUnicode(value)) item.id = item_id return xml_item
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 create_xml_item(self, item): log.logDebug("%s create xml item - %s" % (self, item)) log.debug("{0} create xml item - {1}".format(self, toString(item))) import time from datetime import datetime t = datetime.now() t1 = t.timetuple() uid = int(time.mktime(t1)) if item.id: item.id = uid #log.logDebug("%s create xml item - %s already exists, skipping"%(self,item)) #log.debug('{0} create xml item - {1} already exists, skipping'.format(self, toString(item))) #return item_id = item.get_id() if self.find_item_by_id(item_id): item_id = uid item.id = uid #log.logDebug("%s create xml item - %s already exists (2), skipping"%(self,item)) #log.debug('{0} create xml item - {1} already exists, skipping'.format(self, toString(item))) #return addon_id = item.addon_id xml_item = SubElement(self.xml_root_element.find('items'), 'item') xml_item.set('id', toUnicode(item_id)) xml_item.set('ctime', str(datetime.now())) if addon_id: xml_item.set('addon_id', toUnicode(addon_id)) name = SubElement(xml_item, 'name') name.text = toUnicode(item.name) params = SubElement(xml_item, 'params') for key, value in item.params.iteritems(): params.set(toUnicode(key), toUnicode(value)) item.id = item_id return xml_item
def save_edge(edge, parent_node_el): edge_el = SubElement(parent_node_el, "arc") edge_el.set("node-id", edge.get_target().get_id()) edge_el.set("label", edge.get_label()) edge_el.set("events-count", str(edge.get_events_count())) edge_el.set("time", str(edge.get_time())) edge_el.set("pids", str(edge.get_pids())) return edge_el
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_cats(root, cont, is_channel_cats): global cat_id for item in cont.keys(): if not item[0]: continue elem = SubElement(root, 'Category') if item[1]: elem.set('lang', item[1]) elem.set('id', str(cat_id)) cat_id += 1 if is_channel_cats: for ch in cont[item]: ch_elem = SubElement(elem, 'Channel') ch_elem.set('id', ch) elem.set('channel', 'true') elem.set('name', item[0])
async def send_xml(self, xml_dict): data_root = Element('msg') data_root.set('t', 'sys') sub_element_parent = data_root for sub_element, sub_element_attribute in xml_dict.items(): sub_element_object = SubElement(sub_element_parent, sub_element) if type(xml_dict[sub_element]) is dict: for sub_element_attribute_key, sub_element_attribute_value in xml_dict[sub_element].items(): sub_element_object.set(sub_element_attribute_key, sub_element_attribute_value) else: sub_element_object.text = xml_dict[sub_element] sub_element_parent = sub_element_object xml_data = tostring(data_root) await self.send_line(xml_data.decode('utf-8'))
def create_xml_from_config_dict(config_dict): root = Element("config") root.set("uuid", config_dict["uuid"]) name_node = SubElement(root, "name") name_node.text = config_dict["config_name"] game_node = SubElement(root, "game") game_node.set("uuid", config_dict["parent"]) game_name_node = SubElement(game_node, "name") game_name_node.text = config_dict["game_name"] game_platform_node = SubElement(game_node, "platform") game_platform_node.text = config_dict["platform"] options_node = SubElement(root, "options") for key, value in config_dict.items(): if key in ["game_name", "platform", "config_name", "uuid", "parent"]: continue if key.startswith("file_"): continue option_node = SubElement(options_node, key) option_node.text = value for i in range(100): name = config_dict.get("file_{0}_name".format(i), "") if not name: break sha1 = config_dict.get("file_{0}_sha1".format(i), "") # url = config_dict.get("file_{0}_url".format(i), "") url = sha1_to_url(sha1) file_node = SubElement(root, "file") file_name_node = SubElement(file_node, "name") file_name_node.text = name file_sha1_node = SubElement(file_node, "sha1") file_sha1_node.text = sha1 file_url_node = SubElement(file_node, "url") file_url_node.text = url name = "{0} ({1}, {2})".format(config_dict["game_name"], config_dict["platform"], config_dict["config_name"]) print(tostring(root)) return name, tostring(root)
def setXML(self): """Creates an element tree that can later be appended to a parent tree""" annotation_body = Element("annotation") mentionID = SubElement(annotation_body, 'mention') mentionID.set( 'id', self.getMentionID()) #perhaps this needs to follow eHOST's schema annotatorID = SubElement(annotation_body, "annotator") annotatorID.set('id', 'eHOST_2010') annotatorID.text = self.getAnnotatorID() start = self.getSpan()[0] end = self.getSpan()[1] #span = SubElement(annotation_body, "span",{"start":str(start),"end":str(end)}) #Why is this backwards? spannedText = SubElement(annotation_body, 'spannedText') spannedText.text = self.getText() creationDate = SubElement(annotation_body, "creationDate") creationDate.text = self.getCreationDate() self.XML = annotation_body
def create_new_epg(args, original_epg_filename, m3u_entries): output_str("creating new xml epg for {} m3u items".format( len(m3u_entries))) original_tree = parse(original_epg_filename) original_root = original_tree.getroot() new_root = Element("tv") new_root.set("source-info-name", "py-m3u-epg-editor") new_root.set("generator-info-name", "py-m3u-epg-editor") new_root.set("generator-info-url", "py-m3u-epg-editor") # create a channel element for every channel present in the m3u for channel in original_root.iter('channel'): channel_id = channel.get("id") if any(x.tvg_id == channel_id for x in m3u_entries): output_str("creating channel element for {}".format(channel_id)) new_channel = SubElement(new_root, "channel") new_channel.set("id", channel_id) for elem in channel: new_elem = SubElement(new_channel, elem.tag) new_elem.text = elem.text for attr_key in elem.keys(): attr_val = elem.get(attr_key) new_elem.set(attr_key, attr_val) # now copy all programme elements from the original epg for every channel present in the m3u no_epg_channels = [] for entry in m3u_entries: if entry.tvg_id is not None and entry.tvg_id != "" and entry.tvg_id != "None": output_str("creating programme elements for {}".format( entry.tvg_name)) channel_xpath = 'programme[@channel="' + entry.tvg_id + '"]' for elem in original_tree.iterfind(channel_xpath): if is_in_range(args, elem): programme = SubElement(new_root, elem.tag) for attr_key in elem.keys(): attr_val = elem.get(attr_key) programme.set(attr_key, attr_val) for sub_elem in elem: new_elem = SubElement(programme, sub_elem.tag) new_elem.text = sub_elem.text for attr_key in sub_elem.keys(): attr_val = sub_elem.get(attr_key) new_elem.set(attr_key, attr_val) else: no_epg_channels.append("'{}'".format(entry.tvg_name.lower())) indent(new_root) tree = ElementTree(new_root) save_no_epg_channels(args, no_epg_channels) return tree
def _saveDL(self, filename, shapes, imagePath, tag): try: for s in shapes: if not s['label'][0] in validWriteTypes: raise LabelFileError('Missing write type in line:\n'+s['label']) with open(filename, 'wt') as f: f.write('<?xml version="1.0" encoding="utf-8"?>' + '\n') # Dirty tweak: XML declaration by hand: f.write('<DocumentList>\n') sp = Element('SinglePage', {'FileName':imagePath}) for s in sorted(shapes, key=lambda x: getTop(x['points'])): value = s['label'] line=SubElement(sp, 'Line', {'Value':value}) for prop,val in polyFromPoints(s['points'], tag): line.set(prop,val) indent(sp) ElementTree(sp).write(f, encoding="utf-8") f.write('</DocumentList>\n') except Exception, e: raise LabelFileError(e)
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): sequential_dir = os.path.join(out_dir, 'sequential') if not os.path.exists(sequential_dir): os.makedirs(sequential_dir) sequential = Element('sequential', {'display_name':escape(self.group())}); e = SubElement(sequential, 'vertical') e.set('url_name', self.url_name()+'_vertical') tree = ElementTree(sequential) tree.write(os.path.join(sequential_dir, "{0}.xml".format(self.url_name())) ) vertical_dir = os.path.join(out_dir, 'vertical') if not os.path.exists(vertical_dir): os.makedirs(vertical_dir) vertical = Element('vertical', {'display_name':'MainUnit'}); for c in self.content: c.parent_tag(vertical) c.edx(out_dir) tree = ElementTree(vertical) tree.write(os.path.join(vertical_dir, "{0}.xml".format(self.url_name()+'_vertical')) )
def create_svg_dom(values, bounds): 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 = bounds[2] - bounds[0] height = bounds[3] - bounds[1] root.set(ATTR_WIDTH, str(width)) root.set(ATTR_HEIGHT, str(height)) viewbox = \ str(bounds[0]) + " " + \ str(bounds[1]) + " " + \ 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 values: if segments[4] & EgvParser.MODE_D: 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 return ElementTree(root)
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 saveDictToXml(self, file_name): """ save dict to an xml file :param file_name: :return: None """ dictEle = Element("dict") dictEle.set("description", self.info.description) dictEle.set("count", str(len(self.data))) dictEle.set("author", self.info.author) cur_time = str(datetime.datetime.now()) dictEle.set("create_date", cur_time) for key in self.data: cur_n = self.data[key] # 一个节点 nodeEle = SubElement(dictEle, "node") # 建立一个xmlEle crfgm.node2ele(cur_n, nodeEle) # 关联xmlEle prePathsEle = SubElement(nodeEle, "pre_paths") prePathsEle.set("count", str(len(cur_n.pre_paths))) for path_key in cur_n.pre_paths: prePathEle = SubElement(prePathsEle, "path") pre_path = cur_n.pre_paths[path_key] crfgm.path2ele(pre_path, prePathEle, is_post_path=False) postPathsEle = SubElement(nodeEle, "post_paths") postPathsEle.set("count", str(len(cur_n.post_paths))) for path_key in cur_n.post_paths: postPathEle = SubElement(postPathsEle, "path") post_path = cur_n.post_paths[path_key] crfgm.path2ele(post_path, postPathEle, is_post_path=True) tree = ElementTree(dictEle) try: tree.write(file_name, encoding="utf-8", xml_declaration="version = 1.0") print("dictionary successfully saved in: %s" % file_name) except Exception as e: print("error occurs when write to xml file", e)
def _WriteManifestForImages(self, outputPath, size, mode, packedImageDict): ACCEPTED_MODES = {"uv", "pixel"} assert(mode in ACCEPTED_MODES) # Create the root node. rootNode = Element("atlas") rootNode.set("width", str(size[0])) rootNode.set("height", str(size[1])) # Sort the image paths by their basename so we get constant ordering. sortedImagePathList = sorted(packedImageDict.keys(), key=lambda path: os.path.basename(path)) # Append all image nodes. for imagePath in sortedImagePathList: imageInfo = packedImageDict[imagePath][0] packedImageInfo = packedImageDict[imagePath][1] imageNode = SubElement(rootNode, "image") imageNode.set("name", os.path.basename(imagePath)) if mode == "uv": imageNode.set("u1", str(packedImageInfo.packedBoundingBox[0])) imageNode.set("v1", str(packedImageInfo.packedBoundingBox[1])) imageNode.set("u2", str(packedImageInfo.packedBoundingBox[0] + packedImageInfo.packedBoundingBox[2])) imageNode.set("v2", str(packedImageInfo.packedBoundingBox[1] + packedImageInfo.packedBoundingBox[3])) elif mode == "pixel": imageNode.set("x1", str(packedImageInfo.packPosition[0])) imageNode.set("y1", str(packedImageInfo.packPosition[1])) imageNode.set("x2", str(packedImageInfo.packPosition[0] + imageInfo.boundingBox[2])) imageNode.set("y2", str(packedImageInfo.packPosition[1] + imageInfo.boundingBox[3])) # Prettify the xml xmlData = tostring( rootNode ) dom = xml.dom.minidom.parseString( xmlData ) xmlText = dom.toprettyxml() # Write the xml to the output file. with open( outputPath, "wb" ) as file: file.write( xmlText )
def _save_settings(config_filename): root = Element("settings") root.set("name", WINDOW_TITLE) root.set("version", VERSION) for k, v in user_settings.iteritems(): set_elem = SubElement(root, "set") set_elem.set("name", str(k)) set_elem.set("value", str(v)) scripts_el = SubElement(root, "scripts") for s in user_scripts: script_el = SubElement(scripts_el, "script") script_el.set("name", ntpath.basename(s)) script_el.set("value", s) settings_data = utils.get_pretty_xml(root) with open(config_filename, "w") as f: f.write(settings_data) f.flush()
def serialize(cls, rv, recordSets, rootName="collection", **extra): """ Convert a list of record sets to XML text """ rootElement = Element("{%s}%s" % (eimURI, rootName), **extra) for uuid, recordSet in recordSets.iteritems(): if recordSet is not None: recordSetElement = SubElement(rootElement, "{%s}recordset" % eimURI, uuid=uuid) for record in list(recordSet.inclusions): recordElement = SubElement(recordSetElement, "{%s}record" % (record.URI)) for field in record.__fields__: value = record[field.offset] if value is eim.NoChange: continue else: if value is eim.Inherit: serialized, typeName = serializeValue(field.typeinfo, rv, None) SubElement(recordElement, "{%s}%s" % (record.URI, field.name), missing="true") else: serialized, typeName = serializeValue(field.typeinfo, rv, value) if serialized is None: SubElement(recordElement, "{%s}%s" % (record.URI, field.name)) else: recordElement.set(field.name, serialized) for record in list(recordSet.exclusions): recordElement = SubElement(recordSetElement, "{%s}record" % (record.URI)) recordElement.set(deletedURI, "true") for field in record.__fields__: if isinstance(field, eim.key): value = record[field.offset] serialized, typeName = serializeValue(field.typeinfo, rv, record[field.offset]) if serialized is None: SubElement(recordElement, "{%s}%s" % (record.URI, field.name)) else: recordElement.set(field.name, serialized) else: # item deletion indicated attrs = {deletedURI: "true"} recordSetElement = SubElement(rootElement, "{%s}recordset" % eimURI, uuid=uuid, **attrs) xmlString = tostring(rootElement) return "<?xml version='1.0' encoding='UTF-8'?>%s" % xmlString
def create_xml_item(self, item): log.debug("{0} create xml item - {1}".format(self, item)) if item.id: log.debug('{0} create xml item - {1} already exists, skipping'.format(self, item)) return item_id = item.get_id() if self.find_item_by_id(item_id): log.debug('{0} create xml item - {1} already exists, skipping'.format(self, item)) return addon_id = item.addon_id xml_item = SubElement(self.xml_root_element.find('items'), 'item') xml_item.set('id', str(item_id)) xml_item.set('ctime', str(datetime.now())) if addon_id: xml_item.set('addon_id', str(addon_id)) name = SubElement(xml_item, 'name') name.text = item.name params = SubElement(xml_item, 'params') for key, value in item.params.iteritems(): params.set(str(key), str(value)) item.id = item_id return xml_item
def asXml(self, parentnode, authed=False): from xml.etree.cElementTree import SubElement me = SubElement(parentnode, "user") xmlpopulate(me, self.player_obj) if authed: me.set("x_addrstring", self.ipaddress) else: me.set("address", "") if self.channel.server.hasUserTexture(self.userid): from views import showTexture from django.core.urlresolvers import reverse textureurl = reverse(showTexture, kwargs={ 'server': self.channel.server.id, 'userid': self.userid }) me.set('x_texture', "http://" + Site.objects.get_current().domain + textureurl) if self.mumbleuser and self.mumbleuser.gravatar: me.set('x_gravatar', self.mumbleuser.gravatar)
def create_new_epg(args, original_epg_filename, m3u_entries): output_str("creating new xml epg for {} m3u items".format( len(m3u_entries))) try: original_tree = parse(original_epg_filename) original_root = original_tree.getroot() new_root = Element("tv") new_root.set("source-info-name", "py-m3u-epg-editor") new_root.set("generator-info-name", "py-m3u-epg-editor") new_root.set("generator-info-url", "py-m3u-epg-editor") # create a channel element for every channel present in the m3u for channel in original_root.iter('channel'): channel_id = channel.get("id") if any(x.tvg_id == channel_id for x in m3u_entries): output_str( "creating channel element for {}".format(channel_id)) new_channel = SubElement(new_root, "channel") new_channel.set("id", channel_id) for elem in channel: new_elem = SubElement(new_channel, elem.tag) new_elem.text = elem.text for attr_key in elem.keys(): attr_val = elem.get(attr_key) if elem.tag.lower() == "icon" and args.http_for_images: attr_val = attr_val if attr_val.startswith( "http") else "" new_elem.set(attr_key, attr_val) # now copy all programme elements from the original epg for every channel present in the m3u no_epg_channels = [] for entry in m3u_entries: if entry.tvg_id is not None and entry.tvg_id != "" and entry.tvg_id != "None": output_str("creating programme elements for {}".format( entry.tvg_name)) channel_xpath = 'programme[@channel="' + entry.tvg_id + '"]' channel_programmes = original_tree.findall(channel_xpath) if len(channel_programmes) > 0: for elem in channel_programmes: if is_in_range(args, elem): programme = SubElement(new_root, elem.tag) for attr_key in elem.keys(): attr_val = elem.get(attr_key) programme.set(attr_key, attr_val) for sub_elem in elem: new_elem = SubElement(programme, sub_elem.tag) new_elem.text = sub_elem.text for attr_key in sub_elem.keys(): attr_val = sub_elem.get(attr_key) new_elem.set(attr_key, attr_val) else: no_epg_channels.append(entry) else: no_epg_channels.append(entry) indent(new_root) tree = ElementTree(new_root) if len(no_epg_channels) > 0: save_no_epg_channels(args, no_epg_channels) return tree except Exception as e: # likely a mangled xml parse exception output_str("epg creation failure: {0}".format(e)) return None
# Spit the dummy if IFOvetoed >= alpha_stat*numTemplates: if float( IFOvetoed ) >= alpha_stat*float( numTemplates ): # reset loudestLine and max2F, open and add searchBand # limits to veto_bands.xml print("Warning! IFO-vetoed entire job : " + str( jobNum ) ) IFOvetoed = 0 max2F = 0.0 loudestLine = '' # Append to ifo_v_jobs.txt counter ifo_v_jobs += str(jobNum) + "\n" veto_segments.append( searchBand[0:2]) for jobIdx in search_root.iter('search_band'): if int( jobIdx.find('job').text ) == jobNum: vetoIdx = SubElement( veto_root, "veto_band") indent(vetoIdx, 2) vetoIdx.set('comment', 'IFO_veto') vetoIdx_band = SubElement( vetoIdx, "band") vetoIdx_band.text = jobIdx.find('band').text indent(vetoIdx_band, 3) vetoIdx_band = SubElement( vetoIdx, "freq") vetoIdx_band.text = jobIdx.find('freq').text indent(vetoIdx_band, 3) SubElement( currentSearchJobEl,"num_templates").text = numTemplates indent(loudestEl, 1) SubElement( currentSearchJobEl,"num_templates_vetoed").text = numTemplates indent(loudestEl, 1) if loudestLine: for Idx in range( len( columnIdx )): loudIdx = SubElement( loudestEl, columnIdx[Idx]) loudIdx.text = loudestLine[Idx] indent(loudIdx, 2) # loudest template values are level 2
def save(device, f, version='default'): root = Element(SVG_NAME_TAG) root.set(SVG_ATTR_VERSION, SVG_VALUE_VERSION) root.set(SVG_ATTR_XMLNS, SVG_VALUE_XMLNS) root.set(SVG_ATTR_XMLNS_LINK, SVG_VALUE_XLINK) root.set(SVG_ATTR_XMLNS_EV, SVG_VALUE_XMLNS_EV) root.set("xmlns:meerK40t", "https://github.com/meerk40t/meerk40t/wiki/Namespace") # Native unit is mils, these must convert to mm and to px mils_per_mm = 39.3701 mils_per_px = 1000.0 / 96.0 px_per_mils = 96.0 / 1000.0 device.setting(int, "bed_width", 310) device.setting(int, "bed_height", 210) mm_width = device.bed_width mm_height = device.bed_height root.set(SVG_ATTR_WIDTH, '%fmm' % mm_width) root.set(SVG_ATTR_HEIGHT, '%fmm' % mm_height) px_width = mm_width * mils_per_mm * px_per_mils px_height = mm_height * mils_per_mm * px_per_mils viewbox = '%d %d %d %d' % (0, 0, round(px_width), round(px_height)) scale = 'scale(%f)' % px_per_mils root.set(SVG_ATTR_VIEWBOX, viewbox) elements = device.elements for operation in elements.ops(): subelement = SubElement(root, "operation") c = getattr(operation, 'color') if c is not None: subelement.set('color', str(c)) for key in dir(operation): if key.startswith('_'): continue value = getattr(operation, key) if type(value) not in (int, float, str, bool): continue subelement.set(key, str(value)) if elements.note is not None: subelement = SubElement(root, "note") subelement.set(SVG_TAG_TEXT, elements.note) for element in elements.elems(): if isinstance(element, Path): element = abs(element) subelement = SubElement(root, SVG_TAG_PATH) subelement.set(SVG_ATTR_DATA, element.d(transformed=False)) subelement.set(SVG_ATTR_TRANSFORM, scale) if element.values is not None: for key, val in element.values.items(): if key in ('speed', 'overscan', 'power', 'passes', 'raster_direction', 'raster_step', 'd_ratio'): subelement.set(key, str(val)) elif isinstance(element, SVGText): subelement = SubElement(root, SVG_TAG_TEXT) subelement.text = element.text t = Matrix(element.transform) t *= scale subelement.set( 'transform', 'matrix(%f, %f, %f, %f, %f, %f)' % (t.a, t.b, t.c, t.d, t.e, t.f)) if element.values is not None: for key, val in element.values.items(): if key in ('speed', 'overscan', 'power', 'passes', 'raster_direction', 'raster_step', 'd_ratio', 'font-family', 'font_face', 'font-size', 'font-weight', 'anchor', 'x', 'y'): subelement.set(key, str(val)) else: # Image. subelement = SubElement(root, SVG_TAG_IMAGE) stream = BytesIO() element.image.save(stream, format='PNG') png = b64encode(stream.getvalue()).decode('utf8') subelement.set('xlink:href', "data:image/png;base64,%s" % (png)) subelement.set(SVG_ATTR_X, '0') subelement.set(SVG_ATTR_Y, '0') subelement.set(SVG_ATTR_WIDTH, str(element.image.width)) subelement.set(SVG_ATTR_HEIGHT, str(element.image.height)) subelement.set(SVG_ATTR_TRANSFORM, scale) t = Matrix(element.transform) t *= scale subelement.set( 'transform', 'matrix(%f, %f, %f, %f, %f, %f)' % (t.a, t.b, t.c, t.d, t.e, t.f)) if element.values is not None: for key, val in element.values.items(): if key in ('speed', 'overscan', 'power', 'passes', 'raster_direction', 'raster_step', 'd_ratio'): subelement.set(key, str(val)) stroke = element.stroke if stroke is not None: stroke_opacity = stroke.opacity stroke = str( abs(stroke) ) if stroke is not None and stroke.value is not None else SVG_VALUE_NONE subelement.set(SVG_ATTR_STROKE, stroke) if stroke_opacity != 1.0 and stroke_opacity is not None: subelement.set(SVG_ATTR_STROKE_OPACITY, str(stroke_opacity)) try: stroke_width = str( element.stroke_width ) if element.stroke_width is not None else SVG_VALUE_NONE subelement.set(SVG_ATTR_STROKE_WIDTH, stroke_width) except AttributeError: pass fill = element.fill if fill is not None: fill_opacity = fill.opacity fill = str( abs(fill) ) if fill is not None and fill.value is not None else SVG_VALUE_NONE subelement.set(SVG_ATTR_FILL, fill) if fill_opacity != 1.0 and fill_opacity is not None: subelement.set(SVG_ATTR_FILL_OPACITY, str(fill_opacity)) if element.id is not None: subelement.set(SVG_ATTR_ID, str(element.id)) tree = ElementTree(root) tree.write(f)
def create_new_epg(args, original_epg_filename, m3u_entries): output_str("creating new xml epg for {} m3u items".format(len(m3u_entries))) try: original_tree = parse(original_epg_filename) original_root = original_tree.getroot() new_root = Element("tv") new_root.set("source-info-name", "py-m3u-epg-editor") new_root.set("generator-info-name", "py-m3u-epg-editor") new_root.set("generator-info-url", "py-m3u-epg-editor") # create a channel element for every channel present in the m3u epg_channel_count = 0 for channel in original_root.iter('channel'): channel_id = channel.get("id") if channel_id is not None and channel_id != "" and any(x.tvg_id.lower() == channel_id.lower() for x in m3u_entries): output_str("creating channel element for {}".format(channel_id)) epg_channel_count += 1 new_channel = SubElement(new_root, "channel") new_channel.set("id", channel_id.lower() if not args.preserve_case else channel_id) for elem in channel: new_elem = SubElement(new_channel, elem.tag) new_elem.text = elem.text for attr_key in elem.keys(): attr_val = elem.get(attr_key) if elem.tag.lower() == "icon" and args.http_for_images: attr_val = attr_val if attr_val.startswith("http") else "" new_elem.set(attr_key, attr_val) if args.no_tvg_id and args.force_epg: # create a channel element for every channel present in the m3u where there is no tvg_id and where there is a tvg_name value for entry in m3u_entries: if entry.tvg_id is None or entry.tvg_id == "" or entry.tvg_id == "None": output_str("creating channel element for m3u entry from tvg-name value {}".format(entry.tvg_name)) epg_channel_count += 1 new_channel = SubElement(new_root, "channel") new_channel.set("id", entry.tvg_name) new_elem = SubElement(new_channel, "display-name") new_elem.text = entry.tvg_name if epg_channel_count > 0: # perform any specified channel element sorting if args.xml_sort_type == 'alpha': channels = new_root.findall("channel[@id]") alpha_sorted_channels = sorted(channels, key=lambda ch_elem: (ch_elem.tag, ch_elem.get('id'))) new_root[:] = alpha_sorted_channels elif args.xml_sort_type == 'm3u': channels = new_root.findall("channel[@id]") m3u_sorted_channels = sorted(channels, key=lambda ch_elem: (ch_elem.tag, [x.tvg_id.lower() for x in m3u_entries].index(ch_elem.get('id').lower()))) new_root[:] = m3u_sorted_channels all_epg_programmes_xpath = 'programme' all_epg_programmes = original_tree.findall(all_epg_programmes_xpath) if len(all_epg_programmes) > 0 and not args.preserve_case: # force the channel (tvg-id) attribute value to lowercase to enable a case-insensitive # xpath lookup with: channel_xpath = 'programme[@channel="' + entry.tvg_id.lower() + '"]' for programme in all_epg_programmes: for attr_key in programme.keys(): attr_val = programme.get(attr_key) if attr_key.lower() == 'channel' and attr_val is not None: programme.set(attr_key, attr_val.lower()) # now copy all programme elements from the original epg for every channel present in the m3u no_epg_channels = [] for entry in m3u_entries: if entry.tvg_id is not None and entry.tvg_id != "" and entry.tvg_id != "None": output_str("creating programme elements for {}".format(entry.tvg_name)) # case-insensitive xpath search channel_xpath = entry.tvg_id.lower() if not args.preserve_case else entry.tvg_id channel_xpath = 'programme[@channel="' + channel_xpath + '"]' channel_programmes = original_tree.findall(channel_xpath) if len(channel_programmes) > 0: for elem in channel_programmes: programme_start_timestamp = dateutil.parser.parse(elem.get("start")) if is_in_range(args, programme_start_timestamp): programme = SubElement(new_root, elem.tag) for attr_key in elem.keys(): attr_val = elem.get(attr_key) programme.set(attr_key, attr_val) for sub_elem in elem: new_elem = SubElement(programme, sub_elem.tag) new_elem.text = sub_elem.text for attr_key in sub_elem.keys(): attr_val = sub_elem.get(attr_key) new_elem.set(attr_key, attr_val) for sub_sub_elem in sub_elem: new_sub_elem = SubElement(new_elem, sub_sub_elem.tag) new_sub_elem.text = sub_sub_elem.text for attr_key in sub_sub_elem.keys(): attr_val = sub_sub_elem.get(attr_key) new_sub_elem.set(attr_key, attr_val) else: if not args.no_tvg_id or not args.force_epg: no_epg_channels.append(entry) else: if not args.no_tvg_id or not args.force_epg: no_epg_channels.append(entry) if args.no_tvg_id and args.force_epg: # create programme elements for every channel present in the m3u where there is no tvg_id and where there is a tvg_name value for entry in m3u_entries: if entry.tvg_id is None or entry.tvg_id == "" or entry.tvg_id == "None": output_str("creating pseudo programme elements for m3u entry {}".format(entry.tvg_name)) programme_start_timestamp = datetime.datetime.now(tzlocal.get_localzone()) for i in range(1, 84): # create programme elements within a max 7 day window and no more limited by the configured range if is_in_range(args, programme_start_timestamp): programme_end_timestamp = programme_start_timestamp + datetime.timedelta(hours=2) programme = SubElement(new_root, "programme") programme.set("start", programme_start_timestamp.strftime("%Y%m%d%H0000 %z")) programme.set("stop", programme_end_timestamp.strftime("%Y%m%d%H0000 %z")) programme.set("channel", entry.tvg_name) title_elem = SubElement(programme, "title") title_elem.text = entry.tvg_name desc_elem = SubElement(programme, "desc") desc_elem.text = entry.tvg_name programme_start_timestamp = programme_start_timestamp + datetime.timedelta(hours=i*2) indent(new_root) tree = ElementTree(new_root) if len(no_epg_channels) > 0: save_no_epg_channels(args, no_epg_channels) return tree except Exception as e: # likely a mangled xml parse exception output_str("epg creation failure: {0}".format(e)) return None
def getranges(rangelist, root): for r in rangelist: rangeelem = SubElement(root, 'range') rangeelem.set('from', str(r[0])) rangeelem.set('to', str(r[1]))
def _set_node_props(element, name, value): """Set the properties of the node based on the type of object""" # if it is a ctypes structure if isinstance(value, ctypes.Structure): # create an element for the structure struct_elem = SubElement(element, name) #clsModule = value.__class__.__module__ cls_name = value.__class__.__name__ struct_elem.set("__type__", "{0}".format(cls_name)) # iterate over the fields in the structure for prop_name in value._fields_: prop_name = prop_name[0] item_val = getattr(value, prop_name) if isinstance(item_val, six.integer_types): prop_name += "_LONG" item_val = six.text_type(item_val) struct_elem.set(prop_name, _escape_specials(item_val)) elif hasattr(value, 'tobytes') and hasattr(value, 'size'): try: # if the image is too big then don't try to # write it out - it would probably product a MemoryError # anyway if value.size[0] * value.size[1] > (5000 * 5000): raise MemoryError #print('type(value) = ' + str(type(value))) image_data = base64.encodestring(bz2.compress( value.tobytes())).decode('utf-8') _set_node_props( element, name + "_IMG", { "mode": value.mode, "size_x": value.size[0], "size_y": value.size[1], "data": image_data }) # a system error is raised from time to time when we try to grab # the image of a control that has 0 height or width except (SystemError, MemoryError): pass elif isinstance(value, (list, tuple)): # add the element to hold the values # we do this to be able to support empty lists listelem = SubElement(element, name + "_LIST") for i, attrval in enumerate(value): _set_node_props(listelem, "%s_%05d" % (name, i), attrval) elif isinstance(value, dict): dict_elem = SubElement(element, name) for item_name, val in value.items(): _set_node_props(dict_elem, item_name, val) else: if isinstance(value, bool): value = six.integer_types[-1](value) if isinstance(value, six.integer_types): name += "_LONG" element.set(name, _escape_specials(value))
def installnbattr(xml, src, dst): src = findall("machine\(([^\)]+)", src)[0] if not src: return props = sub('\s*\n\s*|"', "", src).split(",") e = None for p in props: if ":" not in p: if xml.find('fileobject[@name="%s"]' % dst) is not None: return e = SubElement(xml, "fileobject") e.set("name", dst) else: n, v = p.split(":") s = SubElement(e, "attr") s.set("name", n) s.set("stringvalue", v) s = SubElement(e, "attr") s.set("name", "readonly") s.set("boolvalue", "false") s = SubElement(e, "attr") s.set("name", "enabled") s.set("boolvalue", "true")
def addtotree(root, path, perm, size, mtime, uid, gid, type): if len(path) == 1: c = path[0] ec = pathencode(c) e = None for f in root.getchildren(): if f.tag == ec: e = f break if e is None: e = SubElement(root, ec) log.Log("add " + c + "(" + ec + ") to " + str(root), 5) else: log.Log("found " + c + "(" + ec + ") in " + str(root), 7) e.set("perm", perm) e.set("size", -1) e.set("mtime", mtime) e.set("uid", uid) e.set("gid", gid) e.set("type", type) e.set("name", c) else: c = path[0] ec = pathencode(c) for f in root.getchildren(): if f.tag == ec: log.Log( "adding " + path[1] + " to " + c + "(" + ec + ") in " + str(root), 7) addtotree(f, path[1:], perm, size, mtime, uid, gid, type) return f = SubElement(root, ec) log.Log("new " + c + "(" + ec + ") in " + str(root), 5) addtotree(f, path[1:], perm, size, mtime, uid, gid, type)
#!/usr/bin/env python import sys from random import random, randrange from xml.etree.cElementTree import Element, SubElement, tostring from xml.dom.minidom import parseString NR_OF_VECTORS = 5000 VECTOR_SIZE = 25 # number of key's per Vector, should not exceed # WH_DEFAULT_HASHTABLE_SIZE for now (see whash.h) VECTOR_KEY_MIN = 1 VECTOR_KEY_MAX = 100000 FILENAME = "vectors.xml" print >> sys.stderr, "Creating %d vectors with %d dimensions each" %\ (NR_OF_VECTORS, VECTOR_SIZE) doc = Element("vectorlist") for i in range(NR_OF_VECTORS): vector_element = SubElement(doc, "vector") for j in range(VECTOR_SIZE): elem_element = SubElement(vector_element, "elem") key = randrange(VECTOR_KEY_MIN, VECTOR_KEY_MAX) value = random() elem_element.set("key", str(key)) elem_element.set("value", str(value)) out = open(FILENAME, "w") out.writelines(parseString(tostring(doc)).toprettyxml())
class Document(object): """building output waebric xhtml document """ def __init__(self, output, verbose=False): self.lastElement = Element('html') self.tree = ET.ElementTree(self.lastElement) self.trees = [self.tree] self.output = output self.verbose = verbose #@trace def addElement(self, name): self.lastElement = SubElement(self.lastElement, name) return self.lastElement #@trace def addText(self, string): if not len(self.lastElement): e = self.lastElement txt = "%s%s" % (e.text, string) if e.text else string e.text = txt else: e = self.lastElement[-1] txt = "%s%s" % (e.tail, string) if e.tail else string e.tail = txt def addAttribute(self,name,value): if self.lastElement.get(name): value = "%s %s" % (self.lastElement.get(name),value) self.lastElement.set(name, value) def addComment(self, string): self.lastElement.append(ET.Comment(string)) def getFile(self, filename): if self.output: filename = "%s/%s" % (self.output, filename) try: _file = open(filename,'w') except IOError: print "file name %s cannot be opened, no output written" % filename return return _file def setGoodRootElement(self): """make sure we have the correct root ellement according to the wae standard. It could be needed to remove the top html element because i ad it to work correctly with elementtree Libary. """ r = self.tree.getroot() if len(r) == 1 and not r.text and not r[-1].tail: child = r.getchildren()[0] if isinstance(child.tag, str):#check needed for comment element. self.tree._setroot(r[0]) def writeOutput(self, filename): self.setGoodRootElement() _file = self.getFile(filename) DTD = """<?xml version="1.0" encoding="UTF-8"?>\n""" _file.write(DTD) for tree in self.trees: if isinstance(tree, ET.ElementTree): self.tree.write(_file) else: #could be data string. _file.write(tree) _file.write('\n') _file.close() if self.verbose: output = open(_file.name) print output.read() def writeEmptyFile(self, filename): _file = self.getFile(filename) _file.write('') _file.close()
def apply_msgfdb(in_file, msms_run_summary, modifications, num_mods): """ Read output file of MS-GFDB and add child elements to msms_run_summary """ spectrum2element = {} enzyme_list = [ArgC,LysC,Trypsin,LysCP,Chymotrypsin,TrypChymo,TrypsinP,PepsinA, CNBr,V8E,AspN,Formic_acid,AspNambic,V8DE] semi_list = [] sample_enzyme = msms_run_summary.find('sample_enzyme') for f in read_msgfdb(in_file): spectrum = '%(name)s.%(scan)05i.%(scan)05i.%(charge)i' % \ {'name': remove_file_extention(f['#SpecFile']), 'scan': f['Scan#'], 'charge': f['Charge']} enzyme_list, semi_list = what_enzyme(enzyme_list, semi_list, f['Peptide']) peptide_prev_aa = f['Peptide'][0] if peptide_prev_aa == '_': peptide_prev_aa = '-' peptide_middle = f['Peptide'][2:-2] peptide_next_aa = f['Peptide'][-1] if peptide_next_aa == '_': peptide_next_aa = '-' if ' ' in f['Protein']: protein_name, protein_descr = f['Protein'].split(' ', 1) else: protein_name = f['Protein'] protein_descr = '' precursor_neutral_mass = f['Precursor'] * f['Charge'] - f['Charge'] * H_plus if spectrum not in spectrum2element: spectrum_query = SubElement(msms_run_summary, 'spectrum_query') spectrum2element[spectrum] = spectrum_query spectrum_query.append(Element('search_result')) spectrum_query.set('spectrum', spectrum) spectrum_query.set('start_scan', str(f['Scan#'])) spectrum_query.set('end_scan', str(f['Scan#'])) spectrum_query.set('assumed_charge', str(f['Charge'])) spectrum_query.set('precursor_neutral_mass', str(precursor_neutral_mass)) spectrum_query = spectrum2element[spectrum] search_result = spectrum_query.find('search_result') search_hit = SubElement(search_result, 'search_hit') search_hit.set('peptide', "".join(aa for aa in peptide_middle if aa.isalpha())) search_hit.set('peptide_prev_aa', peptide_prev_aa) search_hit.set('peptide_next_aa', peptide_next_aa) search_hit.set('protein', protein_name) search_hit.set('protein_descr', protein_descr) modification_instances = sum((find_modifications(mod, peptide_middle) for mod in modifications), []) calc_neutral_pep_mass = modified_peptide_mass(modification_instances, peptide_middle, num_mods) if modification_instances: modification_info = SubElement(search_hit, 'modification_info') for mass, mass_diff, aa_number, is_opt in modification_instances: maam = SubElement(modification_info, 'mod_aminoacid_mass') maam.set('position', str(aa_number)) maam.set('mass', str(mass)) search_hit.set('calc_neutral_pep_mass', str(calc_neutral_pep_mass)) search_hit.set('massdiff', str(precursor_neutral_mass - calc_neutral_pep_mass)) for field in score_fields: if field in f: SubElement(search_hit, 'search_score', name=field, value=f[field]) # sample_enzyme.set('fidelity',flag) if enzyme_list == []: if semi_list == []: sample_enzyme.set('name','NoEnzyme') sample_enzyme.set('fidelity','nonspecific') else: sample_enzyme.set('fidelity','semispecific') enzyme = re.split("\|",enzyme2name[re.search(r'<(\w+)>',semi_list[0][0]).group(1)]) else: sample_enzyme.set('fidelity','specific') enzyme = re.split("\|",enzyme2name[re.search(r'<(\w+)>',enzyme_list[0][0]).group(1)]) if not(enzyme_list == [] and semi_list == []): sample_enzyme.set('name',enzyme[0]) specificity = SubElement(sample_enzyme, 'specificity') specificity.set('cut',enzyme[1]) if enzyme[2]: specificity.set('no_cut',enzyme[2]) specificity.set('sense',enzyme[3])
def installnbattr(xml, src, dst): src = findall('machine\(([^\)]+)', src)[0] if not src: return props = sub('\s*\n\s*|"', '', src).split(',') e = None for p in props: if ':' not in p: if xml.find('fileobject[@name="%s"]' % dst) is not None: return e = SubElement(xml, 'fileobject') e.set('name', dst) else: n, v = p.split(':') s = SubElement(e, 'attr') s.set('name', n) s.set('stringvalue', v) s = SubElement(e, 'attr') s.set('name', 'readonly') s.set('boolvalue', 'false') s = SubElement(e, 'attr') s.set('name', 'enabled') s.set('boolvalue', 'true')
def saveDictToXml(dict, filepath): dictEle = Element("dict") dictEle.set("description", "Chinese Character Dictionary") dictEle.set("count", str(len(dict))) dictEle.set("author", "yeqiang") cur_time = str(datetime.datetime.now()) dictEle.set("createtime", cur_time) for key in dict: cur_n = dict[key] print(cur_n) nodeEle = SubElement(dictEle, "node") nodeEle.set("key", cur_n.content) #nodeEle.set("content", cur_n.content) prePathsEle = SubElement(nodeEle, "pre_paths") prePathsEle.set("count", str(len(cur_n.prepaths))) for pathkey in cur_n.prepaths: prePathEle = SubElement(prePathsEle, "path") path = cur_n.prepaths[pathkey] #prePathEle.set("key",str(path.pid)) prePathEle.set("touched", str(path.touchtimes)) prePathEle.set("node", path.pre_n.content) #prePathEle.text = path.pid postPathsEle = SubElement(nodeEle, "post_paths") postPathsEle.set("count", str(len(cur_n.postpaths))) for pathkey in cur_n.postpaths: postPathEle = SubElement(postPathsEle, "path") path = cur_n.postpaths[pathkey] #postPathEle.set("key", str(path.pid)) postPathEle.set("touched", str(path.touchtimes)) postPathEle.set("node", path.post_n.content) tree = ElementTree(dictEle) tree.write(filepath, encoding="utf-8", xml_declaration="version = 1.0")
def walk_dict(dictionary, base=None): ''' Walk the tree, handle various cases. Should be broken up. ''' from xml.etree.cElementTree import Element, SubElement # defer log.debug('base: %s', base) for key, val in dictionary.iteritems(): log.debug('%s %s', key, val) tag, attrs = parse_key(key) if tag == 'meta': newattrs = [] for name, content in attrs: if name not in META_STD_ATTRS: newattrs.append(('name', name)) newattrs.append(('content', content)) attrs = newattrs elif tag == 'style': log.debug('val %s, type %s', val, type(val)) val = convert_to_css(val, indent=4) # indent? if (base is None): log.debug('creating root %s', tag) if tag == 'html': base = Element(tag) [ base.set(*attr) for attr in attrs ] walk_dict(val, base=base) else: base = Element('html') log.debug('creating child %s', tag) child = SubElement(base, tag) [ child.set(*attr) for attr in attrs ] walk_dict(val, base=child) continue if isinstance(val, dict): log.debug('creating child %s', tag) child = SubElement(base, tag) [ child.set(*attr) for attr in attrs ] walk_dict(val, base=child) elif isinstance(val, list): log.debug('creating children %s', tag) child = SubElement(base, tag) [ child.set(*attr) for attr in attrs ] for item in val: log.debug('list item: %s', item) if isinstance(item, basestring): if list(child): # has children, put next string behind gchild = list(child)[-1] log.debug('looking at gchild, %s', gchild.tag) gchild.tail = (gchild.tail or '') + item + '\n' else: child.text = (child.text or '') + item + '\n' elif isinstance(item, dict): walk_dict(item, base=child) else: log.debug('creating child %s', tag) child = SubElement(base, tag) [ child.set(*attr) for attr in attrs ] child.text = val if val else '' return base
def serialize(cls, rv, recordSets, rootName="collection", **extra): """ Convert a list of record sets to XML text """ rootElement = Element("{%s}%s" % (eimURI, rootName), **extra) for uuid, recordSet in recordSets.iteritems(): if recordSet is not None: recordSetElement = SubElement(rootElement, "{%s}recordset" % eimURI, uuid=uuid) for record in list(recordSet.inclusions): recordElement = SubElement(recordSetElement, "{%s}record" % (record.URI)) for field in record.__fields__: value = record[field.offset] if value is eim.NoChange: continue else: if value is eim.Inherit: serialized, typeName = serializeValue( field.typeinfo, rv, None) SubElement(recordElement, "{%s}%s" % (record.URI, field.name), missing="true") else: serialized, typeName = serializeValue( field.typeinfo, rv, value) if serialized is None: SubElement( recordElement, "{%s}%s" % (record.URI, field.name)) else: recordElement.set(field.name, serialized) for record in list(recordSet.exclusions): recordElement = SubElement(recordSetElement, "{%s}record" % (record.URI)) recordElement.set(deletedURI, "true") for field in record.__fields__: if isinstance(field, eim.key): value = record[field.offset] serialized, typeName = serializeValue( field.typeinfo, rv, record[field.offset]) if serialized is None: SubElement(recordElement, "{%s}%s" % (record.URI, field.name)) else: recordElement.set(field.name, serialized) else: # item deletion indicated attrs = {deletedURI: "true"} recordSetElement = SubElement(rootElement, "{%s}recordset" % eimURI, uuid=uuid, **attrs) xmlString = tostring(rootElement) return "<?xml version='1.0' encoding='UTF-8'?>%s" % xmlString
# coding=utf-8 import datetime import pyamf from pyamf import remoting from pyamf.remoting import Envelope from pyamf.remoting import Response from pyamf.remoting import HeaderCollection from xml.etree.cElementTree import Element, SubElement from array import * # create an xml document xmlDocument = Element('root') parent = SubElement(xmlDocument, 'parent') child1 = SubElement(parent, 'child') child1.text = "foo" child1.set('id', 'c1') child2 = SubElement(parent, 'child') child2.text = "bar" child2.set('id', 'c2') # create a typed object class Foo: class __amf__: dynamic = False static = ('bar',) pyamf.register_class(Foo, 'Foo'); foo = Foo() foo.bar = 'baz'
def createRulesFromMap(mapobj): """Create an elementtree of groups and layers from a map object""" reverseLayerTypemap = dict([(v, k) for k, v in layerTypeMap.items()]) root = Element('osmmap') tree = ElementTree(root) groupselem = SubElement(root, 'groups') for group in mapobj.groups: groupelem = SubElement(groupselem, 'group') groupelem.set('name', group.name) layerselem = SubElement(groupelem, 'layers') for layer in group.layers: layer.open('r') layerelem = SubElement(layerselem, 'layer') layerelem.set('name', layer.name) layerelem.set('filename', layer.filename) layerelem.set('type', reverseLayerTypemap[layer.layertype]) style = mapobj.getLayerStyle(layer) layerelem.set('style', style.style) layerelem.set('color', style.color) layerelem.set('draworder', str(layer.draworder)) get_visibility(style, layerelem) return tree
def main(): # The following block parses the arguments supplied. parser = OptionParser(usage=usage) parser.add_option("-u", "--course-url", default=None, dest="course_url", help="Specifies the public URL of the course. It is used for the inline Pdf viewer using Google-Docs. [default: %default]") parser.add_option("-v", "--verbose", action="count", dest="verbose", default=False, help="Increase verbosity (specify multiple times for more)") parser.add_option("-o", "--output", default="to_import.tar.gz", metavar="FILE", dest="output", help="Specifies the filename of the generated edx-file relative to the working directory. [default: %default]") parser.add_option("--tmp", metavar="DIR", dest="tmp", help="""Configures the directory to use for the intermediate files. If set, this direcory will not be deleted. If not specified, a temporary directory is created by the operating system and deleted. """ ) (options, sources) = parser.parse_args() global courseURL courseURL = options.course_url # Setting up the logging facility. log_level = logging.WARNING if options.verbose == 1: log_level = logging.INFO elif options.verbose >= 2: log_level = logging.DEBUG fmt = LoggingFormatter() hdlr = logging.StreamHandler(sys.stdout) hdlr.setFormatter(fmt) logging.root.addHandler(hdlr) logging.root.setLevel(log_level) # When debugging, it's always good to know the values of the following variables: logging.debug("Options %s", options) logging.debug("Sources %s", sources) if len(sources) == 0: logging.error("Expects least one source.") parser.print_help() sys.exit(1) try: # Setup of our temorary directory, where we do all the file processing. if options.tmp is None: tmp_dir = tempfile.mkdtemp(); else: tmp_dir = options.tmp if not os.path.exists(tmp_dir): os.makedirs(tmp_dir) logging.debug("Temporary directory: %s", tmp_dir) # The strategy is as follows: # projects is a dictionary with the project name as the key and a list of groups as values. # When all groups are loaded, we transform the dict into a tuple, sort it and sort all the groups inside each project. projects = {} # If we are verbose, we print stdout/stderr of subprocesses (GIT). subprocess_setting = {'stderr':subprocess.PIPE, 'stdout':subprocess.PIPE} if logging.getLogger().getEffectiveLevel() <= logging.DEBUG: subprocess_setting = {'stderr':None, 'stdout':None} # We now load each source. for source in sources: if not os.path.exists(source): if source.endswith(('zip', 'tar.gz')): logging.info("Assuming that %s is a remote archive.", source) # requests is not a core module. try: import requests except ImportError: print """ERROR: The module requests is required but missing for remote archives. You can install it with the following commands: $ easy_install pip $ pip install requests """ sys.exit(1) # We need a 'unique' directory for the local checkout dest = str(uuid.uuid4()) local_path = os.path.join(tmp_dir, dest) os.makedirs(local_path) # Wen need the file-extension of the remote file o = urlparse.urlparse(source) (_ , filename_compressed) = os.path.split(o.path) archive_path = os.path.join(local_path, filename_compressed) logging.info("Downloading remote archive to %s.", archive_path); with open(archive_path, 'wb') as handle: request = requests.get(source, stream=True) # Raise in case of server/network problems: (4xx, 5xx, ...) request.raise_for_status() for block in request.iter_content(1024): if not block: break handle.write(block) if archive_path.endswith('.zip'): # We use the external unzip utility. logging.info("Unzip %s", source) try: p = subprocess.Popen(['unzip', archive_path], cwd=local_path, **subprocess_setting) p.wait() if p.returncode != 0: logging.error("Failed to unzip %s", source) raise SubprocessError("Failed to unzip %s".format(source)) except OSError: logging.error("unzip not found. Do you have unzip installed?") raise if archive_path.endswith('.tar.gz'): # We use the external untar utility. logging.info("untar %s", source) try: p = subprocess.Popen(['tar', '-zxvf', archive_path], cwd=local_path, **subprocess_setting) p.wait() if p.returncode != 0: logging.error("Failed to untar %s", source) raise SubprocessError("Failed to untar %s".format(source)) except OSError: logging.error("tar not found. Do you have tar installed?") raise # We search for a file called group.yaml, which gives us the directory to process path = None for dirname, dirnames, filenames in os.walk(local_path): # Process each file for filename in filenames: if filename == 'group.yaml': logging.debug("Found a group.yaml file inside %s.", dirname) path = dirname if path==None: logging.error("No group.yaml file found in %s", source) raise SubprocessError("No group.yaml file found in %s".format(source)) else: logging.info("There is no directory %s. Assuming that it's a remote GIT repository.", source) # We need a 'unique' directory for the local checkout dest = str(uuid.uuid4()) path = os.path.join(tmp_dir, dest) logging.warning("Cloning %s", source) try: p = subprocess.Popen(['git', 'clone', source, dest], cwd=tmp_dir, **subprocess_setting) p.wait() if p.returncode != 0: logging.error("Failed to clone GIT repository %s", source) raise SubprocessError("Failed to clone GIT repository %s".format(source)) except OSError: logging.error("GIT not found. Do you have GIT installed?") raise else: path = source logging.info("Processing %s", path) # We load the group definition and add it to the corresponding group. g = Group(path) g.load() if g.project() not in projects: projects[g.project()] = Project(g.project()) projects[g.project()].append(g) print "Archiving: %(path)s\n as: %(url_name)s" % { 'path':path, 'url_name':g.url_name()} + ".tar.gz\n" tar = tarfile.open(g.url_name()+".tar.gz", "w:gz") tar.add(path, arcname = g.url_name()) tar.close() upload_files.append(g.url_name()+".tar.gz") # Sort the projects alphabetically projects = projects.values() list.sort(projects, key=operator.attrgetter('project')) # We now have successfully read all groups and we proceed to create the edx course. # Setup the edx directory structure # All the other files and directories inside are uuid named. We don't have to fear a name clash. out_dir = os.path.join(tmp_dir, DISPLAY_NAME) # Delete the output directory, if it already exists if os.path.exists(out_dir): shutil.rmtree(out_dir) os.makedirs(out_dir) # Create course.xml course = Element('course'); course.set('url_name', 'url_name') course.set('org', ORG_NAME) course.set('course', 'course') tree = ElementTree(course) tree.write(os.path.join(out_dir, "course.xml")) # Create course/course.xml course_dir = os.path.join(out_dir, 'course') os.makedirs(course_dir) course = Element('course'); course.set('display_name', DISPLAY_NAME) for project in projects: e = SubElement(course, 'chapter') e.set('url_name', project.url_name()) tree = ElementTree(course) tree.write(os.path.join(course_dir, "{0}.xml".format('url_name')) ) # Let each project and implicitly each group create it's files for project in projects: project.edx(out_dir) # Archive the directory to the output file print "Creating the archive: %(path)s\n" % { 'path':options.output} tar = tarfile.open(options.output, "w:gz") tar.add(out_dir, arcname=os.path.basename(out_dir)) tar.close() import_file.append(options.output) # If any expection occurs, we still want to delete the temporary directory. finally: try: # We don't delete the temp_dir if it's path was specified by the user. if options.tmp is None: shutil.rmtree(tmp_dir) else: logging.warning("The manually set temporary directory won't be deleted (%s).", tmp_dir) except OSError as exc: if exc.errno != 2: # Unless the error says, that the tmp-directory doesn't exist anymore. raise
def _set_node_props(element, name, value): """Set the properties of the node based on the type of object""" # if it is a ctypes structure if isinstance(value, ctypes.Structure): # create an element for the structure struct_elem = SubElement(element, name) #clsModule = value.__class__.__module__ cls_name = value.__class__.__name__ struct_elem.set("__type__", "{0}".format(cls_name)) # iterate over the fields in the structure for prop_name in value._fields_: prop_name = prop_name[0] item_val = getattr(value, prop_name) if isinstance(item_val, six.integer_types): prop_name += "_LONG" item_val = six.text_type(item_val) struct_elem.set(prop_name, _escape_specials(item_val)) elif hasattr(value, 'tobytes') and hasattr(value, 'size'): try: # if the image is too big then don't try to # write it out - it would probably product a MemoryError # anyway if value.size[0] * value.size[1] > (5000*5000): raise MemoryError #print('type(value) = ' + str(type(value))) image_data = base64.encodestring(bz2.compress(value.tobytes())).decode('utf-8') _set_node_props( element, name + "_IMG", { "mode": value.mode, "size_x":value.size[0], "size_y":value.size[1], "data":image_data }) # a system error is raised from time to time when we try to grab # the image of a control that has 0 height or width except (SystemError, MemoryError): pass elif isinstance(value, (list, tuple)): # add the element to hold the values # we do this to be able to support empty lists listelem = SubElement(element, name + "_LIST") for i, attrval in enumerate(value): _set_node_props(listelem, "%s_%05d"%(name, i), attrval) elif isinstance(value, dict): dict_elem = SubElement(element, name) for item_name, val in value.items(): _set_node_props(dict_elem, item_name, val) else: if isinstance(value, bool): value = six.integer_types[-1](value) if isinstance(value, six.integer_types): name += "_LONG" element.set(name, _escape_specials(value))
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 createAP(self, elem, font, autoGdlFile) : e = SubElement(elem, 'glyph') if self.psname : e.set('PSName', self.psname) if self.uid : e.set('UID', self.uid) if self.gid is not None : e.set('GID', str(self.gid)) ce = None if 'classes' in self.properties and self.properties['classes'].strip() : tempClasses = self.properties['classes'] self.properties['classes'] = " ".join(font.filterAutoClasses(self.properties['classes'].split(), autoGdlFile)) for k in sorted(self.anchors.keys()) : v = self.anchors[k] p = SubElement(e, 'point') p.set('type', gr_ap(k)) p.text = "\n " l = SubElement(p, 'location') l.set('x', str(v[0])) l.set('y', str(v[1])) l.tail = "\n " if ce is not None : ce.tail = "\n " ce = p for k in sorted(self.gdlProperties.keys()) : if k == "*skipPasses*" : continue # not set in GDL v = self.gdlProperties[k] if v : p = SubElement(e, 'property') p.set('name', 'GDL_' + k) p.set('value', v) if ce is not None : ce.tail = "\n " ce = p if self.gdl and (not self.name or self.gdl != self.name.GDL()) : p = SubElement(e, 'property') p.set('name', 'GDLName') p.set('value', self.GDLName()) if ce is not None : ce.tail = "\n " ce = p for k in sorted(self.properties.keys()) : v = self.properties[k] if v : p = SubElement(e, 'property') p.set('name', k) p.set('value', v) if ce is not None : ce.tail = "\n " ce = p if self.comment : p = SubElement(e, 'note') p.text = self.comment if ce is not None : ce.tail = "\n " ce = p if 'classes' in self.properties and self.properties['classes'].strip() : self.properties['classes'] = tempClasses if ce is not None : ce.tail = "\n" e.text = "\n " e.tail = "\n" return e
def save_node(node): node_el = SubElement(root, "node") node_el.set("id", str(node.get_id())) node_el.set("size", str(node.get_size())) return node_el