From 027036a8c5b49ddafd7e17cf9bbef47599f5a09f Mon Sep 17 00:00:00 2001 From: Benny Malengier Date: Sun, 9 Dec 2007 10:18:59 +0000 Subject: [PATCH] 2007-12-09 Benny Malengier Add support for name grouping import/export * src/GrampsDb/_GrampsDbWriteXML.py: write group table out * src/GrampsDb/_GrampsBSDDB.py: group table is no sec table, init it together with primary tables! * src/gen/db/base.py: obtain grouping keys * src/gen/db/dbdir.py: group table is no sec table, init it together with primary tables! * src/gen/utils/dbutils.py: grdb -> grdb copy of grouping table, copy mediapath. * src/gen/proxy/proxybase.py: obtain grouping keys, add missing bookmark methods * src/gen/proxy/dbbase.py: add obtain grouping keys method * src/GrampsDbUtils/_ReadXML.py: read in group table * src/plugins/ReadGrdb.py: read in group table svn: r9464 --- ChangeLog | 16 +++++++++++++ src/GrampsDb/_GrampsBSDDB.py | 22 ++++++++++++------ src/GrampsDb/_GrampsDbWriteXML.py | 15 ++++++++++-- src/GrampsDbUtils/_ReadXML.py | 21 +++++++++++++++-- src/gen/db/base.py | 7 +++++- src/gen/db/dbdir.py | 16 ++++++++----- src/gen/proxy/dbbase.py | 6 +++++ src/gen/proxy/proxybase.py | 38 +++++++++++++++++++++++++++++++ src/gen/utils/dbutils.py | 14 ++++++++++-- src/plugins/ReadGrdb.py | 16 +++++++++++++ 10 files changed, 151 insertions(+), 20 deletions(-) diff --git a/ChangeLog b/ChangeLog index ccca1717d..16bcf393c 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,19 @@ +2007-12-09 Benny Malengier + Add support for name grouping import/export + * src/GrampsDb/_GrampsDbWriteXML.py: write group table out + * src/GrampsDb/_GrampsBSDDB.py: group table is no sec table, init it + together with primary tables! + * src/gen/db/base.py: obtain grouping keys + * src/gen/db/dbdir.py: group table is no sec table, init it + together with primary tables! + * src/gen/utils/dbutils.py: grdb -> grdb copy of grouping table, + copy mediapath. + * src/gen/proxy/proxybase.py: obtain grouping keys, add missing + bookmark methods + * src/gen/proxy/dbbase.py: add obtain grouping keys method + * src/GrampsDbUtils/_ReadXML.py: read in group table + * src/plugins/ReadGrdb.py: read in group table + 2007-12-07 Douglas S. Blank * src/PluginUtils/Makefile.am: Missing _PluginsWindows.py * po/test: new unittest dir diff --git a/src/GrampsDb/_GrampsBSDDB.py b/src/GrampsDb/_GrampsBSDDB.py index 7c6448973..d12e5be27 100644 --- a/src/GrampsDb/_GrampsBSDDB.py +++ b/src/GrampsDb/_GrampsBSDDB.py @@ -165,8 +165,11 @@ class GrampsBSDDBDupCursor(GrampsBSDDBAssocCursor): # #------------------------------------------------------------------------- class GrampsBSDDB(GrampsDbBase, UpdateCallback): - """GRAMPS database object. This object is a base class for other - objects.""" + """ GRAMPS database object for Berkeley DB. + This is replaced for internal use by gen/db/dbdir.py + However, this class is still used for import of the 2.2.x + GRDB format. In 3.0+ this format is no longer used. + """ def __init__(self, use_txn = True): """creates a new GrampsDB""" @@ -443,6 +446,15 @@ class GrampsBSDDB(GrampsDbBase, UpdateCallback): self.reference_map = self.__open_table(self.full_name, "reference_map", dbtype=db.DB_BTREE) + self.name_group = db.DB(self.env) + self.name_group.set_flags(db.DB_DUP) + if self.readonly: + self.name_group.open(self.full_name, "name_group", + db.DB_HASH, flags=db.DB_RDONLY) + else: + self.name_group.open(self.full_name, "name_group", + db.DB_HASH, flags=self.__open_flags()) + self.__load_metadata() gstats = self.metadata.get('gender_stats', default=None) @@ -587,11 +599,6 @@ class GrampsBSDDB(GrampsDbBase, UpdateCallback): self.surnames.open(self.full_name, "surnames", db.DB_BTREE, flags=table_flags) - self.name_group = db.DB(self.env) - self.name_group.set_flags(db.DB_DUP) - self.name_group.open(self.full_name, "name_group", - db.DB_HASH, flags=table_flags) - self.id_trans = db.DB(self.env) self.id_trans.set_flags(db.DB_DUP) self.id_trans.open(self.full_name, "idtrans", @@ -1189,6 +1196,7 @@ class GrampsBSDDB(GrampsDbBase, UpdateCallback): self.media_map = None self.event_map = None self.surnames = None + self.name_group = None self.env = None self.metadata = None self.db_is_open = False diff --git a/src/GrampsDb/_GrampsDbWriteXML.py b/src/GrampsDb/_GrampsDbWriteXML.py index 3a7f509cf..0a620950a 100644 --- a/src/GrampsDb/_GrampsDbWriteXML.py +++ b/src/GrampsDb/_GrampsDbWriteXML.py @@ -342,8 +342,7 @@ class GrampsDbXmlWriter(UpdateCallback): # Data is written, now write bookmarks. self.write_bookmarks() - - self.write_metadata() + self.write_namemaps() self.g.write("\n") @@ -357,6 +356,18 @@ class GrampsDbXmlWriter(UpdateCallback): if mediapath is not None: self.write_line("mediapath", mediapath, 2) + def write_namemaps(self): + group_map = self.db.get_name_group_keys() + name_len = len(group_map) + + if name_len > 0: + self.g.write(" \n") + for key in group_map: + value = self.db.get_name_group_mapping(key) + self.g.write(' \n' + % (key, value) ) + self.g.write(" \n") + def write_bookmarks(self): bm_person_len = len(self.db.bookmarks.get()) bm_family_len = len(self.db.family_bookmarks.get()) diff --git a/src/GrampsDbUtils/_ReadXML.py b/src/GrampsDbUtils/_ReadXML.py index 22bd21e7b..a5e2f0ecb 100644 --- a/src/GrampsDbUtils/_ReadXML.py +++ b/src/GrampsDbUtils/_ReadXML.py @@ -393,10 +393,12 @@ class GrampsParser(UpdateCallback): "call" : (None, self.stop_call), "gender" : (None, self.stop_gender), "header" : (None, None), - "last" : (self.start_last, self.stop_last), + "last" : (self.start_last, self.stop_last), + "map" : (self.start_namemap, None), "mediapath" : (None, self.stop_mediapath), "mother" : (self.start_mother, None), - "name" : (self.start_name, self.stop_name), + "name" : (self.start_name, self.stop_name), + "namemaps" : (None, None), "nick" : (None, self.stop_nick), "note" : (self.start_note, self.stop_note), "noteref" : (self.start_noteref, None), @@ -1170,6 +1172,21 @@ class GrampsParser(UpdateCallback): except KeyError: pass + def start_namemap(self, attrs): + type = attrs.get('type') + key = attrs['key'] + value = attrs['value'] + if type == 'group_as': + if self.db.has_name_group_key(key) : + present = self.db.get_name_group_mapping(key) + if not value == present: + msg = _("Your family tree groups name %s together" + " with %s, did not change this grouping to %s") % ( + key, present, value) + self.errmsg(msg) + else: + self.db.set_name_group_mapping(key, value) + def start_last(self, attrs): self.name.prefix = attrs.get('prefix', '') self.name.group_as = attrs.get('group', '') diff --git a/src/gen/db/base.py b/src/gen/db/base.py index b9888a02f..3dc7c6015 100644 --- a/src/gen/db/base.py +++ b/src/gen/db/base.py @@ -262,7 +262,6 @@ class GrampsDbBase(GrampsDBCallback): self.note_bookmarks = GrampsDbBookmarks() self._bm_changes = 0 self.path = "" - self.name_group = {} self.surname_list = [] def set_prefixes(self, person, media, family, source, place, event, @@ -1198,6 +1197,12 @@ class GrampsDbBase(GrampsDBCallback): """ return [unicode(k) for k in self.name_group.keys()] + def has_name_group_key(self, name): + """ + Return if a key exists in the name_group table + """ + return self.name_group.has_key(str(name)) + def set_name_group_mapping(self, name, group): """ Sets the default grouping name for a surname. Needs to be overridden diff --git a/src/gen/db/dbdir.py b/src/gen/db/dbdir.py index 8c8177e28..b3c7b879c 100644 --- a/src/gen/db/dbdir.py +++ b/src/gen/db/dbdir.py @@ -532,7 +532,15 @@ class GrampsDBDir(GrampsDbBase, UpdateCallback): dbtype=db.DB_BTREE) if callback: callback(37) - + + self.name_group = db.DB(self.env) + self.name_group.set_flags(db.DB_DUP) + if self.readonly: + self.name_group.open(_mkname(self.full_name, NAME_GROUP), NAME_GROUP, + db.DB_HASH, flags=db.DB_RDONLY) + else: + self.name_group.open(_mkname(self.full_name, NAME_GROUP), NAME_GROUP, + db.DB_HASH, flags=self.__open_flags()) self.__load_metadata() gstats = self.metadata.get('gender_stats', default=None) @@ -696,11 +704,6 @@ class GrampsDBDir(GrampsDbBase, UpdateCallback): self.surnames.open(_mkname(self.full_name, SURNAMES), SURNAMES, db.DB_BTREE, flags=table_flags) - self.name_group = db.DB(self.env) - self.name_group.set_flags(db.DB_DUP) - self.name_group.open(_mkname(self.full_name, NAME_GROUP), NAME_GROUP, - db.DB_HASH, flags=table_flags) - self.id_trans = db.DB(self.env) self.id_trans.set_flags(db.DB_DUP) self.id_trans.open(_mkname(self.full_name, IDTRANS), IDTRANS, @@ -1290,6 +1293,7 @@ class GrampsDBDir(GrampsDbBase, UpdateCallback): self.media_map = None self.event_map = None self.surnames = None + self.name_group = None self.env = None self.metadata = None self.db_is_open = False diff --git a/src/gen/proxy/dbbase.py b/src/gen/proxy/dbbase.py index 5dd6a92bb..8ca7ca1a1 100644 --- a/src/gen/proxy/dbbase.py +++ b/src/gen/proxy/dbbase.py @@ -533,6 +533,12 @@ class DbBase: """ raise NotImplementedError + def has_name_group_key(self, name): + """ + Return if a key exists in the name_group table + """ + raise NotImplementedError + def get_name_group_keys(self): """ Returns the defined names that have been assigned to a default grouping diff --git a/src/gen/proxy/proxybase.py b/src/gen/proxy/proxybase.py index a29256168..b47a164c2 100644 --- a/src/gen/proxy/proxybase.py +++ b/src/gen/proxy/proxybase.py @@ -67,6 +67,12 @@ class ProxyDbBase(DbBase): """ return self.db.get_name_group_mapping(name) + def has_name_group_key(self, name): + """ + Return if a key exists in the name_group table + """ + return self.db.has_name_group_key(name) + def get_name_group_keys(self): """ Returns the defined names that have been assigned to a default grouping @@ -446,3 +452,35 @@ class ProxyDbBase(DbBase): def has_gramps_id(self, obj_key, gramps_id): return self.db.has_gramps_ids(obj_key, gramps_id) + + def get_bookmarks(self): + """returns the list of Person handles in the bookmarks""" + return self.bookmarks + + def get_family_bookmarks(self): + """returns the list of Person handles in the bookmarks""" + return self.family_bookmarks + + def get_event_bookmarks(self): + """returns the list of Person handles in the bookmarks""" + return self.event_bookmarks + + def get_place_bookmarks(self): + """returns the list of Person handles in the bookmarks""" + return self.place_bookmarks + + def get_source_bookmarks(self): + """returns the list of Person handles in the bookmarks""" + return self.source_bookmarks + + def get_media_bookmarks(self): + """returns the list of Person handles in the bookmarks""" + return self.media_bookmarks + + def get_repo_bookmarks(self): + """returns the list of Person handles in the bookmarks""" + return self.repo_bookmarks + + def get_note_bookmarks(self): + """returns the list of Note handles in the bookmarks""" + return self.note_bookmarks diff --git a/src/gen/utils/dbutils.py b/src/gen/utils/dbutils.py index d19d2f515..e5c5d2659 100644 --- a/src/gen/utils/dbutils.py +++ b/src/gen/utils/dbutils.py @@ -251,7 +251,7 @@ def db_copy(from_db,to_db,callback): } # Start batch transaction to use async TXN and other tricks - trans = to_db.transaction_begin("",batch=True) + trans = to_db.transaction_begin("", batch=True) for table_name in tables.keys(): cursor_func = tables[table_name]['cursor_func'] @@ -268,8 +268,14 @@ def db_copy(from_db,to_db,callback): uc.update() cursor.close() + # Copy name grouping + group_map = from_db.get_name_group_keys() + for key in group_map: + value = from_db.get_name_group_mapping(key) + to_db.set_name_group_mapping(key, value) + # Commit batch transaction: does nothing, except undoing the tricks - to_db.transaction_commit(trans,"") + to_db.transaction_commit(trans, "") # Copy bookmarks over: # we already know that there's no overlap in handles anywhere @@ -288,6 +294,10 @@ def db_copy(from_db,to_db,callback): # Copy db owner to_db.owner = from_db.owner + # Copy other selected metadata + if from_db.get_mediapath() is not None: + to_db.set_mediapath(from_db.get_mediapath()) + def set_birth_death_index(db, person): birth_ref_index = -1 death_ref_index = -1 diff --git a/src/plugins/ReadGrdb.py b/src/plugins/ReadGrdb.py index 9063a554f..ad22b9208 100644 --- a/src/plugins/ReadGrdb.py +++ b/src/plugins/ReadGrdb.py @@ -230,6 +230,22 @@ def importData(database, filename, callback=None, cl=0, use_trans=True): database.repo_bookmarks.append_list(other_database.repo_bookmarks.get()) database.note_bookmarks.append_list(other_database.note_bookmarks.get()) + # Copy grouping table + group_map = other_database.get_name_group_keys() + name_len = len(group_map) + if name_len > 0: + for key in group_map: + value = other_database.get_name_group_mapping(key) + if database.has_name_group_key(key) : + present = database.get_name_group_mapping(key) + if not value == present: + msg = _("Your family tree groups name %s together" + " with %s, did not change this grouping to %s") % ( + key, present, value) + print msg + else: + database.set_name_group_mapping(key, value) + # close the other database and clean things up other_database.close()