Statistiques
| Révision :

root / prepareQMX / prepareQMX.GUI.py @ 16

Historique | Voir | Annoter | Télécharger (8,24 ko)

1
#!/usr/bin/env python
2

    
3
# GRAPHICAL interface for setup of QMX calculations
4
#
5
# Torsten Kerber, ENS LYON: 2011, 07, 11
6
#
7
# This work is supported by Award No. UK-C0017, made by King Abdullah
8
# University of Science and Technology (KAUST)
9

    
10
#---------------------------------------------------------------------------------------------------
11
from Tkinter import *
12
from copy import deepcopy
13
import tkFileDialog
14
import StringIO, os
15

    
16
#---------------------------------------------------------------------------------------------------
17
from qmxEMBED import embedDefinitions
18
from qmxJOB import jobDefinitions
19
from qmxSTR import strClusterDefinitions, strSystemDefinitions
20
from qmxCALC import calcDefinitions, QmxCalcDefinition
21

    
22
from qmxWRITE import writeData
23

    
24
#---------------------------------------------------------------------------------------------------
25
#---------------------------------------------------------------------------------------------------
26
#---------------------------------------------------------------------------------------------------
27
class MyLabelFrame(LabelFrame):
28
    def __init__(self, mainWnd, master, title, name, def_list, fields):
29
        LabelFrame.__init__(self, master, text=title)
30
        self.mainWnd = mainWnd
31
        self.definitions=[]
32

    
33
        for definition in def_list:
34
            definition_new = deepcopy(definition)
35
            definition_new.system = name
36
            self.definitions.append(definition_new)
37

    
38
        if fields is None:
39
            fields = ['']
40

    
41
        self.entries=[]
42
        for key in fields:
43
            text=StringVar()
44
            text.set('')
45
            self.entries.append((key, text, Entry(self, textvariable=text, width=25)))
46
            
47
    def grid(self, **arguments):
48
        LabelFrame.grid(self, arguments, sticky=(N+W+E))
49
        self.listbox.selection_set(first=0)
50
        self.select(None)
51
        
52
    def doLayout(self):
53
        irow=0
54
        for (key, text, textfield) in self.entries:
55
            Label(self, text=key, width=15, anchor=W).grid(row=irow,  column=2)
56
            textfield.grid(row=irow, column=3)
57
            textfield.bind("<KeyRelease>", self.update)
58
            irow+=1
59
        
60
        scrollbar=Scrollbar(self, orient=VERTICAL)
61
        self.listbox=Listbox(self, yscrollcommand=scrollbar.set, height=5, width=15, selectmode=SINGLE, exportselection=0)
62
        self.listbox.grid(row=0, column=0, rowspan=irow, sticky=NS)
63
        self.listbox.bind('<ButtonRelease-1>', self.select)
64

    
65
        scrollbar.config(command=self.listbox.yview)
66
        scrollbar.grid(row=0, column=1, rowspan=irow, sticky=NS)
67
       
68
        for item in self.definitions:
69
            self.listbox.insert(END, item.name)
70
        
71
    def select(self, event):
72
        selection=self.listbox.curselection()
73
        if len(selection) == 1:
74
            self.curr_definition=deepcopy(self.definitions[int(selection[0])])
75
            for (key, text, textfield) in self.entries:
76
                text.set(self.curr_definition.getValue(key))
77
        self.mainWnd.preview()
78
    
79
    def getSelection(self):
80
        return self.curr_definition
81
        
82
    def update(self, event):
83
        for (key, text, textfield) in self.entries:
84
            self.curr_definition.keywords[key]=text.get()
85
        self.mainWnd.preview()
86

    
87
#---------------------------------------------------------------------------------------------------
88
class MainWindow(Frame):
89
    frames=[]
90
    text=None
91
    def __init__(self, master):
92
        Frame.__init__(self, master)
93
        
94
        fields=['import', 'class', 'class.options']
95
        
96
        irow=0
97
        for calcLabel, strLabel, strDefinitions in (
98
                ("high-level",  "cluster", strClusterDefinitions), 
99
                ("low-level",  "system", strSystemDefinitions)):
100
            #column
101
            icolLocal=0;
102
            #build outFrame
103
            outFrame = LabelFrame(self, text=calcLabel+" method - "+strLabel)
104
            outFrame.grid(row=irow, columnspan=3,  sticky=W, pady=5)
105
            irow+=1
106
            
107
            for title, def_list in ((calcLabel, calcDefinitions),  (strLabel, strDefinitions)):
108
                # build frame
109
                myFrame=MyLabelFrame(self, outFrame, title, title, def_list, fields)
110
                myFrame.doLayout()
