From 9e3df066f59ce8d7de8868a66d5c37c0512a5f11 Mon Sep 17 00:00:00 2001 From: gimick Date: Sat, 4 Sep 2010 23:12:29 +0100 Subject: [PATCH] import: cli mode, don't display progress bar; tweak progress bar --- pyfpdb/GuiBulkImport.py | 4 +-- pyfpdb/fpdb.pyw | 2 +- pyfpdb/fpdb_import.py | 75 ++++++++++++++++++++++++++++++----------- 3 files changed, 59 insertions(+), 22 deletions(-) diff --git a/pyfpdb/GuiBulkImport.py b/pyfpdb/GuiBulkImport.py index e311c68e..d503c922 100755 --- a/pyfpdb/GuiBulkImport.py +++ b/pyfpdb/GuiBulkImport.py @@ -142,12 +142,12 @@ class GuiBulkImport(): """returns the vbox of this thread""" return self.vbox - def __init__(self, settings, config, parent, sql = None): + def __init__(self, settings, config, sql = None, parent = None): self.settings = settings self.config = config self.parent = parent - self.importer = fpdb_import.Importer(self, self.settings, config,parent, sql) + self.importer = fpdb_import.Importer(self, self.settings, config, sql, parent) self.vbox = gtk.VBox(False, 0) self.vbox.show() diff --git a/pyfpdb/fpdb.pyw b/pyfpdb/fpdb.pyw index 23bf9df7..af94f83a 100755 --- a/pyfpdb/fpdb.pyw +++ b/pyfpdb/fpdb.pyw @@ -999,7 +999,7 @@ class fpdb: def tab_bulk_import(self, widget, data=None): """opens a tab for bulk importing""" - new_import_thread = GuiBulkImport.GuiBulkImport(self.settings, self.config, self.window, self.sql) + new_import_thread = GuiBulkImport.GuiBulkImport(self.settings, self.config, self.sql, self.window) self.threads.append(new_import_thread) bulk_tab=new_import_thread.get_vbox() self.add_and_display_tab(bulk_tab, _("Bulk Import")) diff --git a/pyfpdb/fpdb_import.py b/pyfpdb/fpdb_import.py index 630c0d73..1b770ce5 100755 --- a/pyfpdb/fpdb_import.py +++ b/pyfpdb/fpdb_import.py @@ -70,7 +70,7 @@ else: psycopg2.extensions.register_type(psycopg2.extensions.UNICODE) class Importer: - def __init__(self, caller, settings, config, parent, sql = None): + def __init__(self, caller, settings, config, sql = None, parent = None): """Constructor""" self.settings = settings self.caller = caller @@ -302,15 +302,13 @@ class Importer: totpartial = 0 toterrors = 0 tottime = 0 - progresscount = 0 - progressgoal = len(self.filelist) - - ProgressDialog = ProgressBar(self.parent) + + #prepare progress popup window + ProgressDialog = ProgressBar(len(self.filelist), self.parent) for file in self.filelist: - progresscount += 1 - ProgressDialog.progress_update(progresscount,progressgoal) + ProgressDialog.progress_update() (stored, duplicates, partial, errors, ttime) = self.import_file_dict(db, file ,self.filelist[file][0], self.filelist[file][1], q) @@ -319,11 +317,12 @@ class Importer: totpartial += partial toterrors += errors + del ProgressDialog + for i in xrange( self.settings['threads'] ): print _("sending finish message queue length ="), q.qsize() db.send_finish_msg(q) - del ProgressDialog return (totstored, totdups, totpartial, toterrors) # end def importFiles @@ -543,25 +542,63 @@ class Importer: class ProgressBar: + """ + Popup window to show progress + + Init method sets up total number of expected iterations + If no parent is passed to init, command line + mode assumed, and does not create a progress bar + """ + def __del__(self): - self.progress.destroy() - - def progress_update(self, fraction, sum): - - progresspercent = float(fraction) / (float(sum) + 1.0) - self.pbar.set_fraction(progresspercent) - self.pbar.set_text(str(fraction) + " / " + str(sum)) + if self.parent: + self.progress.destroy() - def __init__(self, parent): + def progress_update(self): + + if not self.parent: + #nothing to do + return + + self.fraction += 1 + #update sum if fraction exceeds expected total number of iterations + if self.fraction > self.sum: + sum = self.fraction + + #progress bar total set to 1 plus the number of items,to prevent it + #reaching 100% prior to processing fully completing + + progress_percent = float(self.fraction) / (float(self.sum) + 1.0) + progress_text = (self.title + " " + + str(self.fraction) + " / " + str(self.sum)) + + self.pbar.set_fraction(progress_percent) + self.pbar.set_text(progress_text) + + + def __init__(self, sum, parent): + + self.parent = parent + if not self.parent: + #no parent is passed, assume this is being run from the + #command line, so return immediately + return + + self.fraction = 0 + self.sum = sum + self.title = _("Importing") + self.progress = gtk.Window(gtk.WINDOW_TOPLEVEL) self.progress.set_resizable(False) self.progress.set_modal(True) - self.progress.set_transient_for(parent) - self.progress.set_decorated(False) - + self.progress.set_transient_for(self.parent) + self.progress.set_decorated(True) + self.progress.set_deletable(False) + self.progress.set_title(self.title) + vbox = gtk.VBox(False, 5) vbox.set_border_width(10) self.progress.add(vbox)