4c680ba3a23a902f82c932eb476e181613505f5c
[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
25 def configureStretching(self) :
26 top=self.winfo_toplevel()
27 top.rowconfigure(0, weight=1)
28 top.columnconfigure(0, weight=1)
29
30 self.grid(sticky=N+S+E+W, padx=10, pady=10)
31 self.rowconfigure(0, weight=1)
32 self.columnconfigure(0, weight=1)
33
34 def createWidgets(self) :
35 # zone d'affichage des données'
36 self.dataFrame = df = Frame(self)
37 #df.grid(sticky=NW)
38
39 self.identFrame = Identification(df)
40 self.identFrame.grid(sticky=NW)
41 self.nav = Navbar(df, incCallback=self.loadLogFile, decCallback=self.loadLogFile)
42 self.nav.grid()
43
44
45 # barre de boutons
46 self.btnFrame = bf = Frame(self)
47 bf.grid(row=1, column=0, sticky=W+S+E)
48 bf.rowconfigure(0, weight=1)
49 bf.columnconfigure(0, weight=1)
50 bf.columnconfigure(1, weight=1)
51
52
53 self.chooseLogDir = Button(bf, text="Parcourir…", command=self.openFileDialog)
54 self.chooseLogDir.grid(row=0, column=0, sticky=W)
55
56 self.quitButton = Button(bf, text='Terminer', command=self.quit)
57 self.quitButton.grid(row=0, column=1, sticky=E)
58
59 def openFileDialog(self) :
60 self.logDir = tkFileDialog.askdirectory()
61 if self.logDir :
62 self.logFiles = glob(pjoin(self.logDir, '*.log'))
63 self.logFiles.sort()
64 self.dataFrame.grid(row=0, column=0, sticky=NW)
65 self.nav.setSize(len(self.logFiles))
66 self.loadLogFile(self.nav)
67
68 def loadLogFile(self, nav) :
69 index = nav.index - 1
70 filepath = self.logFiles[index]
71 filename = basename(filepath)
72 self.identFrame.setFileName(filename)
73 rf = ResultsFrame(self.dataFrame, filepath)
74 rf.layResults()
75 rf.grid()
76
77
78 class Navbar(Frame) :
79 def __init__(self, master=None, size=1, incCallback=None, decCallback=None) :
80 Frame.__init__(self, master)
81 self.caption = StringVar()
82 self.createWidgets()
83 self.setSize(size)
84 self.incCallback = incCallback if incCallback else lambda x : None
85 self.decCallback = decCallback if decCallback else lambda x : None
86 self.grid()
87 self.caption.set('%d / %d' % (self.index, self.to))
88
89 def createWidgets(self) :
90 self.backBtn = Button(self,
91 text='◀',
92 command = self.dec
93 )
94 self.backBtn.grid(row=0, column=0)
95
96 self.lbl = Label(self, textvariable=self.caption)
97 self.lbl.grid(row=0, column=1)
98
99 self.nextBtn = Button(self,
100 text='▶',
101 command = self.inc)
102 self.nextBtn.grid(row=0, column=2)
103
104 def refreshStates(self) :
105 if self.index == self.from_ :
106 self.backBtn.configure(state=DISABLED)
107 else :
108 self.backBtn.configure(state=NORMAL)
109
110 if self.index < self.to :
111 self.nextBtn.configure(state=NORMAL)
112 else :
113 self.nextBtn.configure(state=DISABLED)
114
115 self.caption.set('%d / %d' % (self.index, self.to))
116
117
118 def dec(self) :
119 self.index = self.index - 1
120 self.refreshStates()
121 self.decCallback(self)
122
123 def inc(self) :
124 self.index = self.index + 1
125 self.refreshStates()
126 self.incCallback(self)
127
128 def setSize(self, size) :
129 self.from_ = 1
130 self.to = size
131 self.index = 1
132 self.refreshStates()
133
134
135 class Identification(Frame) :
136 def __init__(self, master=None) :
137 Frame.__init__(self, master)
138 self.fileName = StringVar()
139 self.createWidgets()
140
141 def setFileName(self, name) :
142 self.fileName.set(name)
143
144 def createWidgets(self) :
145 fileLbl = Label(self, text='Fichier :')
146 fileLbl.grid(row=0, column=0, sticky=E)
147
148 fileNameLbl = Label(self, textvariable=self.fileName)
149 fileNameLbl.grid(row=0, column=1, sticky=W)
150
151 nameLbl = Label(self, text='Patient :')
152 nameLbl.grid(row=1, column=0, sticky=E)
153
154 self.nameEntry = Entry(self, width=40)
155 self.nameEntry.grid(row=1, column=1, sticky=W)
156
157 commentsLbl = Label(self, text='Commentaires :')
158 commentsLbl.grid(row=2, column=0, sticky=E)
159
160 self.commentsText = Text(self, width=40, height=4, undo=True, wrap=WORD)
161 self.commentsText.grid(row=2, column=1, sticky=W)
162
163 class ResultsFrame(Frame) :
164 def __init__(self, master, logFilePath) :
165 Frame.__init__(self, master)
166 self.logFilePath = logFilePath
167
168 def layResults(self) :
169 lfa = LogFileAnalyser(self.logFilePath)
170 results = lfa.analyse()
171 pprint(results)
172 rawPrint = Label(self, text=str(results))
173 rawPrint.grid()
174
175
176 app = Application()
177 app.master.title("Analyseur des sessions MINWii")
178 app.mainloop()