2002-10-20 19:55:16 +05:30
|
|
|
#
|
|
|
|
# Gramps - a GTK+/GNOME based genealogy program
|
|
|
|
#
|
2006-03-11 06:42:06 +05:30
|
|
|
# Copyright (C) 2000-2006 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
|
|
|
|
#
|
|
|
|
|
2008-02-23 14:08:55 +05:30
|
|
|
# $Id:ReorderIds.py 9912 2008-01-22 09:17:46Z acraphae $
|
2003-11-07 21:59:27 +05:30
|
|
|
|
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
|
|
|
|
"""
|
|
|
|
|
2005-03-25 05:54:37 +05:30
|
|
|
#------------------------------------------------------------------------
|
|
|
|
#
|
|
|
|
# standard python modules
|
|
|
|
#
|
|
|
|
#------------------------------------------------------------------------
|
2002-10-20 19:55:16 +05:30
|
|
|
import re
|
2006-04-07 03:32:46 +05:30
|
|
|
from gettext import gettext as _
|
2002-10-20 19:55:16 +05:30
|
|
|
|
2005-03-25 05:54:37 +05:30
|
|
|
#------------------------------------------------------------------------
|
|
|
|
#
|
|
|
|
# GRAMPS modules
|
|
|
|
#
|
|
|
|
#------------------------------------------------------------------------
|
|
|
|
import Utils
|
2007-10-08 22:11:39 +05:30
|
|
|
import gen.lib
|
2006-03-11 06:42:06 +05:30
|
|
|
from PluginUtils import Tool, register_tool
|
2005-03-25 05:54:37 +05:30
|
|
|
|
2002-10-20 19:55:16 +05:30
|
|
|
_findint = re.compile('^[^\d]*(\d+)[^\d]*')
|
|
|
|
|
2005-03-25 05:54:37 +05:30
|
|
|
#-------------------------------------------------------------------------
|
|
|
|
#
|
|
|
|
# Actual tool
|
|
|
|
#
|
|
|
|
#-------------------------------------------------------------------------
|
2006-05-04 07:35:50 +05:30
|
|
|
class ReorderIds(Tool.BatchTool):
|
2008-02-23 14:08:55 +05:30
|
|
|
def __init__(self, dbstate, uistate, options_class, name, callback=None):
|
|
|
|
Tool.BatchTool.__init__(self, dbstate, options_class, name)
|
2006-05-04 07:35:50 +05:30
|
|
|
if self.fail:
|
|
|
|
return
|
2005-12-06 12:08:09 +05:30
|
|
|
|
2006-04-27 07:26:42 +05:30
|
|
|
db = dbstate.db
|
|
|
|
self.uistate = uistate
|
|
|
|
if uistate:
|
2005-12-06 12:08:09 +05:30
|
|
|
self.progress = Utils.ProgressMeter(_('Reordering GRAMPS IDs'),'')
|
|
|
|
else:
|
|
|
|
print "Reordering GRAMPS IDs..."
|
2006-04-27 07:26:42 +05:30
|
|
|
|
2008-02-23 14:08:55 +05:30
|
|
|
self.trans = db.transaction_begin("", batch=True)
|
2006-05-11 05:36:43 +05:30
|
|
|
db.disable_signals()
|
2005-08-18 11:28:28 +05:30
|
|
|
|
2006-04-27 07:26:42 +05:30
|
|
|
if uistate:
|
2005-12-06 12:08:09 +05:30
|
|
|
self.progress.set_pass(_('Reordering People IDs'),
|
|
|
|
db.get_number_of_people())
|
2007-10-08 22:11:39 +05:30
|
|
|
self.reorder(gen.lib.Person,
|
2005-12-06 12:08:09 +05:30
|
|
|
db.get_person_from_gramps_id,
|
|
|
|
db.get_person_from_handle,
|
|
|
|
db.find_next_person_gramps_id,
|
2006-05-04 09:35:57 +05:30
|
|
|
db.person_map,
|
2005-12-06 12:08:09 +05:30
|
|
|
db.commit_person,
|
2008-02-23 14:08:55 +05:30
|
|
|
db.person_prefix)
|
2005-12-06 12:08:09 +05:30
|
|
|
|
2006-04-27 07:26:42 +05:30
|
|
|
if uistate:
|
2005-12-06 12:08:09 +05:30
|
|
|
self.progress.set_pass(_('Reordering Family IDs'),
|
|
|
|
db.get_number_of_families())
|
2007-10-08 22:11:39 +05:30
|
|
|
self.reorder(gen.lib.Family,
|
2005-12-06 12:08:09 +05:30
|
|
|
db.get_family_from_gramps_id,
|
|
|
|
db.get_family_from_handle,
|
|
|
|
db.find_next_family_gramps_id,
|
2006-05-04 09:35:57 +05:30
|
|
|
db.family_map,
|
2005-12-06 12:08:09 +05:30
|
|
|
db.commit_family,
|
2008-02-23 14:08:55 +05:30
|
|
|
db.family_prefix)
|
2006-04-27 07:26:42 +05:30
|
|
|
if uistate:
|
|
|
|
self.progress.set_pass(_('Reordering Event IDs'),
|
|
|
|
db.get_number_of_events())
|
2007-10-08 22:11:39 +05:30
|
|
|
self.reorder(gen.lib.Event,
|
2006-04-27 07:26:42 +05:30
|
|
|
db.get_event_from_gramps_id,
|
|
|
|
db.get_event_from_handle,
|
|
|
|
db.find_next_event_gramps_id,
|
2006-05-04 09:35:57 +05:30
|
|
|
db.event_map,
|
2006-04-27 07:26:42 +05:30
|
|
|
db.commit_event,
|
2008-02-23 14:08:55 +05:30
|
|
|
db.event_prefix)
|
2006-04-27 07:26:42 +05:30
|
|
|
if uistate:
|
2005-12-06 12:08:09 +05:30
|
|
|
self.progress.set_pass(_('Reordering Media Object IDs'),
|
|
|
|
db.get_number_of_media_objects())
|
2007-10-08 22:11:39 +05:30
|
|
|
self.reorder(gen.lib.MediaObject,
|
2005-12-06 12:08:09 +05:30
|
|
|
db.get_object_from_gramps_id,
|
|
|
|
db.get_object_from_handle,
|
|
|
|
db.find_next_object_gramps_id,
|
2006-05-04 09:35:57 +05:30
|
|
|
db.media_map,
|
2005-12-06 12:08:09 +05:30
|
|
|
db.commit_media_object,
|
2008-02-23 14:08:55 +05:30
|
|
|
db.mediaobject_prefix)
|
2006-04-27 07:26:42 +05:30
|
|
|
if uistate:
|
2005-12-06 12:08:09 +05:30
|
|
|
self.progress.set_pass(_('Reordering Source IDs'),
|
|
|
|
db.get_number_of_sources())
|
2007-10-08 22:11:39 +05:30
|
|
|
self.reorder(gen.lib.Source,
|
2005-12-06 12:08:09 +05:30
|
|
|
db.get_source_from_gramps_id,
|
|
|
|
db.get_source_from_handle,
|
|
|
|
db.find_next_source_gramps_id,
|
2006-05-04 09:35:57 +05:30
|
|
|
db.source_map,
|
2005-12-06 12:08:09 +05:30
|
|
|
db.commit_source,
|
2008-02-23 14:08:55 +05:30
|
|
|
db.source_prefix)
|
2006-04-27 07:26:42 +05:30
|
|
|
if uistate:
|
2005-12-06 12:08:09 +05:30
|
|
|
self.progress.set_pass(_('Reordering Place IDs'),
|
|
|
|
db.get_number_of_places())
|
2007-10-08 22:11:39 +05:30
|
|
|
self.reorder(gen.lib.Place,
|
2005-12-06 12:08:09 +05:30
|
|
|
db.get_place_from_gramps_id,
|
|
|
|
db.get_place_from_handle,
|
|
|
|
db.find_next_place_gramps_id,
|
2006-05-04 09:35:57 +05:30
|
|
|
db.place_map,
|
2005-12-06 12:08:09 +05:30
|
|
|
db.commit_place,
|
2008-02-23 14:08:55 +05:30
|
|
|
db.place_prefix)
|
2006-04-27 07:26:42 +05:30
|
|
|
if uistate:
|
|
|
|
self.progress.set_pass(_('Reordering Repository IDs'),
|
|
|
|
db.get_number_of_repositories())
|
2007-10-08 22:11:39 +05:30
|
|
|
self.reorder(gen.lib.Repository,
|
2006-04-27 07:26:42 +05:30
|
|
|
db.get_repository_from_gramps_id,
|
|
|
|
db.get_repository_from_handle,
|
|
|
|
db.find_next_repository_gramps_id,
|
2006-05-04 09:35:57 +05:30
|
|
|
db.repository_map,
|
2006-04-27 07:26:42 +05:30
|
|
|
db.commit_repository,
|
2008-02-23 14:08:55 +05:30
|
|
|
db.repository_prefix)
|
2008-01-11 01:47:05 +05:30
|
|
|
#add reorder notes ID
|
|
|
|
if uistate:
|
|
|
|
self.progress.set_pass(_('Reordering Note IDs'),
|
|
|
|
db.get_number_of_notes())
|
|
|
|
self.reorder(gen.lib.Note,
|
|
|
|
db.get_note_from_gramps_id,
|
|
|
|
db.get_note_from_handle,
|
|
|
|
db.find_next_note_gramps_id,
|
|
|
|
db.note_map,
|
|
|
|
db.commit_note,
|
2008-02-23 14:08:55 +05:30
|
|
|
db.note_prefix)
|
2006-04-27 07:26:42 +05:30
|
|
|
if uistate:
|
2005-12-06 12:08:09 +05:30
|
|
|
self.progress.close()
|
|
|
|
else:
|
|
|
|
print "Done."
|
|
|
|
|
2008-02-23 14:08:55 +05:30
|
|
|
db.transaction_commit(self.trans, _("Reorder GRAMPS IDs"))
|
2006-05-11 05:36:43 +05:30
|
|
|
db.enable_signals()
|
|
|
|
db.request_rebuild()
|
2003-06-16 10:27:25 +05:30
|
|
|
|
2005-12-06 12:08:09 +05:30
|
|
|
def reorder(self, class_type, find_from_id, find_from_handle,
|
2006-05-04 09:35:57 +05:30
|
|
|
find_next_id, table, commit, prefix):
|
2002-10-20 19:55:16 +05:30
|
|
|
dups = []
|
2003-07-06 09:22:38 +05:30
|
|
|
newids = {}
|
2002-10-20 19:55:16 +05:30
|
|
|
key_list = []
|
|
|
|
|
2006-05-04 09:35:57 +05:30
|
|
|
for handle in table.keys():
|
2006-04-27 07:26:42 +05:30
|
|
|
if self.uistate:
|
2005-12-06 12:08:09 +05:30
|
|
|
self.progress.step()
|
2006-05-04 09:35:57 +05:30
|
|
|
|
|
|
|
sdata = table[handle]
|
2004-12-20 04:25:41 +05:30
|
|
|
|
2005-05-11 19:34:47 +05:30
|
|
|
obj = class_type()
|
|
|
|
obj.unserialize(sdata)
|
|
|
|
gramps_id = obj.get_gramps_id()
|
2002-10-20 19:55:16 +05:30
|
|
|
# attempt to extract integer, if we can't, treat it as a
|
|
|
|
# duplicate
|
|
|
|
|
2004-12-20 04:25:41 +05:30
|
|
|
match = _findint.match(gramps_id)
|
2002-10-20 19:55:16 +05:30
|
|
|
if match:
|
2004-10-26 08:50:38 +05:30
|
|
|
# get the integer, build the new handle. Make sure it
|
2002-10-20 19:55:16 +05:30
|
|
|
# hasn't already been chosen. If it has, put this
|
2004-10-26 08:50:38 +05:30
|
|
|
# in the duplicate handle list
|
2002-10-20 19:55:16 +05:30
|
|
|
|
2003-06-30 08:37:24 +05:30
|
|
|
try:
|
|
|
|
index = match.groups()[0]
|
2004-12-20 04:25:41 +05:30
|
|
|
newgramps_id = prefix % int(index)
|
|
|
|
if newgramps_id == gramps_id:
|
|
|
|
newids[newgramps_id] = gramps_id
|
2005-05-11 19:34:47 +05:30
|
|
|
elif find_from_id(newgramps_id) != None:
|
|
|
|
dups.append(obj.get_handle())
|
2003-06-30 08:37:24 +05:30
|
|
|
else:
|
2005-12-06 12:08:09 +05:30
|
|
|
obj.set_gramps_id(newgramps_id)
|
2008-02-23 14:08:55 +05:30
|
|
|
commit(obj, self.trans)
|
2005-05-11 19:34:47 +05:30
|
|
|
newids[newgramps_id] = gramps_id
|
2003-06-30 08:37:24 +05:30
|
|
|
except:
|
2004-10-26 08:50:38 +05:30
|
|
|
dups.append(handle)
|
2002-10-20 19:55:16 +05:30
|
|
|
else:
|
2004-10-26 08:50:38 +05:30
|
|
|
dups.append(handle)
|
2004-12-20 04:25:41 +05:30
|
|
|
|
2002-10-20 19:55:16 +05:30
|
|
|
# go through the duplicates, looking for the first availble
|
2004-10-26 08:50:38 +05:30
|
|
|
# handle that matches the new scheme.
|
2002-10-20 19:55:16 +05:30
|
|
|
|
2006-04-27 07:26:42 +05:30
|
|
|
if self.uistate:
|
2005-12-06 12:08:09 +05:30
|
|
|
self.progress.set_pass(_('Finding and assigning unused IDs'),
|
|
|
|
len(dups))
|
2005-05-11 19:34:47 +05:30
|
|
|
for handle in dups:
|
2006-04-27 07:26:42 +05:30
|
|
|
if self.uistate:
|
2005-12-06 12:08:09 +05:30
|
|
|
self.progress.step()
|
2005-05-11 19:34:47 +05:30
|
|
|
obj = find_from_handle(handle)
|
|
|
|
obj.set_gramps_id(find_next_id())
|
2008-02-23 14:08:55 +05:30
|
|
|
commit(obj, self.trans)
|
2002-10-20 19:55:16 +05:30
|
|
|
|
2005-12-06 12:08:09 +05:30
|
|
|
#------------------------------------------------------------------------
|
|
|
|
#
|
|
|
|
#
|
|
|
|
#
|
|
|
|
#------------------------------------------------------------------------
|
|
|
|
class ReorderIdsOptions(Tool.ToolOptions):
|
|
|
|
"""
|
|
|
|
Defines options and provides handling interface.
|
|
|
|
"""
|
|
|
|
|
2008-02-23 14:08:55 +05:30
|
|
|
def __init__(self, name, person_id=None):
|
|
|
|
Tool.ToolOptions.__init__(self, name, person_id)
|
2005-12-06 12:08:09 +05:30
|
|
|
|
2002-10-20 19:55:16 +05:30
|
|
|
#-------------------------------------------------------------------------
|
|
|
|
#
|
|
|
|
#
|
|
|
|
#
|
|
|
|
#-------------------------------------------------------------------------
|
|
|
|
register_tool(
|
2005-12-06 12:08:09 +05:30
|
|
|
name = 'reorder_ids',
|
|
|
|
category = Tool.TOOL_DBPROC,
|
|
|
|
tool_class = ReorderIds,
|
|
|
|
options_class = ReorderIdsOptions,
|
|
|
|
modes = Tool.MODE_GUI | Tool.MODE_CLI,
|
|
|
|
translated_name = _("Reorder GRAMPS IDs"),
|
2008-03-02 04:17:48 +05:30
|
|
|
status = _("Stable"),
|
2005-12-06 12:08:09 +05:30
|
|
|
author_name = "Donald N. Allingham",
|
|
|
|
author_email = "don@gramps-project.org",
|
2008-03-02 04:17:48 +05:30
|
|
|
description = _("Reorders the gramps IDs "
|
|
|
|
"according to Gramps' default rules.")
|
2002-10-20 19:55:16 +05:30
|
|
|
)
|