gramps/src/plugins/ReorderIds.py

233 lines
8.2 KiB
Python
Raw Normal View History

2002-10-20 19:55:16 +05:30
#
# Gramps - a GTK+/GNOME based genealogy program
#
# 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
#
# $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
"""
#------------------------------------------------------------------------
#
# standard python modules
#
#------------------------------------------------------------------------
2002-10-20 19:55:16 +05:30
import re
from gettext import gettext as _
2002-10-20 19:55:16 +05:30
#------------------------------------------------------------------------
#
# GRAMPS modules
#
#------------------------------------------------------------------------
import Utils
import RelLib
from PluginUtils import Tool, register_tool
2002-10-20 19:55:16 +05:30
_findint = re.compile('^[^\d]*(\d+)[^\d]*')
#-------------------------------------------------------------------------
#
# Actual tool
#
#-------------------------------------------------------------------------
class ReorderIds(Tool.BatchTool):
def __init__(self,dbstate,uistate,options_class,name,callback=None):
Tool.BatchTool.__init__(self,dbstate,options_class,name)
if self.fail:
return
2005-12-06 12:08:09 +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..."
self.trans = db.transaction_begin("",batch=True)
db.disable_signals()
if uistate:
2005-12-06 12:08:09 +05:30
self.progress.set_pass(_('Reordering People IDs'),
db.get_number_of_people())
self.reorder(RelLib.Person,
db.get_person_from_gramps_id,
db.get_person_from_handle,
db.find_next_person_gramps_id,
db.person_map,
2005-12-06 12:08:09 +05:30
db.commit_person,
db.iprefix)
2005-12-06 12:08:09 +05:30
if uistate:
2005-12-06 12:08:09 +05:30
self.progress.set_pass(_('Reordering Family IDs'),
db.get_number_of_families())
self.reorder(RelLib.Family,
db.get_family_from_gramps_id,
db.get_family_from_handle,
db.find_next_family_gramps_id,
db.family_map,
2005-12-06 12:08:09 +05:30
db.commit_family,
db.fprefix)
if uistate:
self.progress.set_pass(_('Reordering Event IDs'),
db.get_number_of_events())
self.reorder(RelLib.Event,
db.get_event_from_gramps_id,
db.get_event_from_handle,
db.find_next_event_gramps_id,
db.event_map,
db.commit_event,
db.eprefix)
if uistate:
2005-12-06 12:08:09 +05:30
self.progress.set_pass(_('Reordering Media Object IDs'),
db.get_number_of_media_objects())
self.reorder(RelLib.MediaObject,
db.get_object_from_gramps_id,
db.get_object_from_handle,
db.find_next_object_gramps_id,
db.media_map,
2005-12-06 12:08:09 +05:30
db.commit_media_object,
db.oprefix)
if uistate:
2005-12-06 12:08:09 +05:30
self.progress.set_pass(_('Reordering Source IDs'),
db.get_number_of_sources())
self.reorder(RelLib.Source,
db.get_source_from_gramps_id,
db.get_source_from_handle,
db.find_next_source_gramps_id,
db.source_map,
2005-12-06 12:08:09 +05:30
db.commit_source,
db.sprefix)
if uistate:
2005-12-06 12:08:09 +05:30
self.progress.set_pass(_('Reordering Place IDs'),
db.get_number_of_places())
self.reorder(RelLib.Place,
db.get_place_from_gramps_id,
db.get_place_from_handle,
db.find_next_place_gramps_id,
db.place_map,
2005-12-06 12:08:09 +05:30
db.commit_place,
db.pprefix)
if uistate:
self.progress.set_pass(_('Reordering Repository IDs'),
db.get_number_of_repositories())
self.reorder(RelLib.Repository,
db.get_repository_from_gramps_id,
db.get_repository_from_handle,
db.find_next_repository_gramps_id,
db.repository_map,
db.commit_repository,
db.rprefix)
if uistate:
2005-12-06 12:08:09 +05:30
self.progress.close()
else:
print "Done."
db.transaction_commit(self.trans,_("Reorder GRAMPS IDs"))
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,
find_next_id, table, commit, prefix):
2002-10-20 19:55:16 +05:30
dups = []
newids = {}
2002-10-20 19:55:16 +05:30
key_list = []
for handle in table.keys():
if self.uistate:
2005-12-06 12:08:09 +05:30
self.progress.step()
sdata = table[handle]
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
match = _findint.match(gramps_id)
2002-10-20 19:55:16 +05:30
if match:
# 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
# in the duplicate handle list
2002-10-20 19:55:16 +05:30
try:
index = match.groups()[0]
newgramps_id = prefix % int(index)
if newgramps_id == gramps_id:
newids[newgramps_id] = gramps_id
elif find_from_id(newgramps_id) != None:
dups.append(obj.get_handle())
else:
2005-12-06 12:08:09 +05:30
obj.set_gramps_id(newgramps_id)
commit(obj,self.trans)
newids[newgramps_id] = gramps_id
except:
dups.append(handle)
2002-10-20 19:55:16 +05:30
else:
dups.append(handle)
2002-10-20 19:55:16 +05:30
# go through the duplicates, looking for the first availble
# handle that matches the new scheme.
2002-10-20 19:55:16 +05:30
if self.uistate:
2005-12-06 12:08:09 +05:30
self.progress.set_pass(_('Finding and assigning unused IDs'),
len(dups))
for handle in dups:
if self.uistate:
2005-12-06 12:08:09 +05:30
self.progress.step()
obj = find_from_handle(handle)
obj.set_gramps_id(find_next_id())
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.
"""
def __init__(self,name,person_id=None):
Tool.ToolOptions.__init__(self,name,person_id)
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"),
status=(_("Stable")),
author_name = "Donald N. Allingham",
author_email = "don@gramps-project.org",
description=_("Reorders the gramps IDs "
"according to gramps' default rules.")
2002-10-20 19:55:16 +05:30
)