2002-10-20 19:55:16 +05:30
|
|
|
#
|
|
|
|
# Gramps - a GTK+/GNOME based genealogy program
|
|
|
|
#
|
2004-05-13 06:19:17 +05:30
|
|
|
# Copyright (C) 2000-2004 Donald N. Allingham
|
2002-10-20 19:55:16 +05:30
|
|
|
#
|
|
|
|
# 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
|
|
|
|
#
|
|
|
|
|
2003-11-07 21:59:27 +05:30
|
|
|
# $Id$
|
|
|
|
|
2002-10-20 19:55:16 +05:30
|
|
|
"""
|
|
|
|
Change id IDs of all the elements in the database to conform to the
|
|
|
|
scheme specified in the database's prefix ids
|
|
|
|
"""
|
|
|
|
|
|
|
|
import re
|
|
|
|
import Utils
|
2003-08-17 07:44:33 +05:30
|
|
|
from gettext import gettext as _
|
2002-10-20 19:55:16 +05:30
|
|
|
|
|
|
|
_findint = re.compile('^[^\d]*(\d+)[^\d]*')
|
|
|
|
|
|
|
|
def runTool(db,active_person,callback):
|
|
|
|
"""Changed person, family, object, source, and place ids"""
|
|
|
|
try:
|
|
|
|
ReorderIds(db,callback)
|
|
|
|
except:
|
|
|
|
import DisplayTrace
|
|
|
|
DisplayTrace.DisplayTrace()
|
|
|
|
|
|
|
|
class ReorderIds:
|
|
|
|
|
|
|
|
def __init__(self,db,callback):
|
2003-06-16 10:27:25 +05:30
|
|
|
|
2002-10-20 19:55:16 +05:30
|
|
|
self.db = db
|
|
|
|
|
2004-02-14 11:10:30 +05:30
|
|
|
self.reorder(db.get_person_id_map(),db.iprefix,db.build_person_display)
|
|
|
|
self.reorder(db.get_family_id_map(),db.fprefix,None)
|
|
|
|
self.reorder(db.get_object_map(),db.oprefix,None)
|
|
|
|
self.reorder(db.get_source_map(),db.sprefix,db.build_source_display)
|
|
|
|
self.reorder(db.get_place_id_map(),db.pprefix,db.build_place_display)
|
2003-11-07 21:59:27 +05:30
|
|
|
Utils.history_broken()
|
2002-10-20 19:55:16 +05:30
|
|
|
callback(1)
|
2003-06-16 10:27:25 +05:30
|
|
|
|
2002-10-20 19:55:16 +05:30
|
|
|
def reorder(self,data_map,prefix,update):
|
|
|
|
"""Try to extract the old integer out of the id, and reuse it
|
|
|
|
if possible. Otherwise, blindly renumber those that can't."""
|
|
|
|
|
|
|
|
dups = []
|
2003-07-06 09:22:38 +05:30
|
|
|
newids = {}
|
2002-10-20 19:55:16 +05:30
|
|
|
key_list = []
|
|
|
|
|
|
|
|
# search all ids in the map
|
|
|
|
|
|
|
|
for x in data_map.keys():
|
|
|
|
key_list.append(x)
|
|
|
|
|
|
|
|
for id in key_list:
|
|
|
|
|
|
|
|
# attempt to extract integer, if we can't, treat it as a
|
|
|
|
# duplicate
|
|
|
|
|
|
|
|
match = _findint.match(id)
|
|
|
|
if match:
|
|
|
|
# get the integer, build the new id. Make sure it
|
|
|
|
# hasn't already been chosen. If it has, put this
|
|
|
|
# in the duplicate id list
|
|
|
|
|
2003-06-30 08:37:24 +05:30
|
|
|
try:
|
|
|
|
index = match.groups()[0]
|
|
|
|
newid = prefix % int(index)
|
|
|
|
if newid == id:
|
2003-07-06 09:22:38 +05:30
|
|
|
newids[newid] = id
|
2003-06-30 08:37:24 +05:30
|
|
|
continue
|
|
|
|
elif data_map.has_key(newid):
|
|
|
|
dups.append(id)
|
|
|
|
else:
|
|
|
|
data = data_map[id]
|
|
|
|
data_map[newid] = data
|
2003-07-06 09:22:38 +05:30
|
|
|
newids[newid] = id
|
2004-02-14 11:10:30 +05:30
|
|
|
data.set_id(newid)
|
2003-06-30 08:37:24 +05:30
|
|
|
del data_map[id]
|
|
|
|
if update:
|
|
|
|
update(newid,id)
|
|
|
|
except:
|
2002-10-20 19:55:16 +05:30
|
|
|
dups.append(id)
|
|
|
|
else:
|
|
|
|
dups.append(id)
|
|
|
|
|
|
|
|
# go through the duplicates, looking for the first availble
|
|
|
|
# id that matches the new scheme.
|
|
|
|
|
|
|
|
index = 0
|
|
|
|
for id in dups:
|
|
|
|
while 1:
|
|
|
|
newid = prefix % index
|
2003-07-06 09:22:38 +05:30
|
|
|
if not newids.has_key(newid):
|
2002-10-20 19:55:16 +05:30
|
|
|
break
|
|
|
|
index = index + 1
|
2003-07-06 09:22:38 +05:30
|
|
|
newids[newid] = newid
|
2002-10-20 19:55:16 +05:30
|
|
|
data = data_map[id]
|
2004-02-14 11:10:30 +05:30
|
|
|
data.set_id(newid)
|
2002-10-20 19:55:16 +05:30
|
|
|
data_map[newid] = data
|
|
|
|
if update:
|
|
|
|
update(newid,id)
|
|
|
|
del data_map[id]
|
|
|
|
|
|
|
|
#-------------------------------------------------------------------------
|
|
|
|
#
|
|
|
|
#
|
|
|
|
#
|
|
|
|
#-------------------------------------------------------------------------
|
|
|
|
from Plugins import register_tool
|
|
|
|
|
|
|
|
register_tool(
|
|
|
|
runTool,
|
|
|
|
_("Reorder gramps IDs"),
|
|
|
|
category=_("Database Processing"),
|
|
|
|
description=_("Reorders the gramps IDs according to gramps' default rules.")
|
|
|
|
)
|
|
|
|
|