111
                myFrame.grid(row=0, column=icolLocal, padx=5, pady=5)
112
                # add frame for analyssis
113
                self.frames.append(myFrame)
114
                # move column
115
                icolLocal += 1
116
            
117
        outFrame = LabelFrame(self, text="General")
118
        outFrame.grid(row=irow, columnspan=3, pady=5); irow+=1
119
        for title, name, def_list, extra_list,  irowLocal, icolLocal,  irspan in (
120
            ("Optimization Method",  "job", jobDefinitions, fields + ['method', 'method.options'], 0, 0, 2), 
121
            ("Embedding Method", "embed", embedDefinitions, fields + ['set_calculator'], 0, 1, 1), 
122
            ("Hybrid Method", "hybrid", [QmxCalcDefinition()], fields, 1, 1, 1)):
123
            # build frame
124
            myFrame=MyLabelFrame(self, outFrame, title, name, def_list, extra_list)
125
            myFrame.doLayout()
126
            myFrame.grid(row=irowLocal, column=icolLocal,  rowspan=irspan, padx=5, pady=5)
127
            # add frame for analyssis
128
            self.frames.append(myFrame)
129
            
130
        outFrame=LabelFrame(self, text="PREVIEW")
131
        scrollbar=Scrollbar(outFrame, orient=VERTICAL)
132
        scrollbar.set(0.0, 1.0)
133
        self.text=Text(outFrame, yscrollcommand=scrollbar.set, width=80, heigh=10, state=DISABLED)
134
        self.text.grid(sticky=(N+W+S+E))
135
        scrollbar.config(command=self.text.yview)
136
        scrollbar.grid(row=0, column=1, sticky=NS)        
137
        outFrame.grid(row=irow, column=0, sticky=W, pady=5);
138

    
139
        myFrame = Frame(self)
140
        buttonExit=Button(myFrame, text="Quit!", command=self.quit)
141
        buttonExit.grid(row=1, column = 1, rowspan=3, sticky= W+E+N+S)
142
        
143
        buttonSave=Button(myFrame, text='save settings', state=DISABLED)
144
        buttonSave.grid(row=1, column = 2, sticky=(EW))
145

    
146
        buttonScript=Button(myFrame, text='Write SCRIPT file', command=self.writeScript)
147
        buttonScript.grid(row=2, column = 2, sticky=EW)
148

    
149
        buttonPython=Button(myFrame, text='Write PYTHON file', command=self.writePython)
150
        buttonPython.grid(row=3, column = 2, sticky=EW)
151

    
152
        myFrame.grid(row=irow, column=2, sticky=SE)
153

    
154
        #--- MainWindow ---
155
        self.grid(sticky=(W+N+S+E))
156
        self.preview()
157
        
158
    def quit(self):
159
        quit()
160
    
161
    def writePython(self):
162
        fileName = tkFileDialog.asksaveasfile(mode='w')
163
        if fileName is None:
164
            return
165
        file = open(fileName,"w")
166
        definitions=[]
167
        for frame in self.frames:
168
            definition = frame.getSelection()
169
            if definition is not None:
170
                definitions.append(definition)
171

    
172
        writeData(file, definitions)
173
        file.close()
174
        os.system("chmod u+x qmx.py")
175
        
176
        
177
    def writeScript(self):
178
        fileName = tkFileDialog.asksaveasfile(mode='w')
179
        if fileName is None:
180
            return
181
        file = open(fileName,"w")
182
        for frame in self.frames:
183
            definition = frame.getSelection()
184
            if definition is None:
185
                continue
186

    
187
            system = definition.system.lower()
188
            data = system+".program : "+definition.name.lower()+"\n"
189
            file.write(data)
190
            for key in definition.keywords:
191
                data=system+"."+key+" : "+definition.getValue(key)+"\n"
192
                file.write(data)
193
            file.write("\n")
194
        file.close()
195

    
196
    def preview(self):
197
        stream = StringIO.StringIO()
198
        definitions=[]
199
        for frame in self.frames:
200
            definition = frame.getSelection()
201
            if definition is not None:
202
                definitions.append(definition)
203

    
204
        writeData(stream, definitions)
205
        s = stream.getvalue()
206
        
207
        if self.text is not None:
208
            pos, end = self.text.yview()
209
            self.text.config(state=NORMAL)
210
            self.text.delete(1.0, END)
211
            self.text.insert(END, s)
212
            self.text.config(state=DISABLED)
213
            self.text.yview(MOVETO, pos)
214

    
215
#---------------------------------------------------------------------------------------------------
216
mainWnd=MainWindow(Tk())
217
mainWnd.mainloop()