2008-02-20 Don Allingham <don@gramps-project.org>
* src/GrampsCfg.py: refactoring * src/gen/utils/test/callback_test.py: refactoring * src/gen/utils/callback.py: refactoring * src/gen/utils/longop.py: refactoring * src/gen/utils/__init__.py: refactoring * src/gen/db/base.py: refactoring * src/gen/Makefile.am: refactoring * src/DisplayState.py: refactoring * src/DbState.py: refactoring * src/PluginUtils/_MenuOptions.py: refactoring * src/ListModel.py: refactoring * src/BaseDoc.py: refactoring svn: r10086
This commit is contained in:
14
ChangeLog
14
ChangeLog
@@ -1,3 +1,17 @@
|
|||||||
|
2008-02-20 Don Allingham <don@gramps-project.org>
|
||||||
|
* src/GrampsCfg.py: refactoring
|
||||||
|
* src/gen/utils/test/callback_test.py: refactoring
|
||||||
|
* src/gen/utils/callback.py: refactoring
|
||||||
|
* src/gen/utils/longop.py: refactoring
|
||||||
|
* src/gen/utils/__init__.py: refactoring
|
||||||
|
* src/gen/db/base.py: refactoring
|
||||||
|
* src/gen/Makefile.am: refactoring
|
||||||
|
* src/DisplayState.py: refactoring
|
||||||
|
* src/DbState.py: refactoring
|
||||||
|
* src/PluginUtils/_MenuOptions.py: refactoring
|
||||||
|
* src/ListModel.py: refactoring
|
||||||
|
* src/BaseDoc.py: refactoring
|
||||||
|
|
||||||
2008-02-20 Brian Matherly <brian@gramps-project.org>
|
2008-02-20 Brian Matherly <brian@gramps-project.org>
|
||||||
* src/plugins/Verify.py:
|
* src/plugins/Verify.py:
|
||||||
0001792: Tools->Utilities->Verify the Data (exclude married names)
|
0001792: Tools->Utilities->Verify the Data (exclude married names)
|
||||||
|
@@ -187,11 +187,13 @@ class PaperSize:
|
|||||||
#
|
#
|
||||||
#------------------------------------------------------------------------
|
#------------------------------------------------------------------------
|
||||||
class PaperStyle:
|
class PaperStyle:
|
||||||
"""Define the various options for a sheet of paper.
|
"""
|
||||||
|
Define the various options for a sheet of paper.
|
||||||
"""
|
"""
|
||||||
def __init__(self, size, orientation,
|
def __init__(self, size, orientation,
|
||||||
lmargin=2.54, rmargin=2.54, tmargin=2.54, bmargin=2.54):
|
lmargin=2.54, rmargin=2.54, tmargin=2.54, bmargin=2.54):
|
||||||
"""Create a new paper style.
|
"""
|
||||||
|
Create a new paper style.
|
||||||
|
|
||||||
@param size: size of the new style
|
@param size: size of the new style
|
||||||
@type size: PaperSize
|
@type size: PaperSize
|
||||||
@@ -215,7 +217,8 @@ class PaperStyle:
|
|||||||
self.__bmargin = bmargin
|
self.__bmargin = bmargin
|
||||||
|
|
||||||
def get_size(self):
|
def get_size(self):
|
||||||
"""Return the size of the paper.
|
"""
|
||||||
|
Return the size of the paper.
|
||||||
|
|
||||||
@returns: object indicating the paper size
|
@returns: object indicating the paper size
|
||||||
@rtype: PaperSize
|
@rtype: PaperSize
|
||||||
@@ -224,7 +227,8 @@ class PaperStyle:
|
|||||||
return self.__size
|
return self.__size
|
||||||
|
|
||||||
def get_orientation(self):
|
def get_orientation(self):
|
||||||
"""Return the orientation of the page.
|
"""
|
||||||
|
Return the orientation of the page.
|
||||||
|
|
||||||
@returns: PAPER_PORTRIAT or PAPER_LANDSCAPE
|
@returns: PAPER_PORTRIAT or PAPER_LANDSCAPE
|
||||||
@rtype: int
|
@rtype: int
|
||||||
@@ -233,7 +237,8 @@ class PaperStyle:
|
|||||||
return self.__orientation
|
return self.__orientation
|
||||||
|
|
||||||
def get_usable_width(self):
|
def get_usable_width(self):
|
||||||
"""Return the width of the page area in centimeters.
|
"""
|
||||||
|
Return the width of the page area in centimeters.
|
||||||
|
|
||||||
The value is the page width less the margins.
|
The value is the page width less the margins.
|
||||||
|
|
||||||
@@ -241,7 +246,8 @@ class PaperStyle:
|
|||||||
return self.__size.get_width() - (self.__rmargin + self.__lmargin)
|
return self.__size.get_width() - (self.__rmargin + self.__lmargin)
|
||||||
|
|
||||||
def get_usable_height(self):
|
def get_usable_height(self):
|
||||||
"""Return the height of the page area in centimeters.
|
"""
|
||||||
|
Return the height of the page area in centimeters.
|
||||||
|
|
||||||
The value is the page height less the margins.
|
The value is the page height less the margins.
|
||||||
|
|
||||||
@@ -249,7 +255,8 @@ class PaperStyle:
|
|||||||
return self.__size.get_height() - (self.__tmargin + self.__bmargin)
|
return self.__size.get_height() - (self.__tmargin + self.__bmargin)
|
||||||
|
|
||||||
def get_right_margin(self):
|
def get_right_margin(self):
|
||||||
"""Return the right margin.
|
"""
|
||||||
|
Return the right margin.
|
||||||
|
|
||||||
@returns: Right margin in centimeters
|
@returns: Right margin in centimeters
|
||||||
@rtype: float
|
@rtype: float
|
||||||
@@ -258,7 +265,8 @@ class PaperStyle:
|
|||||||
return self.__rmargin
|
return self.__rmargin
|
||||||
|
|
||||||
def get_left_margin(self):
|
def get_left_margin(self):
|
||||||
"""Return the left margin.
|
"""
|
||||||
|
Return the left margin.
|
||||||
|
|
||||||
@returns: Left margin in centimeters
|
@returns: Left margin in centimeters
|
||||||
@rtype: float
|
@rtype: float
|
||||||
@@ -267,7 +275,8 @@ class PaperStyle:
|
|||||||
return self.__lmargin
|
return self.__lmargin
|
||||||
|
|
||||||
def get_top_margin(self):
|
def get_top_margin(self):
|
||||||
"""Return the top margin.
|
"""
|
||||||
|
Return the top margin.
|
||||||
|
|
||||||
@returns: Top margin in centimeters
|
@returns: Top margin in centimeters
|
||||||
@rtype: float
|
@rtype: float
|
||||||
@@ -276,7 +285,8 @@ class PaperStyle:
|
|||||||
return self.__tmargin
|
return self.__tmargin
|
||||||
|
|
||||||
def get_bottom_margin(self):
|
def get_bottom_margin(self):
|
||||||
"""Return the bottom margin.
|
"""
|
||||||
|
Return the bottom margin.
|
||||||
|
|
||||||
@returns: Bottom margin in centimeters
|
@returns: Bottom margin in centimeters
|
||||||
@rtype: float
|
@rtype: float
|
||||||
|
@@ -23,10 +23,10 @@ Provides the database state class
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
from gen.db import GrampsDbBase
|
from gen.db import GrampsDbBase
|
||||||
from gen.utils import GrampsDBCallback
|
from gen.utils import Callback
|
||||||
import Config
|
import Config
|
||||||
|
|
||||||
class DbState(GrampsDBCallback):
|
class DbState(Callback):
|
||||||
"""
|
"""
|
||||||
Provides a class to encapsulate the state of the database..
|
Provides a class to encapsulate the state of the database..
|
||||||
"""
|
"""
|
||||||
@@ -42,7 +42,7 @@ class DbState(GrampsDBCallback):
|
|||||||
Initalize the state with an empty (and useless) GrampsDbBase. This is
|
Initalize the state with an empty (and useless) GrampsDbBase. This is
|
||||||
just a place holder until a real DB is assigned.
|
just a place holder until a real DB is assigned.
|
||||||
"""
|
"""
|
||||||
GrampsDBCallback.__init__(self)
|
Callback.__init__(self)
|
||||||
self.db = GrampsDbBase()
|
self.db = GrampsDbBase()
|
||||||
self.open = False
|
self.open = False
|
||||||
self.active = None
|
self.active = None
|
||||||
|
@@ -64,7 +64,7 @@ DISABLED = -1
|
|||||||
# History manager
|
# History manager
|
||||||
#
|
#
|
||||||
#-------------------------------------------------------------------------
|
#-------------------------------------------------------------------------
|
||||||
class History(gen.utils.GrampsDBCallback):
|
class History(gen.utils.Callback):
|
||||||
""" History manages the objects of a certain type that have been viewed,
|
""" History manages the objects of a certain type that have been viewed,
|
||||||
with ability to go back, or forward.
|
with ability to go back, or forward.
|
||||||
When accessing an object, it should be pushed on the History.
|
When accessing an object, it should be pushed on the History.
|
||||||
@@ -76,7 +76,7 @@ class History(gen.utils.GrampsDBCallback):
|
|||||||
}
|
}
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
gen.utils.GrampsDBCallback.__init__(self)
|
gen.utils.Callback.__init__(self)
|
||||||
self.clear()
|
self.clear()
|
||||||
|
|
||||||
def clear(self):
|
def clear(self):
|
||||||
@@ -293,7 +293,7 @@ class WarnHandler(RotateHandler):
|
|||||||
top.run()
|
top.run()
|
||||||
top.destroy()
|
top.destroy()
|
||||||
|
|
||||||
class DisplayState(gen.utils.GrampsDBCallback):
|
class DisplayState(gen.utils.Callback):
|
||||||
|
|
||||||
__signals__ = {
|
__signals__ = {
|
||||||
'filters-changed' : (str, ),
|
'filters-changed' : (str, ),
|
||||||
@@ -310,7 +310,7 @@ class DisplayState(gen.utils.GrampsDBCallback):
|
|||||||
self.uimanager = uimanager
|
self.uimanager = uimanager
|
||||||
self.progress_monitor = progress_monitor
|
self.progress_monitor = progress_monitor
|
||||||
self.window = window
|
self.window = window
|
||||||
gen.utils.GrampsDBCallback.__init__(self)
|
gen.utils.Callback.__init__(self)
|
||||||
self.status = status
|
self.status = status
|
||||||
self.status_id = status.get_context_id('GRAMPS')
|
self.status_id = status.get_context_id('GRAMPS')
|
||||||
self.progress = progress
|
self.progress = progress
|
||||||
|
@@ -132,6 +132,7 @@ class DisplayNameEditor(ManagedWindow.ManagedWindow):
|
|||||||
return (_(" Name Editor"), _("Preferences"))
|
return (_(" Name Editor"), _("Preferences"))
|
||||||
|
|
||||||
class GrampsPreferences(ManagedWindow.ManagedWindow):
|
class GrampsPreferences(ManagedWindow.ManagedWindow):
|
||||||
|
|
||||||
def __init__(self, uistate, dbstate):
|
def __init__(self, uistate, dbstate):
|
||||||
self.dbstate = dbstate
|
self.dbstate = dbstate
|
||||||
ManagedWindow.ManagedWindow.__init__(self, uistate, [], GrampsPreferences)
|
ManagedWindow.ManagedWindow.__init__(self, uistate, [], GrampsPreferences)
|
||||||
|
@@ -96,6 +96,14 @@ class ListModel:
|
|||||||
self.double_click = event_func
|
self.double_click = event_func
|
||||||
self.tree.connect('event', self.__button_press)
|
self.tree.connect('event', self.__button_press)
|
||||||
|
|
||||||
|
def __build_image_column(self, cnum, name, renderer, column):
|
||||||
|
renderer = gtk.CellRendererPixbuf()
|
||||||
|
column = gtk.TreeViewColumn(name[0], renderer)
|
||||||
|
column.add_attribute(renderer, 'pixbuf', cnum)
|
||||||
|
renderer.set_property('height', const.THUMBSCALE / 2)
|
||||||
|
return renderer, column
|
||||||
|
|
||||||
|
|
||||||
def __build_columns(self, dlist):
|
def __build_columns(self, dlist):
|
||||||
"""
|
"""
|
||||||
Builds the columns based of the data in dlist
|
Builds the columns based of the data in dlist
|
||||||
@@ -114,10 +122,7 @@ class ListModel:
|
|||||||
column = gtk.TreeViewColumn(name[0], renderer)
|
column = gtk.TreeViewColumn(name[0], renderer)
|
||||||
column.add_attribute(renderer, 'active', cnum)
|
column.add_attribute(renderer, 'active', cnum)
|
||||||
elif name[0] and name[3] == IMAGE:
|
elif name[0] and name[3] == IMAGE:
|
||||||
renderer = gtk.CellRendererPixbuf()
|
renderer, column = self.__build_image_column(cnum, name, renderer, column)
|
||||||
column = gtk.TreeViewColumn(name[0], renderer)
|
|
||||||
column.add_attribute(renderer, 'pixbuf', cnum)
|
|
||||||
renderer.set_property('height', const.THUMBSCALE/2)
|
|
||||||
else:
|
else:
|
||||||
renderer = gtk.CellRendererText()
|
renderer = gtk.CellRendererText()
|
||||||
renderer.set_fixed_height_from_font(True)
|
renderer.set_fixed_height_from_font(True)
|
||||||
|
@@ -34,7 +34,7 @@ import gen.utils
|
|||||||
# Option class
|
# Option class
|
||||||
#
|
#
|
||||||
#-------------------------------------------------------------------------
|
#-------------------------------------------------------------------------
|
||||||
class Option(gen.utils.GrampsDBCallback):
|
class Option(gen.utils.Callback):
|
||||||
"""
|
"""
|
||||||
This class serves as a base class for all options. All Options must
|
This class serves as a base class for all options. All Options must
|
||||||
minimally provide the services provided by this class. Options are allowed
|
minimally provide the services provided by this class. Options are allowed
|
||||||
@@ -54,7 +54,7 @@ class Option(gen.utils.GrampsDBCallback):
|
|||||||
@type value: The type will depend on the type of option.
|
@type value: The type will depend on the type of option.
|
||||||
@return: nothing
|
@return: nothing
|
||||||
"""
|
"""
|
||||||
gen.utils.GrampsDBCallback.__init__(self)
|
gen.utils.Callback.__init__(self)
|
||||||
self.__value = value
|
self.__value = value
|
||||||
self.__label = label
|
self.__label = label
|
||||||
self.__help_str = ""
|
self.__help_str = ""
|
||||||
|
@@ -3,7 +3,11 @@
|
|||||||
# but that is not necessarily portable.
|
# but that is not necessarily portable.
|
||||||
# If not using GNU make, then list all .py files individually
|
# If not using GNU make, then list all .py files individually
|
||||||
|
|
||||||
SUBDIRS = proxy db lib utils
|
SUBDIRS = \
|
||||||
|
proxy\
|
||||||
|
db\
|
||||||
|
lib\
|
||||||
|
utils
|
||||||
|
|
||||||
pkgdatadir = $(datadir)/@PACKAGE@/gen
|
pkgdatadir = $(datadir)/@PACKAGE@/gen
|
||||||
|
|
||||||
|
@@ -49,7 +49,7 @@ LOG = logging.getLogger(".GrampsDb")
|
|||||||
#-------------------------------------------------------------------------
|
#-------------------------------------------------------------------------
|
||||||
from gen.lib import (MediaObject, Person, Family, Source, Event, Place,
|
from gen.lib import (MediaObject, Person, Family, Source, Event, Place,
|
||||||
Repository, Note, GenderStats, Researcher)
|
Repository, Note, GenderStats, Researcher)
|
||||||
from gen.utils.callback import GrampsDBCallback
|
from gen.utils.callback import Callback
|
||||||
from iterator import CursorIterator
|
from iterator import CursorIterator
|
||||||
|
|
||||||
#-------------------------------------------------------------------------
|
#-------------------------------------------------------------------------
|
||||||
@@ -119,7 +119,7 @@ class GrampsDbBookmarks:
|
|||||||
def insert(self, pos, item):
|
def insert(self, pos, item):
|
||||||
self.bookmarks.insert(pos, item)
|
self.bookmarks.insert(pos, item)
|
||||||
|
|
||||||
class GrampsDbBase(GrampsDBCallback):
|
class GrampsDbBase(Callback):
|
||||||
"""
|
"""
|
||||||
GRAMPS database object. This object is a base class for all
|
GRAMPS database object. This object is a base class for all
|
||||||
database interfaces.
|
database interfaces.
|
||||||
@@ -181,7 +181,7 @@ class GrampsDbBase(GrampsDBCallback):
|
|||||||
be created.
|
be created.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
GrampsDBCallback.__init__(self)
|
Callback.__init__(self)
|
||||||
|
|
||||||
self.set_person_id_prefix('I%04d')
|
self.set_person_id_prefix('I%04d')
|
||||||
self.set_object_id_prefix('O%04d')
|
self.set_object_id_prefix('O%04d')
|
||||||
|
@@ -21,5 +21,5 @@
|
|||||||
from dbutils import *
|
from dbutils import *
|
||||||
from progressmon import ProgressMonitor
|
from progressmon import ProgressMonitor
|
||||||
from longop import LongOpStatus
|
from longop import LongOpStatus
|
||||||
from callback import GrampsDBCallback
|
from callback import Callback
|
||||||
|
|
||||||
|
@@ -8,7 +8,7 @@
|
|||||||
# the Free Software Foundation; either version 2 of the License, or
|
# the Free Software Foundation; either version 2 of the License, or
|
||||||
# (at your option) any later version.
|
# (at your option) any later version.
|
||||||
#
|
#
|
||||||
# This program is distributed in the hope that it will be useful,
|
# This program is distributed in the hope that it will be useful,
|
||||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
# GNU General Public License for more details.
|
# GNU General Public License for more details.
|
||||||
@@ -36,7 +36,6 @@
|
|||||||
or the UI code.
|
or the UI code.
|
||||||
"""
|
"""
|
||||||
import sys
|
import sys
|
||||||
import os
|
|
||||||
import types
|
import types
|
||||||
import traceback
|
import traceback
|
||||||
import inspect
|
import inspect
|
||||||
@@ -49,28 +48,28 @@ log = sys.stderr.write
|
|||||||
#
|
#
|
||||||
#-------------------------------------------------------------------------
|
#-------------------------------------------------------------------------
|
||||||
|
|
||||||
class GrampsDBCallback(object):
|
class Callback(object):
|
||||||
"""
|
"""
|
||||||
Callback and signal support for non-gtk parts of gramps.
|
Callback and signal support objects.
|
||||||
|
|
||||||
Declaring signals
|
Declaring signals
|
||||||
=================
|
=================
|
||||||
|
|
||||||
Classes that want to emit signals need to inherit from the
|
Classes that want to emit signals need to inherit from the
|
||||||
GrampsDBCallback class and ensure that its __init__ method
|
DBCallback class and ensure that its __init__ method
|
||||||
is called. They then need to declare the signals that they
|
is called. They then need to declare the signals that they
|
||||||
can emit and the types of each callbacks arguments. For
|
can emit and the types of each callbacks arguments. For
|
||||||
example::
|
example::
|
||||||
|
|
||||||
class TestSignals(GrampsDBCallback):
|
class TestSignals(Callback):
|
||||||
|
|
||||||
__signals__ = {
|
__signals__ = {
|
||||||
'test-signal' : (int,),
|
'test-signal' : (int, ),
|
||||||
'test-noarg' : None
|
'test-noarg' : None
|
||||||
}
|
}
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
GrampsDBCallback.__init__(self)
|
Callback.__init__(self)
|
||||||
|
|
||||||
The type signature is a tuple of types or classes. The type
|
The type signature is a tuple of types or classes. The type
|
||||||
checking code uses the isinstance method to check that the
|
checking code uses the isinstance method to check that the
|
||||||
@@ -90,7 +89,7 @@ class GrampsDBCallback(object):
|
|||||||
Signals are emitted using the emit method. e.g.::
|
Signals are emitted using the emit method. e.g.::
|
||||||
|
|
||||||
def emit_signal(self):
|
def emit_signal(self):
|
||||||
self.emit('test-signal',(1,))
|
self.emit('test-signal', (1, ))
|
||||||
|
|
||||||
The parameters are passed as a tuple so a single parameter
|
The parameters are passed as a tuple so a single parameter
|
||||||
must be passed as a 1 element tuple.
|
must be passed as a 1 element tuple.
|
||||||
@@ -147,25 +146,25 @@ class GrampsDBCallback(object):
|
|||||||
=============================
|
=============================
|
||||||
|
|
||||||
Signals can be blocked on a per instance bassis or they can be blocked
|
Signals can be blocked on a per instance bassis or they can be blocked
|
||||||
for all instances of the GrampsDBCallback class. disable_signals() can
|
for all instances of the Callback class. disable_signals() can
|
||||||
be used to block the signals for a single instance and disable_all_signals()
|
be used to block the signals for a single instance and disable_all_signals()
|
||||||
can be used to block signals for the class:
|
can be used to block signals for the class:
|
||||||
|
|
||||||
e.g.::
|
e.g.::
|
||||||
|
|
||||||
|
|
||||||
class TestSignals(GrampsDBCallback):
|
class TestSignals(Callback):
|
||||||
|
|
||||||
__signals__ = {
|
__signals__ = {
|
||||||
'test-signal' : (int,),
|
'test-signal' : (int, ),
|
||||||
'test-noarg' : None
|
'test-noarg' : None
|
||||||
}
|
}
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
GrampsDBCallback.__init__(self)
|
Callback.__init__(self)
|
||||||
|
|
||||||
def emit_signal(self):
|
def emit_signal(self):
|
||||||
self.emit('test-signal',(1,))
|
self.emit('test-signal', (1, ))
|
||||||
|
|
||||||
t = TestSignals()
|
t = TestSignals()
|
||||||
|
|
||||||
@@ -178,15 +177,15 @@ class GrampsDBCallback(object):
|
|||||||
t.enable_signals()
|
t.enable_signals()
|
||||||
|
|
||||||
# block all signals
|
# block all signals
|
||||||
GrampsDBCallback.disable_all_signals()
|
Callback.disable_all_signals()
|
||||||
|
|
||||||
...
|
...
|
||||||
|
|
||||||
# unblock all signals
|
# unblock all signals
|
||||||
GrampsDBCallback.enable_all_signals()
|
Callback.enable_all_signals()
|
||||||
|
|
||||||
|
|
||||||
Any signals emited whilst signals are blocked will be lost.
|
Any signals emitted whilst signals are blocked will be lost.
|
||||||
|
|
||||||
|
|
||||||
Debugging signal callbacks
|
Debugging signal callbacks
|
||||||
@@ -197,7 +196,7 @@ class GrampsDBCallback(object):
|
|||||||
lots of logging information. To switch on logging for a single
|
lots of logging information. To switch on logging for a single
|
||||||
instance call self.enable_logging(), to switch it off again call
|
instance call self.enable_logging(), to switch it off again call
|
||||||
self.disable_logging(). To switch on logging for all instance
|
self.disable_logging(). To switch on logging for all instance
|
||||||
you can toggle GrampsDBCallback.__LOG_ALL to True.
|
you can toggle Callback.__LOG_ALL to True.
|
||||||
|
|
||||||
"""
|
"""
|
||||||
|
|
||||||
@@ -205,15 +204,8 @@ class GrampsDBCallback(object):
|
|||||||
# any class derived from this class. This should be toggled using
|
# any class derived from this class. This should be toggled using
|
||||||
# the class methods, dissable_all_signals() and enable_all_signals().
|
# the class methods, dissable_all_signals() and enable_all_signals().
|
||||||
__BLOCK_ALL_SIGNALS = False
|
__BLOCK_ALL_SIGNALS = False
|
||||||
|
__LOG_ALL = False
|
||||||
|
|
||||||
|
|
||||||
# If this is True logging will be turned on for all instances
|
|
||||||
# whether or not instance based logging is enabled.
|
|
||||||
try:
|
|
||||||
__LOG_ALL = int(os.environ.get('GRAMPS_SIGNAL',"0")) == 1
|
|
||||||
except:
|
|
||||||
__LOG_ALL = False
|
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self.__enable_logging = False # controls whether lots of debug
|
self.__enable_logging = False # controls whether lots of debug
|
||||||
# information will be produced.
|
# information will be produced.
|
||||||
@@ -222,7 +214,7 @@ class GrampsDBCallback(object):
|
|||||||
self.__callback_map = {} # dictionary containing all the connected
|
self.__callback_map = {} # dictionary containing all the connected
|
||||||
# callback functions. The keys are the
|
# callback functions. The keys are the
|
||||||
# signal names and the values are tuples
|
# signal names and the values are tuples
|
||||||
# of the form (key,bound_method), where
|
# of the form (key, bound_method), where
|
||||||
# the key is unique within the instance
|
# the key is unique within the instance
|
||||||
# and the bound_method is the callback
|
# and the bound_method is the callback
|
||||||
# that will be called when the signal is
|
# that will be called when the signal is
|
||||||
@@ -232,7 +224,7 @@ class GrampsDBCallback(object):
|
|||||||
# signal names and the values are tuples
|
# signal names and the values are tuples
|
||||||
# containing the list of types of the arguments
|
# containing the list of types of the arguments
|
||||||
# that the callback methods must accept.
|
# that the callback methods must accept.
|
||||||
self._current_key = 0 # counter to give a unique key to each callback.
|
self._current_key = 0 # counter to give a unique key to each callback
|
||||||
self._current_signals = [] # list of all the signals that are currently
|
self._current_signals = [] # list of all the signals that are currently
|
||||||
# being emitted by this instance. This is
|
# being emitted by this instance. This is
|
||||||
# used to prevent recursive emittion of the
|
# used to prevent recursive emittion of the
|
||||||
@@ -261,17 +253,17 @@ class GrampsDBCallback(object):
|
|||||||
|
|
||||||
# Build a signal dict from the list of signal dicts
|
# Build a signal dict from the list of signal dicts
|
||||||
for s in trav(self.__class__):
|
for s in trav(self.__class__):
|
||||||
for (k,v) in s.items():
|
for (k, v) in s.items():
|
||||||
if self.__signal_map.has_key(k):
|
if self.__signal_map.has_key(k):
|
||||||
# signal name clash
|
# signal name clash
|
||||||
sys.err.write("Warning: signal name clash: %s\n" % str(k))
|
sys.stderr.write("Warning: signal name clash: %s\n" % str(k))
|
||||||
self.__signal_map[k] = v
|
self.__signal_map[k] = v
|
||||||
|
|
||||||
# self.__signal_map now contains the connonical list
|
# self.__signal_map now contains the connonical list
|
||||||
# of signals that this instance can emit.
|
# of signals that this instance can emit.
|
||||||
|
|
||||||
self._log("registed signals: \n %s\n" %
|
self._log("registered signals: \n %s\n" %
|
||||||
"\n ".join([ "%s: %s" % (k,v) for (k,v)
|
"\n ".join([ "%s: %s" % (k, v) for (k, v)
|
||||||
in self.__signal_map.items() ]))
|
in self.__signal_map.items() ]))
|
||||||
|
|
||||||
|
|
||||||
@@ -295,12 +287,12 @@ class GrampsDBCallback(object):
|
|||||||
self._current_key += 1
|
self._current_key += 1
|
||||||
self._log("Connecting callback to signal: "
|
self._log("Connecting callback to signal: "
|
||||||
"%s with key: %s\n"
|
"%s with key: %s\n"
|
||||||
% (signal_name,str(self._current_key)))
|
% (signal_name, str(self._current_key)))
|
||||||
self.__callback_map[signal_name].append((self._current_key,callback))
|
self.__callback_map[signal_name].append((self._current_key, callback))
|
||||||
|
|
||||||
return self._current_key
|
return self._current_key
|
||||||
|
|
||||||
def disconnect(self,key):
|
def disconnect(self, key):
|
||||||
"""
|
"""
|
||||||
Disconnect a callback.
|
Disconnect a callback.
|
||||||
"""
|
"""
|
||||||
@@ -308,11 +300,11 @@ class GrampsDBCallback(object):
|
|||||||
# Find the key in the callback map.
|
# Find the key in the callback map.
|
||||||
for signal_name in self.__callback_map.keys():
|
for signal_name in self.__callback_map.keys():
|
||||||
for cb in self.__callback_map[signal_name]:
|
for cb in self.__callback_map[signal_name]:
|
||||||
(skey,fn) = cb
|
(skey, fn) = cb
|
||||||
if skey == key:
|
if skey == key:
|
||||||
# delete the callback from the map.
|
# delete the callback from the map.
|
||||||
self._log("Disconnecting callback from signal"
|
self._log("Disconnecting callback from signal"
|
||||||
": %s with key: %s\n" % (signal_name,
|
": %s with key: %s\n" % (signal_name,
|
||||||
str(key)))
|
str(key)))
|
||||||
self.__callback_map[signal_name].remove(cb)
|
self.__callback_map[signal_name].remove(cb)
|
||||||
|
|
||||||
@@ -323,7 +315,7 @@ class GrampsDBCallback(object):
|
|||||||
arguments that match the types declared for the signals signature.
|
arguments that match the types declared for the signals signature.
|
||||||
"""
|
"""
|
||||||
# Check that signals are not blocked
|
# Check that signals are not blocked
|
||||||
if GrampsDBCallback.__BLOCK_ALL_SIGNALS or \
|
if self.__BLOCK_ALL_SIGNALS or \
|
||||||
self.__block_instance_signals:
|
self.__block_instance_signals:
|
||||||
return
|
return
|
||||||
|
|
||||||
@@ -333,36 +325,36 @@ class GrampsDBCallback(object):
|
|||||||
" from: file: %s\n"
|
" from: file: %s\n"
|
||||||
" line: %d\n"
|
" line: %d\n"
|
||||||
" func: %s\n"
|
" func: %s\n"
|
||||||
% ((str(signal_name),) + inspect.stack()[1][1:4]))
|
% ((str(signal_name), ) + inspect.stack()[1][1:4]))
|
||||||
return
|
return
|
||||||
|
|
||||||
# check that the signal is not already being emitted. This prevents
|
# check that the signal is not already being emitted. This prevents
|
||||||
# against recursive signal emmissions.
|
# against recursive signal emissions.
|
||||||
if signal_name in self._current_signals:
|
if signal_name in self._current_signals:
|
||||||
self._warn("Signal recursion blocked. "
|
self._warn("Signal recursion blocked. "
|
||||||
"Signals was : %s\n"
|
"Signals was : %s\n"
|
||||||
" from: file: %s\n"
|
" from: file: %s\n"
|
||||||
" line: %d\n"
|
" line: %d\n"
|
||||||
" func: %s\n"
|
" func: %s\n"
|
||||||
% ((str(signal_name),) + inspect.stack()[1][1:4]))
|
% ((str(signal_name), ) + inspect.stack()[1][1:4]))
|
||||||
return
|
return
|
||||||
|
|
||||||
try:
|
try:
|
||||||
self._current_signals.append(signal_name)
|
self._current_signals.append(signal_name)
|
||||||
|
|
||||||
# check that args is a tuple. This is a common programming error.
|
# check that args is a tuple. This is a common programming error.
|
||||||
if not (isinstance(args,tuple) or args == None):
|
if not (isinstance(args, tuple) or args == None):
|
||||||
self._warn("Signal emitted with argument that is not a tuple.\n"
|
self._warn("Signal emitted with argument that is not a tuple.\n"
|
||||||
" emit() takes two arguments, the signal name and a \n"
|
" emit() takes two arguments, the signal name and a \n"
|
||||||
" tuple that contains the arguments that are to be \n"
|
" tuple that contains the arguments that are to be \n"
|
||||||
" passed to the callbacks. If you are passing a signal \n"
|
" passed to the callbacks. If you are passing a signal \n"
|
||||||
" argument it must be done as a single element tuple \n"
|
" argument it must be done as a single element tuple \n"
|
||||||
" e.g. emit('my-signal',(1,)) \n"
|
" e.g. emit('my-signal', (1, )) \n"
|
||||||
" signal was: %s\n"
|
" signal was: %s\n"
|
||||||
" from: file: %s\n"
|
" from: file: %s\n"
|
||||||
" line: %d\n"
|
" line: %d\n"
|
||||||
" func: %s\n"
|
" func: %s\n"
|
||||||
% ((str(signal_name),) + inspect.stack()[1][1:4]))
|
% ((str(signal_name), ) + inspect.stack()[1][1:4]))
|
||||||
return
|
return
|
||||||
|
|
||||||
# type check arguments
|
# type check arguments
|
||||||
@@ -373,7 +365,7 @@ class GrampsDBCallback(object):
|
|||||||
" from: file: %s\n"
|
" from: file: %s\n"
|
||||||
" line: %d\n"
|
" line: %d\n"
|
||||||
" func: %s\n"
|
" func: %s\n"
|
||||||
% ((str(signal_name),) + inspect.stack()[1][1:4]))
|
% ((str(signal_name), ) + inspect.stack()[1][1:4]))
|
||||||
return
|
return
|
||||||
|
|
||||||
if len(args) > 0:
|
if len(args) > 0:
|
||||||
@@ -383,42 +375,36 @@ class GrampsDBCallback(object):
|
|||||||
" from: file: %s\n"
|
" from: file: %s\n"
|
||||||
" line: %d\n"
|
" line: %d\n"
|
||||||
" func: %s\n"
|
" func: %s\n"
|
||||||
% ((str(signal_name),) + inspect.stack()[1][1:4]))
|
% ((str(signal_name), ) + inspect.stack()[1][1:4]))
|
||||||
return
|
return
|
||||||
|
|
||||||
if arg_types != None:
|
if arg_types != None:
|
||||||
for i in range(0,len(arg_types)):
|
for i in range(0, len(arg_types)):
|
||||||
if not isinstance(args[i],arg_types[i]):
|
if not isinstance(args[i], arg_types[i]):
|
||||||
self._warn("Signal emitted with "
|
self._warn("Signal emitted with "
|
||||||
"wrong arg types: %s\n"
|
"wrong arg types: %s\n"
|
||||||
" from: file: %s\n"
|
" from: file: %s\n"
|
||||||
" line: %d\n"
|
" line: %d\n"
|
||||||
" func: %s\n"
|
" func: %s\n"
|
||||||
" arg passed was: %s, type of arg passed %s, type should be: %s\n"
|
" arg passed was: %s, type of arg passed %s, type should be: %s\n"
|
||||||
% ((str(signal_name),) + inspect.stack()[1][1:4] +\
|
% ((str(signal_name), ) + inspect.stack()[1][1:4] +\
|
||||||
(args[i],repr(type(args[i])),repr(arg_types[i]))))
|
(args[i], repr(type(args[i])), repr(arg_types[i]))))
|
||||||
return
|
return
|
||||||
|
|
||||||
if signal_name in self.__callback_map.keys():
|
if signal_name in self.__callback_map.keys():
|
||||||
self._log("emmitting signal: %s\n" % (signal_name,))
|
self._log("emitting signal: %s\n" % (signal_name, ))
|
||||||
# Don't bother if there are no callbacks.
|
# Don't bother if there are no callbacks.
|
||||||
for (key,fn) in self.__callback_map[signal_name]:
|
for (key, fn) in self.__callback_map[signal_name]:
|
||||||
self._log("Calling callback with key: %s\n" % (key,))
|
self._log("Calling callback with key: %s\n" % (key, ))
|
||||||
try:
|
try:
|
||||||
if type(fn) == tuple: # call class method
|
if type(fn) == types.FunctionType or \
|
||||||
cb[0](fn[1],*args)
|
type(fn) == types.MethodType: # call func
|
||||||
elif type(fn) == types.FunctionType or \
|
fn(*args)
|
||||||
type(fn) == types.MethodType: # call func
|
|
||||||
fn(*args)
|
|
||||||
# try:
|
|
||||||
# fn(*args)
|
|
||||||
# except Errors.DbError:
|
|
||||||
# display_error()
|
|
||||||
else:
|
else:
|
||||||
self._warn("Badly formed entry in callback map.\n")
|
self._warn("Badly formed entry in callback map.\n")
|
||||||
except:
|
except:
|
||||||
self._warn("Exception occured in callback function.\n"
|
self._warn("Exception occurred in callback function.\n"
|
||||||
"%s" % ("".join(traceback.format_exception(*sys.exc_info())),))
|
"%s" % ("".join(traceback.format_exception(*sys.exc_info())), ))
|
||||||
finally:
|
finally:
|
||||||
self._current_signals.remove(signal_name)
|
self._current_signals.remove(signal_name)
|
||||||
|
|
||||||
@@ -431,7 +417,6 @@ class GrampsDBCallback(object):
|
|||||||
def enable_signals(self):
|
def enable_signals(self):
|
||||||
self.__block_instance_signals = False
|
self.__block_instance_signals = False
|
||||||
|
|
||||||
|
|
||||||
# logging methods
|
# logging methods
|
||||||
|
|
||||||
def disable_logging(self):
|
def disable_logging(self):
|
||||||
@@ -440,36 +425,26 @@ class GrampsDBCallback(object):
|
|||||||
def enable_logging(self):
|
def enable_logging(self):
|
||||||
self.__enable_logging = True
|
self.__enable_logging = True
|
||||||
|
|
||||||
def _log(self,msg):
|
def _log(self, msg):
|
||||||
if GrampsDBCallback.__LOG_ALL or self.__enable_logging:
|
if self.__LOG_ALL or self.__enable_logging:
|
||||||
log("%s: %s" % (self.__class__.__name__, str(msg)))
|
log("%s: %s" % (self.__class__.__name__, str(msg)))
|
||||||
|
|
||||||
def _warn(self,msg):
|
def _warn(self, msg):
|
||||||
log("Warning: %s: %s" % (self.__class__.__name__, str(msg)))
|
log("Warning: %s: %s" % (self.__class__.__name__, str(msg)))
|
||||||
|
|
||||||
#
|
#
|
||||||
# Class methods
|
# Class methods
|
||||||
#
|
#
|
||||||
|
|
||||||
def __disable_all_signals(self):
|
@classmethod
|
||||||
GrampsDBCallback.__BLOCK_ALL_SIGNALS = True
|
def log_all(cls, enable):
|
||||||
|
cls.__LOG_ALL = enable
|
||||||
|
|
||||||
disable_all_signals = classmethod(__disable_all_signals)
|
@classmethod
|
||||||
|
def disable_all_signals(cls):
|
||||||
def __enable_all_signals(self):
|
cls.__BLOCK_ALL_SIGNALS = True
|
||||||
GrampsDBCallback.__BLOCK_ALL_SIGNALS = False
|
|
||||||
|
|
||||||
enable_all_signals = classmethod(__enable_all_signals)
|
@classmethod
|
||||||
|
def enable_all_signals(cls):
|
||||||
|
cls.__BLOCK_ALL_SIGNALS = False
|
||||||
# def display_error():
|
|
||||||
# from QuestionDialog import ErrorDialog
|
|
||||||
# ErrorDialog(
|
|
||||||
# _('Database error'),
|
|
||||||
# _('A problem as been detected in your database. '
|
|
||||||
# 'This is probably caused by opening a database that was '
|
|
||||||
# 'created with one transaction setting when the database was '
|
|
||||||
# 'created with another, or by moving a non-portable database '
|
|
||||||
# 'to a different machine.'))
|
|
||||||
|
|
||||||
|
|
||||||
|
@@ -1,8 +1,8 @@
|
|||||||
import time
|
import time
|
||||||
|
|
||||||
from callback import GrampsDBCallback
|
from callback import Callback
|
||||||
|
|
||||||
class LongOpStatus(GrampsDBCallback):
|
class LongOpStatus(Callback):
|
||||||
"""LongOpStatus provides a way of communicating the status of a long
|
"""LongOpStatus provides a way of communicating the status of a long
|
||||||
running operations. The intended use is that when a long running operation
|
running operations. The intended use is that when a long running operation
|
||||||
is about to start it should create an instance of this class and emit
|
is about to start it should create an instance of this class and emit
|
||||||
@@ -19,7 +19,7 @@ class LongOpStatus(GrampsDBCallback):
|
|||||||
|
|
||||||
Example usage:
|
Example usage:
|
||||||
|
|
||||||
class MyClass(GrampsDBCallback):
|
class MyClass(Callback):
|
||||||
|
|
||||||
__signals__ = {
|
__signals__ = {
|
||||||
'op-start' : object
|
'op-start' : object
|
||||||
@@ -80,7 +80,7 @@ class LongOpStatus(GrampsDBCallback):
|
|||||||
the operation.
|
the operation.
|
||||||
@type can_cancel:
|
@type can_cancel:
|
||||||
"""
|
"""
|
||||||
GrampsDBCallback.__init__(self)
|
Callback.__init__(self)
|
||||||
self._msg = msg
|
self._msg = msg
|
||||||
self._total_steps = total_steps
|
self._total_steps = total_steps
|
||||||
# don't allow intervals less that 1
|
# don't allow intervals less that 1
|
||||||
|
@@ -3,13 +3,13 @@ import unittest
|
|||||||
from test import test_util as tu
|
from test import test_util as tu
|
||||||
tu.path_append_parent()
|
tu.path_append_parent()
|
||||||
|
|
||||||
from gen.utils import GrampsDBCallback
|
from gen.utils import Callback
|
||||||
|
|
||||||
class TestGrampsDBCallback(unittest.TestCase):
|
class TestCallback(unittest.TestCase):
|
||||||
|
|
||||||
def test_simple(self):
|
def test_simple(self):
|
||||||
|
|
||||||
class TestSignals(GrampsDBCallback):
|
class TestSignals(Callback):
|
||||||
|
|
||||||
__signals__ = {
|
__signals__ = {
|
||||||
'test-signal' : (int,)
|
'test-signal' : (int,)
|
||||||
@@ -30,7 +30,7 @@ class TestGrampsDBCallback(unittest.TestCase):
|
|||||||
|
|
||||||
def test_exception_catch(self):
|
def test_exception_catch(self):
|
||||||
|
|
||||||
class TestSignals(GrampsDBCallback):
|
class TestSignals(Callback):
|
||||||
|
|
||||||
__signals__ = {
|
__signals__ = {
|
||||||
'test-signal' : (int,)
|
'test-signal' : (int,)
|
||||||
@@ -68,7 +68,7 @@ class TestGrampsDBCallback(unittest.TestCase):
|
|||||||
|
|
||||||
def test_disconnect(self):
|
def test_disconnect(self):
|
||||||
|
|
||||||
class TestSignals(GrampsDBCallback):
|
class TestSignals(Callback):
|
||||||
|
|
||||||
__signals__ = {
|
__signals__ = {
|
||||||
'test-signal' : (int,)
|
'test-signal' : (int,)
|
||||||
@@ -95,7 +95,7 @@ class TestGrampsDBCallback(unittest.TestCase):
|
|||||||
|
|
||||||
def test_noargs(self):
|
def test_noargs(self):
|
||||||
|
|
||||||
class TestSignals(GrampsDBCallback):
|
class TestSignals(Callback):
|
||||||
|
|
||||||
__signals__ = {
|
__signals__ = {
|
||||||
'test-noargs' : None
|
'test-noargs' : None
|
||||||
@@ -115,7 +115,7 @@ class TestGrampsDBCallback(unittest.TestCase):
|
|||||||
|
|
||||||
def test_no_callback(self):
|
def test_no_callback(self):
|
||||||
|
|
||||||
class TestSignals(GrampsDBCallback):
|
class TestSignals(Callback):
|
||||||
|
|
||||||
__signals__ = {
|
__signals__ = {
|
||||||
'test-noargs' : None
|
'test-noargs' : None
|
||||||
@@ -126,7 +126,7 @@ class TestGrampsDBCallback(unittest.TestCase):
|
|||||||
|
|
||||||
def test_subclassing(self):
|
def test_subclassing(self):
|
||||||
|
|
||||||
class TestSignals(GrampsDBCallback):
|
class TestSignals(Callback):
|
||||||
__signals__ = {
|
__signals__ = {
|
||||||
'test-signal' : (int,)
|
'test-signal' : (int,)
|
||||||
}
|
}
|
||||||
@@ -155,7 +155,7 @@ class TestGrampsDBCallback(unittest.TestCase):
|
|||||||
|
|
||||||
def test_signal_block(self):
|
def test_signal_block(self):
|
||||||
|
|
||||||
class TestSignals(GrampsDBCallback):
|
class TestSignals(Callback):
|
||||||
|
|
||||||
__signals__ = {
|
__signals__ = {
|
||||||
'test-signal' : (int,)
|
'test-signal' : (int,)
|
||||||
@@ -172,11 +172,11 @@ class TestGrampsDBCallback(unittest.TestCase):
|
|||||||
assert len(rl) == 1, "No signal emitted"
|
assert len(rl) == 1, "No signal emitted"
|
||||||
assert rl[0] == 1, "Wrong argument recieved"
|
assert rl[0] == 1, "Wrong argument recieved"
|
||||||
|
|
||||||
GrampsDBCallback.disable_all_signals()
|
Callback.disable_all_signals()
|
||||||
t.emit('test-signal',(1,))
|
t.emit('test-signal',(1,))
|
||||||
assert len(rl) == 1, "Signal emitted while class blocked"
|
assert len(rl) == 1, "Signal emitted while class blocked"
|
||||||
|
|
||||||
GrampsDBCallback.enable_all_signals()
|
Callback.enable_all_signals()
|
||||||
t.emit('test-signal',(1,))
|
t.emit('test-signal',(1,))
|
||||||
assert len(rl) == 2, "Signals not class unblocked"
|
assert len(rl) == 2, "Signals not class unblocked"
|
||||||
|
|
||||||
@@ -190,7 +190,7 @@ class TestGrampsDBCallback(unittest.TestCase):
|
|||||||
|
|
||||||
def test_type_checking(self):
|
def test_type_checking(self):
|
||||||
|
|
||||||
class TestSignals(GrampsDBCallback):
|
class TestSignals(Callback):
|
||||||
__signals__ = {
|
__signals__ = {
|
||||||
'test-int' : (int,),
|
'test-int' : (int,),
|
||||||
'test-list': (list,),
|
'test-list': (list,),
|
||||||
@@ -238,10 +238,10 @@ class TestGrampsDBCallback(unittest.TestCase):
|
|||||||
|
|
||||||
def test_recursion_block(self):
|
def test_recursion_block(self):
|
||||||
|
|
||||||
class TestSignals(GrampsDBCallback):
|
class TestSignals(Callback):
|
||||||
|
|
||||||
__signals__ = {
|
__signals__ = {
|
||||||
'test-recursion' : (GrampsDBCallback,)
|
'test-recursion' : (Callback,)
|
||||||
}
|
}
|
||||||
|
|
||||||
def fn(cb):
|
def fn(cb):
|
||||||
@@ -265,12 +265,12 @@ class TestGrampsDBCallback(unittest.TestCase):
|
|||||||
|
|
||||||
def test_multisignal_recursion_block(self):
|
def test_multisignal_recursion_block(self):
|
||||||
|
|
||||||
class TestSignals(GrampsDBCallback):
|
class TestSignals(Callback):
|
||||||
|
|
||||||
__signals__ = {
|
__signals__ = {
|
||||||
'test-top' : (GrampsDBCallback,),
|
'test-top' : (Callback,),
|
||||||
'test-middle' : (GrampsDBCallback,),
|
'test-middle' : (Callback,),
|
||||||
'test-bottom' : (GrampsDBCallback,)
|
'test-bottom' : (Callback,)
|
||||||
}
|
}
|
||||||
|
|
||||||
def top(cb):
|
def top(cb):
|
||||||
|
Reference in New Issue
Block a user