From e385af623d9c76ec029f76767e26ae43a4cc32de Mon Sep 17 00:00:00 2001 From: Benny Malengier Date: Fri, 7 Dec 2007 22:47:40 +0000 Subject: [PATCH] 2007-11-24 Benny Malengier Add support for name grouping import/export * src/GrampsDb/_GrampsDbBase.py: obtain grouping keys * src/GrampsDb/_ReadXML.py: read in group table * src/GrampsDb/_DbUtils.py: grdb -> grdb copy of grouping table * src/GrampsDb/_ReadGrdb.py: read in group table * src/GrampsDb/_WriteXML.py: write group table out * src/GrampsDb/_GrampsBSDDB.py: group table is no sec table, init it together with primary tables! svn: r9457 --- ChangeLog | 10 ++++++++++ src/GrampsDb/_DbUtils.py | 10 ++++++++-- src/GrampsDb/_GrampsBSDDB.py | 16 ++++++++++------ src/GrampsDb/_GrampsDbBase.py | 6 ++++++ src/GrampsDb/_ReadGrdb.py | 16 ++++++++++++++++ src/GrampsDb/_ReadXML.py | 21 +++++++++++++++++++++ src/GrampsDb/_WriteXML.py | 13 +++++++++++++ 7 files changed, 84 insertions(+), 8 deletions(-) diff --git a/ChangeLog b/ChangeLog index 3dd0d5f56..c2a9743e7 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,13 @@ +2007-11-24 Benny Malengier + Add support for name grouping import/export + * src/GrampsDb/_GrampsDbBase.py: obtain grouping keys + * src/GrampsDb/_ReadXML.py: read in group table + * src/GrampsDb/_DbUtils.py: grdb -> grdb copy of grouping table + * src/GrampsDb/_ReadGrdb.py: read in group table + * src/GrampsDb/_WriteXML.py: write group table out + * src/GrampsDb/_GrampsBSDDB.py: group table is no sec table, init it + together with primary tables! + 2007-11-28 Jim Sack * src/GrampsDb/_ReadGedcom.py : #1399 remove trail whitespace (readahead) fixes discarded reference due to bogus handles that get created diff --git a/src/GrampsDb/_DbUtils.py b/src/GrampsDb/_DbUtils.py index 2508c024a..76c24fb30 100644 --- a/src/GrampsDb/_DbUtils.py +++ b/src/GrampsDb/_DbUtils.py @@ -242,7 +242,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'] @@ -259,8 +259,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 diff --git a/src/GrampsDb/_GrampsBSDDB.py b/src/GrampsDb/_GrampsBSDDB.py index d298e71eb..889cfcb5c 100644 --- a/src/GrampsDb/_GrampsBSDDB.py +++ b/src/GrampsDb/_GrampsBSDDB.py @@ -399,8 +399,16 @@ class GrampsBSDDB(GrampsDbBase,UpdateCallback): dbtype=db.DB_BTREE) callback(37) - self._load_metadata() + 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) if not self.readonly: @@ -529,11 +537,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", @@ -1117,6 +1120,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/_GrampsDbBase.py b/src/GrampsDb/_GrampsDbBase.py index 3b017f6be..7fc63cd31 100644 --- a/src/GrampsDb/_GrampsDbBase.py +++ b/src/GrampsDb/_GrampsDbBase.py @@ -1135,6 +1135,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/GrampsDb/_ReadGrdb.py b/src/GrampsDb/_ReadGrdb.py index aa8f582c7..e68805a8b 100644 --- a/src/GrampsDb/_ReadGrdb.py +++ b/src/GrampsDb/_ReadGrdb.py @@ -196,6 +196,22 @@ def importData(database, filename, callback=None,cl=0,use_trans=True): database.media_bookmarks.append_list(other_database.media_bookmarks.get()) database.repo_bookmarks.append_list(other_database.repo_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() diff --git a/src/GrampsDb/_ReadXML.py b/src/GrampsDb/_ReadXML.py index 1d0859a9d..478a0b1d5 100644 --- a/src/GrampsDb/_ReadXML.py +++ b/src/GrampsDb/_ReadXML.py @@ -393,8 +393,10 @@ class GrampsParser(UpdateCallback): "gender" : (None, self.stop_gender), "header" : (None, None), "last" : (self.start_last, self.stop_last), + "map" : (self.start_namemap, None), "mother" : (self.start_mother,None), "name" : (self.start_name, self.stop_name), + "namemaps" : (None, None), "nick" : (None, self.stop_nick), "note" : (self.start_note, self.stop_note), "p" : (None, self.stop_ptag), @@ -451,6 +453,9 @@ class GrampsParser(UpdateCallback): "rname" : (None, self.stop_rname), } + def errmsg(self, msg): + log.warning(msg) + def find_person_by_gramps_id(self,gramps_id): intid = self.gid2id.get(gramps_id) if intid: @@ -1112,6 +1117,22 @@ 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/GrampsDb/_WriteXML.py b/src/GrampsDb/_WriteXML.py index bb99ed2c6..0bcad1749 100644 --- a/src/GrampsDb/_WriteXML.py +++ b/src/GrampsDb/_WriteXML.py @@ -321,9 +321,22 @@ class XmlWriter(UpdateCallback): # Data is written, now write bookmarks. self.write_bookmarks() + self.write_namemaps() self.g.write("\n") + 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())