Mercurial > pyarq-presupuestos
comparison Gtk/importFiebdc.py @ 1:2ac1551ad2ab version 0.0.0
add code
author | Miguel Ángel Bárcena Rodríguez <miguelangel@obraencurso.es> |
---|---|
date | Sun, 31 Oct 2010 20:07:33 +0100 |
parents | |
children | 4e976a99efb9 |
comparison
equal
deleted
inserted
replaced
0:a1703c4f2990 | 1:2ac1551ad2ab |
---|---|
1 #!/usr/bin/python | |
2 # -*- coding: utf-8 -*- | |
3 ## File importFiebdc.py | |
4 ## This file is part of pyArq-Presupuestos. | |
5 ## | |
6 ## Copyright (C) 2010 Miguel Ángel Bárcena Rodríguez | |
7 ## <miguelangel@obraencurso.es> | |
8 ## | |
9 ## pyArq-Presupuestos is free software: you can redistribute it and/or modify | |
10 ## it under the terms of the GNU General Public License as published by | |
11 ## the Free Software Foundation, either version 3 of the License, or | |
12 ## (at your option) any later version. | |
13 ## | |
14 ## pyArq-Presupuestos is distributed in the hope that it will be useful, | |
15 ## but WITHOUT ANY WARRANTY; without even the implied warranty of | |
16 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
17 ## GNU General Public License for more details. | |
18 ## | |
19 ## You should have received a copy of the GNU General Public License | |
20 ## along with this program. If not, see <http://www.gnu.org/licenses/>. | |
21 | |
22 # Modules | |
23 import sys | |
24 import time | |
25 import os.path | |
26 import pygtk | |
27 pygtk.require('2.0') | |
28 import gtk | |
29 import gobject | |
30 import threading | |
31 gobject.threads_init() | |
32 | |
33 from Generic import utils | |
34 from Generic import globals | |
35 import gui | |
36 | |
37 class FileSelectionWindow(object): | |
38 """importFiebdc.FileSelectionWindow: | |
39 | |
40 Description: | |
41 Class to show the selection file window | |
42 Constructor: | |
43 importFiebdc.FileSelectionWindow(mainWindow, readFileMethod, | |
44 budget, arg_List, cancelMethod) | |
45 Ancestry: | |
46 +-- object | |
47 +-- FileSelectionWindow | |
48 Atributes: | |
49 "__mainWindow": gui.MainWindow object | |
50 "__readFileMethod": Method to read the selected file | |
51 "__budget": Budget object | |
52 "__filename": "file" | |
53 "__cancelMethod": Method to cancel the read method | |
54 "__file": The selected file | |
55 "__window": The selection file window | |
56 Methods: | |
57 __init__(self, mainWindow, readFileMethod, budget | |
58 arg_List, cancelMethod) | |
59 destroy(self, widget) | |
60 main(self) | |
61 _launchProgressWindow(self, _file) | |
62 _openFile(self, w) | |
63 _openFile2(Self, filename) | |
64 """ | |
65 | |
66 def __init__(self, mainWindow, readFileMethod, budget, filename, | |
67 cancelMethod): | |
68 """def __init__(self, mainWindow, readFileMethod, budget, | |
69 filename, cancelMethod) | |
70 | |
71 mainWindow: gui.MainWindow object | |
72 readFileMethod: Method to read the selected file | |
73 budget: base.Budget object | |
74 filename: "file" | |
75 cancelMethod: Method to cancel the read method | |
76 Sets the init atributes, creates the file selection window | |
77 Connects the events: | |
78 * clicked ok button: _openFile | |
79 * clicked cancel button: destroy window | |
80 * destroy event: _destroy | |
81 """ | |
82 # TODO: Add file filter | |
83 self.__mainWindow = mainWindow | |
84 self.__readFileMethod = readFileMethod | |
85 self.__budget = budget | |
86 self.__filename = filename | |
87 self.__cancelMethod = cancelMethod | |
88 self.__file = None | |
89 self.__window = gtk.FileChooserDialog(title=_("Open File"), | |
90 action=gtk.FILE_CHOOSER_ACTION_OPEN, | |
91 buttons=(gtk.STOCK_CANCEL,gtk.RESPONSE_CANCEL, | |
92 gtk.STOCK_OPEN,gtk.RESPONSE_OK)) | |
93 self.__window.set_default_response(gtk.RESPONSE_OK) | |
94 self.__window.set_filename(globals.getHomePath("BUDGET")) | |
95 if self.__window.run() == gtk.RESPONSE_OK: | |
96 self._openFile(self.__window.get_filename()) | |
97 self.__window.destroy() | |
98 | |
99 def _launchProgressWindow(self, file): | |
100 """def _launchProgressWindow(self, file) | |
101 | |
102 Launch the progress window | |
103 """ | |
104 self.__filename = file | |
105 | |
106 _emptyPage = gui.EmptyPage(self.__mainWindow, self.__readFileMethod, | |
107 self.__budget, self.__filename, | |
108 self.__cancelMethod) | |
109 self.__mainWindow.getNotebook().append_page(_emptyPage.widget, | |
110 _emptyPage.title) | |
111 self.__mainWindow.getPageList().append(_emptyPage) | |
112 _emptyPage.run() | |
113 | |
114 def _launchProgressTab(self, file): | |
115 """def _launchProgressTab(self, file) | |
116 | |
117 Launch the progress window | |
118 """ | |
119 self.__filename = file | |
120 print "abriendo fichero", self.__filename | |
121 | |
122 def _openFile(self, filename): | |
123 """def _openFile(self, filename) | |
124 | |
125 filename: the filename to open | |
126 If the selected file has a bc3 extension | |
127 _launchProgressWindow is called | |
128 """ | |
129 _file = filename | |
130 if sys.getfilesystemencoding(): | |
131 _file = _file.decode(sys.getfilesystemencoding()) | |
132 #-# | |
133 _file = _file.encode("utf-8") | |
134 #-# | |
135 self.__file = _file | |
136 _filename = os.path.basename(self.__file) | |
137 _filename_ext = _filename.split(".")[-1] | |
138 if _filename_ext != "bc3" and _filename_ext != "BC3": | |
139 print _("The file must have 'bc3' extension") | |
140 else: | |
141 self.__window.destroy() | |
142 if self.__file: | |
143 # TODO: the file exits? is it not binary?, can it be readed? | |
144 self._launchProgressWindow(self.__file) | |
145 | |
146 | |
147 | |
148 class ProgressWindow(object): | |
149 """importFiebdc.ProgressWindow: | |
150 | |
151 Description: | |
152 Class to show the progress window and launch a thread to read | |
153 the database file | |
154 Constructor: | |
155 importFiebdc.ProgressWindow(mainWindow, readFileMethod, budget, | |
156 filename, cancelMethod) | |
157 Ancestry: | |
158 +-- object | |
159 +-- ProgressWindow | |
160 Atributes: | |
161 "__mainWindow": | |
162 "__readFileMethod": | |
163 "__budget": | |
164 "__filename": | |
165 "__cancelMethod": | |
166 "__children": Thread instance | |
167 "__cancel": list with boolean values | |
168 "__window": progress window widget | |
169 "__progress_bar": probres bar widget | |
170 "__label": label widget | |
171 Methods: | |
172 closeWindow(self) | |
173 __init__(self, mainWindow, readFileMethod, budget | |
174 filename, cancelMethod) | |
175 closeWindow(self) | |
176 main(self) | |
177 _autoClose(self) | |
178 _updateLabel(self, _time) | |
179 _updateProgressBar(self) | |
180 _launchTimeout(self) | |
181 _launchChildren(self, mainWindow, readFileMethod, budget | |
182 filename, cancelMethod) | |
183 _cancelChildren(self,widget=None) | |
184 _destroy(self, widget) | |
185 _delete_event(self, widget, event) | |
186 | |
187 """ | |
188 def __init__(self, mainWindow, readFileMethod, budget, | |
189 filename, cancelMethod): | |
190 """def __init__(self, mainWindow, readFileMethod, budget | |
191 filename, cancelMethod) | |
192 | |
193 mainWindow: gui.MainWindow object | |
194 readFileMethod: Method to read the selected file | |
195 budget: base.Budget object | |
196 filenamer: "file" | |
197 cancelMethod: Method to cancel the read method | |
198 Sets the init atributes, creates the progress window | |
199 Connects the events: | |
200 * destroy signal: self._destroy | |
201 * delete_event signal: self._delete_event | |
202 * clicked cancel button: self._cancelChildren | |
203 """ | |
204 self.__mainWindow = mainWindow | |
205 self.__readFileMethod = readFileMethod | |
206 self.__filename = filename | |
207 self.__budget = budget | |
208 self.__cancelMethod = cancelMethod | |
209 self.__children = None | |
210 self.__cancel = [False, False] | |
211 self.__window = gtk.Window() | |
212 self.__window.set_title(_("Loading file ...")) | |
213 self.__window.connect("destroy", self._destroy) | |
214 self.__window.connect("delete_event", self._delete_event) | |
215 _Vbox1 = gtk.VBox(False, 0) | |
216 self.__window.add(_Vbox1) | |
217 _align = gtk.Alignment(0.5, 0.5, 0, 0) | |
218 _align.show() | |
219 _Vbox1.pack_start(_align, False, False, 5) | |
220 self.__progress_bar = gtk.ProgressBar() | |
221 self.__progress_bar.show() | |
222 _align.add(self.__progress_bar) | |
223 self.__label = gtk.Label() | |
224 self.__label.set_text(_("Time: 0s")) | |
225 self.__label.show() | |
226 _Vbox1.add(self.__label) | |
227 self.__throbber = gtk.Image() | |
228 self.__throbber.set_from_file(globals.getAppPath("THROBBER-ICON")) | |
229 _Vbox1.add(self.__throbber) | |
230 self.__throbber.show() | |
231 self.__animation = gtk.gdk.PixbufAnimation(globals.getAppPath("THROBBER-GIF")) | |
232 _pixbuf = self.__throbber.get_pixbuf() | |
233 self.__throbber.set_from_animation(self.__animation) | |
234 _Hbox1 = gtk.HBox(False, 0) | |
235 _Vbox1.add(_Hbox1) | |
236 _button1 = gtk.Button(_("Cancel")) | |
237 _button1.connect("clicked", self._cancelChildren) | |
238 _button1.show() | |
239 _Hbox1.pack_start(_button1, True, True, 0) | |
240 _Hbox1.show() | |
241 _Vbox1.show() | |
242 | |
243 def main(self): | |
244 """def main(self) | |
245 | |
246 Launch the thread | |
247 Launch the timeouts | |
248 Shows window and starts the GTK+ event processing loop. | |
249 """ | |
250 | |
251 self._launchChildren() | |
252 self._launchTimeout() | |
253 self.__window.show() | |
254 gtk.main() | |
255 | |
256 def closeWindow(self): | |
257 """def closeWindow(self) | |
258 | |
259 Sets the __children atribute to None | |
260 This causes that the timiouts is ended and then the window is | |
261 closed. | |
262 This method is called from thread when it is finished | |
263 """ | |
264 self.__children = None | |
265 | |
266 def _launchTimeout(self): | |
267 """def _launchTimeout(self) | |
268 | |
269 Launch the timeouts: | |
270 1- update progress bar | |
271 2- update time labal | |
272 3- If the other timetouts are stoped the window is closed | |
273 """ | |
274 gobject.timeout_add(100, self._updateProgressBar) | |
275 gobject.timeout_add(1000, self._updateLabel, time.time()) | |
276 self.__cancel = [False, False] | |
277 gobject.timeout_add(1000, self._autoClose) | |
278 | |
279 def _updateProgressBar(self): | |
280 """def _updateProgressBar(self) | |
281 | |
282 update progress bar in a timeout | |
283 If the thread end or is canceled the timeout is stoped | |
284 """ | |
285 if self.__children is None or self.__children.isCanceled() == True: | |
286 self.__cancel[0] = True | |
287 return False | |
288 else: | |
289 self.__progress_bar.pulse() | |
290 return True | |
291 | |
292 def _updateLabel(self, _time): | |
293 """def _updateProgressBar(self) | |
294 | |
295 update time label in a timeout | |
296 If the thread end or is canceled the timeout is stoped | |
297 """ | |
298 if self.__children is None or self.__children.isCanceled() == True: | |
299 self.__cancel[1] = True | |
300 return False | |
301 else: | |
302 _time = time.time() - _time | |
303 self.__label.set_text(utils.mapping(_("Time: $1"), | |
304 ("%.0f" %_time,))) | |
305 return True | |
306 | |
307 def _autoClose(self): | |
308 """def _updateProgressBar(self) | |
309 | |
310 If the time label and progress bar timeouts are stoped the window is | |
311 closed and ist tiemeout is stoped | |
312 """ | |
313 if self.__cancel == [ True, True ]: | |
314 self.__window.destroy() | |
315 return False | |
316 else: | |
317 return True | |
318 | |
319 def _launchChildren(self): | |
320 """_launchChildren(self) | |
321 | |
322 Launch the thread to read the file | |
323 """ | |
324 if self.__children is None: | |
325 self.__children = Thread(self, self.__mainWindow, | |
326 self.__readFileMethod, self.__budget, self.__filename, | |
327 self.__cancelMethod) | |
328 self.__children.start() | |
329 | |
330 def _cancelChildren(self,widget=None): | |
331 """_cancelChildren(self,widget=None) | |
332 | |
333 Method connected to "clicked" singal of cancel button | |
334 Stops the thread and close the window | |
335 """ | |
336 if self.__children: | |
337 self.__children.cancel() | |
338 self.__window.destroy() | |
339 | |
340 def _delete_event(self, widget, event): | |
341 """_delete_event(self, widget, event) | |
342 | |
343 widget: the widget where the event is emitted from | |
344 event: the "gtk.gdk.Event" | |
345 Method connected to "delete_event" signal of window widget | |
346 This signal is emitted when a user press the close titlebar button. | |
347 Stops the thread if exits. | |
348 Returns True so the signal "destroy" is emitted. | |
349 """ | |
350 if self.__children: | |
351 self._cancelChildren() | |
352 return True | |
353 | |
354 def _destroy(self, widget): | |
355 """_destroy(self, widget) | |
356 | |
357 widget: the widget where the event is emitted from | |
358 Method connected to "destroy" signal of window widget | |
359 This signal is emited when the method connected to "delete_event" | |
360 signal returns True or when the program call the destroy() method of | |
361 the gtk.Window widget. | |
362 The window is closed and the GTK+ event processing loop is ended. | |
363 """ | |
364 gtk.main_quit() | |
365 | |
366 class Thread(threading.Thread): | |
367 """importFiebdc.Thread: | |
368 | |
369 Description: | |
370 Thread class to read a file without freeze the gui | |
371 Constructor: | |
372 importFiebdc.Thread(page, mainWindow, | |
373 readFileMethod, arg_tuple, cancelMethod) | |
374 Ancestry: | |
375 +--threading.Thread | |
376 +-- importFiebdc.Thread | |
377 Atributes: | |
378 "__page": The page instanca that launch the thread | |
379 "__mainWindow": gui.MainWindow instance | |
380 "__readFileMethod": Method to read the selected file | |
381 "__arg_tuple": ("file", base.Budget()) | |
382 "__cancelMethod": Method to cancel the read method | |
383 "__cancel": Boolean value, True: the thread is stoped | |
384 Methods: | |
385 __init__(self, page, mainWindow, | |
386 readFileMethod, arg_tuple, cancelMethod) | |
387 run(self) | |
388 cancel(self) | |
389 isCanceled(self) | |
390 """ | |
391 | |
392 def __init__(self, page, mainWindow, | |
393 readFileMethod, budget, filename, cancelMethod): | |
394 """def __init__(self, page, mainWindow, | |
395 readFileMethod, budget, filename, cancelMethod) | |
396 | |
397 page: The page instance that launch the thread | |
398 mainWindow: gui.Mainwindow object | |
399 readFileMethod: Method to read the selected file | |
400 budget: base.Budget object | |
401 filename: "file" | |
402 cancelMethod: Method to cancel the read method | |
403 Sets the instance atributes. | |
404 """ | |
405 super(Thread, self).__init__() | |
406 self.__page = page | |
407 self.__mainWindow = mainWindow | |
408 self.__readFileMethod = readFileMethod | |
409 self.__budget = budget | |
410 self.__filename = filename | |
411 self.__cancelMethod = cancelMethod | |
412 self.__cancel = False | |
413 | |
414 def run(self): | |
415 """run(self) | |
416 | |
417 | |
418 """ | |
419 _result = self.__readFileMethod(self.__budget, self.__filename, | |
420 self.__page) | |
421 if _result is None: | |
422 self.__page.threadCanceled() | |
423 else: | |
424 _mainWindow = self.__mainWindow | |
425 _mainWindow._addBudget(_result) | |
426 self.__page.closeWindow() | |
427 self.clear() | |
428 | |
429 def cancel(self): | |
430 """cancel(self) | |
431 | |
432 Sets the "__cancel" atribute to True and call "__cancelMethod" to stop | |
433 read the file | |
434 """ | |
435 self.__cancel = True | |
436 self.__cancelMethod() | |
437 | |
438 def isCanceled(self): | |
439 """isCanceled(self) | |
440 | |
441 Return True if the thread has been canceled | |
442 """ | |
443 return self.__cancel | |
444 | |
445 def clear(self): | |
446 del self.__page | |
447 del self.__mainWindow | |
448 del self.__readFileMethod | |
449 del self.__budget | |
450 del self.__filename | |
451 del self.__cancelMethod | |
452 del self.__cancel | |
453 | |
454 | |
455 | |
456 |