📄 xmlutil.py
字号:
#!/usr/bin/env python################################################################################## This file is part of Gato (Graph Algorithm Toolbox) # version _VERSION_ from _BUILDDATE_. You can find more information at # http://www.zpr.uni-koeln.de/~gato## file: HMMEd.py# author: Alexander Schliep (schliep@zpr.uni-koeln.de)## Copyright (C) 1998-2002, Alexander Schliep, Winfried Hochstaettler and # ZAIK/ZPR, Universitaet zu Koeln# # Contact: schliep@zpr.uni-koeln.de, wh@zpr.uni-koeln.de ## Information: http://gato.sf.net## This library is free software; you can redistribute it and/or# modify it under the terms of the GNU Library General Public# License as published by the Free Software Foundation; either# version 2 of the License, or (at your option) any later version.## This library is distributed in the hope that it will be useful,# but WITHOUT ANY WARRANTY; without even the implied warranty of# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU# Library General Public License for more details.## You should have received a copy of the GNU Library General Public# License along with this library; if not, write to the Free# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA#### This file is version $Revision: 1.5 $ # from $Date: 2004/04/26 14:58:18 $# last change by $Author: wasinee $.#################################################################################from DataStructures import Point2D,EdgeWeightfrom Graph import Graph, SubGraphfrom xml.dom.minidom import *import EditObjectAttributesDialogfrom EditObjectAttributesDialog import EditObjectAttributesDialog, ValidatingString, ValidatingInt, ValidatingFloat, PopupableInt, Probability, DefaultedInt, DefaultedStringimport whrandomimport stringimport typesimport copyimport sys import HMMXMLimport logginglog = logging.getLogger("xmlutil.py")def typed_assign(var, val): result = type(var)(val) result.__dict__ = var.__dict__ #result.__dict__ = copy.copy(var.__dict__) return resultdef listFromCSV(s, type): return map(type,string.split(s,','))def csvFromList(list, perRow = None): if perRow == None: return string.join(map(str,list), ', ') else: result = "" for start in xrange(0, len(list), perRow): result += string.join(map(str,list[start:start+perRow]), ', ') + ',\n' return result[0:len(result)-2]def writeContents(XMLDoc, XMLNode, data): contents = XMLDoc.createTextNode("%s" % data) XMLNode.appendChild(contents)def writeData(XMLDoc, XMLNode, dataKey, dataValue): data = XMLDoc.createElement("data") data.setAttribute('key', "%s" % dataKey) contents = XMLDoc.createTextNode("%s" % dataValue) data.appendChild(contents) XMLNode.appendChild(data)def writeXMLData(XMLDoc, XMLNode, dataKey, XMLData): data = XMLDoc.createElement("data") data.setAttribute('key', "%s" % dataKey) data.appendChild(XMLData) XMLNode.appendChild(data)def writeXMLTextNode(XMLDoc, XMLNode, keyName, XMLData): data = XMLDoc.createElement(keyName) contents = XMLDoc.createTextNode("%s" % XMLData) data.appendChild(contents) XMLNode.appendChild(data)class NamedDistributions: def __init__(self, itsHMM): self.initialize() self.itsHMM = itsHMM self.code2name = {-1:'None'} self.name2code = {'None':-1} self.maxCode = 0 def initialize(self): self.dist = {} self.order = {} def addDistribution(self, name, order, p): self.dist[name] = p self.order[name] = order self.code2name[self.maxCode] = name self.name2code[name] = self.maxCode self.maxCode += 1 print self.dist def deleteDistribution(self, name): del self.dist[name] del self.order[name] del self.code2name[self.name2code[name]] del self.name2code[name] def fromDOM(self, XMLNode): self.initialize() datas = XMLNode.getElementsByTagName("hmm:background") for data in datas: dataKey = data.attributes['key'].nodeValue dataOrder = int(data.attributes['order'].nodeValue) dataValue = "" for child in data.childNodes: dataValue += child.nodeValue p = listFromCSV(dataValue, types.FloatType) self.addDistribution(dataKey, dataOrder, p) def toDOM(self, XMLDoc, XMLNode): for name in self.dist.keys(): print "background: name = ", name, self.dist[name], self.order[name] background_elem = XMLDoc.createElement("hmm:background") background_elem.setAttribute('key', "%s" % name) background_elem.setAttribute('order', "%s" % self.order[name]) if self.order[name] == 0: contents = XMLDoc.createTextNode(csvFromList(self.dist[name])) else: contents = XMLDoc.createTextNode(csvFromList(self.dist[name], self.itsHMM.hmmAlphabet.size())) background_elem.appendChild(contents) XMLNode.appendChild(background_elem) def names(self): return self.dist.keys() class XMLElementWriter: def __init__(self): import string self._string = string self.element_keys = ['id', 'hmm:low', 'hmm:high', 'hmm:type', 'for', 'gd:type', 'code', 'key', 'order', 'x', 'y', 'source', 'target'] def _write_data(self, writer, data): "Writes datachars to writer." replace = self._string.replace data = replace(data, "&", "&") data = replace(data, "<", "<") data = replace(data, "\"", """) data = replace(data, ">", ">") writer.write(data) def writexml(self, XMLNode, writer, indent="", addindent="", newl=""): """This version of write xml makes sure text nodes are surrounded by tags for easier reading rather than being on lines by themselves.""" # indent = current indentation # addindent = indentation to add to higher levels # newl = newline string if ( XMLNode.nodeType == XMLNode.TEXT_NODE ): self._write_data(writer, "%s%s%s"%(indent, XMLNode.data, newl)) else: writer.write(indent+"<" + XMLNode.nodeName) # build attribute list a_names = [] try: for key in self.element_keys: if ( XMLNode.getAttribute(key) != ""): a_names.append( key ) a_names.sort() except AttributeError: a_names = [] for a_name in a_names: writer.write(" %s=\"" % a_name) self._write_data(writer, XMLNode.getAttribute(a_name)) writer.write("\"") if XMLNode.childNodes: writer.write(">%s"%(newl)) for node in XMLNode.childNodes: if node.nodeType!=node.TEXT_NODE: self.writexml(node,writer,indent+addindent,addindent,newl) else: writer.seek(writer.tell()-1) self.writexml(node,writer,"",addindent,"") if XMLNode.childNodes[-1].nodeType!=node.TEXT_NODE: writer.write("%s</%s>%s" % (indent,XMLNode.nodeName,newl)) else: writer.write("</%s>%s" % (XMLNode.nodeName,newl)) else: writer.write("/>%s"%(newl)) ## Notice for this function:# minidom.Element.toprettyxml is not so pretty and its output format is not compatible# with XMLIO parser. The particular problem with minidom.toprettyxml is that# it put the text data of a text node on a new line, instead of immediately after the element tag.# Because XMLIO cannot parse this format, thus we need our own pretty print program #def toprettyxml( XMLDoc ): # we can't use cStringIO since it doesn't support Unicode strings from StringIO import StringIO writer = StringIO() prettydoc = XMLElementWriter() writer.write('<?xml version="1.0" ?>\n') for node in XMLDoc.childNodes: prettydoc.writexml(node, writer, ""," ", "\n") return writer.getvalue(); class DOM_Map: def __init__(self): self.initialize() def initialize(self): self.name = {} self.desc = {} self.hasDesc = None self.name2code = {} def addCode(self, code, name, desc = None): self.name[code] = name if desc != None: self.desc[code] = desc self.hasDesc = 1 self.name2code[name] = code def low(self): if len(self.name.keys()) > 0: return min(self.name.keys()) else: return 0 def high(self): if len(self.name.keys()) > 0: return max(self.name.keys()) else: return 0 def fromDOM(self, XMLNode): pass def symbolsFromDom(self, XMLNode): symbols = XMLNode.getElementsByTagName("symbol") for symbol in symbols: symbolCode = ValidatingInt(int(symbol.getAttribute("code"))) symbolName = ValidatingString(symbol.firstChild.nodeValue) symbolDesc = symbol.getAttribute("desc") if symbolDesc != None: self.addCode(symbolCode, symbolName, ValidatingString(symbolDesc)) else: self.addCode(symbolCode, symbolName) def toDOM(self, XMLDoc, XMLNode): XMLNode.setAttribute('hmm:low', "%s" % self.low()) XMLNode.setAttribute('hmm:high', "%s" % self.high()) map = XMLDoc.createElement("map") for key in self.name.keys(): symbol = XMLDoc.createElement("symbol") symbol.setAttribute('code', "%s" % key) if self.hasDesc and self.desc[key] != "":
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -