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