2006-05-11 10:41:47 +05:30
|
|
|
#
|
|
|
|
# Gramps - a GTK+/GNOME based genealogy program
|
|
|
|
#
|
2007-06-28 11:11:40 +05:30
|
|
|
# Copyright (C) 2004-2007 Donald N. Allingham
|
2006-05-11 10:41:47 +05:30
|
|
|
#
|
|
|
|
# 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
|
2014-08-09 07:59:07 +05:30
|
|
|
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
2006-05-11 10:41:47 +05:30
|
|
|
#
|
|
|
|
|
|
|
|
"""
|
2013-11-12 05:08:26 +05:30
|
|
|
A set of basic utilities that everything in Gramps can depend upon.
|
2006-05-11 10:41:47 +05:30
|
|
|
|
|
|
|
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
|
2013-11-12 05:08:26 +05:30
|
|
|
other Gramps baggage.
|
2006-05-11 10:41:47 +05:30
|
|
|
"""
|
2015-03-14 21:32:57 +05:30
|
|
|
|
2006-05-11 10:41:47 +05:30
|
|
|
#-------------------------------------------------------------------------
|
|
|
|
#
|
|
|
|
# Python modules
|
|
|
|
#
|
|
|
|
#-------------------------------------------------------------------------
|
|
|
|
import time
|
2012-11-07 23:23:14 +05:30
|
|
|
import collections
|
2012-12-30 23:02:52 +05:30
|
|
|
import logging
|
|
|
|
_LOG = logging.getLogger(".gen")
|
2006-05-11 10:41:47 +05:30
|
|
|
#-------------------------------------------------------------------------
|
|
|
|
#
|
|
|
|
# Callback updater
|
|
|
|
#
|
|
|
|
#-------------------------------------------------------------------------
|
2009-05-21 22:49:50 +05:30
|
|
|
class UpdateCallback(object):
|
2006-05-11 10:41:47 +05:30
|
|
|
"""
|
|
|
|
Basic class providing way of calling the callback to update
|
2010-04-05 18:40:29 +05:30
|
|
|
things during lengthy operations.
|
2006-05-11 10:41:47 +05:30
|
|
|
"""
|
|
|
|
|
2007-06-04 08:19:31 +05:30
|
|
|
def __init__(self, callback, interval=1):
|
2006-05-11 10:41:47 +05:30
|
|
|
"""
|
2013-11-12 05:08:26 +05:30
|
|
|
: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 interval: int
|
2006-05-11 10:41:47 +05:30
|
|
|
"""
|
2012-11-07 23:23:14 +05:30
|
|
|
if isinstance(callback, collections.Callable): # callback is really callable
|
2006-05-11 10:41:47 +05:30
|
|
|
self.update = self.update_real
|
|
|
|
self.callback = callback
|
|
|
|
self.interval = interval
|
2006-05-27 05:08:21 +05:30
|
|
|
self.reset()
|
2006-05-11 10:41:47 +05:30
|
|
|
else:
|
|
|
|
self.update = self.update_empty
|
2007-08-31 08:48:38 +05:30
|
|
|
self.text = ""
|
2006-05-11 10:41:47 +05:30
|
|
|
|
2007-08-31 08:48:38 +05:30
|
|
|
def reset(self, text=""):
|
2006-05-27 05:08:21 +05:30
|
|
|
self.count = 0
|
|
|
|
self.oldval = 0
|
|
|
|
self.oldtime = 0
|
2007-08-31 08:48:38 +05:30
|
|
|
self.text = text
|
2006-05-27 05:08:21 +05:30
|
|
|
|
2007-08-31 08:48:38 +05:30
|
|
|
def set_total(self, total):
|
2006-05-11 11:46:45 +05:30
|
|
|
self.total = total
|
2012-12-30 23:02:52 +05:30
|
|
|
if self.total == 0:
|
|
|
|
_LOG.warning('UpdateCallback with total == 0 created')
|
|
|
|
self.total = 1
|
2006-05-11 10:41:47 +05:30
|
|
|
|
2007-08-31 08:48:38 +05:30
|
|
|
def update_empty(self, count=None):
|
2006-05-11 10:41:47 +05:30
|
|
|
pass
|
|
|
|
|
2007-08-31 08:48:38 +05:30
|
|
|
def update_real(self, count=None):
|
2006-05-11 10:41:47 +05:30
|
|
|
self.count += 1
|
2006-05-11 11:46:45 +05:30
|
|
|
if not count:
|
|
|
|
count = self.count
|
2012-11-08 01:48:16 +05:30
|
|
|
newval = int(100 * count/self.total)
|
2006-05-11 10:41:47 +05:30
|
|
|
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:
|
2007-08-31 08:48:38 +05:30
|
|
|
if self.text:
|
|
|
|
self.callback(newval, text=self.text)
|
|
|
|
else:
|
|
|
|
self.callback(newval)
|
2006-05-11 10:41:47 +05:30
|
|
|
self.oldval = newval
|
|
|
|
self.oldtime = newtime
|