source: git/modules/openmathserver/omxmlreader.py @ b651ed

spielwiese
Last change on this file since b651ed was b651ed, checked in by Michael Brickenstein <bricken@…>, 18 years ago
*bricken: so much fun, lalala git-svn-id: file:///usr/local/Singular/svn/trunk@8364 2c84dea3-7e68-4137-9b89-c4e89433aadc
  • Property mode set to 100644
File size: 3.2 KB
Line 
1from xml.dom.xmlbuilder import *
2#from xml.dom.ext import PrettyPrint
3from xml.dom import Node
4import xml.dom as dom
5#from
6from StringIO import StringIO
7import re
8import sys
9import os.path
10from objects import *
11from cd import *
12def readFile(input_file_name):
13    docstream = open(input_file_name)
14    docIS=DOMInputSource()
15    docIS.byteStream=docstream
16    doc= DOMBuilder().parse(docIS)
17    docstream.close()
18   
19    root=doc.lastChild
20    return root
21   
22def  get_text_in_children(n):
23    t = ""
24    for c in n.childNodes:
25        if c.nodeType== Node.TEXT_NODE:
26            t += c.nodeValue
27    return t
28def remove_white_space(node):
29    remove_list=[]
30    if node.nodeName=="OMC":
31        return
32    for child in node.childNodes:
33        if child.nodeType==dom.Node.TEXT_NODE and not child.data.strip():
34            remove_list.append(child)
35        elif child.hasChildNodes():
36            remove_white_space(child)
37    for node in remove_list:
38        node.parentNode.removeChild(node)
39        node.unlink()
40       
41class OMFromXMLBuilder:
42    def buildFromNode(self, node):
43        if (node.nodeName=="OMI"):
44            content=get_text_in_children(node)
45            return OMint(content)
46        if (node.nodeName=="OMV"):
47            name=node.getAttribute("name") #node.attributes["name"]
48            #print dir(name)
49            return OMvar(name)
50        if (node.nodeName=="OMS"):
51            if node.hasAttribute("cdbase"):
52                #FIXME: obtain from ancestors
53                cdbase=node.getAttribute("cdbase")
54            else: 
55                cdbase=None
56            cdname=node.getAttribute("cd")
57            name=node.getAttribute("name")
58            #print repr(node)
59            #print node.attributes
60            #print "cdbase", cdbase
61            if cdbase==None:
62                cd=OMcd(cdname)
63            else:
64                cd=OMcd(cdname,cdbase)
65            return OMsymbol(name,cd)
66        if (node.nodeName=="OMA"):
67            children=[self.buildFromNode(c) for c in node.childNodes]
68            return OMapplication(children[0],children[1:])
69    def build(self, root):
70        remove_white_space(root)
71        return self.buildFromNode(root)
72#TODO: handle hex floats
73#TODO: handle floats
74#TODO: handle ancestors cdbase
75if __name__=='__main__':
76    import arith1
77   
78    if len(sys.argv)<=2:
79        print "Usage: python omxmlreader [--evaluate] input output"
80    state=0
81    eval=False
82    #TODO: use optparse
83    for arg in sys.argv[1:]:
84        if state==0:
85            if arg=="--evaluate":
86                eval=True
87            else:
88                inputf=arg
89                state=1
90            continue
91        if state==1:
92            outputf=arg
93            continue
94        if state==2:
95            print "argument ignored:", arg
96    from context import Context
97    #inputf=sys.argv[1]
98    root=readFile(inputf)
99    builder=OMFromXMLBuilder()
100    context=Context()
101    context.addCDImplementation(arith1.implementation)
102    doc=builder.build(root)
103    if eval:
104        doc=context.evaluate(doc)
105    output=context.XMLEncodeObject(doc)
106    try:
107        out=open(outputf,"w")
108    except NameError:
109        print "no output file"
110        sys.exit(1)
111    out.write(output)
112    out.close()
113    #print repr(root.getAttribute("blabla"))
Note: See TracBrowser for help on using the repository browser.