def Test(tester): tester.startGroup("Alexander Fayolle's encoding problems and variations") tester.startTest('XML output UTF-8 encoding') d = Sax2.FromXml(source_1) stream = cStringIO.StringIO() Print(d, stream=stream) result = stream.getvalue() if result != expected_1: tester.error('Expected\n"""%s"""\ngot\n"""%s"""' % (repr(expected_1), repr(result))) tester.testDone() tester.startTest('XML output ISO-8859-1 encoding') d = Sax2.FromXml(source_1) stream = cStringIO.StringIO() Print(d, stream=stream, encoding='ISO-8859-1') result = stream.getvalue() if result != expected_2: tester.error('Expected\n"""%s"""\ngot\n"""%s"""' % (repr(expected_2), repr(result))) tester.testDone() return tester.groupDone()
def Test(tester): tester.startGroup("Testing PyExpat") reader = PyExpat.Reader() tester.startTest('Basic test') doc = reader.fromString(source_1) stream = cStringIO.StringIO() Print(doc, stream=stream) result = stream.getvalue() print result #if result != expected_1: # tester.error('Expected\n"""%s"""\ngot\n"""%s"""'%(repr(expected_1), repr(result))) reader.releaseNode(doc) tester.groupDone() tester.startGroup("Testing Sax2") reader = Sax2.Reader() tester.startTest('Basic test') doc = reader.fromString(source_1) stream = cStringIO.StringIO() Print(doc, stream=stream) result = stream.getvalue() print result #if result != expected_1: # tester.error('Expected\n"""%s"""\ngot\n"""%s"""'%(repr(expected_1), repr(result))) reader.releaseNode(doc) return tester.groupDone()
def endDocument(self): from xml.dom.ext import PrettyPrint, Print import StringIO f = StringIO.StringIO() Print(self.doc, f) txt = f.getvalue() f.close() self.cur = None return txt
def serialize(self, output_format=None, converter=None): "Serialize to 'mathml' (default) or any other supported term format." if converter is None: if output_format is None: output_format = 'mathml' if output_format == 'mathml': out = StringIO() Print(self._document, out) return out.getvalue() elif output_format == 'pmathml': raise ValueError, "Presentation MathML requires XSLT." return serialize_dom(self._document, output_format, converter)
def htmlDiff(self): """Return a revised xml document""" from Stream import ListStream from DiffExecutor import XMLDiffExecutor from xml.dom.ext import Print from Products.CNXMLDocument import XMLService diff = XMLDiffExecutor(self.oldValue, self.newValue) diff.execute() strm = ListStream() Print(diff.getRevisedDocument().getDocumentRoot(), stream=strm) # ignore <?xml ..> return strm.read()[38:]
def Test(tester): tester.startGroup( "Nicolas Chauvat <*****@*****.**>'s Printer Bug Report") tester.startTest('Attribute quote print') d = Sax2.FromXml(source_1) stream = cStringIO.StringIO() Print(d, stream=stream) result = stream.getvalue() if result != expected_1: tester.error('Expected\n"""%s"""\ngot\n"""%s"""' % (repr(expected_1), repr(result))) return tester.groupDone()
def toMathml(self, out=None, indent=False): """Convert this MathDOM into MathML and write it to file (or file-like object) out. Pretty printing is activated by setting indent=True.""" if out is None: out = sys.stdout root = self._document.lastChild if root and root.localName != u'math': dom = getDOMImplementation() document = dom.createDocument(MATHML_NAMESPACE_URI, u"math", None) document.lastChild.children[:] = [root] else: document = self._document if indent: PrettyPrint(document, out) else: Print(document, out)
gameElem.appendChild(boardElem) score1, score2 = params['result'].split('-') winner = 1 + (int(score1) < int(score2)) resultElem = doc.createElement('result') resultElem.setAttribute('score1', score1) resultElem.setAttribute('score2', score2) resultElem.setAttribute('winner', str(winner)) resultElem.appendChild(doc.createTextNode(params['resultDesc'])) gameElem.appendChild(resultElem) return doc base_url = 'http://www.codecup.nl/' if len(sys.argv) <> 2: print 'usage: %s <comp>' % sys.argv[0] sys.exit(1) else: comp = sys.argv[1] comp_page = urlopen(base_url + 'competition.php?comp=' + comp).read() for cr in re.findall('competitionround.php[?]cr=(\d+)', comp_page): round_page = urlopen(base_url + 'competitionround.php?cr=' + cr).read() for ga in re.findall('showgame.php[?]ga=(\d+)', round_page): game_page = urlopen(base_url + 'showgame.php?ga=' + ga).read() game = extract_game(game_page) path = '-'.join([comp, cr, ga, 'game.xml']) Print(game, file(path, 'wt'))
reader = Sax2.Reader() # parse the document doc = reader.fromStream(sys.stdin) walker = doc.createTreeWalker(doc.documentElement, NodeFilter.SHOW_ELEMENT, None, 0) while 1: if walker.currentNode.tagName == "text": handle_text_node(walker.currentNode) next = walker.nextNode() if next is None: break for node in deleteNodes: node.parentNode.removeChild(node) # Add the scripting node scr = doc.createElement("script") scr.setAttribute("xlink:href","scr.js"); doc.documentElement.appendChild(scr) scr = doc.createElement("script") scrtext = doc.createTextNode(generate_script(scripts)) scr.appendChild(scrtext) doc.documentElement.appendChild(scr) Print(doc, sys.stdout)
d = Sax2.FromXml('<doc/>') e = d.createElementNS('', 'elt') d.documentElement.appendChild(e) try: e.setAttributeNS('http://logilab', 'att', 'value1') except DOMException, x: if x.code != NAMESPACE_ERR: name = getExceptionName(x.code) tester.error("Wrong exception '%s', expected NAMESPACE_ERR" % name) else: tester.error( 'setAttributeNS with no prefix and non-null URI doesn\'t raise exception.' ) e.setAttributeNS('http://logilab', 'spam:att', 'value1') stream = cStringIO.StringIO() Print(d, stream=stream) result = stream.getvalue() if result != expected_1: tester.error('Expected\n"""%s"""\ngot\n"""%s"""' % (repr(expected_1), repr(result))) stream = cStringIO.StringIO() PrettyPrint(d, stream=stream) result = stream.getvalue() if result != expected_2: tester.error('Expected\n"""%s"""\ngot\n"""%s"""' % (repr(expected_2), repr(result))) tester.testDone() tester.startTest('Document Fragment Printing') d = Sax2.FromXml(source_1)
def writeReorderedSecond(model2, functionMappings, ruleMappings, outfileName): # go through the file and replace all function ids with the corresponding # id from the other file functionDefinitions = model2.getElementsByTagName("functionDefinition") for functionDefinition in functionDefinitions: id = functionDefinition.getAttribute("id") if (id == ""): print "Error. function definition without id found." sys.exit(1) if (id in functionMappings[1].keys()): functionDefinition.setAttribute("id", functionMappings[1][id]) else: # if there is no mapping for a specific function, check if the name is # unique, if not, replace it by a unique name and add this to the mappings newId = id if (id in functionMappings[0].keys()): ext = 1 newId = "function_" + str(ext) while ((newId in functionMappings[0].keys()) or (newId in functionMappings[1].keys())): ext = ext + 1 newId = "function_" + str(ext) functionDefinition.setAttribute("id", newId) functionMappings[1][id] = newId # replace all functions calls with calls to the mapped function ids ciNodes = model2.getElementsByTagName("ci") for ciNode in ciNodes: for node in ciNode.childNodes: if (node.nodeType == node.TEXT_NODE and node.nodeValue.strip() in functionMappings[1].keys()): node.nodeValue = " " + functionMappings[1][ node.nodeValue.strip()] + " " # bring all rules into the same order as in the first file listOfRules = model2.getElementsByTagName("listOfRules") if (len(listOfRules) == 1): listOfRules = listOfRules[0] # delete all existing rules ruleIndex = 0 reOrderedRules = [] for x in range(0, len(ruleMappings[1])): reOrderedRules.append(None) tagNames = ["algebraicRule", "rateRule", "assignmentRule"] for child in listOfRules.childNodes: if (child.nodeType == child.ELEMENT_NODE): if (child.tagName in tagNames): # which position will this rule get newPosition = ruleMappings[1][ruleIndex] if (newPosition == -1): reOrderedRules.append(child) else: reOrderedRules[newPosition] = child ruleIndex = ruleIndex + 1 ruleIndex = 0 #pdb.set_trace() for X in range(0, len(listOfRules.childNodes)): child = listOfRules.childNodes[X] if (child.nodeType == child.ELEMENT_NODE): if (child.tagName in tagNames): if (child != reOrderedRules[ruleIndex]): listOfRules.replaceChild( reOrderedRules[ruleIndex].cloneNode(1), child) ruleIndex = ruleIndex + 1 elif (len(listOfRules) > 1): print "Error. More than one listOfRules elements found in model." sys.exit(1) # write the reordered model outfile = open(outfileName, "w") Print(model2, outfile)
# Begin CVS Header # $Source: /Volumes/Home/Users/shoops/cvs/copasi_dev/copasi/sbml/unittests/scripts/rewriteXML.py,v $ # $Revision: 1.1 $ # $Name: $ # $Author: gauges $ # $Date: 2008/01/28 14:33:50 $ # End CVS Header # Copyright (C) 2008 by Pedro Mendes, Virginia Tech Intellectual # Properties, Inc., EML Research, gGmbH, University of Heidelberg, # and The University of Manchester. # All rights reserved. #!/usr/bin/env python import sys import pdb from xml.dom.ext.reader import Sax2 from xml.dom.ext import Print if (len(sys.argv) != 3): print "Usage: rewriteXML SBMLFILE OUTFILE" sys.exit(1) FILENAME = sys.argv[1] OUTFILE = sys.argv[2] reader = Sax2.Reader() doc = reader.fromStream(open(FILENAME)) outfile = open(OUTFILE, "w") Print(doc, outfile)