a97e8496c6d13eb5996d43c6f1f9b575d694209d
1 # -*- coding: utf-8 -*-
3 Interface graphique pour l'analyse des fichiers de log minwii.
13 from os
.path
import join
as pjoin
14 from os
.path
import basename
15 from os
.path
import getsize
17 from minwii
.loganalyse
import LogFileAnalyser
18 from minwii
.config
import LOGS_DIR
19 from pprint
import pprint
21 class Application(Frame
) :
22 def __init__(self
, master
=None) :
23 Frame
.__init
__(self
, master
)
24 self
.configureStretching()
28 self
.currentFilePath
= ''
29 self
.resultsFrame
= None
31 if os
.path
.exists(LOGS_DIR
) :
32 self
.chooseDirDialog(dir=LOGS_DIR
)
34 def configureStretching(self
) :
35 top
=self
.winfo_toplevel()
36 top
.rowconfigure(0, weight
=1)
37 top
.columnconfigure(0, weight
=1)
39 self
.grid(sticky
=N
+S
+E
+W
, padx
=10, pady
=10)
40 self
.rowconfigure(0, weight
=1)
41 self
.columnconfigure(0, weight
=1)
43 def createWidgets(self
) :
44 # zone d'affichage des données'
45 self
.dataFrame
= df
= Frame(self
)
47 self
.identFrame
= Identification(df
)
48 self
.identFrame
.grid(sticky
=NW
)
51 self
.btnFrame
= bf
= Frame(self
)
52 bf
.grid(row
=1, column
=0, sticky
=W
+S
+E
)
53 bf
.rowconfigure(0, weight
=1)
55 bf
.columnconfigure(i
, weight
=1)
57 self
.chooseLogDir
= Button(bf
, text
="Parcourir…", command
=self
.chooseDirDialog
)
58 self
.chooseLogDir
.grid(row
=0, column
=0, sticky
=W
)
60 self
.nav
= Navbar(bf
, incCallback
=self
.loadLogFile
, decCallback
=self
.loadLogFile
)
62 self
.quitButton
= Button(bf
, text
='Terminer', command
=self
.quit
)
63 self
.quitButton
.grid(row
=0, column
=2, sticky
=E
)
65 def chooseDirDialog(self
, dir=None) :
67 self
.logDir
= tkFileDialog
.askdirectory(title
='Sélectionnez un dossier de fichiers de logs')
71 self
.logFiles
= glob(pjoin(self
.logDir
, '*.log'))
72 self
._cleanupJunkFiles
()
74 self
.logFiles
.reverse()
75 self
.dataFrame
.grid(row
=0, column
=0, sticky
=NW
)
76 self
.nav
.setSize(len(self
.logFiles
))
77 self
.nav
.grid(row
=0, column
=1)
78 self
.loadLogFile(self
.nav
)
80 def _cleanupJunkFiles(self
) :
83 f
= self
.logFiles
.pop()
89 lfa
= LogFileAnalyser(of
)
90 if lfa
.getLastEventTicks() is None :
102 def loadLogFile(self
, nav
) :
103 index
= nav
.index
- 1
104 filepath
= self
.logFiles
[index
]
105 self
.currentFilePath
= filepath
106 lfa
= LogFileAnalyser(self
.currentFilePath
)
107 self
.identFrame
.refresh(lfa
)
108 if self
.resultsFrame
:
109 self
.resultsFrame
.destroy()
110 self
.resultsFrame
= ResultsFrame(self
.dataFrame
)
111 self
.resultsFrame
.layResults(lfa
)
113 self
.resultsFrame
.grid()
116 class Navbar(Frame
) :
117 def __init__(self
, master
=None, size
=1, incCallback
=None, decCallback
=None) :
118 Frame
.__init
__(self
, master
)
119 self
.caption
= StringVar()
122 self
.incCallback
= incCallback
if incCallback
else lambda x
: None
123 self
.decCallback
= decCallback
if decCallback
else lambda x
: None
124 self
.caption
.set('%d / %d' % (self
.index
, self
.to
))
126 def createWidgets(self
) :
127 self
.backBtn
= Button(self
,
131 self
.backBtn
.grid(row
=0, column
=0)
133 self
.lbl
= Label(self
, textvariable
=self
.caption
)
134 self
.lbl
.grid(row
=0, column
=1)
136 self
.nextBtn
= Button(self
,
139 self
.nextBtn
.grid(row
=0, column
=2)
141 def refreshStates(self
) :
142 if self
.index
== self
.from_
:
143 self
.backBtn
.configure(state
=DISABLED
)
145 self
.backBtn
.configure(state
=NORMAL
)
147 if self
.index
< self
.to
:
148 self
.nextBtn
.configure(state
=NORMAL
)
150 self
.nextBtn
.configure(state
=DISABLED
)
152 self
.caption
.set('%d / %d' % (self
.index
, self
.to
))
156 self
.index
= self
.index
- 1
158 self
.decCallback(self
)
161 self
.index
= self
.index
+ 1
163 self
.incCallback(self
)
165 def setSize(self
, size
) :
172 class Identification(Frame
) :
173 def __init__(self
, master
=None) :
174 Frame
.__init
__(self
, master
)
175 self
.fileName
= StringVar()
176 self
.patientName
= StringVar()
179 #def setFileName(self, name) :
180 # self.fileName.set(name)
182 def refresh(self
, lfa
) :
183 filename
= basename(lfa
.logfile
.name
)
184 self
.fileName
.set(filename
)
185 metadata
= lfa
.getMetadata()
186 self
.patientName
.set(metadata
.get('PatientName', ''))
187 self
.commentsText
.delete(1.0, END
)
188 self
.commentsText
.insert(1.0, metadata
.get('Comments', ''))
190 def createWidgets(self
) :
191 fileLbl
= Label(self
, text
='Fichier :')
192 fileLbl
.grid(row
=0, column
=0, sticky
=E
)
194 fileNameLbl
= Label(self
, textvariable
=self
.fileName
)
195 fileNameLbl
.grid(row
=0, column
=1, sticky
=W
)
197 nameLbl
= Label(self
, text
='Patient :')
198 nameLbl
.grid(row
=1, column
=0, sticky
=E
)
200 self
.nameEntry
= Entry(self
, width
=40, textvariable
=self
.patientName
)
201 self
.nameEntry
.grid(row
=1, column
=1, sticky
=W
)
203 commentsLbl
= Label(self
, text
='Commentaires :')
204 commentsLbl
.grid(row
=2, column
=0, sticky
=E
)
206 self
.commentsText
= Text(self
, width
=40, height
=4, undo
=True, wrap
=WORD
)
207 self
.commentsText
.grid(row
=2, column
=1, sticky
=W
)
209 self
.saveBtn
= Button(self
, text
='Enregistrer', command
=self
.saveMetadata
)
210 self
.saveBtn
.grid(row
=3, column
=1, sticky
=E
)
212 def saveMetadata(self
):
213 app
= self
.master
.master
214 filepath
= app
.currentFilePath
215 lfa
= LogFileAnalyser(filepath
, mode
='r+')
216 patientName
= '%s\n' % self
.nameEntry
.get().replace('\n', ' ').strip()
217 comments
= '%s\n' % self
.commentsText
.get(1.0, END
).replace('\n', ' ').strip()
218 metadata
= (('PatientName', self
.nameEntry
.get()),
219 ('Comments', comments
))
220 lfa
.setMetadata(metadata
)
223 class ResultsFrame(Frame
) :
225 def layResults(self
, lfa
) :
226 results
= lfa
.analyse()
228 for i
, kv
in enumerate(results
) :
230 kl
= Label(self
, text
='%s :' % k
)
231 kl
.grid(row
=i
, column
=0, sticky
=E
)
233 vl
= Label(self
, text
=v
)
234 vl
.grid(row
=i
, column
=1, sticky
=W
)
236 msg
= Label(self
, text
="Pas de données exploitables.")
242 app
.master
.title("Analyseur des sessions MINWii")