* src/ReorderIDs.py: bring up to speed with new handle/id methodology,

svn: r4488
This commit is contained in:
Don Allingham 2005-05-05 20:10:21 +00:00
parent 5e93c54df9
commit 277f5bee18
2 changed files with 41 additions and 40 deletions

View File

@ -1,3 +1,6 @@
2005-05-05 Don Allingham <don@gramps-project.org>
* src/ReorderIDs.py: bring up to speed with new handle/id methodology,
2005-05-05 Martin Hawlisch <Martin.Hawlisch@gmx.de> 2005-05-05 Martin Hawlisch <Martin.Hawlisch@gmx.de>
* src/GenericFilter.py: Fix copy-n-paste errors; * src/GenericFilter.py: Fix copy-n-paste errors;
dont crash on empty database dont crash on empty database

View File

@ -39,6 +39,7 @@ from gettext import gettext as _
# #
#------------------------------------------------------------------------ #------------------------------------------------------------------------
import Utils import Utils
import RelLib
from QuestionDialog import WarningDialog from QuestionDialog import WarningDialog
_findint = re.compile('^[^\d]*(\d+)[^\d]*') _findint = re.compile('^[^\d]*(\d+)[^\d]*')
@ -46,14 +47,14 @@ _findint = re.compile('^[^\d]*(\d+)[^\d]*')
def runTool(db,active_person,callback,parent): def runTool(db,active_person,callback,parent):
"""Changed person, family, object, source, and place ids""" """Changed person, family, object, source, and place ids"""
#FIXME -- Remove when the tool is back from the dead #FIXME -- Remove when the tool is back from the dead
WarningDialog(_('Tool currently unavailable'), #WarningDialog(_('Tool currently unavailable'),
_('This tool has not yet been brought up to date ' # _('This tool has not yet been brought up to date '
'after transition to the database, sorry.'),parent.topWindow) # 'after transition to the database, sorry.'),parent.topWindow)
return #return
#FIXME #FIXME
try: try:
ReorderIds(db,callback) ReorderIds(db)
except: except:
import DisplayTrace import DisplayTrace
DisplayTrace.DisplayTrace() DisplayTrace.DisplayTrace()
@ -65,31 +66,43 @@ def runTool(db,active_person,callback,parent):
#------------------------------------------------------------------------- #-------------------------------------------------------------------------
class ReorderIds: class ReorderIds:
def __init__(self,db,callback): def __init__(self,db):
self.db = db self.db = db
self.reorder_person() self.trans = db.transaction_begin()
self.reorder(db.get_family_handle_map(),db.fprefix,None) self.reorder(RelLib.Person, db.get_person_from_gramps_id, db.get_person_from_handle,
self.reorder(db.get_object_map(),db.oprefix,None) db.find_next_person_gramps_id, db.get_person_cursor, db.commit_person,
self.reorder(db.get_source_map(),db.sprefix,db.build_source_display) db.iprefix)
self.reorder(db.get_place_handle_map(),db.pprefix,db.build_place_display) self.reorder(RelLib.Family,db.get_family_from_gramps_id, db.get_family_from_handle,
callback(1) db.find_next_family_gramps_id, db.get_family_cursor, db.commit_family,
db.fprefix)
self.reorder(RelLib.MediaObject, db.get_object_from_gramps_id, db.get_object_from_handle,
db.find_next_object_gramps_id, db.get_media_cursor, db.commit_media_object,
db.oprefix)
self.reorder(RelLib.Source, db.get_source_from_gramps_id, db.get_source_from_handle,
db.find_next_source_gramps_id, db.get_source_cursor, db.commit_source,
db.sprefix)
self.reorder(RelLib.Place, db.get_place_from_gramps_id, db.get_place_from_handle,
db.find_next_place_gramps_id, db.get_place_cursor, db.commit_place,
db.pprefix)
db.transaction_commit(self.trans,_("Reorder gramps IDs"))
def reorder_person(self): def reorder(self, class_type, find_from_id, find_from_handle, find_next_id, get_cursor, commit, prefix):
dups = [] dups = []
newids = {} newids = {}
key_list = [] key_list = []
# search all ids in the map # search all ids in the map
cursor = self.db.get_person_cursor() cursor = get_cursor()
data = cursor.first() data = cursor.first()
while data: while data:
(handle,sdata) = data (handle,sdata) = data
gramps_id = sdata[1] obj = class_type()
obj.unserialize(sdata)
gramps_id = obj.get_gramps_id()
# attempt to extract integer, if we can't, treat it as a # attempt to extract integer, if we can't, treat it as a
# duplicate # duplicate
@ -104,42 +117,27 @@ class ReorderIds:
newgramps_id = prefix % int(index) newgramps_id = prefix % int(index)
if newgramps_id == gramps_id: if newgramps_id == gramps_id:
newids[newgramps_id] = gramps_id newids[newgramps_id] = gramps_id
continue elif find_from_id(newgramps_id) != None:
elif data_map.has_key(newgramps_id): dups.append(obj.get_handle())
dups.append(handle)
else: else:
data = data_map[gramps_id]
data_map[newgramps_id] = data
newids[newgramps_id] = gramps_id
data.set_gramps_id(newgramps_id) data.set_gramps_id(newgramps_id)
del data_map[gramps_id] commit(obj,self.trans)
if update: newids[newgramps_id] = gramps_id
update(newgramps_id,gramps_id)
except: except:
dups.append(handle) dups.append(handle)
else: else:
dups.append(handle) dups.append(handle)
data = cursor.next() data = cursor.next()
cursor.close()
# go through the duplicates, looking for the first availble # go through the duplicates, looking for the first availble
# handle that matches the new scheme. # handle that matches the new scheme.
index = 0 for handle in dups:
for gramps_id in dups: obj = find_from_handle(handle)
while 1: obj.set_gramps_id(find_next_id())
newgramps_id = prefix % index commit(obj,self.trans)
if not newids.has_key(newgramps_id):
break
index = index + 1
newids[newgramps_id] = newgramps_id
data = data_map[gramps_id]
data.set_gramps_id(newgramps_id)
data_map[newgramps_id] = data
if update:
update(newgramps_id,gramps_id)
del data_map[gramps_id]
#------------------------------------------------------------------------- #-------------------------------------------------------------------------
# #