diff --git a/ChangeLog b/ChangeLog index ae2e1e989..9a2caa974 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,31 @@ +2005-12-21 Richard Taylor + * src/GrampsDb: new package for all the db related modules + * src/GedcomInfo.py moved to src/GrampsDb/_GedcomInfo.py + * src/GrampsBSDDB.py moved to src/GrampsDb/_GrampsBSDDB.py + * src/GrampsDBCallback.py moved to src/GrampsDb/_GrampsDBCallback.py + * src/GrampsDbBase.py moved to src/GrampsDb/_GrampsDbBase.py + * src/GrampsGEDDB.py moved to src/GrampsDb/_GrampsGEDDB.py + * src/GrampsInMemDB.py moved to src/GrampsDb/_GrampsInMemDB.py + * src/GrampsXMLDB.py moved to src/GrampsDb/_GrampsXMLDB.py + * src/ReadGedcom.py moved to src/GrampsDb/_ReadGedcom.py + * src/eadGrdb.py moved to src/GrampsDb/_ReadGrdb.py + * src/ReadXML.py moved to src/GrampsDb/_ReadXML.py + * src/WriteGedcom.py moved to src/GrampsDb/_WriteGedcom.py + * src/WriteGrdb.py moved to src/GrampsDb/_WriteGrdb.py + * src/WriteXML.py moved to src/GrampsDb/_WriteXML.py + * src/GrampsDb/__init__.py: new package export file + * src/GrampsDb/_GrampsDbExceptions.py: new module for GrampDb exceptions + * src/GrampsDb/_GrampsDbFactories.py: new module for factory methods + * test/RelLib: new test directory + * test/RelLib: new test directory + * test/GrampsDbBase_Test.py moved to test/GrampsDb/GrampsDbBase_Test.py + * test/GrampsDbTestBase.py moved to test/GrampsDb/GrampsDbTestBase.py + * test/RelLib_Test.py moved to test/RelLib/RelLib_Test.py + * src/ArgHandler.py src/DbPrompter.py src/DisplayState.py + src/Exporter.py src/RepositoryRefEdit.py src/ViewManager.py + src/gramps_main.py: modified to work with new GrampsDb package + * test/RunAllTests.py: find tests in subdirectories + 2005-12-21 Richard Taylor * src/RelLib/_SourceRef.py: added import for Note class diff --git a/src/ArgHandler.py b/src/ArgHandler.py index bfd69a6bc..36389c99e 100644 --- a/src/ArgHandler.py +++ b/src/ArgHandler.py @@ -41,7 +41,7 @@ from gettext import gettext as _ # #------------------------------------------------------------------------- import const -import ReadXML +import GrampsDb import GrampsMime import DbPrompter import QuestionDialog @@ -407,18 +407,17 @@ class ArgHandler: Any errors will cause the os._exit(1) call. """ if format == 'grdb': - import ReadGrdb filename = os.path.normpath(os.path.abspath(filename)) try: - ReadGrdb.importData(self.parent.db,filename,None) + GrampsDb.gramps_db_reader_factory(const.app_gramps)(self.parent.db,filename,None) except: print "Error importing %s" % filename os._exit(1) elif format == 'gedcom': - import ReadGedcom + from GrampsDb import GedcomParser filename = os.path.normpath(os.path.abspath(filename)) try: - g = ReadGedcom.GedcomParser(self.parent.db,filename,None) + g = GedcomParser(self.parent.db,filename,None) g.parse_gedcom_file() g.resolve_refns() del g @@ -427,7 +426,7 @@ class ArgHandler: os._exit(1) elif format == 'gramps-xml': try: - ReadXML.importData(self.parent.db,filename,None,self.parent.cl) + GrampsDb.gramps_db_reader_factory(const.app_gramps_xml)(self.parent.db,filename,None,self.parent.cl) except: print "Error importing %s" % filename os._exit(1) @@ -469,7 +468,7 @@ class ArgHandler: dbname = os.path.join(tmpdir_path,const.xmlFile) try: - ReadXML.importData(self.parent.db,dbname,None) + GrampsDb.gramps_db_reader_factory(const.app_gramps_xml)(self.parent.db,dbname,None) except: print "Error importing %s" % filename os._exit(1) @@ -496,16 +495,14 @@ class ArgHandler: Any errors will cause the os._exit(1) call. """ if format == 'grdb': - import WriteGrdb try: - WriteGrdb.exportData(self.parent.db,filename) + GrampsDb.gramps_db_writer_factory(const.app_gramps)(self.parent.db,filename) except: print "Error exporting %s" % filename os._exit(1) elif format == 'gedcom': - import WriteGedcom try: - gw = WriteGedcom.GedcomWriter(self.parent.db,None,1,filename) + gw = GrampsDb.GedcomWriter(self.parent.db,None,1,filename) ret = gw.export_data(filename) except: print "Error exporting %s" % filename @@ -514,8 +511,7 @@ class ArgHandler: filename = os.path.normpath(os.path.abspath(filename)) if filename: try: - import WriteXML - g = WriteXML.XmlWriter(self.parent.db,None,1,1) + g = GrampsDb.XmlWriter(self.parent.db,None,1,1) ret = g.write(filename) except: print "Error exporting %s" % filename diff --git a/src/DbPrompter.py b/src/DbPrompter.py index 736aa3312..8c45fcedf 100644 --- a/src/DbPrompter.py +++ b/src/DbPrompter.py @@ -50,15 +50,9 @@ import Utils import const import QuestionDialog import PluginMgr -import GrampsBSDDB -import GrampsXMLDB -import GrampsGEDDB +import GrampsDb import GrampsKeys import RecentFiles -import ReadGrdb -import WriteGrdb -import WriteXML -import WriteGedcom import GrampsDisplay from bsddb import db @@ -353,18 +347,16 @@ class ImportDbPrompter: if filetype == const.app_gramps: choose.destroy() - ReadGrdb.importData(self.parent.db,filename) + GrampsDb.gramps_db_reader_factory(filetype)(self.parent.db,filename) self.parent.import_tool_callback() return True elif filetype == const.app_gramps_xml: choose.destroy() - import ReadXML - ReadXML.importData(self.parent.db,filename,self.parent.update_bar) + GrampsDb.gramps_db_reader_factory(filetype)(self.parent.db,filename,self.parent.update_bar) return True elif filetype == const.app_gedcom: choose.destroy() - import ReadGedcom - ReadGedcom.importData(self.parent.db,filename) + GrampsDb.gramps_db_reader_factory(filetype)(self.parent.db,filename) return True (the_path,the_file) = os.path.split(filename) @@ -443,7 +435,7 @@ class NewNativeDbPrompter: close(self.parent) except: pass - self.parent.db = GrampsBSDDB.GrampsBSDDB() + self.parent.db = GrampsDb.gramps_db_factory(const.app_gramps)() self.parent.read_file(filename) # Add the file to the recent items RecentFiles.recent_files(filename,const.app_gramps) @@ -530,17 +522,17 @@ class NewSaveasDbPrompter: _('File type "%s" is unknown to GRAMPS.\n\nValid types are: GRAMPS database, GRAMPS XML, GRAMPS package, and GEDCOM.') % filetype) return False if filetype == const.app_gramps: - WriteGrdb.exportData(self.parent.db,filename,None,None) + GrampsDb.gramps_db_writer_factory(filetype)(self.parent.db,filename,None,None) close(self.parent) - self.parent.db = GrampsBSDDB.GrampsBSDDB() + self.parent.db = GrampsDb.gramps_db_factory(filetype)() elif filetype == const.app_gramps_xml: - WriteXML.exportData(self.parent.db,filename,None,None) + GrampsDb.gramps_db_writer_factory(filetype)(self.parent.db,filename,None,None) close(self.parent) - self.parent.db = GrampsXMLDB.GrampsXMLDB() + self.parent.db = GrampsDb.gramps_db_factory(filetype)() elif filetype == const.app_gedcom: - WriteGedcom.exportData(self.parent.db,filename,None,None) + GrampsDb.gramps_db_writer_factory(filetype)(self.parent.db,filename,None,None) close(self.parent) - self.parent.db = GrampsGEDDB.GrampsGEDDB() + self.parent.db = GrampsDb.gramps_db_factory(filetype)() self.parent.read_file(filename) # Add the file to the recent items RecentFiles.recent_files(filename,const.app_gramps) @@ -574,8 +566,8 @@ def open_native(parent,filename,filetype): GrampsKeys.save_last_import_dir(the_path) success = False - if filetype == const.app_gramps: - state.db = GrampsBSDDB.GrampsBSDDB() + if filetype == const.app_gramps: + state.db = GrampsDb.gramps_db_factory(filetype)() msgxml = gtk.glade.XML(const.gladeFile, "load_message","gramps") msg_top = msgxml.get_widget('load_message') msg_label = msgxml.get_widget('message') @@ -589,10 +581,10 @@ def open_native(parent,filename,filetype): success = self.read_file(filename,update_msg) msg_top.destroy() elif filetype == const.app_gramps_xml: - state.db = GrampsXMLDB.GrampsXMLDB() + state.db = GrampsDb.gramps_db_factory(filetype)() success = self.read_file(filename) elif filetype == const.app_gedcom: - state.db = GrampsGEDDB.GrampsGEDDB() + state.db = GrampsDb.gramps_db_factory(filetype)() success = self.read_file(filename) #if success: diff --git a/src/DisplayState.py b/src/DisplayState.py index f9eb65283..d553edd24 100644 --- a/src/DisplayState.py +++ b/src/DisplayState.py @@ -40,8 +40,7 @@ import gtk # GRAMPS modules # #------------------------------------------------------------------------- -import GrampsDbBase -import GrampsDBCallback +import GrampsDb import GrampsKeys import NameDisplay @@ -50,7 +49,7 @@ import NameDisplay # History manager # #------------------------------------------------------------------------- -class History(GrampsDBCallback.GrampsDBCallback): +class History(GrampsDb.GrampsDBCallback): __signals__ = { 'changed' : (list,), @@ -58,7 +57,7 @@ class History(GrampsDBCallback.GrampsDBCallback): } def __init__(self): - GrampsDBCallback.GrampsDBCallback.__init__(self) + GrampsDb.GrampsDBCallback.__init__(self) self.history = [] self.mhistory = [] self.index = -1 @@ -405,7 +404,7 @@ class ManagedWindow: # Gramps Display State class # #------------------------------------------------------------------------- -class DisplayState(GrampsDBCallback.GrampsDBCallback): +class DisplayState(GrampsDb.GrampsDBCallback): __signals__ = { } @@ -414,7 +413,7 @@ class DisplayState(GrampsDBCallback.GrampsDBCallback): self.dbstate = dbstate self.uimanager = uimanager self.window = window - GrampsDBCallback.GrampsDBCallback.__init__(self) + GrampsDb.GrampsDBCallback.__init__(self) self.status = status self.status_id = status.get_context_id('GRAMPS') self.phistory = History() diff --git a/src/Exporter.py b/src/Exporter.py index e1cb27012..cb2fdd5cc 100644 --- a/src/Exporter.py +++ b/src/Exporter.py @@ -365,8 +365,7 @@ class Exporter: In the future, filter and other options may be added. """ try: - import WriteGrdb - WriteGrdb.exportData(database,filename,person) + GrampsDb.gramps_db_writer_factory(const.app_gramps)(database,filename,person) return 1 except IOError, msg: QuestionDialog.ErrorDialog( _("Could not write file: %s") % filename, diff --git a/src/GedcomInfo.py b/src/GrampsDb/_GedcomInfo.py similarity index 100% rename from src/GedcomInfo.py rename to src/GrampsDb/_GedcomInfo.py diff --git a/src/GrampsBSDDB.py b/src/GrampsDb/_GrampsBSDDB.py similarity index 99% rename from src/GrampsBSDDB.py rename to src/GrampsDb/_GrampsBSDDB.py index 19a52a9a6..b7987d94a 100644 --- a/src/GrampsBSDDB.py +++ b/src/GrampsDb/_GrampsBSDDB.py @@ -50,7 +50,7 @@ except NameError: # #------------------------------------------------------------------------- from RelLib import * -from GrampsDbBase import * +from _GrampsDbBase import * _MINVERSION = 5 _DBVERSION = 9 diff --git a/src/GrampsDBCallback.py b/src/GrampsDb/_GrampsDBCallback.py similarity index 100% rename from src/GrampsDBCallback.py rename to src/GrampsDb/_GrampsDBCallback.py diff --git a/src/GrampsDbBase.py b/src/GrampsDb/_GrampsDbBase.py similarity index 99% rename from src/GrampsDbBase.py rename to src/GrampsDb/_GrampsDbBase.py index 2bd35f973..6aea70d2e 100644 --- a/src/GrampsDbBase.py +++ b/src/GrampsDb/_GrampsDbBase.py @@ -49,7 +49,7 @@ log = sys.stderr.write #------------------------------------------------------------------------- from RelLib import * import GrampsKeys -import GrampsDBCallback +from _GrampsDBCallback import GrampsDBCallback #------------------------------------------------------------------------- # @@ -144,7 +144,7 @@ class GrampsCursor: """ pass -class GrampsDbBase(GrampsDBCallback.GrampsDBCallback): +class GrampsDbBase(GrampsDBCallback): """ GRAMPS database object. This object is a base class for all database interfaces. @@ -196,7 +196,7 @@ class GrampsDbBase(GrampsDBCallback.GrampsDBCallback): be created. """ - GrampsDBCallback.GrampsDBCallback.__init__(self) + GrampsDBCallback.__init__(self) self.readonly = False self.rand = random.Random(time.time()) @@ -1671,7 +1671,7 @@ class Transaction: return self.last - self.first + 1 return 0 -class DbState(GrampsDBCallback.GrampsDBCallback): +class DbState(GrampsDBCallback): __signals__ = { 'database-changed' : (GrampsDbBase,), @@ -1680,7 +1680,7 @@ class DbState(GrampsDBCallback.GrampsDBCallback): } def __init__(self): - GrampsDBCallback.GrampsDBCallback.__init__(self) + GrampsDBCallback.__init__(self) self.db = GrampsDbBase() self.open = False self.active = None diff --git a/src/GrampsDb/_GrampsDbExceptions.py b/src/GrampsDb/_GrampsDbExceptions.py new file mode 100644 index 000000000..dc06f7139 --- /dev/null +++ b/src/GrampsDb/_GrampsDbExceptions.py @@ -0,0 +1,32 @@ +# +# Gramps - a GTK+/GNOME based genealogy program +# +# Copyright (C) 2004-2005 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$ + +"""Exceptions generated by the GrampsDb package.""" + + +class GrampsDbException(Exception): + + def __init__(self, value): + self.value = value + + def __str__(self): + return repr(self.value) diff --git a/src/GrampsDb/_GrampsDbFactories.py b/src/GrampsDb/_GrampsDbFactories.py new file mode 100644 index 000000000..b3ec01edd --- /dev/null +++ b/src/GrampsDb/_GrampsDbFactories.py @@ -0,0 +1,123 @@ +# +# Gramps - a GTK+/GNOME based genealogy program +# +# Copyright (C) 2004-2005 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$ + +""" +This module contains factory methods for accessing the different +GrampsDb backends. These methods should be used obtain the correct class +for a database backend. + +The app_* constants in const.py can be used to indicate which backend is +required e.g.: + + # To get the class for the grdb backend + db_class = GrampsDb.gramps_db_factory(db_type = const.app_gramps) + + # To get a XML writer + GrampsDb.gramps_db_writer_factory(db_type = const.app_gramps_xml) + + # To get a Gedcom reader + GrampsDb.gramps_db_reader_factory(db_type = const.app_gedcom) + +""" +import const + +from _GrampsDbExceptions import GrampsDbException + +def gramps_db_factory(db_type): + """Factory class for obtaining a Gramps database backend. + + @param db_type: the type of backend required. + @type db_type: one of the app_* constants in const.py + + Raises GrampsDbException if the db_type is not recognised. + """ + + if db_type == const.app_gramps: + from _GrampsBSDDB import GrampsBSDDB + cls = GrampsBSDDB + elif db_type == const.app_gramps_xml: + from _GrampsXMLDB import GrampsXMLDB + cls = GrampsXMLDB + elif db_type == const.app_gedcom: + from _GrampsGEDDB import GrampsGEDDB + cls = GrampsGEDDB + else: + raise GrampsDbException("Attempt to create unknown " + "database backend class: " + "db_type = %s" % (str(db_type),)) + + return cls + + +def gramps_db_writer_factory(db_type): + """Factory class for obtaining a Gramps database writers. + + + @param db_type: the type of backend required. + @type db_type: one of the app_* constants in const.py + + Raises GrampsDbException if the db_type is not recognised. + """ + + if db_type == const.app_gramps: + import _WriteGrdb as WriteGrdb + md = WriteGrdb.exportData + elif db_type == const.app_gramps_xml: + import _WriteXML as WriteXML + md = WriteXML.exportData + elif db_type == const.app_gedcom: + import _WriteGedcom as WriteGedcom + md = WriteGedcom.exportData + else: + raise GrampsDbException("Attempt to create a database " + "writer for unknown format: " + "db_type = %s" % (str(db_type),)) + + return md + +def gramps_db_reader_factory(db_type): + """Factory class for obtaining a Gramps database writers. + + @param db_type: the type of backend required. + @type db_type: one of the app_* constants in const.py + + Raises GrampsDbException if the db_type is not recognised. + """ + + if db_type == const.app_gramps: + import _ReadGrdb as ReadGrdb + md = ReadGrdb.importData + elif db_type == const.app_gramps_xml: + import _ReadXML as ReadXML + md = ReadXML.importData + elif db_type == const.app_gedcom: + import _ReadGedcom as ReadGedcom + md = ReadGedcom.importData + else: + raise GrampsDbException("Attempt to create a database " + "reader for unknown format: " + "db_type = %s" % (str(db_type),)) + + return md + + + diff --git a/src/GrampsGEDDB.py b/src/GrampsDb/_GrampsGEDDB.py similarity index 94% rename from src/GrampsGEDDB.py rename to src/GrampsDb/_GrampsGEDDB.py index 80af5f64f..090255ab6 100644 --- a/src/GrampsGEDDB.py +++ b/src/GrampsDb/_GrampsGEDDB.py @@ -26,10 +26,10 @@ of GEDCOM files. """ from RelLib import * -from GrampsInMemDB import * +from _GrampsInMemDB import * -import ReadGedcom -import WriteGedcom +import _ReadGedcom as ReadGedcom +import _WriteGedcom as WriteGedcom #------------------------------------------------------------------------- # diff --git a/src/GrampsInMemDB.py b/src/GrampsDb/_GrampsInMemDB.py similarity index 99% rename from src/GrampsInMemDB.py rename to src/GrampsDb/_GrampsInMemDB.py index 84f44d765..0e91b8e8c 100644 --- a/src/GrampsInMemDB.py +++ b/src/GrampsDb/_GrampsInMemDB.py @@ -33,7 +33,7 @@ from bsddb import dbshelve, db # #------------------------------------------------------------------------- from RelLib import * -from GrampsDbBase import * +from _GrampsDbBase import * import sets class GrampsInMemCursor(GrampsCursor): diff --git a/src/GrampsXMLDB.py b/src/GrampsDb/_GrampsXMLDB.py similarity index 95% rename from src/GrampsXMLDB.py rename to src/GrampsDb/_GrampsXMLDB.py index e17d25467..8980b86fb 100644 --- a/src/GrampsXMLDB.py +++ b/src/GrampsDb/_GrampsXMLDB.py @@ -26,10 +26,10 @@ of GRAMPS XML format. """ from RelLib import * -from GrampsInMemDB import * +from _GrampsInMemDB import * -import ReadXML -import WriteXML +import _ReadXML as ReadXML +import _WriteXML as WriteXML #------------------------------------------------------------------------- # diff --git a/src/ReadGedcom.py b/src/GrampsDb/_ReadGedcom.py similarity index 99% rename from src/ReadGedcom.py rename to src/GrampsDb/_ReadGedcom.py index b2a1f66e2..8f1d3c71c 100644 --- a/src/ReadGedcom.py +++ b/src/GrampsDb/_ReadGedcom.py @@ -57,7 +57,7 @@ from ansel_utf8 import ansel_to_utf8 import Utils import GrampsMime from bsddb import db -from GedcomInfo import * +from _GedcomInfo import * from QuestionDialog import ErrorDialog, WarningDialog #------------------------------------------------------------------------- diff --git a/src/ReadGrdb.py b/src/GrampsDb/_ReadGrdb.py similarity index 99% rename from src/ReadGrdb.py rename to src/GrampsDb/_ReadGrdb.py index aa2668849..e54393c1a 100644 --- a/src/ReadGrdb.py +++ b/src/GrampsDb/_ReadGrdb.py @@ -37,7 +37,7 @@ import sets # Gramps Modules # #------------------------------------------------------------------------- -import GrampsBSDDB +from _GrampsBSDDB import GrampsBSDDB from QuestionDialog import ErrorDialog import Errors @@ -50,7 +50,7 @@ def importData(database, filename, callback=None,cl=0,use_trans=True): filename = os.path.normpath(filename) - other_database = GrampsBSDDB.GrampsBSDDB() + other_database = GrampsBSDDB() try: other_database.load(filename,callback) except: diff --git a/src/ReadXML.py b/src/GrampsDb/_ReadXML.py similarity index 100% rename from src/ReadXML.py rename to src/GrampsDb/_ReadXML.py diff --git a/src/WriteGedcom.py b/src/GrampsDb/_WriteGedcom.py similarity index 99% rename from src/WriteGedcom.py rename to src/GrampsDb/_WriteGedcom.py index 4cdbf4cf8..ba0b0e52f 100644 --- a/src/WriteGedcom.py +++ b/src/GrampsDb/_WriteGedcom.py @@ -49,7 +49,7 @@ import RelLib import GenericFilter import const import Date -import GedcomInfo +import _GedcomInfo as GedcomInfo import Errors import ansel_utf8 import Utils diff --git a/src/WriteGrdb.py b/src/GrampsDb/_WriteGrdb.py similarity index 97% rename from src/WriteGrdb.py rename to src/GrampsDb/_WriteGrdb.py index 98d1e472b..d0691047d 100644 --- a/src/WriteGrdb.py +++ b/src/GrampsDb/_WriteGrdb.py @@ -35,7 +35,7 @@ from gettext import gettext as _ # Gramps Modules # #------------------------------------------------------------------------- -import GrampsBSDDB +from _GrampsBSDDB import GrampsBSDDB from QuestionDialog import ErrorDialog #------------------------------------------------------------------------- @@ -47,7 +47,7 @@ def exportData(database, filename, person=None, callback=None, cl=False): filename = os.path.normpath(filename) - new_database = GrampsBSDDB.GrampsBSDDB() + new_database = GrampsBSDDB() try: new_database.load(filename,callback) except: diff --git a/src/WriteXML.py b/src/GrampsDb/_WriteXML.py similarity index 100% rename from src/WriteXML.py rename to src/GrampsDb/_WriteXML.py diff --git a/src/GrampsDb/__init__.py b/src/GrampsDb/__init__.py new file mode 100644 index 000000000..9be06825c --- /dev/null +++ b/src/GrampsDb/__init__.py @@ -0,0 +1,56 @@ +# +# Gramps - a GTK+/GNOME based genealogy program +# +# Copyright (C) 2004-2005 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$ + +""" +This package implements the GrampsDb database. It provides a number +of different backends for different storage mechanisms. + +A number of importers and exporters are provided to convert between +the different backend formats. + +To obtain a class that implements the backend required you should use the +gramps_db_factory method, likewise for writers use the gramps_db_writer_factory +method and for readers use the gramps_db_reader_factory method. For information +on using these factories see the _GrampsDbFactories.py file comments. + +The package also contains GrampsDBCallback which provides signal/slot type +functionality to allow objects to hook into signals that are generated from +the database objects. Read the comments in _GrampsDBCallback.py for more +information. +""" + +from _GrampsDbBase import DbState + +from _GrampsDbFactories import \ + gramps_db_factory, \ + gramps_db_writer_factory, \ + gramps_db_reader_factory + + +from _ReadGedcom import GedcomParser +from _WriteGedcom import GedcomWriter +from _WriteXML import XmlWriter + +from _GrampsDbExceptions import GrampsDbException + +from _GrampsDBCallback import GrampsDBCallback + diff --git a/src/RepositoryRefEdit.py b/src/RepositoryRefEdit.py index c7fb815af..af90c9898 100644 --- a/src/RepositoryRefEdit.py +++ b/src/RepositoryRefEdit.py @@ -50,7 +50,6 @@ import RelLib import Date import DateEdit import DateHandler -import GrampsDBCallback import AutoComp diff --git a/src/ViewManager.py b/src/ViewManager.py index 5d6eff546..21d703a29 100644 --- a/src/ViewManager.py +++ b/src/ViewManager.py @@ -51,10 +51,7 @@ import DbPrompter import const import PluginMgr import GrampsKeys -import GrampsDbBase -import GrampsBSDDB -import GrampsGEDDB -import GrampsXMLDB +import GrampsDb import GrampsCfg import Errors import DisplayTrace @@ -556,7 +553,7 @@ class ViewManager: self.state.db.close() except: pass - self.state.change_database(GrampsBSDDB.GrampsBSDDB()) + self.state.change_database(GrampsDb.gramps_db_factory(app_gramps)()) self.read_file(filename) self.state.db.request_rebuild() self.change_page(None,None) @@ -580,7 +577,7 @@ class ViewManager: success = False if filetype == const.app_gramps: - self.state.change_database(GrampsBSDDB.GrampsBSDDB()) + self.state.change_database(GrampsDb.gramps_db_factory(db_type = const.app_gramps)()) msgxml = gtk.glade.XML(const.gladeFile, "load_message","gramps") msg_top = msgxml.get_widget('load_message') msg_label = msgxml.get_widget('message') @@ -596,12 +593,12 @@ class ViewManager: self.change_page(None,None) msg_top.destroy() elif filetype == const.app_gramps_xml: - self.state.change_database(GrampsXMLDB.GrampsXMLDB()) + self.state.change_database(GrampsDb.gramps_db_factory(db_type = const.app_gramps_xml)()) success = self.read_file(filename) self.state.db.request_rebuild() self.change_page(None,None) elif filetype == const.app_gedcom: - self.state.change_database(GrampsGEDDB.GrampsGEDDB()) + self.state.change_database(GrampsDb.gramps_db_factory(db_type = const.app_gedcom)()) success = self.read_file(filename) self.state.db.request_rebuild() self.change_page(None,None) @@ -781,20 +778,18 @@ class ViewManager: if filetype == const.app_gramps: choose.destroy() - ReadGrdb.importData(self.state.db,filename) + GrampsDb.gramps_db_reader_factory(filetype)(self.state.db,filename) self.parent.import_tool_callback() return True elif filetype == const.app_gramps_xml: choose.destroy() - import ReadXML self.progress.show() - ReadXML.importData(self.state.db,filename,self.pulse_progressbar) + GrampsDb.gramps_db_reader_factory(filetype)(self.state.db,filename,self.pulse_progressbar) self.progress.hide() return True elif filetype == const.app_gedcom: choose.destroy() - import ReadGedcom - ReadGedcom.importData(self.state.db,filename) + GrampsDb.gramps_db_reader_factory(filetype)(self.state.db,filename) return True (the_path,the_file) = os.path.split(filename) diff --git a/src/gramps_main.py b/src/gramps_main.py index 48e53b756..1f9c7a596 100755 --- a/src/gramps_main.py +++ b/src/gramps_main.py @@ -38,8 +38,7 @@ import PersonView import RepositoryView import GrampsDisplay import RelLib -import GrampsDbBase -import GrampsBSDDB +import GrampsDb import PedView import MapView import FamilyView @@ -155,7 +154,7 @@ class Gramps: register_stock_icons() - state = GrampsDbBase.DbState() + state = GrampsDb.DbState() vm = ViewManager.ViewManager(state) vm.register_view(PersonView.PersonView) vm.register_view(FamilyView.FamilyView) diff --git a/test/GrampsDbBase_Test.py b/test/GrampsDb/GrampsDbBase_Test.py similarity index 75% rename from test/GrampsDbBase_Test.py rename to test/GrampsDb/GrampsDbBase_Test.py index 771f70f16..636d33504 100644 --- a/test/GrampsDbBase_Test.py +++ b/test/GrampsDb/GrampsDbBase_Test.py @@ -13,14 +13,64 @@ try: set() except NameError: from sets import Set as set - -import GrampsBSDDB + +import const import RelLib logger = logging.getLogger('Gramps.GrampsDbBase_Test') from GrampsDbTestBase import GrampsDbBaseTest - +import GrampsDb + +class FactoryTest(unittest.TestCase): + """Test the GrampsDb Factory classes.""" + + def test_gramps_db_factory(self): + """test than gramps_db_factory returns the correct classes.""" + + cls = GrampsDb.gramps_db_factory(db_type = const.app_gramps) + assert cls.__name__ == "GrampsBSDDB", \ + "Returned class is %s " % str(cls.__class__.__name__) + + cls = GrampsDb.gramps_db_factory(db_type = const.app_gramps_xml) + assert cls.__name__ == "GrampsXMLDB", \ + "Returned class is %s " % str(cls.__class__.__name__) + + cls = GrampsDb.gramps_db_factory(db_type = const.app_gedcom) + assert cls.__name__ == "GrampsGEDDB", \ + "Returned class is %s " % str(cls.__class__.__name__) + + self.assertRaises(GrampsDb.GrampsDbException, GrampsDb.gramps_db_factory, "gibberish") + + def test_gramps_db_writer_factory(self): + """Test that gramps_db_writer_factory returns the correct method.""" + + md = GrampsDb.gramps_db_writer_factory(db_type = const.app_gramps) + assert callable(md), "Returned method is %s " % str(md) + + md = GrampsDb.gramps_db_writer_factory(db_type = const.app_gramps_xml) + assert callable(md), "Returned method is %s " % str(md) + + md = GrampsDb.gramps_db_writer_factory(db_type = const.app_gedcom) + assert callable(md), "Returned method is %s " % str(md) + + self.assertRaises(GrampsDb.GrampsDbException, GrampsDb.gramps_db_writer_factory, "gibberish") + + def test_gramps_db_reader_factory(self): + """Test that gramps_db_reader_factory returns the correct method.""" + + md = GrampsDb.gramps_db_reader_factory(db_type = const.app_gramps) + assert callable(md), "Returned method is %s " % str(md) + + md = GrampsDb.gramps_db_reader_factory(db_type = const.app_gramps_xml) + assert callable(md), "Returned method is %s " % str(md) + + md = GrampsDb.gramps_db_reader_factory(db_type = const.app_gedcom) + assert callable(md), "Returned method is %s " % str(md) + + self.assertRaises(GrampsDb.GrampsDbException, GrampsDb.gramps_db_reader_factory, "gibberish") + + class ReferenceMapTest (GrampsDbBaseTest): """Test methods on the GrampsDbBase class that are related to the reference_map @@ -180,6 +230,7 @@ class ReferenceMapTest (GrampsDbBaseTest): def testSuite(): suite = unittest.makeSuite(ReferenceMapTest,'test') + suite.addTests(unittest.makeSuite(FactoryTest,'test')) return suite def perfSuite(): diff --git a/test/GrampsDbTestBase.py b/test/GrampsDb/GrampsDbTestBase.py similarity index 98% rename from test/GrampsDbTestBase.py rename to test/GrampsDb/GrampsDbTestBase.py index 7b34c442e..aec3f16fa 100644 --- a/test/GrampsDbTestBase.py +++ b/test/GrampsDb/GrampsDbTestBase.py @@ -14,7 +14,8 @@ try: except NameError: from sets import Set as set -import GrampsBSDDB +import GrampsDb +import const import RelLib logger = logging.getLogger('Gramps.GrampsDbTestBase') @@ -27,7 +28,7 @@ class GrampsDbBaseTest(unittest.TestCase): self._tmpdir = tempfile.mkdtemp() self._filename = os.path.join(self._tmpdir,'test.grdb') - self._db = GrampsBSDDB.GrampsBSDDB() + self._db = GrampsDb.gramps_db_factory(const.app_gramps)() self._db.load(self._filename, None, # callback "w") diff --git a/test/RelLib_Test.py b/test/RelLib/RelLib_Test.py similarity index 81% rename from test/RelLib_Test.py rename to test/RelLib/RelLib_Test.py index f2d8670cb..9f363bca3 100644 --- a/test/RelLib_Test.py +++ b/test/RelLib/RelLib_Test.py @@ -7,7 +7,6 @@ import time import traceback import sys -sys.path.append('../src') try: set() @@ -19,9 +18,8 @@ import RelLib logger = logging.getLogger('Gramps.RelLib_Test') -from GrampsDbTestBase import GrampsDbBaseTest -class PrimaryObjectTest (GrampsDbBaseTest): +class PrimaryObjectTest (unittest.TestCase): """Test methods on the PrimaryObject class""" diff --git a/test/RunAllTests.py b/test/RunAllTests.py index a6c061666..e4c87a445 100644 --- a/test/RunAllTests.py +++ b/test/RunAllTests.py @@ -5,9 +5,12 @@ import logging import os +import sys import unittest from optparse import OptionParser +sys.path.append('../src') + def make_parser(): usage = "usage: %prog [options]" parser = OptionParser(usage) @@ -19,18 +22,29 @@ def make_parser(): def getTestSuites(): + # Sorry about this line, but it is the easiest way of doing it. + # It just walks the filetree from '.' downwards and returns + # a tuple per directory of (dirpatch,filelist) if the directory + # contains any test files. - test_modules = [ i for i in os.listdir('.') if i[-8:] == "_Test.py" ] + paths = [(f[0],f[2]) for f in os.walk('.') \ + if len ([i for i in f[2] \ + if i[-8:] == "_Test.py"]) ] - test_suites = [] - perf_suites = [] - for module in test_modules: - mod = __import__(module[:-3]) - test_suites.append(mod.testSuite()) - try: - perf_suites.append(mod.perfSuite()) - except: - pass + for (dir,test_modules) in paths: + sys.path.append(dir) + + test_suites = [] + perf_suites = [] + for module in test_modules: + if module[-8:] != "_Test.py": + break + mod = __import__(module[:-3]) + test_suites.append(mod.testSuite()) + try: + perf_suites.append(mod.perfSuite()) + except: + pass return (test_suites,perf_suites)