Statistiques
| Révision :

root / prepareQMX / prepareQMX.GUI.py @ 14

Historique | Voir | Annoter | Télécharger (8,03 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 StringIO, os
14

    
15
#---------------------------------------------------------------------------------------------------
16
from qmxEMBED import embedDefinitions
17
from qmxJOB import jobDefinitions
18
from qmxSTR import strDefinitions
19
from qmxCALC import calcDefinitions, QmxCalcDefinition
20

    
21
from qmxWRITE import writeData
22

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

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

    
38
        fields=['import', 'class', 'class.options']
39
        if extras is not None:
40
            for key in extras:
41
                fields.append(key)
42

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

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

    
89
#---------------------------------------------------------------------------------------------------
90
class MainWindow(Frame):
91
    frames=[]
92
    text=None
93
    def __init__(self, master):
94
        Frame.__init__(self, master)
95
        
96
        #--- Structures ---
97
        bigFrame = LabelFrame(self, text='Structures')
98
        irow=0; icol=0
99
        def_list=embedDefinitions
100
        extras=['method', 'method.options', 'calculator']
101
        frame=MyLabelFrame(self, bigFrame, 'Embed', def_list, extras)
102
        frame.doLayout()
103
        frame.grid(row=irow, column=icol)
104
        self.frames.append(frame)
105

    
106
        icol+=1;
107
        for label in ['System', 'Cluster']:
108
            def_list=strDefinitions
109
            frame=MyLabelFrame(self, bigFrame, label, def_list, None)
110
            frame.doLayout()
111
            frame.grid(row=irow, column=icol)
112
            self.frames.append(frame)
113
            icol+=1
114
        bigFrame.grid(row=0, columnspan=3)
115

    
116
        #--- Methods ---
117
        bigFrame = LabelFrame(self, text='Methods')
118
        irow=0; icol=0
119
        def_list=[QmxCalcDefinition()]
120
        frame=MyLabelFrame(self, bigFrame, 'Qmx', def_list, None)
121
        frame.doLayout()
122
        frame.grid(row=irow, column=icol)
123
        self.frames.append(frame)
124

    
125
        icol+=1
126
        for label in ['Low-Level', 'High-Level']:
127
            def_list=calcDefinitions
128
            frame=MyLabelFrame(self, bigFrame, label, def_list, None)
129
            frame.doLayout()
130
            frame.grid(row=irow, column=icol)
131
            self.frames.append(frame)
132
            icol+=1
133
        bigFrame.grid(row=1, columnspan=3)
134
        
135
        #--- General ---
136
        bigFrame = LabelFrame(self, text='General')
137
        extras=['method', 'method.options']
138
        frame=MyLabelFrame(self, bigFrame, 'Job', jobDefinitions, extras)
139
        frame.doLayout()
140
        frame.grid()
141
        self.frames.append(frame)
142
        bigFrame.grid(row=2, sticky=NW)
143

    
144
        frame=LabelFrame(self, text="PREVIEW")
145
        scrollbar=Scrollbar(frame, orient=VERTICAL)
146
        scrollbar.set(0.0, 1.0)
147
        self.text=Text(frame, yscrollcommand=scrollbar.set, width=60, heigh=10, state=DISABLED)
148
        self.text.grid(sticky=(N+W+S+E))
149
        scrollbar.config(command=self.text.yview)
150
        scrollbar.grid(row=0, column=1, sticky=NS)        
151
        frame.grid(row=2, column=1)
152

    
153
        frame = Frame(self)
154

    
155
        #image = PhotoImage(file="exit.gif")
156
        buttonExit=Button(frame, text="Quit!", command=self.quit)
157
        #buttonExit.image = image
158
        buttonExit.grid(row=1, column = 1, rowspan=3, sticky= W+E+N+S)
159
        
160
        buttonSave=Button(frame, text='save settings', state=DISABLED)
161
        buttonSave.grid(row=1, column = 2, sticky=(EW))
162

    
163
        buttonScript=Button(frame, text='Write qmx.in input file', command=self.writeScript)
164
        buttonScript.grid(row=2, column = 2, sticky=EW)
165

    
166
        buttonPython=Button(frame, text='Write qmx.py script file', command=self.writePython)
167
        buttonPython.grid(row=3, column = 2, sticky=EW)
168

    
169
        frame.grid(row=2, column=2, sticky=SE)
170

    
171
        #--- MainWindow ---
172
        self.grid(sticky=(W+N+S+E))
173
        self.preview()
174
        
175
    def quit(self):
176
        quit()
177
    
178
    def writePython(self):
179
        file = open("qmx.py","w")
180
        definitions=[]
181
        for frame in self.frames:
182
            definition = frame.getSelection()
183
            if definition is not None:
184
                definitions.append(definition)
185

    
186
        writeData(file, definitions)
187
        file.close()
188
        os.system("chmod u+x qmx.py")
189
        
190
        
191
    def writeScript(self):
192
        file = open("qmx.in","w")
193
        for frame in self.frames:
194
            definition = frame.getSelection()
195
            if definition is None:
196
                continue
197

    
198
            system = definition.system.lower()
199
            data = system+".program : "+definition.name.lower()+"\n"
200
            file.write(data)
201
            for key in definition.keywords:
202
                data=system+"."+key+" : "+definition.getValue(key)+"\n"
203
                file.write(data)
204
            file.write("\n")
205
        file.close()
206

    
207
    def preview(self):
208
        stream = StringIO.StringIO()
209
        definitions=[]
210
        for frame in self.frames:
211
            definition = frame.getSelection()
212
            if definition is not None:
213
                definitions.append(definition)
214

    
215
        writeData(stream, definitions)
216
        s = stream.getvalue()
217
        
218
        if self.text is not None:
219
            pos, end = self.text.yview()
220
            self.text.config(state=NORMAL)
221
            self.text.delete(1.0, END)
222
            self.text.insert(END, s)
223
            self.text.config(state=DISABLED)
224
            self.text.yview(MOVETO, pos)
225

    
226
#---------------------------------------------------------------------------------------------------
227
mainWnd=MainWindow(Tk())
228
mainWnd.mainloop()