From ae59d17a60a8771815c9c62766e46ac9d9f64224 Mon Sep 17 00:00:00 2001 From: Alex Roitman Date: Thu, 11 May 2006 05:11:47 +0000 Subject: [PATCH] In .: * src/GrampsDb/_DbUtils.py (db_copy): Use UpdateCallback class. * src/BasicUtils.py: Add module. * src/Makefile.am (gdir_PYTHON): Add new file. In po: 2006-05-10 Alex Roitman * POTFILES.in: Add new file. svn: r6617 --- gramps2/ChangeLog | 3 ++ gramps2/po/ChangeLog | 3 ++ gramps2/po/POTFILES.in | 1 + gramps2/src/BasicUtils.py | 83 ++++++++++++++++++++++++++++++++ gramps2/src/GrampsDb/_DbUtils.py | 53 ++++++++------------ gramps2/src/Makefile.am | 3 +- 6 files changed, 112 insertions(+), 34 deletions(-) create mode 100644 gramps2/src/BasicUtils.py diff --git a/gramps2/ChangeLog b/gramps2/ChangeLog index 3ec8be5b8..f59ba1573 100644 --- a/gramps2/ChangeLog +++ b/gramps2/ChangeLog @@ -1,4 +1,7 @@ 2006-05-10 Alex Roitman + * src/GrampsDb/_DbUtils.py (db_copy): Use UpdateCallback class. + * src/BasicUtils.py: Add module. + * src/Makefile.am (gdir_PYTHON): Add new file. * src/DataViews/_MediaView.py (edit): Handle the exception. * src/plugins/BookReport.py (__init__): Fix url. * src/plugins/ChangeTypes.py (run_tool): Disable/enable signals. diff --git a/gramps2/po/ChangeLog b/gramps2/po/ChangeLog index 7f3026993..770c159a0 100644 --- a/gramps2/po/ChangeLog +++ b/gramps2/po/ChangeLog @@ -1,3 +1,6 @@ +2006-05-10 Alex Roitman + * POTFILES.in: Add new file. + 2006-05-07 Alex Roitman * POTFILES.in: Add missing file. diff --git a/gramps2/po/POTFILES.in b/gramps2/po/POTFILES.in index 2da8e61a9..5c102b25b 100644 --- a/gramps2/po/POTFILES.in +++ b/gramps2/po/POTFILES.in @@ -9,6 +9,7 @@ src/ArgHandler.py src/Assistant.py src/AutoComp.py src/BaseDoc.py +src/BasicUtils.py src/Bookmarks.py src/ColumnOrder.py src/const.py diff --git a/gramps2/src/BasicUtils.py b/gramps2/src/BasicUtils.py new file mode 100644 index 000000000..661dabfa7 --- /dev/null +++ b/gramps2/src/BasicUtils.py @@ -0,0 +1,83 @@ +# +# Gramps - a GTK+/GNOME based genealogy program +# +# Copyright (C) 2004-2006 Donald N. Allingham +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +# + +# $Id: __init__.py 6086 2006-03-06 03:54:58Z dallingham $ + +""" +A set of basic utilities that everything in GRAMPS can depend upon. + +The goal is to have this module not depend on any other gramps module. +That way, e.g. database classes can safely depend on that without +other GRAMPS baggage. +""" + +#------------------------------------------------------------------------- +# +# Python modules +# +#------------------------------------------------------------------------- +import time + +#------------------------------------------------------------------------- +# +# Callback updater +# +#------------------------------------------------------------------------- +class UpdateCallback: + """ + Basic class providing way of calling the callback to update + things during lenghty operations. + """ + + def __init__(self,callback,interval=1): + """ + @param callback: a function with one arg to execute every so often + @type callback: function + @param interval: number of seconds at most between the updates + @type callback: int + """ + if '__call__' in dir(callback): # callback is really callable + self.update = self.update_real + self.callback = callback + + self.count = 0 + self.oldval = 0 + self.oldtime = 0 + self.total = self.get_total() + self.interval = interval + else: + self.update = self.update_empty + + def get_total(self): + assert False, "Needs to be defined in the derived class" + + def update_empty(self): + pass + + def update_real(self): + self.count += 1 + newval = int(100.0*self.count/self.total) + newtime = time.time() + time_has_come = self.interval and (newtime-self.oldtime>self.interval) + value_changed = newval!=self.oldval + if value_changed or time_has_come: + self.callback(newval) + self.oldval = newval + self.oldtime = newtime diff --git a/gramps2/src/GrampsDb/_DbUtils.py b/gramps2/src/GrampsDb/_DbUtils.py index f94505402..d6b980fd2 100644 --- a/gramps2/src/GrampsDb/_DbUtils.py +++ b/gramps2/src/GrampsDb/_DbUtils.py @@ -22,6 +22,7 @@ import RelLib +import BasicUtils def remove_family_relationships(db, family_handle, trans=None): family = db.get_family_from_handle(family_handle) @@ -145,25 +146,27 @@ def add_child_to_family(db, family, child, db.transaction_commit(trans, _('Add child to family') ) -def db_copy(from_db,to_db,callback): - if '__call__' in dir(callback): # callback is really callable - update = update_real +class DbUpdateCallback(BasicUtils.UpdateCallback): + def __init__(self,db,callback,interval=1): + self.db = db + BasicUtils.UpdateCallback.__init__(self,callback,interval) - # Prepare length for the callback - person_len = from_db.get_number_of_people() - family_len = from_db.get_number_of_families() - event_len = from_db.get_number_of_events() - source_len = from_db.get_number_of_sources() - place_len = from_db.get_number_of_places() - repo_len = from_db.get_number_of_repositories() - obj_len = from_db.get_number_of_media_objects() + def get_total(self): + person_len = self.db.get_number_of_people() + family_len = self.db.get_number_of_families() + event_len = self.db.get_number_of_events() + source_len = self.db.get_number_of_sources() + place_len = self.db.get_number_of_places() + repo_len = self.db.get_number_of_repositories() + obj_len = self.db.get_number_of_media_objects() + + return person_len + family_len + event_len + \ + place_len + source_len + obj_len + repo_len - total = person_len + family_len + event_len + place_len + \ - source_len + obj_len + repo_len - else: - update = update_empty - total = 0 +def db_copy(from_db,to_db,callback): + uc = DbUpdateCallback(from_db,callback) + primary_tables = { 'Person': {'cursor_func': from_db.get_person_cursor, 'table': to_db.person_map }, @@ -190,9 +193,6 @@ def db_copy(from_db,to_db,callback): else: add_data = add_data_dict - oldval = 0 - count = 0 - # Start batch transaction to use async TXN and other tricks trans = to_db.transaction_begin("",batch=True) @@ -206,8 +206,7 @@ def db_copy(from_db,to_db,callback): (handle,data) = item add_data(to_db,table,handle,data) item = cursor.next() - count,oldval = update(callback,count,oldval,total) - update(callback,count,oldval,total) + uc.update() cursor.close() # Commit batch transaction: does nothing, except undoing the tricks @@ -219,7 +218,6 @@ def db_copy(from_db,to_db,callback): data = from_db.metadata.get(handle) to_db.metadata[handle] = data - def add_data_txn(db,table,handle,data): the_txn = db.env.txn_begin() table.put(handle,data,txn=the_txn) @@ -230,14 +228,3 @@ def add_data_notxn(db,table,handle,data): def add_data_dict(db,table,handle,data): table[handle] = data - -def update_empty(callback,count,oldval,total): - pass - -def update_real(callback,count,oldval,total): - count += 1 - newval = int(100.0*count/total) - if newval != oldval: - callback(newval) - oldval = newval - return count,oldval diff --git a/gramps2/src/Makefile.am b/gramps2/src/Makefile.am index e92173bda..cee89d1e3 100644 --- a/gramps2/src/Makefile.am +++ b/gramps2/src/Makefile.am @@ -78,7 +78,8 @@ gdir_PYTHON = \ ViewManager.py\ SelectFamily.py\ SelectSource.py\ - UndoHistory.py + UndoHistory.py\ + BasicUtils.py # Clean up all the byte-compiled files MOSTLYCLEANFILES = *pyc *pyo