a632d601209b57e0b1d7262239543615ffc44187
[minwii.git] / src / minwii / logapp.py
1 # -*- coding: utf-8 -*-
2 """
3 Interface graphique pour l'analyse des fichiers de log minwii.
4
5 $Id$
6 $URL$
7 """
8
9 from Tkinter import *
10 import tkFileDialog
11 from glob import glob
12 from os.path import join as pjoin
13 from os.path import basename
14 from loganalyse import LogFileAnalyser
15 from pprint import pprint
16
17 class Application(Frame) :
18 def __init__(self, master=None) :
19 Frame.__init__(self, master)
20 self.configureStretching()
21 self.createWidgets()
22 self.logDir = ''
23 self.logFiles = []
24 self.resultsFrame = None
25
26 def configureStretching(self) :
27 top=self.winfo_toplevel()
28 top.rowconfigure(0, weight=1)
29 top.columnconfigure(0, weight=1)
30
31 self.grid(sticky=N+S+E+W, padx=10, pady=10)
32 self.rowconfigure(0, weight=1)
33 self.columnconfigure(0, weight=1)
34
35 def createWidgets(self) :
36 # zone d'affichage des données'
37 self.dataFrame = df = Frame(self)
38 #df.grid(sticky=NW)
39
40 self.identFrame = Identification(df)
41 self.identFrame.grid(sticky=NW)
42 self.nav = Navbar(df, incCallback=self.loadLogFile, decCallback=self.loadLogFile)
43 # self.nav.grid()
44
45
46 # barre de boutons
47 self.btnFrame = bf = Frame(self)
48 bf.grid(row=1, column=0, sticky=W+S+E)
49 bf.rowconfigure(0, weight=1)
50 bf.columnconfigure(0, weight=1)
51 bf.columnconfigure(1, weight=1)
52
53
54 self.chooseLogDir = Button(bf, text="Parcourir…", command=self.openFileDialog)
55 self.chooseLogDir.grid(row=0, column=0, sticky=W)
56
57 self.nav = Navbar(bf, incCallback=self.loadLogFile, decCallback=self.loadLogFile)
58 #self.nav.grid(row=0, column=1)
59
60 self.quitButton = Button(bf, text='Terminer', command=self.quit)
61 self.quitButton.grid(row=0, column=2, sticky=E)
62
63 def openFileDialog(self) :
64 self.logDir = tkFileDialog.askdirectory()
65 if self.logDir :
66 self.logFiles = glob(pjoin(self.logDir, '*.log'))
67 self.logFiles.sort()
68 self.dataFrame.grid(row=0, column=0, sticky=NW)
69 self.nav.setSize(len(self.logFiles))
70 self.nav.grid(row=0, column=1)
71 self.loadLogFile(self.nav)
72
73 def loadLogFile(self, nav) :
74 index = nav.index - 1
75 filepath = self.logFiles[index]
76 filename = basename(filepath)
77 self.identFrame.setFileName(filename)
78 if self.resultsFrame :
79 self.resultsFrame.destroy()
80 self.resultsFrame = ResultsFrame(self.dataFrame, filepath)
81 self.resultsFrame.layResults()
82 self.resultsFrame.grid()
83
84
85 class Navbar(Frame) :
86 def __init__(self, master=None, size=1, incCallback=None, decCallback=None) :
87 Frame.__init__(self, master)
88 self.caption = StringVar()
89 self.createWidgets()
90 self.setSize(size)
91 self.incCallback = incCallback if incCallback else lambda x : None
92 self.decCallback = decCallback if decCallback else lambda x : None
93 self.caption.set('%d / %d' % (self.index, self.to))
94
95 def createWidgets(self) :
96 self.backBtn = Button(self,
97 text='◀',
98 command = self.dec
99 )
100 self.backBtn.grid(row=0, column=0)
101
102 self.lbl = Label(self, textvariable=self.caption)
103 self.lbl.grid(row=0, column=1)
104
105 self.nextBtn = Button(self,
106 text='▶',
107 command = self.inc)
108 self.nextBtn.grid(row=0, column=2)
109
110 def refreshStates(self) :
111 if self.index == self.from_ :
112 self.backBtn.configure(state=DISABLED)
113 else :
114 self.backBtn.configure(state=NORMAL)
115
116 if self.index < self.to :
117 self.nextBtn.configure(state=NORMAL)
118 else :
119 self.nextBtn.configure(state=DISABLED)
120
121 self.caption.set('%d / %d' % (self.index, self.to))
122
123
124 def dec(self) :
125 self.index = self.index - 1
126 self.refreshStates()
127 self.decCallback(self)
128
129 def inc(self) :
130 self.index = self.index + 1
131 self.refreshStates()
132 self.incCallback(self)
133
134 def setSize(self, size) :
135 self.from_ = 1
136 self.to = size
137 self.index = 1
138 self.refreshStates()
139
140
141 class Identification(Frame) :
142 def __init__(self, master=None) :
143 Frame.__init__(self, master)
144 self.fileName = StringVar()
145 self.createWidgets()
146
147 def setFileName(self, name) :
148 self.fileName.set(name)
149
150 def createWidgets(self) :
151 fileLbl = Label(self, text='Fichier :')
152 fileLbl.grid(row=0, column=0, sticky=E)
153
154 fileNameLbl = Label(self, textvariable=self.fileName)
155 fileNameLbl.grid(row=0, column=1, sticky=W)
156
157 nameLbl = Label(self, text='Patient :')
158 nameLbl.grid(row=1, column=0, sticky=E)
159
160 self.nameEntry = Entry(self, width=40)
161 self.nameEntry.grid(row=1, column=1, sticky=W)
162
163 commentsLbl = Label(self, text='Commentaires :')
164 commentsLbl.grid(row=2, column=0, sticky=E)
165
166 self.commentsText = Text(self, width=40, height=4, undo=True, wrap=WORD)
167 self.commentsText.grid(row=2, column=1, sticky=W)
168
169 class ResultsFrame(Frame) :
170 def __init__(self, master, logFilePath) :
171 Frame.__init__(self, master)
172 self.logFilePath = logFilePath
173
174 def layResults(self) :
175 lfa = LogFileAnalyser(self.logFilePath)
176 results = lfa.analyse()
177 if results :
178 for i, kv in enumerate(results.items()) :
179 k, v = kv
180 kl = Label(self, text='%s :' % k)
181 kl.grid(row=i, column=0, sticky=E)
182
183 vl = Label(self, text=v)
184 vl.grid(row=i, column=1, sticky=W)
185 else :
186 msg = Label(self, text="Pas de données exploitables.")
187 msg.grid()
188
189
190
191 app = Application()
192 app.master.title("Analyseur des sessions MINWii")
193 app.mainloop()