File: odexml.py

package info (click to toggle)
model-builder 0.4.1-6
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 1,056 kB
  • sloc: python: 3,916; fortran: 690; makefile: 18; sh: 11
file content (142 lines) | stat: -rw-r--r-- 5,223 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#-----------------------------------------------------------------------------
# Name:        odexml.py
# Purpose:     Read and write model files in xml format
#
# Author:      Flavio Codeco Coelho
#
# Created:     2006/06/30
# RCS-ID:      $Id: odexml.py $ 
# Copyright:   (c) 2006
# Licence:     GPL
# New field:   Whatever
#-----------------------------------------------------------------------------
from xml.dom import minidom, Node
import __version__ as V
import wxversion
wxversion.select('2.8')
import wx, sys, os
from numpy import array

class Model:
    def __init__(self):
        self.model = minidom.Document()
        self.model.appendChild(self.model.createComment("Model Builder %s - model definition file"%V.version))
        self.body = self.model.createElement('body')
        self.model.appendChild(self.body)
        #output = self.model.createElement('output')
        #self.model.appendChild(output)
        self.modict = {}
        self.path = None
    def Create(self,**kwargs):
        """
        Creates all the elements and append to the 'body' section.
        Takes a dictionary or keyword elements as input.
        """
        eqlist = kwargs.pop('equations').split('\n')
        parlist = kwargs.pop('parameters').split('\n')
        #creating model name
        modelname = self.model.createElement('modelname')
        modelname.appendChild(self.model.createTextNode(kwargs.pop('modelname').strip()))
        self.body.appendChild(modelname)
        #creating equations
        for n,e in enumerate(eqlist):
            equation = self.model.createElement('equation')
            self.body.appendChild(equation)
            equation.setAttribute('number',str(n))
            equation.appendChild(self.model.createTextNode(e.strip()))
        #creating parameters
        for n,p in enumerate(parlist):
            parameter = self.model.createElement('parameter')
            self.body.appendChild(parameter)
            parameter.setAttribute('number',str(n))
            parameter.appendChild(self.model.createTextNode(p.strip()))
        # Creating others
        for k,v in kwargs.items():
            Key = self.model.createElement(k)
            self.body.appendChild(Key)
            Key.appendChild(self.model.createTextNode(str(v).strip()))
    def addResults(self,results):
        """
        appends results matrix to the xml file.
        """
        pass
    def saveFile(self,filename):
        """
        Saves the generated XML to disk.
        """
        os.chdir(self.path)
        #if os.path.exists(filename):
        #    os.unlink(filename)
        f = open(filename,'w')
        f.write(self.model.toprettyxml())
        f.close()
        print "saved file", filename, "at",self.path
        
    def readFile(self,filename):
        """
        read a model from an xml document.
        """
##        try:
        doc = minidom.parse(filename)
        print 'Done parsing'
        if self.Verify(doc):
            self.model = doc
            self.readFromDom()
            return 0
        else:
            print 'This is not a model file'
            return 1
##        except:
##            print 'There was a problem parsing the model file.',
##            print sys.exc_info()
##            return 2
            
    def readFromDom(self):
        """
        Prepares self.modict
        from the data contained in the 
        dom object.
        """
        equations = ''
        parameters = ''
        for child in self.model.getElementsByTagName('body')[0].childNodes:
            if child.nodeType == Node.ELEMENT_NODE:
                #print child.tagName
                if child.tagName == 'equation':
                    for node in child.childNodes:
                        equations += node.data.strip() +'\n'
                elif child.tagName == 'parameter':
                    for node in child.childNodes:
                        parameters += node.data.strip() +'\n'
                else:
                    for node in child.childNodes:
                        self.modict[child.tagName.encode('ascii','ignore')] = node.data.strip().encode('ascii','ignore')
            else: pass 
        self.modict['equations'] = equations[:-1]
        self.modict['parameters'] = parameters[:-1]
    
    def Verify(self,doc):
        """
        Check if the xml file was generated by modelbuilder
        """
        for child in doc.childNodes:
            if child.nodeType == Node.COMMENT_NODE:
                if child.data.startswith('Model Builder'):
                    if V.version not in child.data:
                        print 'this model was generated in a different version of Model Builder.'
                        return 2
                    else:
                        print 'verify ok!'
                        return 1
                else: 
                    print 'not a Model Builder file!!'
                    return 0
                

if __name__ == '__main__':
    m = Model() # add a call to run your script here
    m.Create(**{'modelname':'teste','equations':'x+y\nz+x','parameters':'1\n2\n3'})
    m.saveFile('modelo.xml')
    m.readFile('modelo.xml')
    print m.model.toprettyxml()
    print m.modict