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

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