Learn how easy it is to sync an existing GitHub or Google Code repo to a SourceForge project! See Demo

Close

[5680be]: gramps / plugins / tool / mergecitations.py Maximize Restore History

Download this file

mergecitations.py    263 lines (224 with data), 9.9 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
#
# Gramps - a GTK+/GNOME based genealogy program
#
# Copyright (C) 2000-2007 Donald N. Allingham
# Copyright (C) 2008 Brian G. Matherly
# Copyright (C) 2010 Jakim Friant
# Copyright (C) 2011 Tim G L Lyons
#
# 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
#
"""Tools/Family Tree Processing/MergeCitations"""
#------------------------------------------------------------------------
#
# Python modules
#
#------------------------------------------------------------------------
from __future__ import print_function
import logging
LOG = logging.getLogger(".citation")
#-------------------------------------------------------------------------
#
# GNOME libraries
#
#-------------------------------------------------------------------------
from gi.repository import Gtk
#-------------------------------------------------------------------------
#
# GRAMPS modules
#
#-------------------------------------------------------------------------
from gramps.gen.const import GRAMPS_LOCALE as glocale
_ = glocale.translation.sgettext
ngettext = glocale.translation.ngettext # else "nearby" comments are ignored
from gramps.gen.utils.string import conf_strings
from gramps.gen.const import URL_MANUAL_PAGE
from gramps.gui.utils import ProgressMeter
from gramps.gui.plug import tool
from gramps.gui.dialog import OkDialog
from gramps.gui.display import display_help
from gramps.gen.datehandler import get_date
from gramps.gui.managedwindow import ManagedWindow
from gramps.gen.merge import MergeCitationQuery
from gramps.gui.glade import Glade
from gramps.gen.db import DbTxn
from gramps.gen.lib import (Person, Family, Event, Place, MediaObject, Citation,
Repository)
from gramps.gen.errors import MergeError
#-------------------------------------------------------------------------
#
# Constants
#
#-------------------------------------------------------------------------
ALL_FIELDS = 0
IGNORE_DATE = 1
IGNORE_CONFIDENCE = 2
IGNORE_BOTH = 3
_val2label = {
ALL_FIELDS : _("Match on Page/Volume, Date and Confidence"),
IGNORE_DATE : _("Ignore Date"),
IGNORE_CONFIDENCE : _("Ignore Confidence"),
IGNORE_BOTH : _("Ignore Date and Confidence")
}
WIKI_HELP_PAGE = '%s_-_Tools' % URL_MANUAL_PAGE
WIKI_HELP_SEC = _('manual|Merge_citations...')
#-------------------------------------------------------------------------
#
# The Actual tool.
#
#-------------------------------------------------------------------------
class MergeCitations(tool.BatchTool,ManagedWindow):
def __init__(self, dbstate, user, options_class, name, callback=None):
uistate = user.uistate
ManagedWindow.__init__(self, uistate, [], self.__class__)
self.dbstate = dbstate
self.set_window(Gtk.Window(), Gtk.Label(), '')
tool.BatchTool.__init__(self, dbstate, user, options_class, name)
if not self.fail:
uistate.set_busy_cursor(True)
self.run()
uistate.set_busy_cursor(False)
def run(self):
top = Glade(toplevel="mergecitations")
# retrieve options
fields = self.options.handler.options_dict['fields']
dont_merge_notes = self.options.handler.options_dict['dont_merge_notes']
my_menu = Gtk.ListStore(str, object)
for val in sorted(_val2label):
my_menu.append([_val2label[val], val])
self.notes_obj = top.get_object("notes")
self.notes_obj.set_active(dont_merge_notes)
self.notes_obj.show()
self.menu = top.get_object("menu")
self.menu.set_model(my_menu)
self.menu.set_active(fields)
window = top.toplevel
window.show()
# self.set_window(window, top.get_object('title'),
# _('Merge citations'))
self.set_window(window, top.get_object('title2'),
_("Notes, media objects and data-items of matching "
"citations will be combined."))
top.connect_signals({
"on_merge_ok_clicked" : self.on_merge_ok_clicked,
"destroy_passed_object" : self.cancel,
"on_help_clicked" : self.on_help_clicked,
"on_delete_merge_event" : self.close,
"on_delete_event" : self.close,
})
self.show()
def cancel(self, obj):
"""
on cancel, update the saved values of the options.
"""
fields = self.menu.get_model()[self.menu.get_active()][1]
dont_merge_notes = int(self.notes_obj.get_active())
LOG.debug("cancel fields %d dont_merge_notes %d" %
(fields, dont_merge_notes))
self.options.handler.options_dict['fields'] = fields
self.options.handler.options_dict['dont_merge_notes'] = dont_merge_notes
# Save options
self.options.handler.save_options()
self.close(obj)
def build_menu_names(self, obj):
return (_("Tool settings"),_("Merge citations tool"))
def on_help_clicked(self, obj):
"""Display the relevant portion of GRAMPS manual"""
display_help(WIKI_HELP_PAGE , WIKI_HELP_SEC)
def on_merge_ok_clicked(self, obj):
"""
Performs the actual merge of citations
(Derived from ExtractCity)
"""
fields = self.menu.get_model()[self.menu.get_active()][1]
dont_merge_notes = int(self.notes_obj.get_active())
LOG.debug("fields %d dont_merge_notes %d" % (fields, dont_merge_notes))
self.options.handler.options_dict['fields'] = fields
self.options.handler.options_dict['dont_merge_notes'] = dont_merge_notes
# Save options
self.options.handler.save_options()
self.progress = ProgressMeter(_('Checking Sources'), '')
self.progress.set_pass(_('Looking for citation fields'),
self.db.get_number_of_citations())
db = self.dbstate.db
db.disable_signals()
num_merges = 0
for handle in db.iter_source_handles():
dict = {}
citation_handle_list = list(db.find_backlink_handles(handle))
for (class_name, citation_handle) in citation_handle_list:
if class_name != Citation.__name__:
raise MergeError("Encountered an object of type %s "
"that has a citation reference." % class_name)
citation = db.get_citation_from_handle(citation_handle)
key = citation.get_page()
if fields != IGNORE_DATE and fields != IGNORE_BOTH:
key += "\n" + get_date(citation)
if fields != IGNORE_CONFIDENCE and fields != IGNORE_BOTH:
key += "\n" + \
conf_strings[citation.get_confidence_level()]
if key in dict and \
(not dont_merge_notes or len(citation.note_list) == 0):
citation_match_handle = dict[key]
citation_match = \
db.get_citation_from_handle(citation_match_handle)
try:
query = MergeCitationQuery(
self.dbstate, citation_match, citation)
query.execute()
except AssertionError:
print("Tool/Family Tree processing/MergeCitations", \
"citation1 gramps_id", citation_match.get_gramps_id(), \
"citation2 gramps_id", citation.get_gramps_id() , \
"citation backlink handles", \
list(db.find_backlink_handles(citation.get_handle())))
num_merges += 1
elif (not dont_merge_notes or len(citation.note_list) == 0):
dict[key] = citation_handle
self.progress.step()
db.enable_signals()
db.request_rebuild()
self.progress.close()
OkDialog(_("Number of merges done"),
# translators: leave all/any {...} untranslated
ngettext("{number_of} citation merged",
"{number_of} citations merged", num_merges
).format(number_of=num_merges) )
self.close(obj)
#------------------------------------------------------------------------
#
#
#
#------------------------------------------------------------------------
class MergeCitationsOptions(tool.ToolOptions):
"""
Defines options and provides handling interface.
"""
def __init__(self, name,person_id=None):
tool.ToolOptions.__init__(self, name,person_id)
# Options specific for this report
self.options_dict = {
'fields' : 1,
'dont_merge_notes' : 0,
}
self.options_help = {
'dont_merge_notes' :
("=0/1","Whether to merge citations if they have notes",
["Merge citations with notes",
"Do not merge citations with notes"],
False),
'fields' : ("=num","Threshold for matching",
"Integer number")
}